Skip to content

Commit

Permalink
Add The Ability to Give Passive Effects Sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Apr 8, 2022
1 parent e5c5db3 commit 23bbbf3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/effects/passive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hecs::{Entity, World};
mod turtle_shell;

use crate::player::PlayerEventKind;
use crate::PlayerEvent;
use crate::{AnimatedSprite, AnimatedSpriteMetadata, PlayerEvent};

static mut PASSIVE_EFFECT_FUNCS: Option<HashMap<String, PassiveEffectFn>> = None;

Expand Down Expand Up @@ -46,6 +46,8 @@ pub struct PassiveEffectInstance {
pub name: String,
pub function: Option<PassiveEffectFn>,
pub activated_on: Vec<PlayerEventKind>,
pub sprite: Option<AnimatedSprite>,
pub sprite_entity: Option<Entity>,
pub particle_effect_id: Option<String>,
pub event_particle_effect_id: Option<String>,
pub blocks_damage: bool,
Expand All @@ -64,6 +66,8 @@ impl PassiveEffectInstance {
name: meta.name,
function,
activated_on: meta.activated_on,
sprite: meta.sprite.map(Into::into),
sprite_entity: None,
particle_effect_id: meta.particle_effect_id,
event_particle_effect_id: meta.event_particle_effect_id,
blocks_damage: meta.blocks_damage,
Expand Down Expand Up @@ -127,4 +131,8 @@ pub struct PassiveEffectMetadata {
/// This is the duration of the effect.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub duration: Option<f32>,

/// An optional sprite to add to the player along with the effect
#[serde(alias = "animation")]
pub sprite: Option<AnimatedSpriteMetadata>,
}
62 changes: 57 additions & 5 deletions src/player/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use crate::player::{
Player, PlayerAttributes, PlayerController, PlayerEventQueue, JUMP_SOUND_ID, LAND_SOUND_ID,
RESPAWN_DELAY,
};
use crate::{CollisionWorld, Item, Map, PhysicsBody, PlayerEvent, Resources};
use crate::{
CollisionWorld, Drawable, DrawableKind, Item, Map, PhysicsBody, PlayerEvent, Resources,
};

const SLIDE_STOP_THRESHOLD: f32 = 2.0;
const JUMP_FRAME_COUNT: u16 = 8;
Expand Down Expand Up @@ -193,16 +195,56 @@ pub fn update_player_states(world: &mut World) {
pub fn update_player_passive_effects(world: &mut World) {
let mut function_calls = Vec::new();

for (entity, (player, events)) in world.query::<(&mut Player, &mut PlayerEventQueue)>().iter() {
let mut sprites_to_spawn = Vec::new();
let mut sprites_to_despawn = Vec::new();

for (entity, (player, player_transform, player_drawable, events)) in world
.query::<(&mut Player, &Transform, &Drawable, &mut PlayerEventQueue)>()
.iter()
{
let dt = get_frame_time();

dbg!(player
.passive_effects
.iter()
.map(|x| &x.name)
.collect::<Vec<_>>());

for effect in &mut player.passive_effects {
effect.duration_timer += dt;

// Move the sprite to follow the player
if let Some(sprite_entity) = effect.sprite_entity {
let mut transform = world.get_mut::<Transform>(sprite_entity).unwrap();
transform.position = player_transform.position;
}

// Spawn the effect sprite if it hasn't been spawned yet
if let Some(sprite) = effect.sprite.take() {
let sprite_entity = world.reserve_entity();

let drawable = Drawable {
draw_order: player_drawable.draw_order + 1,
kind: DrawableKind::AnimatedSprite(sprite),
};

sprites_to_spawn.push((sprite_entity, drawable, player_transform.position));

effect.sprite_entity = Some(sprite_entity);
}
}

player
.passive_effects
.retain(|effect| !effect.is_depleted());
player.passive_effects.retain(|effect| {
if effect.is_depleted() {
if let Some(sprite_entity) = effect.sprite_entity {
sprites_to_despawn.push(sprite_entity);
}

false
} else {
true
}
});

events.queue.push(PlayerEvent::Update { dt });

Expand Down Expand Up @@ -230,6 +272,16 @@ pub fn update_player_passive_effects(world: &mut World) {
for (f, player_entity, item_entity, event) in function_calls.drain(0..) {
f(world, player_entity, item_entity, event);
}

for (sprite_entity, drawable, position) in sprites_to_spawn {
world
.insert(sprite_entity, (Transform::from(position), drawable))
.unwrap();
}

for entity in sprites_to_despawn {
world.despawn(entity).unwrap();
}
}

pub fn on_player_damage(world: &mut World, damage_from_entity: Entity, damage_to_entity: Entity) {
Expand Down

0 comments on commit 23bbbf3

Please sign in to comment.