Some code cleanup

This commit is contained in:
SeanOMik 2023-09-08 00:13:46 -04:00
parent 81c3b5be55
commit 2e225ccd09
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
12 changed files with 20 additions and 111 deletions

17
Cargo.lock generated
View File

@ -886,16 +886,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "hecs"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d2711ad60b74f2f3d0c0ac338a58410a5249da44005971ae806d2925e6b5167"
dependencies = [
"hashbrown 0.13.2",
"spin",
]
[[package]]
name = "hermit-abi"
version = "0.3.2"
@ -1113,7 +1103,6 @@ dependencies = [
"edict",
"gilrs-core",
"glam",
"hecs",
"image",
"instant",
"petgraph",
@ -1782,12 +1771,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]]
name = "spirv"
version = "0.2.0+1.5.4"

View File

@ -21,7 +21,6 @@ tobj = { version = "3.2.1", features = [
]}
instant = "0.1"
async-trait = "0.1.65"
hecs = "0.10.3"
glam = { version = "0.24.0", features = ["bytemuck"] }
petgraph = "0.6.3"
gilrs-core = "0.5.6"

13
src/ecs/resources/mod.rs → src/castable_any.rs Executable file → Normal file
View File

@ -1,15 +1,4 @@
use std::{any::Any, collections::VecDeque};
use winit::event::WindowEvent;
pub mod window_state;
pub use window_state::*;
pub mod events;
pub use events::*;
pub mod event_queue;
pub use event_queue::*;
use std::any::Any;
pub trait CastableAny: Send + Sync + 'static {
fn as_any(&self) -> &dyn Any;

View File

@ -1,7 +1,6 @@
use edict::Component;
use winit::dpi::PhysicalSize;
use crate::{math::{Angle, OPENGL_TO_WGPU_MATRIX, Transform}, render::camera::CameraProjectionMode};
use crate::{math::{Angle, Transform}, render::camera::CameraProjectionMode};
#[derive(Clone, Component)]
pub struct CameraComponent {

View File

@ -1,6 +1,11 @@
use std::{collections::{HashMap, VecDeque}, any::{TypeId, Any}, cell::{RefCell, Ref, RefMut}};
use super::{CastableAny, Events, Event};
use crate::castable_any::CastableAny;
pub trait Event: Clone + Send + Sync + 'static {}
impl<T: Clone + Send + Sync + 'static> Event for T {}
pub type Events<T> = VecDeque<T>;
pub struct EventQueue {
events: HashMap<TypeId, RefCell<Box<dyn CastableAny>>>,

View File

@ -6,7 +6,8 @@ use tracing::warn;
use crate::game::Controls;
pub mod components;
pub mod resources;
pub mod events;
pub use events::*;
/// A trait that represents a simple system
pub trait SimpleSystem {

View File

@ -1,6 +0,0 @@
use std::collections::VecDeque;
pub trait Event: Clone + Send + Sync + 'static {}
impl<T: Clone + Send + Sync + 'static> Event for T {}
pub type Events<T> = VecDeque<T>;

View File

@ -1,15 +0,0 @@
use std::any::Any;
use super::CastableAny;
#[derive(Clone, Default)]
pub struct WindowState {
pub is_focused: bool,
pub is_cursor_inside_window: bool,
}
impl WindowState {
pub fn new() -> Self {
Self::default()
}
}

View File

@ -14,67 +14,24 @@ use tracing_subscriber::{
use winit::{window::{WindowBuilder, Window}, event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, DeviceEvent}, event_loop::{EventLoop, ControlFlow}};
use crate::{render::renderer::{Renderer, BasicRenderer}, input_event::InputEvent, ecs::{SimpleSystem, SystemDispatcher, resources::{WindowState, Events, EventQueue}}, input::InputSystem};
use crate::{render::renderer::{Renderer, BasicRenderer}, input_event::InputEvent, ecs::{SimpleSystem, SystemDispatcher, EventQueue, Events}, input::InputSystem};
pub struct Controls<'a> {
pub world: &'a mut edict::World,
}
struct TickCounter {
counter: u32,
last_second: Instant,
changed: bool,
tps: f32,
/// the time (in seconds) that passes between each tick
tick_time: f32
#[derive(Clone, Default)]
pub struct WindowState {
pub is_focused: bool,
pub is_cursor_inside_window: bool,
}
impl TickCounter {
fn new() -> Self {
Self {
counter: 0,
last_second: Instant::now(),
tps: 0.0,
changed: false,
tick_time: 0.0,
}
}
/// Returns true if the tps changed
fn tick(&mut self) -> bool {
self.counter += 1;
if self.last_second.elapsed().as_secs() > 0 {
self.tick_time = 1000.0 / self.counter as f32;
self.tps = self.counter as f32;
self.changed = true;
self.counter = 0;
self.last_second = Instant::now();
}
self.changed
}
/// Gets the change in ticks per second
fn get_change(&mut self) -> Option<f32> {
match self.changed {
true => {
self.changed = false;
Some(self.tps)
},
false => None,
}
}
/// Get the time (in seconds) between ticks
fn get_tick_time(&self) -> f32 {
self.tick_time
impl WindowState {
pub fn new() -> Self {
Self::default()
}
}
struct GameLoop {
window: Arc<Window>,
renderer: Box<dyn Renderer>,
@ -83,7 +40,6 @@ struct GameLoop {
/// higher priority systems
engine_sys_dispatcher: SystemDispatcher,
user_sys_dispatcher: SystemDispatcher,
fps_counter: TickCounter,
}
impl GameLoop {
@ -95,7 +51,6 @@ impl GameLoop {
world,
engine_sys_dispatcher: SystemDispatcher::new(),
user_sys_dispatcher: user_systems,
fps_counter: TickCounter::new(),
}
}

View File

@ -5,7 +5,7 @@ use glam::Vec2;
use tracing::{warn, debug};
use winit::event::{ElementState, MouseScrollDelta};
use crate::{ecs::{SimpleSystem, resources::EventQueue}, input_event::InputEvent};
use crate::{ecs::{SimpleSystem, EventQueue}, input_event::InputEvent};
pub type KeyCode = winit::event::VirtualKeyCode;

View File

@ -1,7 +1,5 @@
use winit::{event::{DeviceId, KeyboardInput, ModifiersState, MouseScrollDelta, TouchPhase, MouseButton, AxisId, Touch, WindowEvent, ElementState}, dpi::PhysicalPosition};
use crate::ecs::resources::CastableAny;
/// Wrapper around events from `winit::WindowEvent` that are specific to input related events.
///
/// The `winit::WindowEvent` enum has many values that are related to inputs.

View File

@ -5,6 +5,7 @@ mod resources;
mod ecs;
mod math;
mod input;
mod castable_any;
use atomicell::Ref;
use ecs::components::mesh::MeshComponent;