Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: separate alarm emote from face animations #901

Merged
merged 1 commit into from
Jan 15, 2024
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
4 changes: 3 additions & 1 deletion assets/game.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ core:
- ./player/skins/sharky/sharky.player.yaml
- ./player/skins/orcy/orcy.player.yaml

player_emotes:
alarm: /player/emotes/alarm.emote.yaml

player_hats:
- /player/hats/pirate.hat.yaml
- /player/hats/straw.hat.yaml
Expand Down Expand Up @@ -319,4 +322,3 @@ core:
- /elements/environment/coral_spikes/coral_spikes.element.yaml

experimental_maps: []

4 changes: 4 additions & 0 deletions assets/player/emotes/alarm.atlas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
image: ./alarm.png
tile_size: [46, 32]
columns: 2
rows: 1
10 changes: 10 additions & 0 deletions assets/player/emotes/alarm.emote.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Alarm
atlas: ./alarm.atlas.yaml

offset: [11, 15]
animation:
# Matches player face FPS
fps: 9
frames:
- 1
- 0
Binary file added assets/player/emotes/alarm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/player/skins/fishy/fishy-face.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/player/skins/orcy/orcy-face.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/player/skins/pescy/pescy-face.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/player/skins/sharky/sharky-face.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/core/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct CoreMeta {
pub config: CoreConfigMeta,
pub map_tilesets: SVec<Handle<Atlas>>,
pub players: SVec<Handle<PlayerMeta>>,
pub player_emotes: SMap<Ustr, Handle<EmoteMeta>>,
pub player_hats: SVec<Handle<HatMeta>>,
pub stable_maps: SVec<Handle<MapMeta>>,
pub map_elements: SVec<Handle<ElementMeta>>,
Expand Down
11 changes: 11 additions & 0 deletions src/core/metadata/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ fn deserialize_body_animations<'de, D: Deserializer<'de>>(
Ok(BodyAnimationsMeta { offsets, frames })
}

/// Metadata for a player emote.
#[derive(HasSchema, Clone, Debug, Default)]
#[type_data(metadata_asset("emote"))]
#[repr(C)]
pub struct EmoteMeta {
pub name: Ustr,
pub atlas: Handle<Atlas>,
pub offset: Vec2,
pub animation: AnimatedSprite,
}

/// Metadata for a player hat.
#[derive(HasSchema, Clone, Debug, Default)]
#[type_data(metadata_asset("hat"))]
Expand Down
95 changes: 86 additions & 9 deletions src/core/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ enum EmoteState {
#[default]
Neutral,
/// The player is emoting.
Emoting(Emote),
Emoting((Emote, Entity)),
}

/// A component representing a region in which a player should emote in some way.
Expand Down Expand Up @@ -115,11 +115,86 @@ pub enum Emote {
}

impl Emote {
pub fn animation_key(&self) -> &str {
pub fn face_animation_key(&self) -> Ustr {
match self {
Emote::Alarm => "emote_alarm",
Emote::Alarm => ustr("emote_alarm"),
}
}

pub fn emote_animation_key(&self) -> Ustr {
match self {
Emote::Alarm => ustr("alarm"),
}
}
}

impl Emote {
pub fn start_animation(player_ent: Entity, emote: Self) -> StaticSystem<(), ()> {
(move |meta: Root<GameMeta>,
assets: ResMut<AssetServer>,
mut emote_states: CompMut<EmoteState>,
mut entities: ResMut<Entities>,
mut transforms: CompMut<Transform>,
mut attachments: CompMut<PlayerBodyAttachment>,
mut sprites: CompMut<AtlasSprite>,
mut animated_sprites: CompMut<AnimatedSprite>| {
let emote_key = emote.emote_animation_key();
let Some(emote_meta) = meta
.core
.player_emotes
.iter()
.find(|(key, _)| **key == emote_key)
.map(|(_, handle)| assets.get(*handle))
else {
return;
};

let ent = entities.create();

let emote_state = emote_states.get_mut(player_ent).unwrap();
*emote_state = EmoteState::Emoting((emote, ent));

let transform = *transforms.get(player_ent).unwrap();
transforms.insert(ent, transform);
attachments.insert(
ent,
PlayerBodyAttachment {
player: player_ent,
offset: emote_meta.offset.extend(PlayerLayers::FACE_Z_OFFSET),
head: true,
..default()
},
);
sprites.insert(
ent,
AtlasSprite {
atlas: emote_meta.atlas,
..default()
},
);
animated_sprites.insert(
ent,
AnimatedSprite {
frames: emote_meta.animation.frames.clone(),
fps: emote_meta.animation.fps,
repeat: true,
..default()
},
);
})
.system()
}

pub fn stop_animation(player_ent: Entity) -> StaticSystem<(), ()> {
(move |mut emote_states: CompMut<EmoteState>, mut entities: ResMut<Entities>| {
let emote_state = emote_states.get_mut(player_ent).unwrap();
if let EmoteState::Emoting((_, emote_ent)) = *emote_state {
entities.kill(emote_ent);
}
*emote_state = EmoteState::Neutral;
})
.system()
}
}

/// Marker component indicating that a player has been killed.
Expand Down Expand Up @@ -842,6 +917,7 @@ fn player_facial_animations(
transforms: Comp<Transform>,
atlas_sprites: Comp<AtlasSprite>,
mut emote_states: CompMut<EmoteState>,
mut commands: Commands,
players_killed: Comp<PlayerKilled>,
animation_bank_sprites: CompMut<AnimationBankSprite>,
) {
Expand Down Expand Up @@ -899,17 +975,18 @@ fn player_facial_animations(

if let Some(new_emote) = triggered_emote {
if let EmoteState::Emoting(already_emote) = emote_state {
if new_emote != *already_emote {
player_layer.face_anim = new_emote.animation_key().into();
*emote_state = EmoteState::Emoting(new_emote);
if new_emote != already_emote.0 {
player_layer.face_anim = new_emote.face_animation_key();
commands.add(Emote::stop_animation(player_ent));
commands.add(Emote::start_animation(player_ent, new_emote));
}
} else {
player_layer.face_anim = new_emote.animation_key().into();
*emote_state = EmoteState::Emoting(new_emote);
player_layer.face_anim = new_emote.face_animation_key();
commands.add(Emote::start_animation(player_ent, new_emote));
}
} else {
*emote_state = EmoteState::Neutral;
player_layer.face_anim = animation_bank.current;
commands.add(Emote::stop_animation(player_ent));
}
}
}
Expand Down
Loading