Skip to content

Commit

Permalink
Fix mouse and hitbox
Browse files Browse the repository at this point in the history
  • Loading branch information
andraantariksa committed Feb 11, 2022
1 parent 787901f commit 51301ae
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 55 deletions.
10 changes: 4 additions & 6 deletions src/entity/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum PatrolState {
}

#[derive(Clone)]
pub struct Target {
pub struct SphereTarget {
shooted: bool,
delete_timer: Option<Timer>,
validity: Option<Validity>,
Expand All @@ -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<Validity>, patrol: Patrol) -> Self {
let validity_state = if let Some(x) = &validity {
ValidityState::Valid(Timer::new(x.valid_duration))
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 23 additions & 14 deletions src/scene/classic_game_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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::<Target>(entity) {
if let Ok(mut target) = self.world.get_mut::<SphereTarget>(entity) {
if target.try_shoot(audio_context) {
let shoot_time = self.delta_shoot_time.get_duration();
self.delta_shoot_time.reset();
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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),
),
}
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 18 additions & 11 deletions src/scene/elimination_game_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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),
Expand All @@ -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::<Target>(entity) {
if let Ok(mut target) = self.world.get_mut::<SphereTarget>(entity) {
if target.try_shoot(audio_context) {
let shoot_time = self.delta_shoot_time.get_duration();
self.delta_shoot_time.reset();
Expand Down
19 changes: 12 additions & 7 deletions src/scene/hit_and_dodge_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<Gunman>(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 {
Expand Down
Loading

0 comments on commit 51301ae

Please sign in to comment.