This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The game now ends when two airplanes collide. When the game ends, it transitions back to the `ready` state so that a new game can be started. Collision detection is implemented using simple rectangular bounding boxes. For this game, this method is reliable enough since airplanes cannot move faster than their own size in a frame. Meaning airplanes cannot glitch through other objects.
- Loading branch information
Showing
11 changed files
with
132 additions
and
15 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,21 @@ | ||
use bevy::prelude::*; | ||
|
||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Component)] | ||
pub struct Collider; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Collider; | ||
|
||
#[test] | ||
fn trait_send() { | ||
fn assert_send<T: Send>() {} | ||
assert_send::<Collider>(); | ||
} | ||
|
||
#[test] | ||
fn trait_sync() { | ||
fn assert_sync<T: Sync>() {} | ||
assert_sync::<Collider>(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
pub use self::airplane::*; | ||
pub use self::airplane_id::*; | ||
pub use self::collider::*; | ||
pub use self::flight_plan::*; | ||
pub use self::location::*; | ||
pub use self::speed::*; | ||
|
||
mod airplane; | ||
mod airplane_id; | ||
mod collider; | ||
mod flight_plan; | ||
mod location; | ||
mod speed; |
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
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,55 @@ | ||
use bevy::prelude::*; | ||
use bevy::sprite::collide_aabb::collide; | ||
|
||
use atc::v1::get_game_state_response::GameState; | ||
|
||
use crate::components::{AirplaneId, Collider, AIRPLANE_SIZE}; | ||
use crate::event::{Event, EventBus}; | ||
|
||
pub struct Size { | ||
airplane: Vec2, | ||
} | ||
|
||
impl Default for Size { | ||
fn default() -> Self { | ||
Self { | ||
airplane: Vec2::new(AIRPLANE_SIZE, AIRPLANE_SIZE), | ||
} | ||
} | ||
} | ||
|
||
pub fn detect_collision( | ||
mut app_state: ResMut<State<GameState>>, | ||
query: Query<(Entity, &AirplaneId, &Collider, &Transform)>, | ||
event_bus: Local<EventBus>, | ||
size: Local<Size>, | ||
) { | ||
'outer: for (entity1, airplane_id1, _, transform1) in query.iter() { | ||
for (entity2, airplane_id2, _, transform2) in query.iter() { | ||
if entity1 == entity2 { | ||
continue; | ||
} | ||
|
||
if collide( | ||
transform1.translation, | ||
size.airplane, | ||
transform2.translation, | ||
size.airplane, | ||
) | ||
.is_some() | ||
{ | ||
event_bus | ||
.sender() | ||
.send(Event::AirplaneCollided( | ||
airplane_id1.clone(), | ||
airplane_id2.clone(), | ||
)) | ||
.expect("failed to send event"); // TODO: Handle error | ||
|
||
app_state.set(GameState::Ready).unwrap(); | ||
|
||
break 'outer; | ||
} | ||
} | ||
} | ||
} |
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