feat: Add frame time calcs
This commit is contained in:
+6
-6
@@ -1,12 +1,12 @@
|
||||
use crate::{draw::Drawable, enemy::Enemy, gun::Bullet, player::Player, tick::Tick};
|
||||
use crate::{draw::Drawable, enemy::Enemy, gun::Bullet, player::Player, tick::Ticker};
|
||||
|
||||
pub trait EntityTrait {
|
||||
fn as_tick(&mut self) -> &mut dyn Tick;
|
||||
fn as_tick(&mut self) -> &mut dyn Ticker;
|
||||
fn as_draw(&self) -> &dyn Drawable;
|
||||
}
|
||||
|
||||
impl<T: Tick + Drawable> EntityTrait for T {
|
||||
fn as_tick(&mut self) -> &mut dyn Tick {
|
||||
impl<T: Ticker + Drawable> EntityTrait for T {
|
||||
fn as_tick(&mut self) -> &mut dyn Ticker {
|
||||
self
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ impl Drawable for Box<dyn EntityTrait> {
|
||||
}
|
||||
}
|
||||
|
||||
impl Tick for Box<dyn EntityTrait> {
|
||||
impl Ticker for Box<dyn EntityTrait> {
|
||||
fn tick(&mut self) {
|
||||
self.as_tick().tick();
|
||||
}
|
||||
@@ -76,7 +76,7 @@ impl Drawable for Entity {
|
||||
}
|
||||
}
|
||||
|
||||
impl Tick for Entity {
|
||||
impl Ticker for Entity {
|
||||
fn tick(&mut self) {
|
||||
match self {
|
||||
Entity::Player(player) => player.tick(),
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
use macroquad::prelude::*;
|
||||
|
||||
use crate::{draw::Drawable, movement::*, tick::Tick};
|
||||
use crate::{draw::Drawable, movement::*, tick::Ticker};
|
||||
|
||||
/// The shot made by a player or an enemy
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@@ -16,7 +16,7 @@ impl Bullet {
|
||||
}
|
||||
}
|
||||
|
||||
impl Tick for Bullet {
|
||||
impl Ticker for Bullet {
|
||||
fn tick(&mut self) {
|
||||
self.movement.tick();
|
||||
}
|
||||
@@ -74,7 +74,7 @@ impl Drawable for Gun {
|
||||
}
|
||||
}
|
||||
|
||||
impl Tick for Gun {
|
||||
impl Ticker for Gun {
|
||||
fn tick(&mut self) {
|
||||
self.bullets.tick();
|
||||
}
|
||||
|
||||
+18
-13
@@ -1,15 +1,17 @@
|
||||
#![allow(dead_code)]
|
||||
use std::time::Duration;
|
||||
|
||||
use draw::Drawable as _;
|
||||
use tick::Tick as _;
|
||||
use macroquad::prelude::*;
|
||||
mod enemy;
|
||||
mod gun;
|
||||
mod player;
|
||||
mod world;
|
||||
mod movement;
|
||||
mod entity;
|
||||
mod tick;
|
||||
use tick::{TickEvery, Ticker as _};
|
||||
mod draw;
|
||||
mod enemy;
|
||||
mod entity;
|
||||
mod gun;
|
||||
mod movement;
|
||||
mod player;
|
||||
mod tick;
|
||||
mod world;
|
||||
|
||||
fn window_conf() -> Conf {
|
||||
Conf {
|
||||
@@ -26,14 +28,17 @@ fn window_conf() -> Conf {
|
||||
#[macroquad::main(window_conf)]
|
||||
async fn main() {
|
||||
let mut cursor = Cursor::default();
|
||||
let mut world = world::World::new();
|
||||
cursor.handle_mouse();
|
||||
let mut world = world::World::new().every(Duration::from_millis(15));
|
||||
|
||||
loop {
|
||||
world.tick();
|
||||
world.draw();
|
||||
|
||||
cursor.handle_mouse();
|
||||
// world.tick();
|
||||
// world.draw();
|
||||
cursor.draw();
|
||||
world.draw();
|
||||
world.tick();
|
||||
world.ticker.next_frame();
|
||||
|
||||
next_frame().await
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
use macroquad::prelude::*;
|
||||
|
||||
use crate::tick::Tick;
|
||||
use crate::tick::Ticker;
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum MovementType {
|
||||
Speed(f32),
|
||||
@@ -41,7 +41,7 @@ impl Movement {
|
||||
}
|
||||
}
|
||||
|
||||
impl Tick for Movement {
|
||||
impl Ticker for Movement {
|
||||
fn tick(&mut self) {
|
||||
self.tick();
|
||||
}
|
||||
|
||||
+8
-7
@@ -1,6 +1,6 @@
|
||||
use macroquad::prelude::*;
|
||||
|
||||
use crate::{draw::Drawable, gun::Gun, tick::Tick};
|
||||
use crate::{draw::Drawable, gun::Gun, tick::Ticker};
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Player {
|
||||
@@ -37,9 +37,14 @@ impl 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.pos += self.velocity;
|
||||
self.gun.as_mut().map(|gun| {
|
||||
gun.pos = self.pos;
|
||||
gun.direction = self.direction;
|
||||
gun.shoot();
|
||||
gun.tick();
|
||||
});
|
||||
@@ -52,12 +57,8 @@ impl Drawable for Player {
|
||||
}
|
||||
}
|
||||
|
||||
impl Tick for Player {
|
||||
impl Ticker 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();
|
||||
self.tick()
|
||||
}
|
||||
}
|
||||
|
||||
+38
-4
@@ -1,23 +1,57 @@
|
||||
pub trait Tick {
|
||||
use core::time::Duration;
|
||||
|
||||
use crate::draw::Drawable;
|
||||
|
||||
pub trait Ticker {
|
||||
fn tick(&mut self);
|
||||
}
|
||||
|
||||
impl<T: Tick> Tick for Box<T> {
|
||||
impl<T: Ticker> Ticker for Box<T> {
|
||||
fn tick(&mut self) {
|
||||
self.as_mut().tick();
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Tick> Tick for Vec<T> {
|
||||
impl<T: Ticker> Ticker for Vec<T> {
|
||||
fn tick(&mut self) {
|
||||
self.iter_mut().for_each(|item| item.tick());
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Tick> Tick for Option<T> {
|
||||
impl<T: Ticker> Ticker for Option<T> {
|
||||
fn tick(&mut self) {
|
||||
if let Some(item) = self {
|
||||
item.tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Tick<T> {
|
||||
pub ticker: T,
|
||||
pub time: Duration,
|
||||
}
|
||||
|
||||
pub trait TickEvery: Sized + Ticker {
|
||||
fn every(self, time: Duration) -> Tick<Self>;
|
||||
}
|
||||
|
||||
impl TickEvery for crate::world::World {
|
||||
fn every(self, time: Duration) -> Tick<Self> {
|
||||
Tick { ticker: self, time }
|
||||
}
|
||||
}
|
||||
|
||||
impl Ticker for Tick<crate::world::World> {
|
||||
fn tick(&mut self) {
|
||||
if self.ticker.since_last_tick >= self.time.as_secs_f32() {
|
||||
self.ticker.tick();
|
||||
self.ticker.since_last_tick = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Drawable> Drawable for Tick<T> {
|
||||
fn draw(&self) {
|
||||
self.ticker.draw()
|
||||
}
|
||||
}
|
||||
|
||||
+13
-10
@@ -1,10 +1,11 @@
|
||||
|
||||
use macroquad::miniquad::window::screen_size;
|
||||
use macroquad::prelude::*;
|
||||
|
||||
use crate::draw::Drawable;
|
||||
use crate::entity::Entity;
|
||||
use crate::player::Player;
|
||||
use crate::tick::Tick;
|
||||
use crate::tick::Ticker;
|
||||
|
||||
/// The world struct
|
||||
/// This contains the player, the enemies, and the center of the world
|
||||
@@ -16,6 +17,7 @@ pub struct World {
|
||||
tick: u64,
|
||||
entities: Vec<Entity>,
|
||||
frame_time: f32, // The rolling average frame time for the last 5 frames
|
||||
pub since_last_tick: f32,
|
||||
}
|
||||
|
||||
impl World {
|
||||
@@ -36,14 +38,15 @@ impl World {
|
||||
self.entities.tick();
|
||||
}
|
||||
|
||||
// fn next_frame(&mut self) {
|
||||
// let current_frame_time = get_frame_time();
|
||||
// if self.frame_time == 0.0 {
|
||||
// self.frame_time = current_frame_time;
|
||||
// } else {
|
||||
// self.frame_time = (self.frame_time * 4.0 + current_frame_time) / 5.0;
|
||||
// }
|
||||
// }
|
||||
pub fn next_frame(&mut self) {
|
||||
let current_frame_time = get_frame_time();
|
||||
if self.frame_time == 0.0 {
|
||||
self.frame_time = current_frame_time;
|
||||
} else {
|
||||
self.frame_time = (self.frame_time * 4.0 + current_frame_time) / 5.0;
|
||||
}
|
||||
self.since_last_tick += current_frame_time;
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawable for World {
|
||||
@@ -53,7 +56,7 @@ impl Drawable for World {
|
||||
}
|
||||
}
|
||||
|
||||
impl Tick for World {
|
||||
impl Ticker for World {
|
||||
fn tick(&mut self) {
|
||||
self.tick()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user