-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
176 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import p5js_gleam.{type P5} | ||
import p5js_gleam/bindings as p5 | ||
import vector.{type Vector} | ||
|
||
pub const size = 6.0 | ||
|
||
pub const damage = 10 | ||
|
||
/// Represents an obstacle that the player must avoid. | ||
pub type Obstacle { | ||
/// Represents an obstacle that the player must avoid. | ||
Obstacle( | ||
/// The position of the obstacle. | ||
position: Vector, | ||
) | ||
} | ||
|
||
/// Checks if the object at the given position with given radius collides with the bullet. | ||
pub fn collides_with( | ||
obstacle: Obstacle, | ||
position: Vector, | ||
object_size: Float, | ||
) -> Bool { | ||
vector.distance(obstacle.position, position) | ||
<. size /. 2.0 +. object_size /. 2.0 | ||
} | ||
|
||
/// Renders the obstacle to the screen | ||
pub fn draw(p: P5, obstacle: Obstacle) { | ||
let position = obstacle.position | ||
let spike1 = 4.0 | ||
let spike2 = 5.0 | ||
p5.stroke(p, "#f4424b") | ||
|> p5.fill("#f4424b") | ||
|> p5.circle(position.x, position.y, size) | ||
|> p5.line( | ||
position.x -. spike1, | ||
position.y -. spike1, | ||
position.x +. spike1, | ||
position.y +. spike1, | ||
) | ||
|> p5.line( | ||
position.x -. spike1, | ||
position.y +. spike1, | ||
position.x +. spike1, | ||
position.y -. spike1, | ||
) | ||
|> p5.line(position.x, position.y -. spike2, position.x, position.y +. spike2) | ||
|> p5.line(position.x -. spike2, position.y, position.x +. spike2, position.y) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import obstacle.{type Obstacle, Obstacle} | ||
import startest.{describe, it} | ||
import startest/expect | ||
import vector.{Vector} | ||
|
||
pub fn obstacle_tests() { | ||
describe("obstacle", [ | ||
it("collides_with", fn() { | ||
expect.to_equal( | ||
obstacle.collides_with( | ||
Obstacle(Vector(0.0, 0.0, 0.0)), | ||
Vector(10.0, 0.0, 0.0), | ||
1.0, | ||
), | ||
False, | ||
) | ||
expect.to_equal( | ||
obstacle.collides_with( | ||
Obstacle(Vector(0.0, 0.0, 0.0)), | ||
Vector(0.0, 0.0, 0.0), | ||
1.0, | ||
), | ||
True, | ||
) | ||
}), | ||
]) | ||
} |