Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions #4

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions bdi_game/src/display/actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::simulation::grid;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::mouse::MouseButton;
use sdl2::{EventPump, Sdl};
use std::collections::vec_deque::{Iter, IterMut};
use std::collections::VecDeque;

pub enum Action {
ClickGridCell(grid::GridPoint),
QuitGame(),
}

impl Action {
pub fn check_click_grid_cell(x: i32, y: i32) -> Option<Self> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go with something like from_click

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed it to from_left_click in case we want to use right mouse button later.

todo!()
}
}

pub struct ActionPump {
sdl_pump: EventPump,
actions: VecDeque<Action>,
}

impl ActionPump {
pub fn new(sdl_context: Sdl) -> Self {
Self {
sdl_pump: sdl_context
.event_pump()
.expect("Failed to initialize SDL Event Pump."),
actions: VecDeque::new(),
}
}

pub fn poll_action(&mut self) -> Option<Action> {
self.actions.pop_front()
}

pub fn poll_iter(&mut self) -> Iter<'_, Action> {
self.actions.iter()
}

pub fn poll_iter_mut(&mut self) -> IterMut<'_, Action> {
self.actions.iter_mut()
}

fn pump_actions(&mut self) {
for sdl_ev in self.sdl_pump.poll_iter() {
let action = Self::match_input(sdl_ev);
if let Some(action) = action {
self.actions.push_back(action)
}
}
}

fn match_input(sdl_ev: Event) -> Option<Action> {
match sdl_ev {
Event::KeyDown { keycode, .. } => Self::match_key_down(keycode.unwrap()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid .unwrap() at all cost :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added if keycode.is_some() match guard so it always works

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try if Event::KeyDown { Some(keycode), .. } works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, i get error, keycode itself is of type Option, so there is no point in wrapping it in Some()


Event::MouseButtonDown {
mouse_btn, x, y, ..
} => Self::match_button_down(mouse_btn, x, y),

_ => None,
}
}

fn match_key_down(key: Keycode) -> Option<Action> {
match key {
Keycode::Escape => Some(Action::QuitGame()),
_ => None,
}
}

fn match_button_down(button: MouseButton, x: i32, y: i32) -> Option<Action> {
match button {
MouseButton::Left => Action::check_click_grid_cell(x, y),
_ => None,
}
}
}
1 change: 1 addition & 0 deletions bdi_game/src/display/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod actions;
pub mod game_display;
pub mod sdl_display;
2 changes: 1 addition & 1 deletion bdi_game/src/simulation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod grid;
pub mod grid;
pub mod simulation;
pub mod world_grid;
pub mod world_state;
Loading