feat: Add some enemies
This commit is contained in:
+80
-19
@@ -1,6 +1,9 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
use macroquad::prelude::*;
|
use macroquad::prelude::*;
|
||||||
|
use miniquad::window::screen_size;
|
||||||
|
|
||||||
pub struct Character {
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub struct Player {
|
||||||
pos: Vec2,
|
pos: Vec2,
|
||||||
pointing: Vec2,
|
pointing: Vec2,
|
||||||
velocity: Vec2,
|
velocity: Vec2,
|
||||||
@@ -20,23 +23,21 @@ fn window_conf() -> Conf {
|
|||||||
|
|
||||||
#[macroquad::main(window_conf)]
|
#[macroquad::main(window_conf)]
|
||||||
async fn main() {
|
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 cursor = Cursor::default();
|
||||||
|
let mut world = World::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
character.handle_inputs();
|
world.handle_inputs();
|
||||||
character.draw();
|
world.tick();
|
||||||
|
world.draw();
|
||||||
|
|
||||||
cursor.handle_mouse();
|
cursor.handle_mouse();
|
||||||
cursor.draw();
|
cursor.draw();
|
||||||
next_frame().await
|
next_frame().await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Character {
|
impl Player {
|
||||||
pub fn new(pos: Vec2, pointing: Vec2, velocity: Vec2) -> Self {
|
pub fn new(pos: Vec2, pointing: Vec2, velocity: Vec2) -> Self {
|
||||||
Self {
|
Self {
|
||||||
pos,
|
pos,
|
||||||
@@ -50,7 +51,6 @@ impl Character {
|
|||||||
let distance = self.pos.distance(self.pointing);
|
let distance = self.pos.distance(self.pointing);
|
||||||
// increase the velocity scaled to the distance
|
// increase the velocity scaled to the distance
|
||||||
self.velocity = (self.pointing - self.pos).normalize() * distance / 15f32;
|
self.velocity = (self.pointing - self.pos).normalize() * distance / 15f32;
|
||||||
self.move_with_velocity();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_mouse(&mut self) {
|
pub fn handle_mouse(&mut self) {
|
||||||
@@ -95,28 +95,89 @@ impl Cursor {
|
|||||||
pub struct Enemy {
|
pub struct Enemy {
|
||||||
/// The current position of the enemy
|
/// The current position of the enemy
|
||||||
pos: Vec2,
|
pos: Vec2,
|
||||||
/// The class of the enemy
|
|
||||||
class: EnemyClass,
|
|
||||||
/// Health of the enemy
|
/// Health of the enemy
|
||||||
health: u32,
|
health: u32,
|
||||||
/// Velocity of the enemy moving towards the player // This is normalized to 1
|
/// Velocity of the enemy moving towards the player // This is normalized to 1
|
||||||
velocity: Vec2,
|
velocity: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum EnemyClass {
|
impl Enemy {
|
||||||
Melee,
|
pub fn new(pos: Vec2, health: u32, velocity: Vec2) -> Self {
|
||||||
Ranged,
|
Self {
|
||||||
Boss,
|
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
|
/// The world struct
|
||||||
/// This contains the player, the enemies, and the center of the world
|
/// This contains the player, the enemies, and the center of the world
|
||||||
pub struct World {
|
pub struct World {
|
||||||
player: Character,
|
player: Player,
|
||||||
enemies: Vec<Enemy>,
|
enemies: Vec<Enemy>,
|
||||||
center: Vec2,
|
center: Vec2,
|
||||||
|
size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_with_velocity(pos: &mut Vec2, velocity: Vec2) {
|
impl World {
|
||||||
*pos += velocity;
|
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),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user