diff --git a/src/entity/target.rs b/src/entity/target.rs index 8177fb5..38f606c 100644 --- a/src/entity/target.rs +++ b/src/entity/target.rs @@ -41,7 +41,7 @@ enum PatrolState { } #[derive(Clone)] -pub struct Target { +pub struct SphereTarget { shooted: bool, delete_timer: Option, validity: Option, @@ -53,7 +53,7 @@ pub struct Target { pub const SPEED_LIN: f32 = 5.0; pub const SPEED_POL: f32 = 0.3; -impl Target { +impl SphereTarget { pub fn new(validity: Option, patrol: Patrol) -> Self { let validity_state = if let Some(x) = &validity { ValidityState::Valid(Timer::new(x.valid_duration)) @@ -109,14 +109,14 @@ impl Target { ); audio_context.push(Sink::Regular(sink)); - if !self.is_fake_target() { + if !self.is_invalid_target() { self.shooted = true; self.delete_timer = Some(Timer::new(0.3)); } self.shooted } - pub fn is_fake_target(&self) -> bool { + pub fn is_invalid_target(&self) -> bool { match self.validity_state { ValidityState::Invalid(_) => true, _ => false, @@ -195,7 +195,6 @@ impl Target { match &self.patrol_state { PatrolState::AToB => { if (*c - b).abs() <= SPEED_POL { - //println!("State B to A {} {} {}", a, b, c); self.patrol_state = PatrolState::BToA; } else { let mut sign = (b - *c).signum(); @@ -208,7 +207,6 @@ impl Target { } PatrolState::BToA => { if (*c - a).abs() <= SPEED_POL { - //println!("State A to B {} {} {}", a, b, c); self.patrol_state = PatrolState::AToB; } else { let mut sign = (a - *c).signum(); diff --git a/src/game.rs b/src/game.rs index b285807..a0ebf36 100644 --- a/src/game.rs +++ b/src/game.rs @@ -110,15 +110,15 @@ impl Game { if self.window.is_cursor_grabbed() { let mut dir_diff = nalgebra::Vector2::new(0.0, 0.0); if self.input_manager.is_keyboard_press(&VirtualKeyCode::Left) { - dir_diff.x += 400.0 * delta_time; + dir_diff.x += 400.0; } else if self.input_manager.is_keyboard_press(&VirtualKeyCode::Right) { - dir_diff.x -= 400.0 * delta_time; + dir_diff.x -= 400.0; } if self.input_manager.is_keyboard_press(&VirtualKeyCode::Up) { - dir_diff.y += 400.0 * delta_time; + dir_diff.y += 400.0; } else if self.input_manager.is_keyboard_press(&VirtualKeyCode::Down) { - dir_diff.y -= 400.0 * delta_time; + dir_diff.y -= 400.0; } self.input_manager.mouse_movement += dir_diff; diff --git a/src/scene/classic_game_scene.rs b/src/scene/classic_game_scene.rs index b35225e..3c58ec8 100644 --- a/src/scene/classic_game_scene.rs +++ b/src/scene/classic_game_scene.rs @@ -13,7 +13,7 @@ use crate::animation::InOutAnimation; use crate::audio::{AudioContext, AUDIO_FILE_SHOOT}; use crate::audio::{Sink, AUDIO_FILE_SHOOTED}; use crate::database::Database; -use crate::entity::target::{Patrol, Target, Validity}; +use crate::entity::target::{Patrol, SphereTarget, Validity}; use crate::gui::ConrodHandle; use crate::input_manager::InputManager; @@ -131,7 +131,7 @@ impl ClassicGameScene { &mut world, &mut physics, Vector3::new(0.0, 3.0, -15.0), - Target::new(None, Patrol::None), + SphereTarget::new(None, Patrol::None), ); spawn_container( @@ -274,6 +274,8 @@ impl Scene for ClassicGameScene { _control_flow: &mut ControlFlow, _database: &mut Database, ) -> SceneOp { + let mut shoot_trigger = false; + let round_timer_sec = self.round_timer.get_duration(); let mut ui_cell = conrod_handle.get_ui_mut().set_widgets(); @@ -337,7 +339,9 @@ impl Scene for ClassicGameScene { let mut scene_op = SceneOp::None; if !self.freeze { - renderer.camera.move_direction(input_manager.mouse_movement); + renderer + .camera + .move_direction(input_manager.mouse_movement * delta_time); let _player_position = update_player_position( delta_time, @@ -393,7 +397,7 @@ impl Scene for ClassicGameScene { delta_time, ); - self.shoot(input_manager, audio_context, &renderer.camera, delta_time); + shoot_trigger = true; if self.round_timer.is_finished() { self.game_state = GameState::Finishing(Timer::new(FINISHING_DURATION)); @@ -434,6 +438,10 @@ impl Scene for ClassicGameScene { &self.physics.rigid_body_set, &self.physics.collider_set, ); + + if shoot_trigger { + self.shoot(&input_manager, audio_context, &renderer.camera, delta_time); + } } drop(ui_cell); @@ -523,7 +531,7 @@ impl ClassicGameScene { let entity = Entity::from_bits(collider.user_data as u64).unwrap(); let mut need_to_spawn = false; - if let Ok(mut target) = self.world.get_mut::(entity) { + if let Ok(mut target) = self.world.get_mut::(entity) { if target.try_shoot(audio_context) { let shoot_time = self.delta_shoot_time.get_duration(); self.delta_shoot_time.reset(); @@ -565,8 +573,9 @@ impl ClassicGameScene { fn target_disposal(&mut self) { let mut missed_secondary = false; let mut fake_secondary = false; - for (id, (target, collider_handle)) in - self.world.query_mut::<(&mut Target, &ColliderHandle)>() + for (id, (target, collider_handle)) in self + .world + .query_mut::<(&mut SphereTarget, &ColliderHandle)>() { if target.is_need_to_be_deleted() { self.entity_to_remove.push(id); @@ -578,7 +587,7 @@ impl ClassicGameScene { ); if !target.is_shooted() { - if target.is_fake_target() { + if target.is_invalid_target() { fake_secondary = true; } missed_secondary = true; @@ -611,19 +620,19 @@ impl ClassicGameScene { &mut self.world, &mut self.physics, Vector3::new(0.0, 3.0, -15.0), - Target::new(None, Patrol::None), + SphereTarget::new(None, Patrol::None), ), GameDifficulty::Medium => spawn_target( &mut self.world, &mut self.physics, Vector3::new(0.0, 3.0, self.rng.sample(Uniform::new(-39.0, -15.0))), - Target::new(None, Patrol::None), + SphereTarget::new(None, Patrol::None), ), GameDifficulty::Hard => spawn_target( &mut self.world, &mut self.physics, Vector3::new(0.0, 3.0, self.rng.sample(Uniform::new(-39.0, -15.0))), - Target::new(None, Patrol::None), + SphereTarget::new(None, Patrol::None), ), } } @@ -640,7 +649,7 @@ impl ClassicGameScene { &mut self.world, &mut self.physics, pos, - Target::new_with_delete_duration( + SphereTarget::new_with_delete_duration( self.secondary_delete_timer.clone(), None, Patrol::None, @@ -667,7 +676,7 @@ impl ClassicGameScene { &mut self.world, &mut self.physics, pos, - Target::new_with_delete_duration( + SphereTarget::new_with_delete_duration( self.secondary_delete_timer.clone(), validity, Patrol::None, @@ -698,7 +707,7 @@ impl ClassicGameScene { &mut self.world, &mut self.physics, pos, - Target::new_with_delete_duration( + SphereTarget::new_with_delete_duration( self.secondary_delete_timer.clone(), validity, patrol, diff --git a/src/scene/elimination_game_scene.rs b/src/scene/elimination_game_scene.rs index 1dd8d43..60e8fb6 100644 --- a/src/scene/elimination_game_scene.rs +++ b/src/scene/elimination_game_scene.rs @@ -32,7 +32,7 @@ use crate::window::Window; use conrod_core::widget::{Canvas, Text}; use conrod_core::widget_ids; -use crate::entity::target::{Patrol, Target, Validity}; +use crate::entity::target::{Patrol, SphereTarget, Validity}; use crate::camera::Camera; use crate::renderer::rendering_info::BackgroundType; @@ -218,6 +218,8 @@ impl Scene for EliminationGameScene { _control_flow: &mut ControlFlow, _database: &mut Database, ) -> SceneOp { + let mut shoot_trigger = false; + let round_timer_sec = self.round_stopwatch.get_duration(); let mut ui_cell = conrod_handle.get_ui_mut().set_widgets(); @@ -283,7 +285,9 @@ impl Scene for EliminationGameScene { let mut scene_op = SceneOp::None; if !self.freeze { - renderer.camera.move_direction(input_manager.mouse_movement); + renderer + .camera + .move_direction(input_manager.mouse_movement * delta_time); let _player_position = update_player_position( delta_time, @@ -334,8 +338,9 @@ impl Scene for EliminationGameScene { &mut self.rng, ); - for (id, (target, collider_handle)) in - self.world.query_mut::<(&mut Target, &ColliderHandle)>() + for (id, (target, collider_handle)) in self + .world + .query_mut::<(&mut SphereTarget, &ColliderHandle)>() { if target.is_need_to_be_deleted() { self.entity_to_remove.push(id); @@ -358,7 +363,7 @@ impl Scene for EliminationGameScene { delta_time, ); - self.shoot(&input_manager, audio_context, &renderer.camera, delta_time); + shoot_trigger = true; if !is_any_target_exists(&mut self.world) { self.game_state = GameState::Finishing(Timer::new(FINISHING_DURATION)); @@ -399,6 +404,10 @@ impl Scene for EliminationGameScene { &self.physics.rigid_body_set, &self.physics.collider_set, ); + + if shoot_trigger { + self.shoot(&input_manager, audio_context, &renderer.camera, delta_time); + } } drop(ui_cell); @@ -486,7 +495,7 @@ impl EliminationGameScene { &mut self.world, &mut self.physics, pos, - Target::new(None, Patrol::None), + SphereTarget::new(None, Patrol::None), ); } GameDifficulty::Medium => { @@ -510,7 +519,7 @@ impl EliminationGameScene { &mut self.world, &mut self.physics, pos, - Target::new(validity, Patrol::None), + SphereTarget::new(validity, Patrol::None), ); } GameDifficulty::Hard => { @@ -527,7 +536,7 @@ impl EliminationGameScene { &mut self.world, &mut self.physics, pos, - Target::new( + SphereTarget::new( None, Patrol::Polar { or: Vector3::new(0.0, 0.0, 0.0), @@ -554,19 +563,17 @@ impl EliminationGameScene { if input_manager.is_mouse_press(&MouseButton::Left) && self.shoot_timer.is_finished() { self.shoot_animation.trigger(); self.shoot_timer.reset(0.4); - let sink = rodio::Sink::try_new(&audio_context.output_stream_handle).unwrap(); sink.append( rodio::Decoder::new(BufReader::new(Cursor::new(AUDIO_FILE_SHOOT.to_vec()))) .unwrap(), ); audio_context.push(Sink::Regular(sink)); - if let Some((handle, _distance)) = shoot_ray(&self.physics, camera) { let collider = self.physics.collider_set.get(handle).unwrap(); let entity = Entity::from_bits(collider.user_data as u64).unwrap(); - if let Ok(mut target) = self.world.get_mut::(entity) { + if let Ok(mut target) = self.world.get_mut::(entity) { if target.try_shoot(audio_context) { let shoot_time = self.delta_shoot_time.get_duration(); self.delta_shoot_time.reset(); diff --git a/src/scene/hit_and_dodge_scene.rs b/src/scene/hit_and_dodge_scene.rs index d3e3322..651a681 100644 --- a/src/scene/hit_and_dodge_scene.rs +++ b/src/scene/hit_and_dodge_scene.rs @@ -258,6 +258,8 @@ impl Scene for HitAndDodgeGameScene { _control_flow: &mut ControlFlow, _database: &mut Database, ) -> SceneOp { + let mut shoot_trigger = false; + let round_timer_sec = self.round_timer.get_duration(); let mut ui_cell = conrod_handle.get_ui_mut().set_widgets(); @@ -323,7 +325,9 @@ impl Scene for HitAndDodgeGameScene { let mut scene_op = SceneOp::None; if !self.freeze { - renderer.camera.move_direction(input_manager.mouse_movement); + renderer + .camera + .move_direction(input_manager.mouse_movement * delta_time); let _player_position = update_player_position( delta_time, @@ -385,7 +389,7 @@ impl Scene for HitAndDodgeGameScene { delta_time, ); - self.shoot(input_manager, audio_context, &renderer.camera, delta_time); + shoot_trigger = true; if self.round_timer.is_finished() { self.game_state = GameState::Finishing(Timer::new(FINISHING_DURATION)); @@ -426,9 +430,14 @@ impl Scene for HitAndDodgeGameScene { &self.physics.rigid_body_set, &self.physics.collider_set, ); - self.bullet_disposal(); + + if shoot_trigger { + self.shoot(&input_manager, audio_context, &renderer.camera, delta_time); + } } + self.bullet_disposal(); + drop(ui_cell); if game_finished { @@ -504,23 +513,19 @@ impl HitAndDodgeGameScene { if input_manager.is_mouse_press(&MouseButton::Left) && self.shoot_timer.is_finished() { self.shoot_animation.trigger(); self.shoot_timer.reset(0.4); - let sink = rodio::Sink::try_new(&audio_context.output_stream_handle).unwrap(); sink.append( rodio::Decoder::new(BufReader::new(Cursor::new(AUDIO_FILE_SHOOT.to_vec()))) .unwrap(), ); audio_context.push(Sink::Regular(sink)); - if let Some((handle, _distance)) = shoot_ray(&self.physics, camera) { let collider = self.physics.collider_set.get(handle).unwrap(); if let Some(entity) = Entity::from_bits(collider.user_data as u64) { if let Ok(mut gunman) = self.world.get_mut::(entity) { gunman.hit(); - let shoot_time = self.delta_shoot_time.get_duration(); self.delta_shoot_time.reset(); - self.score.hit += 1; self.score.score += ((100.0 * (7.0 - shoot_time)) as i32).max(100); } else { diff --git a/src/scene/score_history_scene.rs b/src/scene/score_history_scene.rs index 00f9313..2563e41 100644 --- a/src/scene/score_history_scene.rs +++ b/src/scene/score_history_scene.rs @@ -79,10 +79,12 @@ impl Scene for ScoreHistoryScene { Value::I64(x) => *x as usize, _ => unreachable!(), }; - self.difficulty_selection = match m.get("difficulty").unwrap() { - Value::I64(x) => *x as usize, - _ => unreachable!(), - }; + if let Some(y) = m.get("difficulty") { + self.difficulty_selection = match y { + Value::I64(x) => *x as usize, + _ => unreachable!(), + }; + } } self.scores = GameModeScores::read( database, diff --git a/src/scene/settings_scene.rs b/src/scene/settings_scene.rs index b43199a..d54cac5 100644 --- a/src/scene/settings_scene.rs +++ b/src/scene/settings_scene.rs @@ -330,7 +330,7 @@ impl Scene for SettingsScene { .mid_top_of(self.ids.mouse_sensitivity_canvas) .set(self.ids.mouse_sensitivity_slider_label, &mut ui_cell); - for value in Slider::new(renderer.camera.sensitivity, 0.1f32, 3f32) + for value in Slider::new(renderer.camera.sensitivity, 0.1f32, 100f32) .mid_bottom_of(self.ids.mouse_sensitivity_canvas) .label(&format!("{:.3}", renderer.camera.sensitivity)) .wh(Dimensions::new(200.0, 30.0)) diff --git a/src/systems/gunman.rs b/src/systems/gunman.rs index c938e44..52f3709 100644 --- a/src/systems/gunman.rs +++ b/src/systems/gunman.rs @@ -25,9 +25,13 @@ pub fn spawn_gunman( .build(), ); physics.collider_set.insert_with_parent( - ColliderBuilder::new(SharedShape::cuboid(0.3 * 0.2, 1.4 * 0.2, 0.4 * 0.2)) - .user_data(entity.to_bits().get() as u128) - .build(), + ColliderBuilder::new(SharedShape::cuboid( + 0.3 * 3.1 * 0.2, + 1.4 * 1.4 * 0.2, + 0.4 * 3.0 * 0.2, + )) + .user_data(entity.to_bits().get() as u128) + .build(), rigid_body_handle, &mut physics.rigid_body_set, ); diff --git a/src/systems/target.rs b/src/systems/target.rs index 2faa316..cec1e61 100644 --- a/src/systems/target.rs +++ b/src/systems/target.rs @@ -1,4 +1,4 @@ -use crate::entity::target::Target; +use crate::entity::target::SphereTarget; use crate::physics::GamePhysics; use crate::renderer::render_objects::ShapeType; use crate::renderer::Renderer; @@ -11,7 +11,7 @@ pub fn spawn_target( world: &mut World, physics: &mut GamePhysics, pos: Vector3, - target: Target, + target: SphereTarget, ) { let entity = world.reserve_entity(); world.spawn_at( @@ -30,7 +30,7 @@ pub fn spawn_target( pub fn is_any_target_exists(world: &mut World) -> bool { let mut exists = false; - for (_, (_)) in world.query_mut::<(&Target)>() { + for (_, (_)) in world.query_mut::<(&SphereTarget)>() { exists = true; break; } @@ -38,7 +38,7 @@ pub fn is_any_target_exists(world: &mut World) -> bool { } pub fn enqueue_target(world: &mut World, physics: &mut GamePhysics, renderer: &mut Renderer) { - for (_id, (collider_handle, target)) in world.query_mut::<(&ColliderHandle, &Target)>() { + for (_id, (collider_handle, target)) in world.query_mut::<(&ColliderHandle, &SphereTarget)>() { let collider = physics.collider_set.get(*collider_handle).unwrap(); let (objects, ref mut bound) = renderer.render_objects.next(); @@ -60,7 +60,9 @@ pub fn update_target( delta_time: f32, rng: &mut SmallRng, ) { - for (_id, (target, collider_handle)) in world.query_mut::<(&mut Target, &ColliderHandle)>() { + for (_id, (target, collider_handle)) in + world.query_mut::<(&mut SphereTarget, &ColliderHandle)>() + { let target_collider = physics.collider_set.get_mut(*collider_handle).unwrap(); let mut target_pos = *target_collider.translation(); target.update(delta_time, &mut target_pos);