Compare commits
1 commit
0b9cea1f93
...
70b03e10ef
Author | SHA1 | Date | |
---|---|---|---|
70b03e10ef |
7 changed files with 514 additions and 549 deletions
|
@ -6,7 +6,7 @@ use super::engine_settings::EngineSettings;
|
|||
use anyhow::Result;
|
||||
use assetpath::AssetPath;
|
||||
use context::prelude::cgmath::vec3;
|
||||
use ecs::Resource;
|
||||
use ecs::{Resource, resources::Resource as ResourceTrait};
|
||||
|
||||
use crate::assets::asset_manager::AssetManager;
|
||||
use crate::prelude::*;
|
||||
|
@ -20,7 +20,7 @@ pub struct Engine {
|
|||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn new(
|
||||
pub fn new<T: ResourceTrait + EventConsumer>(
|
||||
#[allow(unused)] mut create_info: EngineCreateInfo<'_>,
|
||||
world: &mut WorldBuilder,
|
||||
) -> Result<()> {
|
||||
|
@ -171,10 +171,11 @@ impl Engine {
|
|||
world.resources.insert(engine_settings);
|
||||
|
||||
world.add_system(1, Self::start_frame);
|
||||
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);
|
||||
|
||||
world.add_system(100, Self::render_scene);
|
||||
world.add_system(10_000_000, Self::render_gui);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -37,82 +37,166 @@ pub trait EventSystem<Func, T> {
|
|||
fn event_system(world_builder: &mut WorldBuilder, func: Func);
|
||||
}
|
||||
|
||||
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<'_>, $( &mut $t, )* ) -> Result<()> + Send + Sync + 'static,
|
||||
impl<Func, T> EventSystem<Func, T> for Engine
|
||||
where
|
||||
Func: Fn(&mut T, &mut Commands, EngineEvent<'_>) -> Result<()> + Send + Sync + 'static,
|
||||
T: ResourceTrait,
|
||||
$(
|
||||
$t: ResourceTrait,
|
||||
)*
|
||||
{
|
||||
{
|
||||
fn event_system(world_builder: &mut WorldBuilder, func: Func) {
|
||||
world_builder.add_system(
|
||||
0,
|
||||
move |world: &World,
|
||||
|world: &World,
|
||||
commands: &mut Commands,
|
||||
consumer: &mut T,
|
||||
gui_handler: &mut GuiHandler,
|
||||
input_map: &mut InputMap,
|
||||
context: &mut Context,
|
||||
$(
|
||||
[< $t:lower >]: &mut $t,
|
||||
)*
|
||||
|
|
||||
context: &mut Context|
|
||||
-> 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)?;
|
||||
func(
|
||||
consumer,
|
||||
commands,
|
||||
EngineEvent::MouseMotion(x, y),
|
||||
$(
|
||||
[< $t:lower >],
|
||||
)*
|
||||
)?;
|
||||
consumer.event(commands, EngineEvent::MouseMotion(x, y))?;
|
||||
}
|
||||
Event::MouseButtonDown(mouse_button) => {
|
||||
if !gui_handler.mouse_down(mouse_button)? {
|
||||
func(
|
||||
consumer,
|
||||
commands,
|
||||
EngineEvent::MouseButtonDown(mouse_button),
|
||||
$(
|
||||
[< $t:lower >],
|
||||
)*
|
||||
)?;
|
||||
consumer.event(commands, EngineEvent::MouseButtonDown(mouse_button))?;
|
||||
}
|
||||
}
|
||||
Event::MouseButtonUp(mouse_button) => {
|
||||
if !gui_handler.mouse_up(mouse_button)? {
|
||||
func(
|
||||
consumer,
|
||||
commands,
|
||||
EngineEvent::MouseButtonUp(mouse_button),
|
||||
$(
|
||||
[< $t:lower >],
|
||||
)*
|
||||
)?;
|
||||
consumer.event(commands, EngineEvent::MouseButtonUp(mouse_button))?;
|
||||
}
|
||||
}
|
||||
Event::MouseWheel(x, y, direction) => {
|
||||
func(
|
||||
consumer,
|
||||
commands,
|
||||
EngineEvent::MouseWheel(x, y, direction),
|
||||
$(
|
||||
[< $t:lower >],
|
||||
)*
|
||||
)?;
|
||||
consumer.event(commands, EngineEvent::MouseWheel(x, y, direction))?
|
||||
}
|
||||
Event::KeyDown(keycode) => {
|
||||
if let Some(direction) =
|
||||
input_map.direction_mapping.get(&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)?
|
||||
{
|
||||
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(());
|
||||
}
|
||||
|
@ -138,40 +222,68 @@ macro_rules! impl_event_system {
|
|||
_ => (),
|
||||
}
|
||||
|
||||
func(
|
||||
consumer,
|
||||
commands,
|
||||
EngineEvent::KeyDown(keycode),
|
||||
$(
|
||||
[< $t:lower >],
|
||||
)*
|
||||
)?;
|
||||
}
|
||||
Event::KeyUp(keycode) => {
|
||||
if input_map.direction_mapping.get(&keycode).is_some()
|
||||
&& gui_handler.update_selection(GuiDirection::None)?
|
||||
{
|
||||
return Ok(());
|
||||
consumer.event(commands, EngineEvent::KeyDown(keycode))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
func(
|
||||
consumer,
|
||||
#[inline]
|
||||
fn button_up_event<T: EventConsumer>(
|
||||
commands: &mut Commands,
|
||||
consumer: &mut T,
|
||||
controller: &Controller,
|
||||
button: ControllerButton,
|
||||
) -> Result<()> {
|
||||
consumer.event(
|
||||
commands,
|
||||
EngineEvent::KeyUp(keycode),
|
||||
$(
|
||||
[< $t:lower >],
|
||||
)*
|
||||
EngineEvent::ControllerButtonUp(controller, button),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn button_down_event<T: EventConsumer>(
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
consumer: &mut T,
|
||||
controller: &Controller,
|
||||
button: ControllerButton,
|
||||
) -> Result<()> {
|
||||
if gui_handler.check_navigatable() {
|
||||
Self::check_button_down(commands, gui_handler, consumer, controller, button)?;
|
||||
} else {
|
||||
consumer.event(
|
||||
commands,
|
||||
EngineEvent::ControllerButtonDown(controller, button),
|
||||
)?;
|
||||
}
|
||||
Event::TextInput(text) => {
|
||||
if let Some(writeable) = gui_handler.writeable() {
|
||||
for c in text.chars() {
|
||||
writeable.add_letter(gui_handler, c)?;
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
||||
Event::ControllerButtonDown(controller, button) => {
|
||||
if gui_handler.check_navigatable() {
|
||||
|
||||
#[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()? {
|
||||
|
@ -192,9 +304,7 @@ macro_rules! impl_event_system {
|
|||
}
|
||||
|
||||
ControllerButton::LeftButton => {
|
||||
if gui_handler
|
||||
.previous_tab_topgui(commands, false)?
|
||||
{
|
||||
if gui_handler.previous_tab_topgui(commands, false)? {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
@ -206,16 +316,13 @@ macro_rules! impl_event_system {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
@ -224,8 +331,7 @@ macro_rules! impl_event_system {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
@ -234,8 +340,7 @@ macro_rules! impl_event_system {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
@ -244,8 +349,7 @@ macro_rules! impl_event_system {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
@ -257,156 +361,23 @@ macro_rules! impl_event_system {
|
|||
}
|
||||
|
||||
if !gui_handler.accept_custom_selection(commands, button)? {
|
||||
func(
|
||||
consumer,
|
||||
commands,
|
||||
EngineEvent::ControllerButtonDown(
|
||||
controller, button,
|
||||
),
|
||||
$(
|
||||
[< $t:lower >],
|
||||
)*
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
func(
|
||||
consumer,
|
||||
consumer.event(
|
||||
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 >],
|
||||
)*
|
||||
)?;
|
||||
}
|
||||
|
||||
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 >],
|
||||
)*
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Event::FileDrop(filename) => {
|
||||
func(
|
||||
consumer,
|
||||
commands,
|
||||
EngineEvent::FileDrop(filename),
|
||||
$(
|
||||
[< $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)?;
|
||||
}
|
||||
}
|
||||
|
||||
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,);
|
||||
|
|
|
@ -10,7 +10,7 @@ pub use crate::engine::{
|
|||
asset_handler::{AssetHandler, AssetLoader},
|
||||
engine::*,
|
||||
engine_create_info::EngineCreateInfo,
|
||||
engine_event_handling::{EngineEvent, EventSystem},
|
||||
engine_event_handling::{EngineEvent, EventConsumer, EventSystem},
|
||||
engine_settings::*,
|
||||
};
|
||||
|
||||
|
|
|
@ -8,22 +8,10 @@ use engine::prelude::{
|
|||
*,
|
||||
};
|
||||
|
||||
use crate::celestial_object::*;
|
||||
use crate::{FREE_CAMERA_CONTROL, celestial_object::*};
|
||||
|
||||
#[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, Resource)]
|
||||
struct PlayerEntity(Entity);
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
enum Input {
|
||||
|
@ -50,7 +38,7 @@ enum Control {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, Resource)]
|
||||
pub struct InputSettings {
|
||||
struct InputSettings {
|
||||
mappings: HashMap<(String, u32, Input), (Control, bool)>,
|
||||
}
|
||||
|
||||
|
@ -121,23 +109,23 @@ impl InputSettings {
|
|||
pub struct Game;
|
||||
|
||||
impl Game {
|
||||
pub fn update(&mut self, commands: &mut Commands) -> Result<()> {
|
||||
// if FREE_CAMERA_CONTROL {
|
||||
// let now = commands.now();
|
||||
pub fn update(
|
||||
&mut self,
|
||||
commands: &mut Commands,
|
||||
scene: &mut Scene,
|
||||
camera_control: &mut FreeCameraControl,
|
||||
) -> 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<'_>,
|
||||
input_settings: &mut InputSettings,
|
||||
) -> Result<()> {
|
||||
if let Some(event) = Self::joystick_movement(commands, event, input_settings)? {
|
||||
pub fn event(&mut self, commands: &mut Commands, event: EngineEvent<'_>) -> Result<()> {
|
||||
if let Some(event) = Self::motion_concepts(world, event)? {
|
||||
match event {
|
||||
EngineEvent::JoystickAdded(joystick) => {
|
||||
println!("joystick {} added", joystick.name());
|
||||
|
@ -154,78 +142,85 @@ 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>(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>(
|
||||
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()?;
|
||||
|
||||
// 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>(
|
||||
commands: &mut Commands,
|
||||
world: &mut World,
|
||||
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)
|
||||
|
@ -235,18 +230,14 @@ impl Game {
|
|||
};
|
||||
|
||||
match control {
|
||||
Control::Throttle => {
|
||||
commands.write_event(PlayerInputEvent::Throttle(normalized))
|
||||
}
|
||||
Control::Throttle => player_control.set_throttle(normalized),
|
||||
Control::StrafeHorizontal => {
|
||||
commands.write_event(PlayerInputEvent::StrafeHorizontal(normalized))
|
||||
player_control.set_left_right_strafe(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::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::PrimaryWeapon => (),
|
||||
Control::SecondaryWeapon => (),
|
||||
|
@ -263,17 +254,14 @@ 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(())
|
||||
}
|
||||
|
||||
|
@ -286,9 +274,9 @@ impl Game {
|
|||
0.02,
|
||||
FreeSpaceControlSettings::default(),
|
||||
));
|
||||
fighter.insert_component(Player);
|
||||
|
||||
world.add_entity(fighter)?;
|
||||
let player = PlayerEntity(world.add_entity(fighter)?);
|
||||
world.resources.insert(player);
|
||||
world.resources.insert(InputSettings::default());
|
||||
|
||||
let mut example_sun = CelestialObject::new(world, CelestialClass::Sun, 5, None)?;
|
||||
|
@ -377,33 +365,3 @@ 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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use anyhow::Result;
|
|||
use ecs::*;
|
||||
use engine::prelude::*;
|
||||
|
||||
use crate::game::{Game, InputSettings};
|
||||
use crate::game::Game;
|
||||
|
||||
#[derive(Default, Resource)]
|
||||
pub enum GameState {
|
||||
|
@ -14,24 +14,26 @@ pub enum GameState {
|
|||
}
|
||||
|
||||
impl GameState {
|
||||
pub fn update(commands: &mut Commands, game_state: &mut GameState) -> Result<bool> {
|
||||
pub fn update(
|
||||
commands: &mut Commands,
|
||||
game_state: &mut GameState,
|
||||
scene: &mut Scene,
|
||||
camera_control: &mut FreeCameraControl,
|
||||
) -> Result<bool> {
|
||||
match game_state {
|
||||
GameState::Startup => *game_state = GameState::Game(Game),
|
||||
GameState::Game(game) => game.update(commands)?,
|
||||
GameState::Game(game) => game.update(commands, scene, camera_control)?,
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn event(
|
||||
&mut self,
|
||||
commands: &mut Commands,
|
||||
event: EngineEvent<'_>,
|
||||
input_settings: &mut InputSettings,
|
||||
) -> Result<()> {
|
||||
impl EventConsumer for GameState {
|
||||
fn event(&mut self, commands: &mut Commands, event: EngineEvent<'_>) -> Result<()> {
|
||||
match self {
|
||||
GameState::Startup => (),
|
||||
GameState::Game(game) => game.event(commands, event, input_settings)?,
|
||||
GameState::Game(game) => game.event(commands, event)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -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,8 +21,7 @@ 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(engine_ci, &mut world_builder)?;
|
||||
Engine::event_system(&mut world_builder, GameState::event);
|
||||
Engine::new::<GameState>(engine_ci, &mut world_builder)?;
|
||||
|
||||
world_builder.add_system(10, GameState::update);
|
||||
world_builder.resources.insert(GameState::default());
|
||||
|
@ -41,12 +40,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();
|
||||
|
|
|
@ -3,15 +3,32 @@ 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(EngineCreateInfo::default(), &mut world_builder)?;
|
||||
Engine::event_system(&mut world_builder, GameState::event);
|
||||
Engine::new::<GameState>(EngineCreateInfo::default(), &mut world_builder)?;
|
||||
Engine::event_system(&mut world_builder);
|
||||
|
||||
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)?;
|
||||
|
@ -37,17 +54,13 @@ impl GameState {
|
|||
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
fn event(
|
||||
&mut self,
|
||||
commands: &mut Commands,
|
||||
event: EngineEvent<'_>,
|
||||
camera_control: &mut FreeCameraControl,
|
||||
scene: &mut Scene,
|
||||
) -> Result<()> {
|
||||
impl EventConsumer for GameState {
|
||||
fn event(&mut self, commands: &mut Commands, event: EngineEvent<'_>) -> Result<()> {
|
||||
match self {
|
||||
GameState::Startup => (),
|
||||
GameState::Game(game) => game.event(commands, event, camera_control, scene)?,
|
||||
GameState::Game(game) => game.event(commands, event)?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -57,27 +70,48 @@ impl 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<'_>,
|
||||
camera_control: &mut FreeCameraControl,
|
||||
scene: &mut Scene,
|
||||
) -> Result<()> {
|
||||
fn event(&mut self, commands: &mut Commands, event: EngineEvent<'_>) -> Result<()> {
|
||||
match event {
|
||||
EngineEvent::MouseButtonDown(MouseButton::Left) => {
|
||||
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())?;
|
||||
commands.write_event(CameraControlStart)
|
||||
}
|
||||
EngineEvent::MouseButtonUp(MouseButton::Left) => commands.write_event(CameraControlEnd),
|
||||
EngineEvent::MouseMotion(x, y) => commands.write_event(CameraControlMove { x, y }),
|
||||
|
||||
_ => (),
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue