From 33a57a8f2060234f82d59738b161c9e7f6f1ace3 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 19 Jun 2024 22:45:40 +0530 Subject: [PATCH] feat: Added ticking for bullets --- src/gun.rs | 58 ++++++++++++++++++++++++++++++++++++++++--------- src/movement.rs | 12 +++++----- src/player.rs | 17 +++++++++++---- 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/src/gun.rs b/src/gun.rs index 63e4b56..6f6599e 100644 --- a/src/gun.rs +++ b/src/gun.rs @@ -1,6 +1,6 @@ use macroquad::prelude::*; -use crate::movement::*; +use crate::{draw::Drawable, movement::*, tick::Tick}; /// The shot made by a player or an enemy #[derive(Debug, Clone, Copy)] @@ -11,11 +11,24 @@ pub struct Bullet { } impl Bullet { - pub fn draw(&self) { + fn draw(&self) { draw_circle(self.movement.pos.x, self.movement.pos.y, 4.0, GREEN); } } +impl Tick for Bullet { + fn tick(&mut self) { + self.movement.tick(); + } +} + +impl Drawable for Bullet { + fn draw(&self) { + self.draw(); + } +} + +#[derive(Debug, Clone, Copy)] pub enum GunType { ShotGun, Pistol, @@ -23,21 +36,46 @@ pub enum GunType { } /// This is basically an abstraction of a bullet_factory lets say +#[derive(Debug, Clone)] pub struct Gun { + pub pos: Vec2, pub damage: u32, pub direction: Vec2, pub type_: GunType, + pub bullets: Vec, } impl Gun { - pub fn shoot(&mut self, pos: Vec2) -> Bullet { - Bullet { - movement: Movement { - pos, - direction: self.direction, - type_: MovementType::Speed(5.0), - }, - damage: self.damage, + pub fn new(pos: Vec2, damage: u32, direction: Vec2, type_: GunType) -> Self { + Self { + pos, + damage, + direction, + type_, + bullets: Vec::new(), } } + pub fn shoot(&mut self) { + let bullet = Bullet { + movement: Movement { + pos: self.pos, + direction: self.direction, + type_: MovementType::FASTER, + }, + damage: self.damage, + }; + self.bullets.push(bullet); + } +} + +impl Drawable for Gun { + fn draw(&self) { + self.bullets.draw() + } +} + +impl Tick for Gun { + fn tick(&mut self) { + self.bullets.tick(); + } } diff --git a/src/movement.rs b/src/movement.rs index ad97832..a74a368 100644 --- a/src/movement.rs +++ b/src/movement.rs @@ -8,12 +8,12 @@ pub enum MovementType { } impl MovementType { - const FAST: Self = Self::Speed(5.0); - const MEDIUM: Self = Self::Speed(3.0); - const SLOW: Self = Self::Speed(1.0); - const STOP: Self = Self::Speed(0.0); - const FASTER: Self = Self::Acceleration(1.0); - const SLOWER: Self = Self::Acceleration(-1.0); + pub const FAST: Self = Self::Speed(5.0); + pub const MEDIUM: Self = Self::Speed(3.0); + pub const SLOW: Self = Self::Speed(1.0); + pub const STOP: Self = Self::Speed(0.0); + pub const FASTER: Self = Self::Acceleration(1.0); + pub const SLOWER: Self = Self::Acceleration(-1.0); } #[derive(Debug, Clone, Copy)] diff --git a/src/player.rs b/src/player.rs index 18f2da9..204eaac 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,6 +1,6 @@ use macroquad::prelude::*; -use crate::{draw::Drawable, tick::Tick}; +use crate::{draw::Drawable, gun::Gun, tick::Tick}; #[derive(Debug, Default, Clone)] pub struct Player { @@ -8,6 +8,7 @@ pub struct Player { pub pointing: Vec2, pub direction: Vec2, pub velocity: Vec2, + pub gun: Option, } impl Player { @@ -15,28 +16,33 @@ impl Player { let mouse_pos = mouse_position(); let pointing = vec2(mouse_pos.0, mouse_pos.1); let direction = (pos - pointing).normalize(); + let gun = Some(Gun::new(pos, 10, direction, crate::gun::GunType::Pistol)); Self { pos, pointing, direction, velocity: vec2(0.0, 0.0), + gun, } } fn handle_inputs(&mut self) { let mouse_pos = mouse_position(); self.pointing = vec2(mouse_pos.0, mouse_pos.1); - self.direction = (self.pointing - self.pos).normalize(); - let distance = self.pos.distance(self.pointing); - self.velocity = self.direction * distance / 15f32; } fn draw(&self) { draw_circle(self.pos.x, self.pos.y, 16.0, BLUE); + self.gun.draw(); } fn tick(&mut self) { self.pos += self.velocity; + self.gun.as_mut().map(|gun| { + gun.pos = self.pos; + gun.shoot(); + gun.tick(); + }); } } @@ -49,6 +55,9 @@ impl Drawable for Player { impl Tick for Player { fn tick(&mut self) { self.handle_inputs(); + self.direction = (self.pointing - self.pos).normalize(); + let distance = self.pos.distance(self.pointing); + self.velocity = self.direction * distance / 15f32; self.tick(); } }