o3 refactor
This commit is contained in:
parent
341d531db3
commit
049f522bb1
19 changed files with 508 additions and 214 deletions
|
|
@ -5,4 +5,7 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
winit = "0.30"
|
||||
glam = "0.30.4"
|
||||
glam = "0.30.4"
|
||||
raidillon_core = { path = "../raidillon_core" }
|
||||
hecs = "0.10.5"
|
||||
raidillon_render = { path = "../raidillon_render" }
|
||||
57
raidillon_input/src/camera_system.rs
Normal file
57
raidillon_input/src/camera_system.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use glam::Vec3;
|
||||
use hecs::World;
|
||||
|
||||
use crate::camera::FPSCameraController;
|
||||
use crate::Action;
|
||||
use raidillon_core::{System, AssetManager, EventHandler, GameEvent};
|
||||
use raidillon_render::Camera;
|
||||
|
||||
pub struct CameraSystem {
|
||||
controller: FPSCameraController,
|
||||
camera_entity: hecs::Entity,
|
||||
}
|
||||
|
||||
impl CameraSystem {
|
||||
pub fn new(camera_entity: hecs::Entity) -> Self {
|
||||
Self {
|
||||
controller: FPSCameraController::new(Vec3::new(0.0, 0.0, 2.0)),
|
||||
camera_entity,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, world: &mut World, dt: f32) {
|
||||
// After processing events, write camera pose back to ECS component.
|
||||
if let Ok(mut cam) = world.query_one_mut::<&mut Camera>(self.camera_entity) {
|
||||
cam.eye = self.controller.position;
|
||||
cam.center = self.controller.position + self.controller.front();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl System<crate::Action> for CameraSystem {
|
||||
fn update(&mut self, world: &mut World, _assets: &AssetManager, _events: &mut raidillon_core::EventBus<crate::Action>, dt: f32) {
|
||||
self.update(world, dt);
|
||||
}
|
||||
}
|
||||
|
||||
impl EventHandler<Action> for CameraSystem {
|
||||
fn handle(&mut self, event: &GameEvent<Action>) {
|
||||
match event {
|
||||
GameEvent::InputAction(action) => {
|
||||
match action {
|
||||
Action::MoveForward => self.controller.position += self.controller.front() * 0.1,
|
||||
Action::MoveBackward => self.controller.position -= self.controller.front() * 0.1,
|
||||
Action::MoveLeft => {
|
||||
let right = self.controller.front().cross(Vec3::Y).normalize();
|
||||
self.controller.position -= right * 0.1;
|
||||
}
|
||||
Action::MoveRight => {
|
||||
let right = self.controller.front().cross(Vec3::Y).normalize();
|
||||
self.controller.position += right * 0.1;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
44
raidillon_input/src/input_system.rs
Normal file
44
raidillon_input/src/input_system.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use crate::{Input, Action};
|
||||
use raidillon_core::{System, AssetManager};
|
||||
use hecs::World;
|
||||
use raidillon_core::EventBus;
|
||||
use raidillon_core::GameEvent;
|
||||
|
||||
pub struct InputSystem {
|
||||
input: Input<Action>,
|
||||
}
|
||||
|
||||
impl InputSystem {
|
||||
pub fn new() -> Self {
|
||||
let mut input = Input::<Action>::new();
|
||||
use winit::keyboard::KeyCode;
|
||||
input.map_key(KeyCode::KeyW, Action::MoveForward);
|
||||
input.map_key(KeyCode::KeyS, Action::MoveBackward);
|
||||
input.map_key(KeyCode::KeyA, Action::MoveLeft);
|
||||
input.map_key(KeyCode::KeyD, Action::MoveRight);
|
||||
Self { input }
|
||||
}
|
||||
|
||||
pub fn handle_event<T>(&mut self, event: &winit::event::Event<T>) {
|
||||
self.input.handle_event(event);
|
||||
}
|
||||
|
||||
pub fn update(&mut self, bus: &mut EventBus<Action>) {
|
||||
for action in [Action::MoveForward, Action::MoveBackward, Action::MoveLeft, Action::MoveRight] {
|
||||
if self.input.action_held(action) {
|
||||
bus.emit(GameEvent::InputAction(action));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn end_frame(&mut self) {
|
||||
self.input.end_frame();
|
||||
}
|
||||
}
|
||||
|
||||
impl System<crate::Action> for InputSystem {
|
||||
fn update(&mut self, _world: &mut World, _assets: &AssetManager, events: &mut raidillon_core::EventBus<crate::Action>, _dt: f32) {
|
||||
self.update(events);
|
||||
self.end_frame();
|
||||
}
|
||||
}
|
||||
|
|
@ -4,9 +4,23 @@ use std::hash::Hash;
|
|||
use winit::event::{DeviceEvent, ElementState, Event, WindowEvent};
|
||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
||||
pub enum Action {
|
||||
MoveForward,
|
||||
MoveBackward,
|
||||
MoveLeft,
|
||||
MoveRight,
|
||||
}
|
||||
|
||||
pub mod input_system;
|
||||
pub use input_system::InputSystem;
|
||||
|
||||
pub mod camera;
|
||||
pub use camera::FPSCameraController;
|
||||
|
||||
pub mod camera_system;
|
||||
pub use camera_system::CameraSystem;
|
||||
|
||||
pub struct Input<A: Copy + Eq + Hash> {
|
||||
pressed_keys: HashSet<KeyCode>,
|
||||
pressed_once: HashSet<KeyCode>,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue