Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/drawables/animated_sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,27 @@ pub struct AnimatedSprite {
pub wait_timer: f32,
}

impl From<AnimatedSpriteMetadata> for AnimatedSprite {
fn from(meta: AnimatedSpriteMetadata) -> Self {
let animations = meta
.animations
.into_iter()
.map(Into::into)
.collect::<Vec<_>>();

let params = AnimatedSpriteParams {
scale: meta.scale.unwrap_or(1.0),
offset: meta.offset,
pivot: meta.pivot,
tint: meta.tint.unwrap_or(color::WHITE),
autoplay_id: meta.autoplay_id,
..Default::default()
};

AnimatedSprite::new(&meta.texture_id, animations.as_slice(), params)
}
}

impl AnimatedSprite {
pub fn new(texture_id: &str, animations: &[Animation], params: AnimatedSpriteParams) -> Self {
let animations = animations.to_vec();
Expand Down
19 changes: 6 additions & 13 deletions src/effects/active/triggered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::particles::{ParticleEmitter, ParticleEmitterMetadata};
use crate::physics;
use crate::player::{Player, PlayerState};
use crate::{ActiveEffectMetadata, AnimatedSpriteMetadata, CollisionWorld, PhysicsBody};
use crate::{Drawable, PhysicsBodyParams};
use crate::{Drawable, DrawableKind, PhysicsBodyParams};

const TRIGGERED_EFFECT_DRAW_ORDER: u32 = 5;

Expand Down Expand Up @@ -114,19 +114,12 @@ pub fn spawn_triggered_effect(
));

if let Some(meta) = meta.sprite.clone() {
let animations = meta
.animations
.clone()
.into_iter()
.map(|a| a.into())
.collect::<Vec<_>>();
let animated_sprite = meta.into();

let mut drawable = Drawable::new_animated_sprite(
TRIGGERED_EFFECT_DRAW_ORDER,
&meta.texture_id,
animations.as_slice(),
meta.clone().into(),
);
let mut drawable = Drawable {
draw_order: TRIGGERED_EFFECT_DRAW_ORDER,
kind: DrawableKind::AnimatedSprite(animated_sprite),
};

{
let sprite = drawable.get_animated_sprite_mut().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/effects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ pub use active::{ActiveEffectKind, ActiveEffectMetadata, TriggeredEffectTrigger}
#[serde(untagged)]
pub enum AnyEffectParams {
Active(ActiveEffectMetadata),
Passive(PassiveEffectMetadata),
Passive(Box<PassiveEffectMetadata>),
}
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>,
}
28 changes: 3 additions & 25 deletions src/gui/select_character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use crate::gui::{
};
use crate::player::PlayerCharacterMetadata;
use crate::{
draw_one_animated_sprite, update_one_animated_sprite, AnimatedSprite, AnimatedSpriteMetadata,
Resources,
draw_one_animated_sprite, update_one_animated_sprite, AnimatedSpriteMetadata, Resources,
};
use core::input::{update_gamepad_context, GameInputScheme};
use core::Transform;
Expand Down Expand Up @@ -59,17 +58,7 @@ pub async fn show_select_characters_menu(

let meta: AnimatedSpriteMetadata = character.sprite.clone().into();

let animations = meta
.animations
.iter()
.cloned()
.map(|a| a.into())
.collect::<Vec<_>>();

let sprite =
AnimatedSprite::new(&meta.texture_id, animations.as_slice(), meta.clone().into());

animated_sprites.push(sprite);
animated_sprites.push(meta.into());
}

let mut is_ready = false;
Expand Down Expand Up @@ -246,18 +235,7 @@ pub async fn show_select_characters_menu(

let meta: AnimatedSpriteMetadata = character.sprite.clone().into();

let animations = meta
.animations
.iter()
.cloned()
.map(|a| a.into())
.collect::<Vec<_>>();

animated_sprites[i] = AnimatedSprite::new(
&meta.texture_id,
animations.as_slice(),
meta.clone().into(),
);
animated_sprites[i] = meta.into();
}

is_ready = !selected_params.iter().any(|params| params.is_none());
Expand Down
33 changes: 4 additions & 29 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use macroquad::prelude::*;
use serde::{Deserialize, Serialize};

use crate::{
ActiveEffectMetadata, AnimatedSprite, AnimatedSpriteMetadata, CollisionWorld, Drawable,
PassiveEffectMetadata, PhysicsBody, QueuedAnimationAction, Resources,
ActiveEffectMetadata, AnimatedSpriteMetadata, CollisionWorld, Drawable, PassiveEffectMetadata,
PhysicsBody, QueuedAnimationAction, Resources,
};

use core::{Result, Transform};
Expand Down Expand Up @@ -178,19 +178,7 @@ pub fn spawn_item(world: &mut World, position: Vec2, meta: MapItemMetadata) -> R
collider_size.y as i32,
);

let animations = meta
.sprite
.animations
.clone()
.into_iter()
.map(|a| a.into())
.collect::<Vec<_>>();

let sprite = AnimatedSprite::new(
&meta.sprite.texture_id,
animations.as_slice(),
meta.sprite.clone().into(),
);
let sprite = meta.sprite.into();

sprites.push((SPRITE_ANIMATED_SPRITE_ID, sprite));

Expand Down Expand Up @@ -257,20 +245,7 @@ pub fn spawn_item(world: &mut World, position: Vec2, meta: MapItemMetadata) -> R
}

if let Some(effect_sprite) = meta.effect_sprite {
let animations = effect_sprite
.animations
.clone()
.into_iter()
.map(|a| a.into())
.collect::<Vec<_>>();

let mut sprite = AnimatedSprite::new(
&effect_sprite.texture_id,
animations.as_slice(),
effect_sprite.clone().into(),
);

sprite.is_deactivated = true;
let sprite = effect_sprite.into();

sprites.push((EFFECT_ANIMATED_SPRITE_ID, sprite));
}
Expand Down
20 changes: 6 additions & 14 deletions src/map/decoration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use hecs::{Entity, World};

use serde::{Deserialize, Serialize};

use crate::{AnimatedSpriteMetadata, Drawable};
use crate::{AnimatedSpriteMetadata, Drawable, DrawableKind};
use core::Transform;

const DECORATION_DRAW_ORDER: u32 = 0;
Expand All @@ -26,22 +26,14 @@ impl Decoration {
}

pub fn spawn_decoration(world: &mut World, position: Vec2, meta: DecorationMetadata) -> Entity {
let animations = meta
.sprite
.animations
.clone()
.into_iter()
.map(|m| m.into())
.collect::<Vec<_>>();
let sprite = meta.sprite.into();

world.spawn((
Decoration::new(&meta.id),
Transform::from(position),
Drawable::new_animated_sprite(
DECORATION_DRAW_ORDER,
&meta.sprite.texture_id,
animations.as_slice(),
meta.sprite.clone().into(),
),
Drawable {
draw_order: DECORATION_DRAW_ORDER,
kind: DrawableKind::AnimatedSprite(sprite),
},
))
}
56 changes: 51 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,50 @@ 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();

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 +266,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