From a70e64d8b7b65aad32c7eb1cce74c1143a632ff7 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Wed, 19 Jun 2024 03:50:48 +0530 Subject: [PATCH] feat: Add some enemies --- src/main.rs | 99 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 271afeb..74ae0e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ +#![allow(dead_code)] use macroquad::prelude::*; +use miniquad::window::screen_size; -pub struct Character { +#[derive(Debug, Default, Clone)] +pub struct Player { pos: Vec2, pointing: Vec2, velocity: Vec2, @@ -20,23 +23,21 @@ fn window_conf() -> Conf { #[macroquad::main(window_conf)] async fn main() { - let mut character = Character::new( - Vec2::new(100.0, 100.0), - Vec2::new(0.0, 0.0), - vec2(0f32, 0f32), - ); let mut cursor = Cursor::default(); + let mut world = World::new(); loop { - character.handle_inputs(); - character.draw(); + world.handle_inputs(); + world.tick(); + world.draw(); + cursor.handle_mouse(); cursor.draw(); next_frame().await } } -impl Character { +impl Player { pub fn new(pos: Vec2, pointing: Vec2, velocity: Vec2) -> Self { Self { pos, @@ -50,7 +51,6 @@ impl Character { let distance = self.pos.distance(self.pointing); // increase the velocity scaled to the distance self.velocity = (self.pointing - self.pos).normalize() * distance / 15f32; - self.move_with_velocity(); } pub fn handle_mouse(&mut self) { @@ -95,28 +95,89 @@ impl Cursor { pub struct Enemy { /// The current position of the enemy pos: Vec2, - /// The class of the enemy - class: EnemyClass, /// Health of the enemy health: u32, /// Velocity of the enemy moving towards the player // This is normalized to 1 velocity: Vec2, } -pub enum EnemyClass { - Melee, - Ranged, - Boss, +impl Enemy { + pub fn new(pos: Vec2, health: u32, velocity: Vec2) -> Self { + Self { + pos, + health, + velocity, + } + } + pub fn draw(&self) { + draw_circle(self.pos.x, self.pos.y, 16.0, RED); + } +} + +/// The shot made by a player or an enemy +pub struct Bullet { + /// The position of the bullet + pos: Vec2, + /// The bullet travels at constant speed + speed: f32, + // velocity: Vec2, + damage: u32, } /// The world struct /// This contains the player, the enemies, and the center of the world pub struct World { - player: Character, + player: Player, enemies: Vec, center: Vec2, + size: Vec2, } -pub fn move_with_velocity(pos: &mut Vec2, velocity: Vec2) { - *pos += velocity; +impl World { + pub fn new() -> Self { + let (x, y) = screen_size(); + let center = vec2(x / 2., y / 2.); + + Self { + player: Player::new(center, center, center), + enemies: Vec::default(), + center, + size: vec2(x, y), + } + } + pub fn handle_inputs(&mut self) { + self.player.handle_inputs(); + } + pub fn draw(&self) { + self.player.draw(); + self.enemies.iter().for_each(|enemy| enemy.draw()); + } + + pub fn spawn_enemy(&mut self) { + let enemy = Enemy { + pos: random_vec2_in_bounds(self.size), + health: 100, + velocity: vec2(0., 0.), + }; + self.enemies.push(enemy); + } + + pub fn tick(&mut self) { + self.player.move_with_velocity(); + let enemies_count = self.enemies.len(); + if enemies_count < 10 && (macroquad::time::get_time() / (2 * enemies_count) as f64) > 1.0 { + self.spawn_enemy(); + } + for enemy in self.enemies.iter_mut() { + enemy.velocity = (self.player.pos - enemy.pos).normalize(); + enemy.pos += enemy.velocity; + } + } +} + +fn random_vec2_in_bounds(bounds: Vec2) -> Vec2 { + vec2( + rand::gen_range(0.0, bounds.x), + rand::gen_range(0.0, bounds.y), + ) }