From 628dd5ad8a05113abb2701fa98e6619464eb0470 Mon Sep 17 00:00:00 2001 From: artem Date: Sat, 27 Jan 2024 22:50:37 +0300 Subject: [PATCH] stack animations --- data/scene.rgs | Bin 15113 -> 16033 bytes game/src/lib.rs | 20 +- game/src/map.rs | 17 +- game/src/player.rs | 121 ++++++++- settings.ron | 634 ++++++++++++++++++++++----------------------- 5 files changed, 452 insertions(+), 340 deletions(-) diff --git a/data/scene.rgs b/data/scene.rgs index c26651618930d378e756440d9844c2b117ae9bba..762341b3f3d96a6ff9615440ab04079553ec4f84 100644 GIT binary patch delta 361 zcmeAyTUfhcI~(f(tFjH9lk=p+HeX`XWwNpQRK1^pfx&Qllj%Orp7VPY<7@5NxCQJM zEH&O25hJvZF{aX<1E?S%zc{lbGe3{JasDlbmaf}&4NZGDTXS+Svp&cdSr0N$d2=?; zKVH@aLWOHU+@GukbCZBJg6JieSXh4D|ivY#!&$(z@?>0mi3(7ytkO delta 212 zcmZ2j+gY|@I~(hVSs!cXOwN-M+kA;lm&t~apSk3twR2ap|* zUz}NznV-k~>-b5Bnw$@|tJz96TXS+SvrhP>(Eu_~d2)e)^X7FtyLnkR6bm+jgnqJa zCPx# diff --git a/game/src/lib.rs b/game/src/lib.rs index 3f5eb6d..40eb628 100644 --- a/game/src/lib.rs +++ b/game/src/lib.rs @@ -50,7 +50,7 @@ impl ScriptTrait for Player { if let WindowEvent::KeyboardInput { event, .. } = event { if let PhysicalKey::Code(keycode) = event.physical_key { let is_pressed = event.state == ElementState::Pressed; - self.key_checker(keycode, is_pressed); + self.key_checker(context, keycode, is_pressed); } } if let WindowEvent::Touch(touch) = event { @@ -58,27 +58,27 @@ impl ScriptTrait for Player { TouchPhase::Started => { Log::info(format!("touch x {:?}, y {:?}", touch.location.x, touch.location.y)); if touch.location.x > (self.window_width / 2) as f64 { - self.key_checker(KeyCode::ArrowRight, true); - self.key_checker(KeyCode::ArrowLeft, false); + self.key_checker(context, KeyCode::ArrowRight, true); + self.key_checker(context, KeyCode::ArrowLeft, false); } else { - self.key_checker(KeyCode::ArrowLeft, true); - self.key_checker(KeyCode::ArrowRight, false); + self.key_checker(context, KeyCode::ArrowLeft, true); + self.key_checker(context, KeyCode::ArrowRight, false); } if touch.location.y < (self.window_height / 2) as f64 { - self.key_checker(KeyCode::ArrowUp, true); + self.key_checker(context, KeyCode::ArrowUp, true); } if touch.location.y > (self.window_height / 2) as f64 { - self.key_checker(KeyCode::ArrowUp, false); + self.key_checker(context, KeyCode::ArrowUp, false); } }, TouchPhase::Ended => { if touch.location.x > (self.window_width / 2) as f64 { - self.key_checker(KeyCode::ArrowRight, false); + self.key_checker(context, KeyCode::ArrowRight, false); } else { - self.key_checker(KeyCode::ArrowLeft, false); + self.key_checker(context, KeyCode::ArrowLeft, false); } if touch.location.y < (self.window_height / 2) as f64 { - self.key_checker(KeyCode::ArrowUp, false); + self.key_checker(context, KeyCode::ArrowUp, false); } }, diff --git a/game/src/map.rs b/game/src/map.rs index 08ad1cd..5aa5206 100644 --- a/game/src/map.rs +++ b/game/src/map.rs @@ -69,7 +69,7 @@ fn build_dynamic_block( .with_mass(mass) // Turn off gravity for tile .with_gravity_scale(1.) - .with_lin_damping(1.0) + .with_lin_damping(2.0) // Set tile to be static and not rotate .with_rotation_locked(false) .with_body_type(RigidBodyType::Dynamic) @@ -186,11 +186,11 @@ fn build_stone_block(resource_manager: &ResourceManager, graph: &mut Graph, x: f build_block( graph, x as f32, - y + 1.0, + y + 0.7, 1.0, build_material( resource_manager, - "assets/data/objects/Mushroom_2.png".to_owned(), + "assets/data/objects/Stone.png".to_owned(), ), shape, ) @@ -245,5 +245,14 @@ pub fn build_map(graph: &mut Graph, resource_manager: &ResourceManager) { build_tree_block(resource_manager, graph, -30.0, bottom_y); build_box_block(resource_manager, graph, 10.0, bottom_y); - build_air_island_block(resource_manager, graph, 15.0, bottom_y + 2.0) + build_air_island_block(resource_manager, graph, 15.0, bottom_y + 2.5); + build_air_island_block(resource_manager, graph, 18.0, bottom_y + 4.0); + build_air_island_block(resource_manager, graph, 21.0, bottom_y + 5.5); + build_box_block(resource_manager, graph, 21.0, bottom_y+ 5.5); + + build_air_island_block(resource_manager, graph, -15.0, bottom_y + 2.5); + build_air_island_block(resource_manager, graph, -18.0, bottom_y + 4.0); + build_air_island_block(resource_manager, graph, -21.0, bottom_y + 5.5); + + } diff --git a/game/src/player.rs b/game/src/player.rs index ef97725..1bfe4f7 100644 --- a/game/src/player.rs +++ b/game/src/player.rs @@ -1,10 +1,12 @@ +use std::collections::VecDeque; + use fyrox::{ core::{ algebra::{Vector2, Vector3}, pool::Handle, reflect::prelude::*, - visitor::prelude::*, uuid_provider, + visitor::prelude::*, }, keyboard::KeyCode, scene::animation::spritesheet::SpriteSheetAnimation, @@ -20,16 +22,29 @@ pub struct Player { move_right: bool, jump: bool, animations: Vec, - current_animation: u32, + fight: bool, + + #[reflect(hidden)] + #[visit(skip)] + current_animation: VecDeque, current_frame: u32, start_y_position: f32, + start_fight_last: f32, prev_y_velosity: f32, pub window_height: u32, // TODO: need change place pub window_width: u32, } +#[derive(Clone, Copy, PartialEq)] +enum Animations { + Steps = 0, + Idle = 1, + Jump = 2, + Fight = 3 +} + uuid_provider!(Player = "c5671d19-9f1a-4286-8486-add4ebaadaec"); impl Player { @@ -53,11 +68,45 @@ impl Player { } } - pub fn key_checker(&mut self, keycode: KeyCode, is_pressed: bool) { + pub fn key_checker(&mut self, context: &mut ScriptContext, keycode: KeyCode, is_pressed: bool) { match keycode { KeyCode::ArrowLeft => self.move_left = is_pressed, KeyCode::ArrowRight => self.move_right = is_pressed, KeyCode::ArrowUp => self.jump = is_pressed, + KeyCode::Space => { + // if (context.elapsed_time - self.start_fight_last) > 0.3 { + // self.fight = false; + // } + if !is_pressed { + self.fight = false; + self.start_fight_last = 0.0; + return; + } + if (context.elapsed_time - self.start_fight_last) < 0.1 { + self.fight = false; + self.start_fight_last = context.elapsed_time; + return; + } + self.start_fight_last = context.elapsed_time; + self.fight = is_pressed; + // if self.fight && is_pressed { + // self.fight = false; + // return; + // } + // // if self.fight && !is_pressed { + // // self.start_fight_last = context.elapsed_time; + // // self.fight = false; + // // return; + // // } + // // if !self.fight && !is_pressed { + // // self.fight = false; + // // return; + // // } + // if !self.fight && is_pressed { + // self.fight = true; + // return; + // } + }, _ => (), } } @@ -144,23 +193,77 @@ impl Player { None => {} } } + fn add_animation_to_stack(&mut self, animation: Animations) { + if self.current_animation.len() > 2 { + return; + } + let mut last_anim: Option = None; + match self.current_animation.front() { + Some(data) => { + last_anim = Some(data.clone()); + }, + None => {} + } + match last_anim { + Some(last_anim_data) => { + if last_anim_data == (animation as u32) { + return; + } + if animation == Animations::Idle && last_anim_data != Animations::Idle as u32 { + return; + } + self.current_animation.push_front(animation as u32) + } + None => { + self.current_animation.push_front(animation as u32) + } + } + } + fn animate_choose(&mut self, context: &mut ScriptContext) { + let rigid_body = context.scene.graph[context.handle].cast_mut::(); + if let Some(rigid_data) = rigid_body { + if self.jump && (rigid_data.lin_vel().y as i32) < 1 { + self.add_animation_to_stack(Animations::Idle); + // self.current_animation.push_front(Animations::Idle as u32) ; + } + if self.jump { + self.add_animation_to_stack(Animations::Jump); + self.current_animation.push_front(Animations::Jump as u32); + } + } - fn animate_choose(&mut self) { let x_speed = self.get_speed(); - + if self.fight { + self.add_animation_to_stack(Animations::Fight); + // self.current_animation.push_front(Animations::Fight as u32); + } if x_speed != 0.0 { - self.current_animation = 0; + self.add_animation_to_stack(Animations::Steps); + // self.current_animation.push_front(Animations::Steps as u32); } else { - self.current_animation = 1; + self.add_animation_to_stack(Animations::Idle); + // self.current_animation.push_front(Animations::Idle as u32); } } + pub fn animation_do(&mut self, context: &mut ScriptContext) { - self.animate_choose(); - let current_animation = self.animations.get_mut(self.current_animation as usize); + self.animate_choose(context); + //let can_update_frame = self.is_need_update_anim_frame(context); + let mut current_animation = Animations::Idle as usize; + if !self.current_animation.is_empty() { + match self.current_animation.pop_back() { + Some(anim) => { + current_animation = anim as usize; + } + None => {} + } + } + let current_animation = self.animations.get_mut(current_animation ); match current_animation { Some(animation_data) => { animation_data.update(context.dt); + if let Some(sprite) = context .scene .graph diff --git a/settings.ron b/settings.ron index 6930542..cb90912 100644 --- a/settings.ron +++ b/settings.ron @@ -322,37 +322,73 @@ ), node_infos: { ( - index: 74, + index: 71, generation: 1, ): ( is_expanded: true, ), ( - index: 94, + index: 26, generation: 1, ): ( is_expanded: true, ), ( - index: 2, + index: 54, generation: 1, ): ( is_expanded: true, ), ( - index: 10, + index: 52, generation: 1, ): ( is_expanded: true, ), ( - index: 47, + index: 72, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 62, generation: 1, ): ( is_expanded: true, ), ( - index: 87, + index: 16, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 81, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 11, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 46, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 7, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 42, generation: 1, ): ( is_expanded: false, @@ -364,109 +400,7 @@ is_expanded: false, ), ( - index: 52, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 39, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 22, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 42, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 5, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 77, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 15, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 18, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 33, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 13, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 80, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 59, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 83, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 4, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 9, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 75, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 29, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 7, + index: 6, generation: 1, ): ( is_expanded: true, @@ -477,6 +411,42 @@ ): ( is_expanded: true, ), + ( + index: 50, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 48, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 2, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 69, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 66, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 3, + generation: 1, + ): ( + is_expanded: true, + ), ( index: 35, generation: 1, @@ -484,17 +454,197 @@ is_expanded: true, ), ( - index: 95, + index: 5, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 27, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 28, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 34, generation: 1, ): ( is_expanded: true, ), ( - index: 16, + index: 47, generation: 1, ): ( is_expanded: true, ), + ( + index: 91, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 49, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 59, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 57, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 20, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 1, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 13, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 45, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 87, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 29, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 60, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 17, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 33, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 36, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 44, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 24, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 86, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 74, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 32, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 55, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 77, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 30, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 80, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 14, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 82, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 15, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 22, + generation: 1, + ): ( + is_expanded: false, + ), ( index: 21, generation: 1, @@ -507,78 +657,12 @@ ): ( is_expanded: false, ), - ( - index: 6, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 36, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 62, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 25, - generation: 1, - ): ( - is_expanded: false, - ), ( index: 93, generation: 1, ): ( is_expanded: false, ), - ( - index: 41, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 23, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 84, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 37, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 45, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 92, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 90, - generation: 1, - ): ( - is_expanded: false, - ), ( index: 0, generation: 1, @@ -586,64 +670,58 @@ is_expanded: true, ), ( - index: 17, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 60, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 54, + index: 65, generation: 1, ): ( is_expanded: true, ), ( - index: 27, + index: 83, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 92, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 39, generation: 1, ): ( is_expanded: false, ), ( - index: 26, + index: 10, generation: 1, ): ( is_expanded: true, ), ( - index: 44, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 50, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 49, + index: 25, generation: 1, ): ( is_expanded: false, ), ( - index: 82, + index: 23, generation: 1, ): ( is_expanded: true, ), ( - index: 81, + index: 4, generation: 1, ): ( - is_expanded: false, + is_expanded: true, + ), + ( + index: 18, + generation: 1, + ): ( + is_expanded: true, ), ( index: 56, @@ -652,13 +730,31 @@ is_expanded: true, ), ( - index: 28, + index: 90, generation: 1, ): ( is_expanded: false, ), ( - index: 30, + index: 95, + generation: 1, + ): ( + is_expanded: true, + ), + ( + index: 9, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 37, + generation: 1, + ): ( + is_expanded: false, + ), + ( + index: 58, generation: 1, ): ( is_expanded: true, @@ -670,61 +766,37 @@ is_expanded: true, ), ( - index: 57, + index: 63, generation: 1, ): ( is_expanded: false, ), ( - index: 40, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 66, + index: 19, generation: 1, ): ( is_expanded: false, ), ( - index: 20, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 71, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 48, + index: 75, generation: 1, ): ( is_expanded: false, ), ( - index: 1, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 69, + index: 78, generation: 1, ): ( is_expanded: false, ), ( - index: 46, + index: 43, generation: 1, ): ( is_expanded: false, ), ( - index: 51, + index: 94, generation: 1, ): ( is_expanded: true, @@ -736,83 +808,17 @@ is_expanded: true, ), ( - index: 65, + index: 40, generation: 1, ): ( is_expanded: true, ), ( - index: 43, + index: 84, generation: 1, ): ( is_expanded: false, ), - ( - index: 24, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 14, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 58, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 55, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 86, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 78, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 11, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 91, - generation: 1, - ): ( - is_expanded: true, - ), - ( - index: 19, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 3, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 34, - generation: 1, - ): ( - is_expanded: true, - ), ( index: 31, generation: 1, @@ -820,19 +826,13 @@ is_expanded: true, ), ( - index: 72, + index: 41, generation: 1, ): ( - is_expanded: false, + is_expanded: true, ), ( - index: 63, - generation: 1, - ): ( - is_expanded: false, - ), - ( - index: 32, + index: 51, generation: 1, ): ( is_expanded: true,