feat: Added ticking for bullets
This commit is contained in:
+48
-10
@@ -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<Bullet>,
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -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)]
|
||||
|
||||
+13
-4
@@ -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<Gun>,
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user