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

Add entity animation sounds #208

Merged
merged 1 commit into from
Dec 27, 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
67 changes: 58 additions & 9 deletions korangar/src/loaders/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::sync::Arc;

use cgmath::{Array, Vector2};
use derive_new::new;
use korangar_audio::{AudioEngine, SoundEffectKey};
#[cfg(feature = "debug")]
use korangar_debug::logging::{print_debug, Colorize, Timer};
use korangar_interface::elements::PrototypeElement;
use korangar_interface::elements::{ElementCell, PrototypeElement};
use korangar_util::container::{Cacheable, SimpleCache};
use korangar_util::FileLoader;
use ragnarok_bytes::{ByteReader, FromBytes};
Expand All @@ -25,6 +26,11 @@ use crate::renderer::SpriteRenderer;
const MAX_CACHE_COUNT: u32 = 256;
const MAX_CACHE_SIZE: usize = 64 * 1024 * 1024;

// TODO: NHA The numeric value of action types are based on the EntityType!
// For example "Dead" is 8 for the PC and 4 for a monster.
// This means we need to refactor the AnimationState, so that the mouse
// uses a different animation state struct (since we can't do simple usize
// conversions).
#[derive(Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum ActionType {
#[default]
Expand All @@ -39,6 +45,26 @@ impl From<ActionType> for usize {
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum ActionEvent {
/// Start playing a WAV sound file.
Sound { key: SoundEffectKey },
/// An attack event when the "flinch" animation is played.
Attack,
/// Start playing a WAV sound file.
Unknown,
}

impl PrototypeElement<InterfaceSettings> for ActionEvent {
fn to_element(&self, display: String) -> ElementCell<InterfaceSettings> {
match self {
Self::Sound { .. } => PrototypeElement::to_element(&"Sound", display),
Self::Attack => PrototypeElement::to_element(&"Attack", display),
Self::Unknown => PrototypeElement::to_element(&"Unknown", display),
}
}
}

#[derive(Clone, Debug, new)]
pub struct AnimationState<T = ActionType> {
pub action: T,
Expand Down Expand Up @@ -100,6 +126,8 @@ impl<T> AnimationState<T> {
pub struct Actions {
pub actions: Vec<Action>,
pub delays: Vec<f32>,
#[hidden_element]
pub events: Vec<ActionEvent>,
#[cfg(feature = "debug")]
actions_data: ActionsData,
}
Expand All @@ -118,9 +146,9 @@ impl Actions {
T: Into<usize> + Copy,
{
let direction = camera_direction % 8;
let aa = animation_state.action.into() * 8 + direction;
let a = &self.actions[aa % self.actions.len()];
let delay = self.delays[aa % self.delays.len()];
let animation_action = animation_state.action.into() * 8 + direction;
let action = &self.actions[animation_action % self.actions.len()];
let delay = self.delays[animation_action % self.delays.len()];

let factor = animation_state
.factor
Expand All @@ -129,14 +157,14 @@ impl Actions {

let frame = animation_state
.duration
.map(|duration| animation_state.time * a.motions.len() as u32 / duration)
.map(|duration| animation_state.time * action.motions.len() as u32 / duration)
.unwrap_or_else(|| (animation_state.time as f32 / factor) as u32);
// TODO: work out how to avoid losing digits when casting timg to an f32. When
// TODO: work out how to avoid losing digits when casting timing to an f32. When
// fixed remove set_start_time in MouseCursor.

let fs = &a.motions[frame as usize % a.motions.len()];
let motion = &action.motions[frame as usize % action.motions.len()];

for sprite_clip in &fs.sprite_clips {
for sprite_clip in &motion.sprite_clips {
// `get` instead of a direct index in case a fallback was loaded
let Some(texture) = sprite.textures.get(sprite_clip.sprite_number as usize) else {
return;
Expand Down Expand Up @@ -186,13 +214,15 @@ impl Cacheable for Actions {

pub struct ActionLoader {
game_file_loader: Arc<GameFileLoader>,
audio_engine: Arc<AudioEngine<GameFileLoader>>,
cache: SimpleCache<String, Arc<Actions>>,
}

impl ActionLoader {
pub fn new(game_file_loader: Arc<GameFileLoader>) -> Self {
pub fn new(game_file_loader: Arc<GameFileLoader>, audio_engine: Arc<AudioEngine<GameFileLoader>>) -> Self {
Self {
game_file_loader,
audio_engine,
cache: SimpleCache::new(
NonZeroU32::new(MAX_CACHE_COUNT).unwrap(),
NonZeroUsize::new(MAX_CACHE_SIZE).unwrap(),
Expand Down Expand Up @@ -231,6 +261,24 @@ impl ActionLoader {
}
};

let events: Vec<ActionEvent> = actions_data
.events
.iter()
.enumerate()
.map(|(_index, event)| {
if event.name.ends_with(".wav") {
let key = self.audio_engine.load(&event.name);
ActionEvent::Sound { key }
} else if event.name == "atk" || event.name == "atk.txt" {
ActionEvent::Attack
} else {
#[cfg(feature = "debug")]
print_debug!("Found unknown event at index `{}`: {:?}", _index, event.name);
ActionEvent::Unknown
}
})
.collect();

#[cfg(feature = "debug")]
let saved_actions_data = actions_data.clone();

Expand All @@ -241,6 +289,7 @@ impl ActionLoader {
let sprite = Arc::new(Actions {
actions: actions_data.actions,
delays,
events,
#[cfg(feature = "debug")]
actions_data: saved_actions_data,
});
Expand Down
19 changes: 18 additions & 1 deletion korangar/src/loaders/animation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use korangar_util::container::SimpleCache;
use num::Zero;

use super::error::LoadError;
use crate::loaders::{ActionLoader, SpriteLoader};
use crate::loaders::{ActionEvent, ActionLoader, SpriteLoader};
use crate::world::{Animation, AnimationData, AnimationFrame, AnimationFramePart, AnimationPair};
use crate::{Color, EntityType};

Expand Down Expand Up @@ -155,7 +155,18 @@ impl AnimationLoader {
color,
..Default::default()
};

let event: Option<ActionEvent> = if let Some(event_id) = motion.event_id
&& event_id != -1
&& let Some(event) = animation_pair.actions.events.get(event_id as usize).copied()
{
Some(event)
} else {
None
};

let frame = AnimationFrame {
event,
size,
top_left: Vector2::zero(),
offset,
Expand Down Expand Up @@ -423,7 +434,9 @@ fn merge_frame(frames: &mut [AnimationFrame]) -> AnimationFrame {
},
..Default::default()
};

let frame = AnimationFrame {
event: None,
size: Vector2::new(1, 1),
top_left: Vector2::zero(),
offset: Vector2::zero(),
Expand All @@ -433,6 +446,7 @@ fn merge_frame(frames: &mut [AnimationFrame]) -> AnimationFrame {
#[cfg(feature = "debug")]
vertical_matrix: Matrix4::identity(),
};

return frame;
}

Expand All @@ -454,6 +468,8 @@ fn merge_frame(frames: &mut [AnimationFrame]) -> AnimationFrame {
new_frame_parts.append(&mut frame.frame_parts);
}

let event = frames.iter().filter_map(|frame| frame.event).next();

// The origin is set at (0,0).
//
// The top-left point of the rectangle is calculated as
Expand All @@ -465,6 +481,7 @@ fn merge_frame(frames: &mut [AnimationFrame]) -> AnimationFrame {
// The new offset is calculated as
// center_point - origin.
AnimationFrame {
event,
size: Vector2::new(new_width, new_height),
top_left: Vector2::zero(),
offset: Vector2::new(top_left_x + (new_width - 1) / 2, top_left_y + (new_height - 1) / 2),
Expand Down
Loading
Loading