Skip to content

Commit

Permalink
Merge pull request #22
Browse files Browse the repository at this point in the history
tests_sven
  • Loading branch information
SvenWlf authored Jun 2, 2024
2 parents 403c76c + 260b85b commit 0d58429
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 6 deletions.
6 changes: 6 additions & 0 deletions game/src/asset_system/assets_loading.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use bevy::prelude::*;
use bevy_ecs_ldtk::prelude::*;

/// Sets up the game by spawning the camera and the ldtk world
///
/// This function setsup the camera and the ldtk file in which the world is saved in.
/// # Arguments
/// * `commands` - A mutable reference to the commands
/// * `asset_server` - A resource that loads the assets
pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let mut camera = Camera2dBundle::default();
camera.projection.scale = 0.5;
Expand Down
20 changes: 20 additions & 0 deletions game/src/asset_system/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ use bevy::prelude::*;
use bevy_ecs_ldtk::prelude::*;
use bevy_rapier2d::prelude::*;

/// Bundle for player collider
///
/// This bundle is used to create a collider for the player entity.
///
/// # Fields
///
/// * `collider` - The collider for the player entity.
/// * `rigid_body` - The rigid lets the game know that this body can be affected by external sources.
/// * `velocity` - The velocity of the player entity.
/// * `rotation_constraints` - The rotation constraints stop the player from rolling like a ball.
/// * `gravity_scale` - The gravity scale of the player entity.
/// * `friction` - The friction of the player entity adds resistance when moving on another object.
/// * `density` - The density of the player entity, lets the player have density making it more realistic.
#[derive(Clone, Default, Bundle, LdtkIntCell)]
pub struct ColliderBundle {
pub collider: Collider,
Expand All @@ -13,6 +26,13 @@ pub struct ColliderBundle {
pub density: ColliderMassProperties,
}

/// Creates an instance of the `ColliderBundle` struct for every player entity.
///
/// can be easily expanded to include more entities using the match case.
///
/// # Arguments
///
/// * `entity_instance` - A reference to an `EntityInstance` struct like a player.
impl From<&EntityInstance> for ColliderBundle {
fn from(entity_instance: &EntityInstance) -> ColliderBundle {
let rotation_constraints = LockedAxes::ROTATION_LOCKED;
Expand Down
64 changes: 64 additions & 0 deletions game/src/asset_system/finish_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,34 @@ use bevy_rapier2d::geometry::{ActiveEvents, Collider, Friction, Sensor};
use bevy_rapier2d::pipeline::CollisionEvent;
use crate::score_system::time::TimeText;

/// FinsihLine component
///
/// # Fields
#[derive(Default, Component)]
pub struct FinishLine;

/// Bundle for FinishLine
///
/// # Fields
#[derive(Default, Bundle, LdtkIntCell)]
pub struct FinishLineBundle {
finishline: FinishLine,
}

/// Spawns finishline collisions
///
/// spawns the finishline collisions for the level.
/// combines horizontally next to each other in to plates for performance reasons.
/// this combination thechnique id from the bevy ecs ldtk example.
///
/// # Arguments
///
/// * `commands` - A mutable reference to the `Commands` struct.
/// * `finishline_query` - A query that fetches the grid coordinates and parent of the finishline entity.
/// * `parent_query` - A query that fetches the parent of the finishline entity.
/// * `level_query` - A query that fetches the entity and level iid.
/// * `ldtk_projects` - A query that fetches the handle of the ldtk project.
/// * `ldtk_project_assets` - A resource that stores the assets of the ldtk project.
pub fn spawn_finishline_collision(
mut commands: Commands,
finishline_query: Query<(&GridCoords, &Parent), Added<FinishLine>>,
Expand Down Expand Up @@ -168,17 +188,32 @@ pub fn spawn_finishline_collision(
}
}

/// FinishLineDetection component
///
/// # Fields
#[derive(Clone, Default, Component)]
pub struct FinishLineDetection {
pub on_finishline: bool,
}


/// Bundle for FinishLineDetection
///
/// # Fields
#[derive(Component)]
pub struct FinishLineSensor {
pub finishline_detection_entity: Entity,
pub intersecting_finishline_entities: HashSet<Entity>,
}

/// Spawns finishline sensors
///
/// this spawns the finsihline sensors on any entity that has a finishline detection component.
///
/// # Arguments
///
/// * `commands` - A mutable reference to the `Commands` struct.
/// * `detect_finishline_for` - A query that fetches the entity and collider of the finishline detection.
pub fn spawn_finishline_sensor(
mut commands: Commands,
detect_finishline_for: Query<(Entity, &Collider), Added<FinishLineDetection>>,
Expand Down Expand Up @@ -211,6 +246,16 @@ pub fn spawn_finishline_sensor(
}
}

/// Detects finishline collisions
///
/// this detects the finishline collisions for the finishline sensors.
///
/// # Arguments
///
/// * `finishline_sensors` - A query that fetches the finishline sensors.
/// * `collisions` - An event reader that reads the collision events.
/// * `collidables` - A query that fetches the entity and collider of the collidable entities.
/// * `finishlines` - A query that fetches the entity of the finishline entities.
pub fn finishline_detection(
mut finishline_sensors: Query<&mut FinishLineSensor>,
mut collisions: EventReader<CollisionEvent>,
Expand Down Expand Up @@ -245,11 +290,30 @@ pub fn finishline_detection(
}
}

/// FinishLineEvent
///
/// this stores the time elapsed for the finsihline event.
/// needed for updating the higscore.
///
/// # Fields
#[derive(Event)]
pub struct FinishLineEvent{
pub elapsed_time: u64,
}

/// Update event on finishline
///
/// this sends an event when the player reaches the finishline, to update the highscore.
/// sets the elapsed time to 0 and resets the time text.
/// teleports the player to the start of the level.
///
/// # Arguments
///
/// * `finishline_detectors` - A query that fetches the finishline detectors.
/// * `finishline_sensors` - A query that fetches the finishline sensors.
/// * `finishline_events` - An event writer that writes the finishline events.
/// * `transforms` - A query that fetches the transform of the entities.
/// * `time_text` - A query that fetches the time text.
pub fn update_on_finishline(
mut finishline_detectors: Query<&mut FinishLineDetection>,
finishline_sensors: Query<&FinishLineSensor, Changed<FinishLineSensor>>,
Expand Down
10 changes: 10 additions & 0 deletions game/src/asset_system/ghost_physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ use bevy::prelude::Bundle;
use bevy_rapier2d::dynamics::{LockedAxes, RigidBody, Velocity};
use bevy_rapier2d::geometry::{Collider, ColliderMassProperties, Friction};

/// Component for ghost player entity
///
/// # Fields
///
/// * `collider` - The collider for the ghost player entity.
/// * `rigid_body` - The rigid body for the ghost player entity.
/// * `velocity` - The velocity of the ghost player entity.
/// * `rotation_constraints` - The rotation constraints for the ghost player entity.
/// * `friction` - The friction of the ghost player entity.
/// * `density` - The density of the ghost player entity.
#[derive(Clone, Default, Bundle)]
pub struct GhostColliderBundle {
pub collider: Collider,
Expand Down
23 changes: 23 additions & 0 deletions game/src/asset_system/players.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ use crate::asset_system::finish_lines::FinishLineDetection;
use crate::asset_system::traps::TrapDetection;

use crate::input_system;

/// Component for player entity
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct Player;

/// Bundle for player entity
///
/// # Fields
///
/// * `player` - The player entity.
/// * `sprite_sheet_bundle` - The sprite sheet bundle for the player entity, comes from the ldtk File.
/// * `grid_coords` - The grid coordinates of the player entity.
/// * `collider_bundle` - The collider bundle for the player entity.
/// * `ground_detection` - The ground detection for the player entity.
/// * `trap_detection` - The trap detection for the player entity.
/// * `finishline_detection` - The finish line detection for the player entity.
/// * `input_handler` - The input handler for the player entity.
#[derive(Clone, Default, Bundle, LdtkEntity)]
pub struct PlayerBundle {
pub player: Player,
Expand All @@ -25,10 +39,19 @@ pub struct PlayerBundle {
pub input_handler: input_system::input_handler::InputHandler,
}

/// Component for ghost player entity
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct GhostPlayer {
pub id: u64,
}

/// Bundle for ghost player entity
///
/// # Fields
///
/// * `ghost_player` - The ghost player entity.
/// * `sprite_sheet_bundle` - The sprite sheet bundle for the ghost player entity.
/// * `ghost_collider_bundle` - The collider bundle for the ghost player entity.
#[derive(Clone, Default, Bundle, LdtkEntity)]
pub struct GhostPlayerBundle {
pub ghost_player: GhostPlayer,
Expand Down
61 changes: 61 additions & 0 deletions game/src/asset_system/traps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,34 @@ use bevy_rapier2d::geometry::{ActiveEvents, Collider, Friction, Sensor};
use bevy_rapier2d::pipeline::CollisionEvent;
use crate::score_system::time::TimeText;

/// Component for traps
#[derive(Default, Component)]
pub struct Trap;

/// Bundle for traps
///
/// # Fields
///
/// * `trap` - The trap entity.
#[derive(Default, Bundle, LdtkIntCell)]
pub struct TrapBundle {
trap: Trap,
}

/// Spawns trap collisions
///
/// spawns a collider for every trap tile in the level.
/// combines adjacent trap tiles into larger rectangles to reduce the number of colliders and improve performance.
/// seen in bevy ecs ldtk examples.
///
/// # Arguments
///
/// * `commands` - A mutable reference to the `Commands` struct.
/// * `trap_query` - A query that gets the grid coordinates and parent of the trap entity.
/// * `parent_query` - A query that gets the parent of the trap entity.
/// * `level_query` - A query that gets the entity and level iid.
/// * `ldtk_projects` - A query that gets the handle of the ldtk project.
/// * `ldtk_project_assets` - A resource that loads the ldtk project assets.
pub fn spawn_trap_collision(
mut commands: Commands,
trap_query: Query<(&GridCoords, &Parent), Added<Trap>>,
Expand Down Expand Up @@ -59,6 +79,7 @@ pub fn spawn_trap_collision(
}
});


if !trap_query.is_empty() {
level_query.iter().for_each(|(level_entity, level_iid)| {
if let Some(level_traps) = level_to_trap_locations.get(&level_entity) {
Expand Down Expand Up @@ -169,17 +190,37 @@ pub fn spawn_trap_collision(
}
}

/// Component for trap detection
///
/// # Fields
///
/// * `on_trap` - A boolean that is true if the player is on a trap.
#[derive(Clone, Default, Component)]
pub struct TrapDetection {
pub on_trap: bool,
}

/// Component for trap sensor
///
/// # Fields
///
/// * `trap_detection_entity` - The entity that detects traps.
/// * `intersecting_trap_entities` - A hash set of entities that intersect with the trap sensor.
#[derive(Component)]
pub struct TrapSensor {
pub trap_detection_entity: Entity,
pub intersecting_trap_entities: HashSet<Entity>,
}


/// Spawns trap sensors
///
/// this spawns a sensor for every entity that has a trap detection component.
///
/// # Arguments
///
/// * `commands` - A mutable reference to the `Commands` struct.
/// * `detect_trap_for` - A query that gets the entity and collider of the trap detection component.
pub fn spawn_trap_sensor(
mut commands: Commands,
detect_trap_for: Query<(Entity, &Collider), Added<TrapDetection>>,
Expand Down Expand Up @@ -212,6 +253,16 @@ pub fn spawn_trap_sensor(
}
}

/// Detects traps
///
/// this function detects if the player is on a trap.
///
/// # Arguments
///
/// * `trap_sensors` - A query that gets the trap sensor.
/// * `collisions` - An event reader that reads collision events.
/// * `collidables` - A query that gets the entity and collider of the collidable entities.
/// * `traps` - A query that gets the entity of the traps.
pub fn trap_detection(
mut trap_sensors: Query<&mut TrapSensor>,
mut collisions: EventReader<CollisionEvent>,
Expand Down Expand Up @@ -246,6 +297,16 @@ pub fn trap_detection(
}
}

/// Updates the trap detection
///
/// this function teleports the player to the beginning of the level and resets the timer if the player hits on a trap.
///
/// # Arguments
///
/// * `trap_detectors` - A query that gets the trap detection component.
/// * `trap_sensors` - A query that gets the trap sensor component.
/// * `transforms` - A query that gets the transform component.
/// * `time_text` - A query that gets the time text component.
pub fn update_on_trap(
mut trap_detectors: Query<&mut TrapDetection>,
trap_sensors: Query<&TrapSensor, Changed<TrapSensor>>,
Expand Down
Loading

0 comments on commit 0d58429

Please sign in to comment.