Skip to content

Commit

Permalink
User proper types for action of animations
Browse files Browse the repository at this point in the history
  • Loading branch information
hasenbanck committed Dec 15, 2024
1 parent 90fae2d commit a3d56bb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
18 changes: 11 additions & 7 deletions korangar/src/interface/cursor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ pub enum MouseCursorState {
Unsure3,
}

impl Into<usize> for MouseCursorState {
fn into(self) -> usize {
self as usize
}
}

pub struct MouseCursor {
sprite: Arc<Sprite>,
actions: Arc<Actions>,
animation_state: AnimationState,
animation_state: AnimationState<MouseCursorState>,
shown: bool,
}

impl MouseCursor {
pub fn new(sprite_loader: &mut SpriteLoader, action_loader: &mut ActionLoader) -> Self {
let sprite = sprite_loader.get("cursors.spr").unwrap();
let actions = action_loader.get("cursors.act").unwrap();
let animation_state = AnimationState::new(ClientTick(0));
let animation_state = AnimationState::new(MouseCursorState::Default, ClientTick(0));
let shown = true;

Self {
Expand Down Expand Up @@ -70,13 +76,11 @@ impl MouseCursor {
}

pub fn set_state(&mut self, state: MouseCursorState, client_tick: ClientTick) {
let new_state = state as usize;

if self.animation_state.action != new_state {
if self.animation_state.action != state {
self.animation_state.start_time = client_tick;
}

self.animation_state.action = new_state;
self.animation_state.action = state;
}

#[cfg_attr(feature = "debug", korangar_debug::profile("render mouse cursor"))]
Expand Down Expand Up @@ -116,7 +120,7 @@ impl MouseCursor {

// TODO: figure out how this is actually supposed to work
let direction = match self.animation_state.action {
0 | 2 | 4 => 0,
MouseCursorState::Default | MouseCursorState::Click | MouseCursorState::RotateCamera => 0,
_ => 7,
};

Expand Down
4 changes: 2 additions & 2 deletions korangar/src/inventory/skills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use korangar_interface::state::{PlainRemote, PlainTrackedState, TrackedState};
use ragnarok_packets::{ClientTick, SkillId, SkillInformation, SkillLevel, SkillType};

use crate::loaders::{ActionLoader, Actions, AnimationState, Sprite, SpriteLoader};
use crate::loaders::{ActionLoader, ActionType, Actions, AnimationState, Sprite, SpriteLoader};

#[derive(Clone, Debug)]
pub struct Skill {
Expand Down Expand Up @@ -38,7 +38,7 @@ impl SkillTree {
sprite,
actions,
// FIX: give correct client tick
animation_state: AnimationState::new(ClientTick(0)),
animation_state: AnimationState::new(ActionType::Idle, ClientTick(0)),
}
})
.collect();
Expand Down
39 changes: 28 additions & 11 deletions korangar/src/loaders/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,23 @@ use crate::renderer::SpriteRenderer;
const MAX_CACHE_COUNT: u32 = 256;
const MAX_CACHE_SIZE: usize = 64 * 1024 * 1024;

#[derive(Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum ActionType {
#[default]
Idle = 0,
Walk = 1,
Dead = 8,
}

impl Into<usize> for ActionType {
fn into(self) -> usize {
self as usize
}
}

#[derive(Clone, Debug, new)]
pub struct AnimationState {
#[new(default)]
pub action: usize,
pub struct AnimationState<T = ActionType> {
pub action: T,
pub start_time: ClientTick,
#[new(default)]
pub time: u32,
Expand All @@ -38,28 +51,30 @@ pub struct AnimationState {
pub factor: Option<f32>,
}

impl AnimationState {
impl AnimationState<ActionType> {
pub fn idle(&mut self, client_tick: ClientTick) {
self.action = 0;
self.action = ActionType::Idle;
self.start_time = client_tick;
self.duration = None;
self.factor = None;
}

pub fn walk(&mut self, movement_speed: usize, client_tick: ClientTick) {
self.action = 1;
self.action = ActionType::Walk;
self.start_time = client_tick;
self.duration = None;
self.factor = Some(movement_speed as f32 * 100.0 / 150.0);
}

pub fn dead(&mut self, client_tick: ClientTick) {
self.action = 8;
self.action = ActionType::Dead;
self.start_time = client_tick;
self.duration = None;
self.factor = None;
}
}

impl<T> AnimationState<T> {
pub fn update(&mut self, client_tick: ClientTick) {
let mut time = client_tick.0.saturating_sub(self.start_time.0);

Expand Down Expand Up @@ -90,18 +105,20 @@ pub struct Actions {
}

impl Actions {
pub fn render(
pub fn render<T>(
&self,
renderer: &impl SpriteRenderer,
sprite: &Sprite,
animation_state: &AnimationState,
animation_state: &AnimationState<T>,
position: ScreenPosition,
camera_direction: usize,
color: Color,
application: &InterfaceSettings,
) {
) where
T: Into<usize> + Copy,
{
let direction = camera_direction % 8;
let aa = animation_state.action * 8 + direction;
let aa = animation_state.action.into() * 8 + direction;
let a = &self.actions[aa % self.actions.len()];
let delay = self.delays[aa % self.delays.len()];

Expand Down
6 changes: 3 additions & 3 deletions korangar/src/world/animation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ragnarok_packets::EntityId;
#[cfg(feature = "debug")]
use crate::graphics::DebugRectangleInstruction;
use crate::graphics::{Color, EntityInstruction};
use crate::loaders::{Actions, AnimationState, Sprite};
use crate::loaders::{ActionType, Actions, AnimationState, Sprite};
use crate::world::{Camera, EntityType};

const TILE_SIZE: f32 = 10.0;
Expand Down Expand Up @@ -86,7 +86,7 @@ impl AnimationData {
pub fn get_frame(&self, animation_state: &AnimationState, camera: &dyn Camera, head_direction: usize) -> &AnimationFrame {
let camera_direction = camera.camera_direction();
let direction = (camera_direction + head_direction) % 8;
let aa = animation_state.action * 8 + direction;
let aa = animation_state.action as usize * 8 + direction;
let delay = self.delays[aa % self.delays.len()];
let animation = &self.animations[aa % self.animations.len()];

Expand All @@ -106,7 +106,7 @@ impl AnimationData {
let mut frame = &animation.frames[time];

// Remove Doridori animation from Player
if self.entity_type == EntityType::Player && animation_state.action == 0 {
if self.entity_type == EntityType::Player && animation_state.action == ActionType::Idle {
frame = &animation.frames[0];
}
return frame;
Expand Down
6 changes: 3 additions & 3 deletions korangar/src/world/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::interface::application::InterfaceSettings;
use crate::interface::layout::{ScreenPosition, ScreenSize};
use crate::interface::theme::GameTheme;
use crate::interface::windows::WindowCache;
use crate::loaders::{ActionLoader, AnimationLoader, AnimationState, ScriptLoader, SpriteLoader};
use crate::loaders::{ActionLoader, ActionType, AnimationLoader, AnimationState, ScriptLoader, SpriteLoader};
use crate::renderer::GameInterfaceRenderer;
#[cfg(feature = "debug")]
use crate::renderer::MarkerRenderer;
Expand Down Expand Up @@ -325,7 +325,7 @@ impl Common {
.get(sprite_loader, action_loader, entity_type, &entity_part_files)
.unwrap();
let details = ResourceState::Unavailable;
let animation_state = AnimationState::new(client_tick);
let animation_state = AnimationState::new(ActionType::Idle, client_tick);

let mut common = Self {
grid_position,
Expand Down Expand Up @@ -551,7 +551,7 @@ impl Common {
if steps.len() > 1 {
self.active_movement = Movement::new(steps, starting_timestamp.0).into();

if self.animation_state.action != 1 {
if self.animation_state.action != ActionType::Walk {
self.animation_state.walk(self.movement_speed, starting_timestamp);
}
}
Expand Down

0 comments on commit a3d56bb

Please sign in to comment.