Commit e9392a74 authored by Rahix's avatar Rahix 🦀
Browse files

gamestate: Remove target argument from event()

It's only relevant for mouse events so only make it available there.
parent 6836b014
......@@ -54,14 +54,17 @@ pub trait State {
Transition::Keep
}
#[allow(unused_variables)]
fn event(&mut self, target: &str, event: Event) -> Transition {
fn event(&mut self, event: Event) -> Transition {
Transition::Keep
}
}
#[derive(Debug, Clone)]
pub enum Event<'a> {
MouseClick(nalgebra::Point2<f32>),
MouseClick {
target: &'a str,
coords: nalgebra::Point2<f32>,
},
KeyDown(&'a str),
KeyUp(&'a str),
}
......@@ -167,8 +170,8 @@ impl StateMachineHandle {
StateMachine::do_transition(&self.0, t)
}
pub fn do_event(&self, id: &str, event: Event) {
let t = StateMachine::active(&self.0).event(id, event);
pub fn do_event(&self, event: Event) {
let t = StateMachine::active(&self.0).event(event);
self.do_transition(t)
}
}
......@@ -197,7 +200,10 @@ impl<'a> StateInitializer<'a> {
let click = nalgebra::Point2::new(event.client_x() as f64, event.client_y() as f64);
let canvas_click = utils::screen_to_contain_canvas(&canvas, click);
let canvas_click = nalgebra::Point2::new(canvas_click.x as f32, canvas_click.y as f32);
handle.do_event(&id2, Event::MouseClick(canvas_click));
handle.do_event(Event::MouseClick {
target: &id2,
coords: canvas_click,
});
});
}
......@@ -206,9 +212,9 @@ impl<'a> StateInitializer<'a> {
self.event_handlers
.register_keyevents(move |keydown, event| {
if keydown {
handle.do_event("", Event::KeyDown(&event.key()));
handle.do_event(Event::KeyDown(&event.key()));
} else {
handle.do_event("", Event::KeyUp(&event.key()));
handle.do_event(Event::KeyUp(&event.key()));
}
});
}
......@@ -250,8 +256,8 @@ impl StateStorage {
self.state.update()
}
fn event(&mut self, target: &str, event: Event) -> Transition {
self.state.event(target, event)
fn event(&mut self, event: Event) -> Transition {
self.state.event(event)
}
}
......@@ -307,8 +313,8 @@ impl StateMachine {
std::cell::RefMut::map(this.borrow_mut(), |sm| sm.states.last_mut().unwrap())
}
pub fn do_event(this: &Rc<RefCell<StateMachine>>, id: &str, event: Event) {
let t = StateMachine::active(this).event(id, event);
pub fn do_event(this: &Rc<RefCell<StateMachine>>, event: Event) {
let t = StateMachine::active(this).event(event);
StateMachine::do_transition(this, t);
}
......
......@@ -108,13 +108,14 @@ impl gamestate::State for HeavenState {
gamestate::Transition::Loop
}
fn event(&mut self, target: &str, event: gamestate::Event) -> gamestate::Transition {
match (target, event) {
("heaven-start-shift", gamestate::Event::MouseClick { .. }) => {
gamestate::Transition::push(states::LevelLoadingState::new())
}
(target, event) => {
crate::console_log!("unknown event from {:?}: {:?}", target, event);
fn event(&mut self, event: gamestate::Event) -> gamestate::Transition {
match event {
gamestate::Event::MouseClick {
target: "heaven-start-shift",
..
} => gamestate::Transition::push(states::LevelLoadingState::new()),
event => {
crate::console_log!("unknown event {:?}", event);
gamestate::Transition::Keep
}
}
......
......@@ -70,7 +70,7 @@ impl gamestate::State for InGameState {
.unwrap();
}
fn event(&mut self, _target: &str, event: gamestate::Event) -> gamestate::Transition {
fn event(&mut self, event: gamestate::Event) -> gamestate::Transition {
crate::console_log!("event: {:?}", event);
gamestate::Transition::Keep
}
......
......@@ -31,13 +31,14 @@ impl gamestate::State for MainMenuState {
.unwrap();
}
fn event(&mut self, target: &str, event: gamestate::Event) -> gamestate::Transition {
match (target, event) {
("main-menu-play", gamestate::Event::MouseClick { .. }) => {
gamestate::Transition::push(states::HeavenState::new())
}
(target, event) => {
crate::console_log!("unknown event from {:?}: {:?}", target, event);
fn event(&mut self, event: gamestate::Event) -> gamestate::Transition {
match event {
gamestate::Event::MouseClick {
target: "main-menu-play",
..
} => gamestate::Transition::push(states::HeavenState::new()),
event => {
crate::console_log!("unknown event {:?}", event);
gamestate::Transition::Keep
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment