Skip to content

Commit

Permalink
cleanup & add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nelson137 committed Jan 11, 2024
1 parent 6a1ea85 commit 33d6643
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 63 deletions.
120 changes: 61 additions & 59 deletions src/core/elements/flappy_jellyfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl AssetGetFlappyJellyfishMeta for SchemaBox {
pub fn session_plugin(session: &mut Session) {
session
.stages
.add_system_to_stage(CoreStage::PostUpdate, move_flappy_jellyfish)
.add_system_to_stage(CoreStage::PostUpdate, control_flappy_jellyfish)
.add_system_to_stage(CoreStage::PostUpdate, explode_flappy_jellyfish);
}

Expand All @@ -53,6 +53,8 @@ pub struct FlappyJellyfish {
pub jellyfish: Entity,
}

/// A player with a jellyfish is holding shoot. Take control of the flappy if
/// one exists or spawn one.
pub fn spawn_or_take_control(
owner: Entity,
jellyfish_ent: Entity,
Expand All @@ -78,13 +80,15 @@ pub fn spawn_or_take_control(
.system()
}

/// Take control of the flappy associated with a jellyfish for a player.
fn take_control(owner: Entity, flappy_ent: Entity) -> StaticSystem<(), ()> {
(move |mut player_driving: CompMut<PlayerDrivingJellyfish>| {
player_driving.insert(owner, PlayerDrivingJellyfish { flappy: flappy_ent });
})
.system()
}

/// Spawn a flappy jellyfish.
fn spawn(owner: Entity, jellyfish_ent: Entity) -> StaticSystem<(), ()> {
(move |mut entities: ResMut<Entities>,
element_handles: Comp<ElementHandle>,
Expand Down Expand Up @@ -153,6 +157,53 @@ fn spawn(owner: Entity, jellyfish_ent: Entity) -> StaticSystem<(), ()> {
.system()
}

const SPEED_X: f32 = 324.0;
const SPEED_JUMP: f32 = 3.5;
const GRAVITY: f32 = 0.1;
const MIN_SPEED: Vec2 = vec2(-SPEED_X, -4.0);
const MAX_SPEED: Vec2 = vec2(SPEED_X, 4.0);

fn control_flappy_jellyfish(
time: Res<Time>,
entities: Res<Entities>,
player_driving: Comp<PlayerDrivingJellyfish>,
player_indexes: Comp<PlayerIdx>,
player_inputs: Res<MatchInputs>,
mut explode_flappies: CompMut<ExplodeFlappyJellyfish>,
mut bodies: CompMut<KinematicBody>,
) {
let t = time.delta_seconds();

for (_player_ent, (driving, player_idx)) in
entities.iter_with((&player_driving, &player_indexes))
{
let owner_control = player_inputs.players[player_idx.0 as usize].control;

if owner_control.grab_just_pressed {
explode_flappies.insert(driving.flappy, ExplodeFlappyJellyfish);
continue;
}

let Some(body) = bodies.get_mut(driving.flappy) else {
continue;
};

if owner_control.left == owner_control.right {
body.velocity.x = 0.0;
} else if owner_control.left > f32::EPSILON {
body.velocity.x = -owner_control.left * SPEED_X * t;
} else if owner_control.right > f32::EPSILON {
body.velocity.x = owner_control.right * SPEED_X * t;
}

if owner_control.jump_just_pressed {
body.velocity.y += SPEED_JUMP;
}

body.velocity = body.velocity.clamp(MIN_SPEED, MAX_SPEED);
}
}

/// A marker component for flappy jellyfish to indicate that it should explode.
#[derive(Clone, Copy, Debug, Default, HasSchema)]
pub struct ExplodeFlappyJellyfish;
Expand All @@ -168,8 +219,6 @@ fn explode_flappy_jellyfish(
invincibles: Comp<Invincibility>,
bodies: Comp<KinematicBody>,
map: Res<LoadedMap>,
player_driving: Comp<PlayerDrivingJellyfish>,
player_inputs: Res<MatchInputs>,
element_handles: Comp<ElementHandle>,
assets: Res<AssetServer>,
mut transforms: CompMut<Transform>,
Expand Down Expand Up @@ -214,16 +263,6 @@ fn explode_flappy_jellyfish(
}
}

for (_player_ent, (driving, player_idx)) in
entities.iter_with((&player_driving, &player_indexes))
{
let owner_control = player_inputs.players[player_idx.0 as usize].control;
if owner_control.grab_just_pressed {
explode_flappy_entities.push(driving.flappy);
continue;
}
}

for flappy_ent in explode_flappy_entities {
let Some(flappy) = flappy_jellyfishes.get(flappy_ent) else {
continue;
Expand All @@ -233,7 +272,9 @@ fn explode_flappy_jellyfish(
};
jellyfish.flappy.take();

// Get data for the explosion
/*
* Get explosion data
*/

let Some(flappy_meta) = element_handles
.get(flappy.jellyfish)
Expand All @@ -251,12 +292,12 @@ fn explode_flappy_jellyfish(
};
explosion_transform.translation.z = -10.0;

// Despawn the flappy
/*
* Setup the explosion
*/

entities.kill(flappy_ent);

// Setup the explosion

audio_events.play(flappy_meta.explosion_sound, flappy_meta.explosion_volume);

trauma_events.send(5.0);
Expand Down Expand Up @@ -300,7 +341,9 @@ fn explode_flappy_jellyfish(
);
}

// Despawn the jellyfish if out of ammo
/*
* Despawn the jellyfish if out of ammo
*/

if jellyfish.ammo == 0 {
dehydrate_jellyfish.insert(
Expand All @@ -312,44 +355,3 @@ fn explode_flappy_jellyfish(
}
}
}

const SPEED_X: f32 = 324.0;
const SPEED_JUMP: f32 = 3.5;
const GRAVITY: f32 = 0.1;
const MIN_SPEED: Vec2 = vec2(-SPEED_X, -4.0);
const MAX_SPEED: Vec2 = vec2(SPEED_X, 4.0);

fn move_flappy_jellyfish(
time: Res<Time>,
entities: Res<Entities>,
player_driving: Comp<PlayerDrivingJellyfish>,
player_indexes: Comp<PlayerIdx>,
player_inputs: Res<MatchInputs>,
mut bodies: CompMut<KinematicBody>,
) {
let t = time.delta_seconds();

for (_player_ent, (driving, player_idx)) in
entities.iter_with((&player_driving, &player_indexes))
{
let owner_control = player_inputs.players[player_idx.0 as usize].control;

let Some(body) = bodies.get_mut(driving.flappy) else {
continue;
};

if owner_control.left == owner_control.right {
body.velocity.x = 0.0;
} else if owner_control.left > f32::EPSILON {
body.velocity.x = -owner_control.left * SPEED_X * t;
} else if owner_control.right > f32::EPSILON {
body.velocity.x = owner_control.right * SPEED_X * t;
}

if owner_control.jump_just_pressed {
body.velocity.y += SPEED_JUMP;
}

body.velocity = body.velocity.clamp(MIN_SPEED, MAX_SPEED);
}
}
6 changes: 4 additions & 2 deletions src/core/elements/jellyfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn session_plugin(session: &mut Session) {
.stages
.add_system_to_stage(CoreStage::PreUpdate, hydrate)
.add_system_to_stage(CoreStage::PreUpdate, dehydrate)
.add_system_to_stage(CoreStage::PostUpdate, update_jellyfish_driving);
.add_system_to_stage(CoreStage::PostUpdate, update_player_driving);
flappy_jellyfish::session_plugin(session);
}

Expand All @@ -53,6 +53,8 @@ impl Jellyfish {
}
}

/// A marker component for players to indicate that they are driving a flappy
/// jellyfish.
#[derive(Clone, Debug, Default, HasSchema)]
pub struct PlayerDrivingJellyfish {
pub flappy: Entity,
Expand Down Expand Up @@ -172,7 +174,7 @@ fn on_jellyfish_drop(entity: Entity, max_ammo: u32) -> StaticSystem<(), ()> {
.system()
}

fn update_jellyfish_driving(
fn update_player_driving(
entities: Res<Entities>,
player_indexes: Comp<PlayerIdx>,
player_inventories: Comp<Inventory>,
Expand Down
2 changes: 0 additions & 2 deletions src/core/player/state/states/drive_jellyfish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ pub fn player_state_transition(
continue;
}
if player_state.current != *ID {
debug!("set player state driving");
player_state.current = *ID;
}
} else if player_state.current == *ID {
debug!("set player state idle");
player_state.current = *idle::ID;
}
}
Expand Down

0 comments on commit 33d6643

Please sign in to comment.