Compare commits

...

2 commits

Author SHA1 Message Date
0b9cea1f93 Update Rust crate ron to 0.10.0 2025-04-11 09:02:21 +00:00
d2e70481ce Fix examples 2025-04-11 09:59:51 +02:00
8 changed files with 551 additions and 516 deletions

View file

@ -26,7 +26,7 @@ members = [
destructure_traitobject = "0.3.0"
itertools = "0.14.0"
serde = { version = "1.0.203", features = ["derive"] }
ron = "0.9.0"
ron = "0.10.0"
paste = "1.0.15"
chrono = { version = "0.4.35", features = ["serde"] }
anyhow = { version = "1.0.86", features = ["backtrace"] }

View file

@ -6,7 +6,7 @@ use super::engine_settings::EngineSettings;
use anyhow::Result;
use assetpath::AssetPath;
use context::prelude::cgmath::vec3;
use ecs::{Resource, resources::Resource as ResourceTrait};
use ecs::Resource;
use crate::assets::asset_manager::AssetManager;
use crate::prelude::*;
@ -20,7 +20,7 @@ pub struct Engine {
}
impl Engine {
pub fn new<T: ResourceTrait + EventConsumer>(
pub fn new(
#[allow(unused)] mut create_info: EngineCreateInfo<'_>,
world: &mut WorldBuilder,
) -> Result<()> {
@ -171,10 +171,9 @@ impl Engine {
world.resources.insert(engine_settings);
world.add_system(1, Self::start_frame);
world.add_system(u32::MAX, Self::end_frame);
world.add_system(100, Self::render_scene);
world.add_system(10_000, Self::render_scene);
world.add_system(10_000_000, Self::render_gui);
world.add_system(u32::MAX, Self::end_frame);
Ok(())
}

View file

@ -37,166 +37,82 @@ pub trait EventSystem<Func, T> {
fn event_system(world_builder: &mut WorldBuilder, func: Func);
}
impl<Func, T> EventSystem<Func, T> for Engine
macro_rules! impl_event_system {
( $( $t:ident, )* ) => {
paste::paste! {
impl<Func, T, $( $t, )* > EventSystem<Func, (T, $( $t, )*)> for Engine
where
Func: Fn(&mut T, &mut Commands, EngineEvent<'_>) -> Result<()> + Send + Sync + 'static,
Func: Fn(&mut T, &mut Commands, EngineEvent<'_>, $( &mut $t, )* ) -> Result<()> + Send + Sync + 'static,
T: ResourceTrait,
$(
$t: ResourceTrait,
)*
{
fn event_system(world_builder: &mut WorldBuilder, func: Func) {
world_builder.add_system(
0,
|world: &World,
move |world: &World,
commands: &mut Commands,
consumer: &mut T,
gui_handler: &mut GuiHandler,
input_map: &mut InputMap,
context: &mut Context|
context: &mut Context,
$(
[< $t:lower >]: &mut $t,
)*
|
-> Result<bool> {
let res = context.events(world, |event| {
Self::event(commands, gui_handler, input_map, consumer, event, func)
})?;
gui_handler.process_callbacks(commands)?;
Ok(res)
},
)
}
}
impl Engine {
pub(crate) fn event<T>(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
input: &InputMap,
consumer: &mut T,
event: Event<'_>,
func: Func,
) -> anyhow::Result<()> {
match event {
Event::MouseMotion(x, y) => {
gui_handler.set_mouse_pos(x, y)?;
consumer.event(commands, EngineEvent::MouseMotion(x, y))?;
func(
consumer,
commands,
EngineEvent::MouseMotion(x, y),
$(
[< $t:lower >],
)*
)?;
}
Event::MouseButtonDown(mouse_button) => {
if !gui_handler.mouse_down(mouse_button)? {
consumer.event(commands, EngineEvent::MouseButtonDown(mouse_button))?;
func(
consumer,
commands,
EngineEvent::MouseButtonDown(mouse_button),
$(
[< $t:lower >],
)*
)?;
}
}
Event::MouseButtonUp(mouse_button) => {
if !gui_handler.mouse_up(mouse_button)? {
consumer.event(commands, EngineEvent::MouseButtonUp(mouse_button))?;
func(
consumer,
commands,
EngineEvent::MouseButtonUp(mouse_button),
$(
[< $t:lower >],
)*
)?;
}
}
Event::MouseWheel(x, y, direction) => {
consumer.event(commands, EngineEvent::MouseWheel(x, y, direction))?
func(
consumer,
commands,
EngineEvent::MouseWheel(x, y, direction),
$(
[< $t:lower >],
)*
)?;
}
Event::KeyDown(keycode) => {
Self::key_down_event(commands, gui_handler, input, consumer, keycode)?;
}
Event::KeyUp(keycode) => {
Self::key_up_event(commands, gui_handler, input, consumer, keycode)?;
}
Event::TextInput(text) => {
Self::text_input(gui_handler, text)?;
}
Event::ControllerButtonDown(controller, button) => {
Self::button_down_event(commands, gui_handler, consumer, controller, button)?;
}
Event::ControllerButtonUp(controller, button) => {
Self::button_up_event(commands, consumer, controller, button)?;
}
Event::ControllerAxis(controller) => {
if !gui_handler.check_navigatable() {
Self::axis_event(commands, consumer, &controller)?
} else {
gui_handler.update_selection(controller.direction())?;
}
}
Event::ControllerAdded(controller) => {
Self::controller_added(commands, consumer, controller)?
}
Event::ControllerRemoved(controller) => {
Self::controller_removed(commands, consumer, controller)?
}
Event::JoystickAxis(joystick, axis_index, value) => consumer.event(
commands,
EngineEvent::JoystickAxis(joystick, axis_index, value),
)?,
Event::JoystickButtonDown(joystick) => {
consumer.event(commands, EngineEvent::JoystickButtonDown(joystick))?
}
Event::JoystickButtonUp(joystick) => {
consumer.event(commands, EngineEvent::JoystickButtonUp(joystick))?
}
Event::JoystickAdded(joystick) => {
consumer.event(commands, EngineEvent::JoystickAdded(joystick))?
}
Event::JoystickRemoved(joystick) => {
consumer.event(commands, EngineEvent::JoystickRemoved(joystick))?
}
Event::FileDrop(filename) => {
consumer.event(commands, EngineEvent::FileDrop(filename))?
}
}
Ok(())
}
}
impl Engine {
#[inline]
fn controller_added<T: EventConsumer>(
commands: &mut Commands,
consumer: &mut T,
controller: &Controller,
) -> Result<()> {
consumer.event(commands, EngineEvent::ControllerAdded(controller))?;
Ok(())
}
#[inline]
fn controller_removed<T: EventConsumer>(
commands: &mut Commands,
consumer: &mut T,
controller: &Controller,
) -> Result<()> {
consumer.event(commands, EngineEvent::ControllerRemoved(controller))?;
Ok(())
}
#[inline]
fn key_up_event<T: EventConsumer>(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
input: &InputMap,
consumer: &mut T,
keycode: Keycode,
) -> Result<()> {
if input.direction_mapping.get(&keycode).is_some()
&& gui_handler.update_selection(GuiDirection::None)?
if let Some(direction) =
input_map.direction_mapping.get(&keycode)
{
return Ok(());
}
consumer.event(commands, EngineEvent::KeyUp(keycode))?;
Ok(())
}
#[inline]
fn key_down_event<T: EventConsumer>(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
input: &InputMap,
consumer: &mut T,
keycode: Keycode,
) -> Result<()> {
if let Some(direction) = input.direction_mapping.get(&keycode) {
if gui_handler.update_selection(*direction)? {
return Ok(());
}
@ -222,68 +138,40 @@ impl Engine {
_ => (),
}
consumer.event(commands, EngineEvent::KeyDown(keycode))?;
Ok(())
}
#[inline]
fn button_up_event<T: EventConsumer>(
commands: &mut Commands,
consumer: &mut T,
controller: &Controller,
button: ControllerButton,
) -> Result<()> {
consumer.event(
func(
consumer,
commands,
EngineEvent::ControllerButtonUp(controller, button),
EngineEvent::KeyDown(keycode),
$(
[< $t:lower >],
)*
)?;
Ok(())
}
Event::KeyUp(keycode) => {
if input_map.direction_mapping.get(&keycode).is_some()
&& gui_handler.update_selection(GuiDirection::None)?
{
return Ok(());
}
#[inline]
fn button_down_event<T: EventConsumer>(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
consumer: &mut T,
controller: &Controller,
button: ControllerButton,
) -> Result<()> {
func(
consumer,
commands,
EngineEvent::KeyUp(keycode),
$(
[< $t:lower >],
)*
)?;
}
Event::TextInput(text) => {
if let Some(writeable) = gui_handler.writeable() {
for c in text.chars() {
writeable.add_letter(gui_handler, c)?;
}
}
}
Event::ControllerButtonDown(controller, button) => {
if gui_handler.check_navigatable() {
Self::check_button_down(commands, gui_handler, consumer, controller, button)?;
} else {
consumer.event(
commands,
EngineEvent::ControllerButtonDown(controller, button),
)?;
}
Ok(())
}
#[inline]
fn axis_event<T: EventConsumer>(
commands: &mut Commands,
consumer: &mut T,
controller: &Controller,
) -> Result<()> {
consumer.event(
commands,
EngineEvent::ControllerAxis(controller, controller.controller_axis()),
)?;
Ok(())
}
#[inline]
fn check_button_down<T: EventConsumer>(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
consumer: &mut T,
controller: &Controller,
button: ControllerButton,
) -> Result<()> {
match button {
ControllerButton::A => {
if gui_handler.accept_selection()? {
@ -304,7 +192,9 @@ impl Engine {
}
ControllerButton::LeftButton => {
if gui_handler.previous_tab_topgui(commands, false)? {
if gui_handler
.previous_tab_topgui(commands, false)?
{
return Ok(());
}
}
@ -316,13 +206,16 @@ impl Engine {
}
ControllerButton::LeftTrigger => {
if gui_handler.previous_tab_topgui(commands, true)? {
if gui_handler
.previous_tab_topgui(commands, true)?
{
return Ok(());
}
}
ControllerButton::DPadDown => {
let selection_res = gui_handler.update_selection(GuiDirection::Down)?;
let selection_res = gui_handler
.update_selection(GuiDirection::Down)?;
gui_handler.update_selection(GuiDirection::None)?;
if selection_res {
@ -331,7 +224,8 @@ impl Engine {
}
ControllerButton::DPadUp => {
let selection_res = gui_handler.update_selection(GuiDirection::Up)?;
let selection_res = gui_handler
.update_selection(GuiDirection::Up)?;
gui_handler.update_selection(GuiDirection::None)?;
if selection_res {
@ -340,7 +234,8 @@ impl Engine {
}
ControllerButton::DPadRight => {
let selection_res = gui_handler.update_selection(GuiDirection::Right)?;
let selection_res = gui_handler
.update_selection(GuiDirection::Right)?;
gui_handler.update_selection(GuiDirection::None)?;
if selection_res {
@ -349,7 +244,8 @@ impl Engine {
}
ControllerButton::DPadLeft => {
let selection_res = gui_handler.update_selection(GuiDirection::Left)?;
let selection_res = gui_handler
.update_selection(GuiDirection::Left)?;
gui_handler.update_selection(GuiDirection::None)?;
if selection_res {
@ -361,23 +257,156 @@ impl Engine {
}
if !gui_handler.accept_custom_selection(commands, button)? {
consumer.event(
func(
consumer,
commands,
EngineEvent::ControllerButtonDown(
controller, button,
),
$(
[< $t:lower >],
)*
)?;
}
} else {
func(
consumer,
commands,
EngineEvent::ControllerButtonDown(controller, button),
$(
[< $t:lower >],
)*
)?;
}
}
Event::ControllerButtonUp(controller, button) => {
func(
consumer,
commands,
EngineEvent::ControllerButtonUp(controller, button),
$(
[< $t:lower >],
)*
)?;
}
Event::ControllerAxis(controller) => {
if !gui_handler.check_navigatable() {
func(
consumer,
commands,
EngineEvent::ControllerAxis(
controller,
controller.controller_axis(),
),
$(
[< $t:lower >],
)*
)?;
} else {
gui_handler.update_selection(controller.direction())?;
}
}
Event::ControllerAdded(controller) => {
func(
consumer,
commands,
EngineEvent::ControllerAdded(controller),
$(
[< $t:lower >],
)*
)?;
}
Event::ControllerRemoved(controller) => {
func(
consumer,
commands,
EngineEvent::ControllerRemoved(controller),
$(
[< $t:lower >],
)*
)?;
}
Ok(())
Event::JoystickAxis(joystick, axis_index, value) => {
func(
consumer,
commands,
EngineEvent::JoystickAxis(joystick, axis_index, value),
$(
[< $t:lower >],
)*
)?;
}
Event::JoystickButtonDown(joystick) => {
func(
consumer,
commands,
EngineEvent::JoystickButtonDown(joystick),
$(
[< $t:lower >],
)*
)?;
}
Event::JoystickButtonUp(joystick) => {
func(
consumer,
commands,
EngineEvent::JoystickButtonUp(joystick),
$(
[< $t:lower >],
)*
)?;
}
Event::JoystickAdded(joystick) => {
func(
consumer,
commands,
EngineEvent::JoystickAdded(joystick),
$(
[< $t:lower >],
)*
)?;
}
Event::JoystickRemoved(joystick) => {
func(
consumer,
commands,
EngineEvent::JoystickRemoved(joystick),
$(
[< $t:lower >],
)*
)?;
}
#[inline]
fn text_input(gui_handler: &mut GuiHandler, text: String) -> Result<()> {
if let Some(writeable) = gui_handler.writeable() {
for c in text.chars() {
writeable.add_letter(gui_handler, c)?;
Event::FileDrop(filename) => {
func(
consumer,
commands,
EngineEvent::FileDrop(filename),
$(
[< $t:lower >],
)*
)?;
}
}
Ok(())
})?;
gui_handler.process_callbacks(commands)?;
Ok(res)
},
)
}
}
}
};
}
impl_event_system!();
impl_event_system!(A, B,);
impl_event_system!(A, B, C,);
impl_event_system!(A, B, C, D,);
impl_event_system!(A, B, C, D, E,);
impl_event_system!(A, B, C, D, E, F,);

View file

@ -10,7 +10,7 @@ pub use crate::engine::{
asset_handler::{AssetHandler, AssetLoader},
engine::*,
engine_create_info::EngineCreateInfo,
engine_event_handling::{EngineEvent, EventConsumer, EventSystem},
engine_event_handling::{EngineEvent, EventSystem},
engine_settings::*,
};

View file

@ -8,10 +8,22 @@ use engine::prelude::{
*,
};
use crate::{FREE_CAMERA_CONTROL, celestial_object::*};
use crate::celestial_object::*;
#[derive(Clone, Copy, Debug, Resource)]
struct PlayerEntity(Entity);
#[derive(Debug)]
struct Player;
impl EntityComponent for Player {
fn name(&self) -> &str {
Self::debug_name()
}
}
impl ComponentDebug for Player {
fn debug_name() -> &'static str {
"Player"
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum Input {
@ -38,7 +50,7 @@ enum Control {
}
#[derive(Clone, Debug, Resource)]
struct InputSettings {
pub struct InputSettings {
mappings: HashMap<(String, u32, Input), (Control, bool)>,
}
@ -109,23 +121,23 @@ impl InputSettings {
pub struct Game;
impl Game {
pub fn update(
&mut self,
commands: &mut Commands,
scene: &mut Scene,
camera_control: &mut FreeCameraControl,
) -> Result<()> {
if FREE_CAMERA_CONTROL {
let now = commands.now();
pub fn update(&mut self, commands: &mut Commands) -> Result<()> {
// if FREE_CAMERA_CONTROL {
// let now = commands.now();
camera_control.update(now, scene.view_mut())?;
}
// camera_control.update(now, scene.view_mut())?;
// }
Ok(())
}
pub fn event(&mut self, commands: &mut Commands, event: EngineEvent<'_>) -> Result<()> {
if let Some(event) = Self::motion_concepts(world, event)? {
pub fn event(
&mut self,
commands: &mut Commands,
event: EngineEvent<'_>,
input_settings: &mut InputSettings,
) -> Result<()> {
if let Some(event) = Self::joystick_movement(commands, event, input_settings)? {
match event {
EngineEvent::JoystickAdded(joystick) => {
println!("joystick {} added", joystick.name());
@ -142,85 +154,78 @@ impl Game {
Ok(())
}
fn motion_concepts<'a>(
world: &mut World,
event: EngineEvent<'a>,
) -> Result<Option<EngineEvent<'a>>> {
if FREE_CAMERA_CONTROL {
Self::free_camera(world, event)
} else {
Self::joystick_movement(world, event)
}
}
// fn motion_concepts<'a>(
// world: &mut World,
// event: EngineEvent<'a>,
// ) -> Result<Option<EngineEvent<'a>>> {
// if FREE_CAMERA_CONTROL {
// Self::free_camera(world, event)
// } else {
// Self::joystick_movement(world, event)
// }
// }
fn free_camera<'a>(
world: &mut World,
event: EngineEvent<'a>,
) -> Result<Option<EngineEvent<'a>>> {
match event {
EngineEvent::MouseButtonDown(MouseButton::Middle) => {
let camera_control: &mut FreeCameraControl = world.resources.get_mut()?;
camera_control.mouse_down();
}
EngineEvent::MouseButtonUp(MouseButton::Middle) => {
let camera_control: &mut FreeCameraControl = world.resources.get_mut()?;
camera_control.mouse_release();
}
EngineEvent::MouseMotion(x, y) => {
let (scene, camera_control): (&mut Scene, &mut FreeCameraControl) =
world.resources.get_mut()?;
// (fn free_camera<'a>(event: EngineEvent<'a>) -> Result<Option<EngineEvent<'a>>> {
// match event {
// EngineEvent::MouseButtonDown(MouseButton::Middle) => {
// let camera_control: &mut FreeCameraControl = world.resources.get_mut()?;
// camera_control.mouse_down();
// }
// EngineEvent::MouseButtonUp(MouseButton::Middle) => {
// let camera_control: &mut FreeCameraControl = world.resources.get_mut()?;
// camera_control.mouse_release();
// }
// EngineEvent::MouseMotion(x, y) => {
// let (scene, camera_control): (&mut Scene, &mut FreeCameraControl) =
// world.resources.get_mut()?;
camera_control.mouse_move(x, y, scene.view_mut())?;
// camera_control.mouse_move(x, y, scene.view_mut())?;
return Ok(Some(event));
}
EngineEvent::KeyDown(key) => {
let camera_control: &mut FreeCameraControl = world.resources.get_mut()?;
// return Ok(Some(event));
// }
// EngineEvent::KeyDown(key) => {
// let camera_control: &mut FreeCameraControl = world.resources.get_mut()?;
match key {
Keycode::W => camera_control.forward_back(1.0),
Keycode::A => camera_control.left_right(-1.0),
Keycode::S => camera_control.forward_back(-1.0),
Keycode::D => camera_control.left_right(1.0),
Keycode::Space => camera_control.up_down(1.0),
Keycode::LCtrl => camera_control.up_down(-1.0),
// match key {
// Keycode::W => camera_control.forward_back(1.0),
// Keycode::A => camera_control.left_right(-1.0),
// Keycode::S => camera_control.forward_back(-1.0),
// Keycode::D => camera_control.left_right(1.0),
// Keycode::Space => camera_control.up_down(1.0),
// Keycode::LCtrl => camera_control.up_down(-1.0),
_ => return Ok(Some(event)),
}
}
EngineEvent::KeyUp(key) => {
let camera_control: &mut FreeCameraControl = world.resources.get_mut()?;
// _ => return Ok(Some(event)),
// }
// }
// EngineEvent::KeyUp(key) => {
// let camera_control: &mut FreeCameraControl = world.resources.get_mut()?;
match key {
Keycode::W => camera_control.forward_back(-1.0),
Keycode::A => camera_control.left_right(1.0),
Keycode::S => camera_control.forward_back(1.0),
Keycode::D => camera_control.left_right(-1.0),
Keycode::Space => camera_control.up_down(-1.0),
Keycode::LCtrl => camera_control.up_down(1.0),
// match key {
// Keycode::W => camera_control.forward_back(-1.0),
// Keycode::A => camera_control.left_right(1.0),
// Keycode::S => camera_control.forward_back(1.0),
// Keycode::D => camera_control.left_right(-1.0),
// Keycode::Space => camera_control.up_down(-1.0),
// Keycode::LCtrl => camera_control.up_down(1.0),
_ => return Ok(Some(event)),
}
}
// _ => return Ok(Some(event)),
// }
// }
_ => return Ok(Some(event)),
}
// _ => return Ok(Some(event)),
// }
Ok(None)
}
// Ok(None)
// })
fn joystick_movement<'a>(
world: &mut World,
commands: &mut Commands,
event: EngineEvent<'a>,
input_settings: &mut InputSettings,
) -> Result<Option<EngineEvent<'a>>> {
let player = world.resources.get::<PlayerEntity>();
let (fighter_object, resources) = world.entity_resources(player.0)?;
match event {
EngineEvent::JoystickAxis(joystick, axis_index, value) => {
let mut normalized = value as f32 * (i16::MAX as f32).recip();
let input_settings = resources.get::<InputSettings>();
let player_control = fighter_object.get_component_mut::<FreeSpaceControl>()?;
if let Some((control, inverted)) =
input_settings.map_axis(joystick.name(), joystick.id(), axis_index)
@ -230,14 +235,18 @@ impl Game {
};
match control {
Control::Throttle => player_control.set_throttle(normalized),
Control::StrafeHorizontal => {
player_control.set_left_right_strafe(normalized)
Control::Throttle => {
commands.write_event(PlayerInputEvent::Throttle(normalized))
}
Control::StrafeVertical => player_control.set_up_down_strafe(normalized),
Control::Yaw => player_control.set_yaw(normalized),
Control::Pitch => player_control.set_pitch(normalized),
Control::Roll => player_control.set_roll(normalized),
Control::StrafeHorizontal => {
commands.write_event(PlayerInputEvent::StrafeHorizontal(normalized))
}
Control::StrafeVertical => {
commands.write_event(PlayerInputEvent::StrafeVertical(normalized))
}
Control::Yaw => commands.write_event(PlayerInputEvent::Yaw(normalized)),
Control::Pitch => commands.write_event(PlayerInputEvent::Pitch(normalized)),
Control::Roll => commands.write_event(PlayerInputEvent::Roll(normalized)),
Control::PrimaryWeapon => (),
Control::SecondaryWeapon => (),
@ -254,14 +263,17 @@ impl Game {
impl Game {
pub fn setup_updates(world_builder: &mut WorldBuilder) -> Result<()> {
if !FREE_CAMERA_CONTROL {
// if !FREE_CAMERA_CONTROL {
world_builder.add_update(1_000, Self::camera_update)?;
}
// }
world_builder.add_update(100, Self::celestial_velocity_update)?;
world_builder.add_update(200, Self::player_orientation)?;
world_builder.add_update(110, Self::celestial_buffer_update)?;
world_builder.events.register_event::<PlayerInputEvent>();
world_builder.events.add_reader(PlayerInputEvent::event);
Ok(())
}
@ -274,9 +286,9 @@ impl Game {
0.02,
FreeSpaceControlSettings::default(),
));
fighter.insert_component(Player);
let player = PlayerEntity(world.add_entity(fighter)?);
world.resources.insert(player);
world.add_entity(fighter)?;
world.resources.insert(InputSettings::default());
let mut example_sun = CelestialObject::new(world, CelestialClass::Sun, 5, None)?;
@ -365,3 +377,33 @@ impl Game {
Ok(())
}
}
enum PlayerInputEvent {
Throttle(f32),
StrafeHorizontal(f32),
StrafeVertical(f32),
Yaw(f32),
Pitch(f32),
Roll(f32),
}
impl PlayerInputEvent {
fn event(
&self,
_commands: &mut Commands,
mut query: Query<(&mut FreeSpaceControl, &mut Player)>,
) -> Result<()> {
let (player_control, _) = &mut *query;
match *self {
PlayerInputEvent::Throttle(v) => player_control.set_throttle(v),
PlayerInputEvent::StrafeHorizontal(v) => player_control.set_left_right_strafe(v),
PlayerInputEvent::StrafeVertical(v) => player_control.set_up_down_strafe(v),
PlayerInputEvent::Yaw(v) => player_control.set_yaw(v),
PlayerInputEvent::Pitch(v) => player_control.set_pitch(v),
PlayerInputEvent::Roll(v) => player_control.set_roll(v),
}
Ok(())
}
}

View file

@ -3,7 +3,7 @@ use anyhow::Result;
use ecs::*;
use engine::prelude::*;
use crate::game::Game;
use crate::game::{Game, InputSettings};
#[derive(Default, Resource)]
pub enum GameState {
@ -14,26 +14,24 @@ pub enum GameState {
}
impl GameState {
pub fn update(
commands: &mut Commands,
game_state: &mut GameState,
scene: &mut Scene,
camera_control: &mut FreeCameraControl,
) -> Result<bool> {
pub fn update(commands: &mut Commands, game_state: &mut GameState) -> Result<bool> {
match game_state {
GameState::Startup => *game_state = GameState::Game(Game),
GameState::Game(game) => game.update(commands, scene, camera_control)?,
GameState::Game(game) => game.update(commands)?,
}
Ok(true)
}
}
impl EventConsumer for GameState {
fn event(&mut self, commands: &mut Commands, event: EngineEvent<'_>) -> Result<()> {
pub fn event(
&mut self,
commands: &mut Commands,
event: EngineEvent<'_>,
input_settings: &mut InputSettings,
) -> Result<()> {
match self {
GameState::Startup => (),
GameState::Game(game) => game.event(commands, event)?,
GameState::Game(game) => game.event(commands, event, input_settings)?,
}
Ok(())

View file

@ -12,7 +12,7 @@ use game::Game;
use game_state::GameState;
use skybox::SkyBox;
const FREE_CAMERA_CONTROL: bool = false;
// const FREE_CAMERA_CONTROL: bool = false;
fn main() -> Result<()> {
let mut world_builder = World::builder();
@ -21,7 +21,8 @@ fn main() -> Result<()> {
engine_ci.resource_base_path = "/home/michaelh/Sync/space_game/".to_string();
engine_ci.asset_directories.gltf_file_directory = "objects".into();
Engine::new::<GameState>(engine_ci, &mut world_builder)?;
Engine::new(engine_ci, &mut world_builder)?;
Engine::event_system(&mut world_builder, GameState::event);
world_builder.add_system(10, GameState::update);
world_builder.resources.insert(GameState::default());
@ -40,12 +41,12 @@ fn main() -> Result<()> {
.into_iter(),
)?;
if FREE_CAMERA_CONTROL {
let scene: &mut Scene = world_builder.resources.get_mut()?;
let view = scene.view_mut();
let camera_control = FreeCameraControl::new(view)?;
world_builder.resources.insert(camera_control);
}
// if FREE_CAMERA_CONTROL {
// let scene: &mut Scene = world_builder.resources.get_mut()?;
// let view = scene.view_mut();
// let camera_control = FreeCameraControl::new(view)?;
// world_builder.resources.insert(camera_control);
// }
Game::setup_updates(&mut world_builder)?;
let mut world = world_builder.build();

View file

@ -3,32 +3,15 @@ use anyhow::Result;
use ecs::*;
use engine::prelude::*;
struct CameraControlStart;
struct CameraControlEnd;
struct CameraControlMove {
x: u32,
y: u32,
}
fn main() -> Result<()> {
let mut world_builder = World::builder();
Engine::new::<GameState>(EngineCreateInfo::default(), &mut world_builder)?;
Engine::event_system(&mut world_builder);
Engine::new(EngineCreateInfo::default(), &mut world_builder)?;
Engine::event_system(&mut world_builder, GameState::event);
world_builder.add_system(10, GameState::update);
world_builder.resources.insert(GameState::default());
world_builder.events.register_event::<CameraControlStart>();
world_builder.events.register_event::<CameraControlEnd>();
world_builder.events.register_event::<CameraControlMove>();
world_builder.events.add_reader(Game::enable_camera_control);
world_builder
.events
.add_reader(Game::disable_camera_control);
world_builder.events.add_reader(Game::update_mouse);
let scene: &mut Scene = world_builder.resources.get_mut()?;
let view = scene.view_mut();
let camera_control = FreeCameraControl::new(view)?;
@ -54,13 +37,17 @@ impl GameState {
Ok(true)
}
}
impl EventConsumer for GameState {
fn event(&mut self, commands: &mut Commands, event: EngineEvent<'_>) -> Result<()> {
fn event(
&mut self,
commands: &mut Commands,
event: EngineEvent<'_>,
camera_control: &mut FreeCameraControl,
scene: &mut Scene,
) -> Result<()> {
match self {
GameState::Startup => (),
GameState::Game(game) => game.event(commands, event)?,
GameState::Game(game) => game.event(commands, event, camera_control, scene)?,
}
Ok(())
@ -70,48 +57,27 @@ impl EventConsumer for GameState {
struct Game;
impl Game {
fn enable_camera_control(
_: &CameraControlStart,
_commands: &mut Commands,
camera_control: &mut FreeCameraControl,
) -> Result<()> {
camera_control.mouse_down();
Ok(())
}
fn disable_camera_control(
_: &CameraControlEnd,
_commands: &mut Commands,
camera_control: &mut FreeCameraControl,
) -> Result<()> {
camera_control.mouse_release();
Ok(())
}
fn update_mouse(
camera_move: &CameraControlMove,
_commands: &mut Commands,
camera_control: &mut FreeCameraControl,
scene: &mut Scene,
) -> Result<()> {
camera_control.mouse_move(camera_move.x, camera_move.y, scene.view_mut())?;
Ok(())
}
fn update(&mut self, _commands: &mut Commands) -> Result<()> {
Ok(())
}
fn event(&mut self, commands: &mut Commands, event: EngineEvent<'_>) -> Result<()> {
fn event(
&mut self,
_commands: &mut Commands,
event: EngineEvent<'_>,
camera_control: &mut FreeCameraControl,
scene: &mut Scene,
) -> Result<()> {
match event {
EngineEvent::MouseButtonDown(MouseButton::Left) => {
commands.write_event(CameraControlStart)
camera_control.mouse_down();
}
EngineEvent::MouseButtonUp(MouseButton::Left) => {
camera_control.mouse_release();
}
EngineEvent::MouseMotion(x, y) => {
camera_control.mouse_move(x, y, scene.view_mut())?;
}
EngineEvent::MouseButtonUp(MouseButton::Left) => commands.write_event(CameraControlEnd),
EngineEvent::MouseMotion(x, y) => commands.write_event(CameraControlMove { x, y }),
_ => (),
}