mirror of
https://github.com/Noratrieb/game-wip-dontplay.git
synced 2026-01-17 04:45:02 +01:00
Add basic block selection with mouse
This commit is contained in:
parent
98965a8c86
commit
f07bd9c713
4 changed files with 66 additions and 26 deletions
46
src/app.rs
46
src/app.rs
|
|
@ -1,7 +1,7 @@
|
||||||
use std::fmt::{self};
|
use std::fmt::{self};
|
||||||
|
|
||||||
use egui_sfml::{egui, SfEgui};
|
use egui_sfml::{egui, SfEgui};
|
||||||
use gamedebug_core::imm_dbg;
|
use gamedebug_core::{imm, imm_dbg};
|
||||||
use sfml::{
|
use sfml::{
|
||||||
graphics::{Color, RenderTarget, RenderWindow},
|
graphics::{Color, RenderTarget, RenderWindow},
|
||||||
window::{Event, Key},
|
window::{Event, Key},
|
||||||
|
|
@ -11,7 +11,7 @@ use crate::{
|
||||||
debug::DebugState,
|
debug::DebugState,
|
||||||
game::{for_each_tile_on_screen, GameState},
|
game::{for_each_tile_on_screen, GameState},
|
||||||
graphics::{self, NATIVE_RESOLUTION},
|
graphics::{self, NATIVE_RESOLUTION},
|
||||||
input::KbInput,
|
input::Input,
|
||||||
math::{wp_to_tp, WorldPos, TILE_SIZE},
|
math::{wp_to_tp, WorldPos, TILE_SIZE},
|
||||||
res::Res,
|
res::Res,
|
||||||
world::Tile,
|
world::Tile,
|
||||||
|
|
@ -24,7 +24,7 @@ pub struct App {
|
||||||
pub game: GameState,
|
pub game: GameState,
|
||||||
pub res: Res,
|
pub res: Res,
|
||||||
pub sf_egui: SfEgui,
|
pub sf_egui: SfEgui,
|
||||||
pub kb_input: KbInput,
|
pub input: Input,
|
||||||
pub debug: DebugState,
|
pub debug: DebugState,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,7 +38,7 @@ impl App {
|
||||||
game: GameState::default(),
|
game: GameState::default(),
|
||||||
res: Res::load()?,
|
res: Res::load()?,
|
||||||
sf_egui,
|
sf_egui,
|
||||||
kb_input: KbInput::default(),
|
input: Input::default(),
|
||||||
debug: DebugState::default(),
|
debug: DebugState::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -48,7 +48,7 @@ impl App {
|
||||||
self.do_event_handling();
|
self.do_event_handling();
|
||||||
self.do_update();
|
self.do_update();
|
||||||
self.do_rendering();
|
self.do_rendering();
|
||||||
self.kb_input.clear_pressed();
|
self.input.clear_pressed();
|
||||||
gamedebug_core::inc_frame();
|
gamedebug_core::inc_frame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -56,7 +56,7 @@ impl App {
|
||||||
fn do_event_handling(&mut self) {
|
fn do_event_handling(&mut self) {
|
||||||
while let Some(ev) = self.rw.poll_event() {
|
while let Some(ev) = self.rw.poll_event() {
|
||||||
self.sf_egui.add_event(&ev);
|
self.sf_egui.add_event(&ev);
|
||||||
self.kb_input.update_from_event(&ev);
|
self.input.update_from_event(&ev);
|
||||||
match ev {
|
match ev {
|
||||||
Event::Closed => self.should_quit = true,
|
Event::Closed => self.should_quit = true,
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
@ -65,24 +65,24 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_update(&mut self) {
|
fn do_update(&mut self) {
|
||||||
self.debug.update(&self.kb_input);
|
self.debug.update(&self.input);
|
||||||
if self.debug.freecam {
|
if self.debug.freecam {
|
||||||
self.do_freecam();
|
self.do_freecam();
|
||||||
} else {
|
} else {
|
||||||
let spd = if self.kb_input.down(Key::LShift) {
|
let spd = if self.input.down(Key::LShift) {
|
||||||
16.0
|
16.0
|
||||||
} else if self.kb_input.down(Key::LControl) {
|
} else if self.input.down(Key::LControl) {
|
||||||
256.0
|
256.0
|
||||||
} else {
|
} else {
|
||||||
4.0
|
4.0
|
||||||
};
|
};
|
||||||
if self.kb_input.down(Key::Left) {
|
if self.input.down(Key::Left) {
|
||||||
self.game.player.col_en.move_x(-spd, |_, _| false);
|
self.game.player.col_en.move_x(-spd, |_, _| false);
|
||||||
}
|
}
|
||||||
if self.kb_input.down(Key::Right) {
|
if self.input.down(Key::Right) {
|
||||||
self.game.player.col_en.move_x(spd, |_, _| false);
|
self.game.player.col_en.move_x(spd, |_, _| false);
|
||||||
}
|
}
|
||||||
if self.kb_input.pressed(Key::Up) {
|
if self.input.pressed(Key::Up) {
|
||||||
self.game.player.vspeed = -14.0;
|
self.game.player.vspeed = -14.0;
|
||||||
}
|
}
|
||||||
self.game.player.vspeed = self.game.player.vspeed.clamp(-80., 80.);
|
self.game.player.vspeed = self.game.player.vspeed.clamp(-80., 80.);
|
||||||
|
|
@ -112,26 +112,36 @@ impl App {
|
||||||
self.game.camera_offset.x = (x - NATIVE_RESOLUTION.w as i32 / 2) as u32;
|
self.game.camera_offset.x = (x - NATIVE_RESOLUTION.w as i32 / 2) as u32;
|
||||||
self.game.camera_offset.y = (y - NATIVE_RESOLUTION.h as i32 / 2) as u32;
|
self.game.camera_offset.y = (y - NATIVE_RESOLUTION.h as i32 / 2) as u32;
|
||||||
}
|
}
|
||||||
|
if self.input.lmb_down {
|
||||||
|
let loc = self.input.mouse_down_loc;
|
||||||
|
let mut wpos = self.game.camera_offset;
|
||||||
|
wpos.x = wpos.x.saturating_add_signed(loc.x.into());
|
||||||
|
wpos.y = wpos.y.saturating_add_signed(loc.y.into());
|
||||||
|
imm!("Mouse down at {}, {}", wpos.x, wpos.y);
|
||||||
|
let tpos = wpos.tile_pos();
|
||||||
|
imm_dbg!(tpos);
|
||||||
|
self.game.world.tile_at_mut(tpos).id = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_freecam(&mut self) {
|
fn do_freecam(&mut self) {
|
||||||
let spd = if self.kb_input.down(Key::LShift) {
|
let spd = if self.input.down(Key::LShift) {
|
||||||
100
|
100
|
||||||
} else if self.kb_input.down(Key::LControl) {
|
} else if self.input.down(Key::LControl) {
|
||||||
1000
|
1000
|
||||||
} else {
|
} else {
|
||||||
2
|
2
|
||||||
};
|
};
|
||||||
if self.kb_input.down(Key::Left) {
|
if self.input.down(Key::Left) {
|
||||||
self.game.camera_offset.x = self.game.camera_offset.x.saturating_sub(spd);
|
self.game.camera_offset.x = self.game.camera_offset.x.saturating_sub(spd);
|
||||||
}
|
}
|
||||||
if self.kb_input.down(Key::Right) {
|
if self.input.down(Key::Right) {
|
||||||
self.game.camera_offset.x = self.game.camera_offset.x.saturating_add(spd);
|
self.game.camera_offset.x = self.game.camera_offset.x.saturating_add(spd);
|
||||||
}
|
}
|
||||||
if self.kb_input.down(Key::Up) {
|
if self.input.down(Key::Up) {
|
||||||
self.game.camera_offset.y = self.game.camera_offset.y.saturating_sub(spd);
|
self.game.camera_offset.y = self.game.camera_offset.y.saturating_sub(spd);
|
||||||
}
|
}
|
||||||
if self.kb_input.down(Key::Down) {
|
if self.input.down(Key::Down) {
|
||||||
self.game.camera_offset.y = self.game.camera_offset.y.saturating_add(spd);
|
self.game.camera_offset.y = self.game.camera_offset.y.saturating_add(spd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use sfml::window::Key;
|
use sfml::window::Key;
|
||||||
|
|
||||||
use crate::input::KbInput;
|
use crate::input::Input;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct DebugState {
|
pub struct DebugState {
|
||||||
|
|
@ -9,7 +9,7 @@ pub struct DebugState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DebugState {
|
impl DebugState {
|
||||||
pub fn update(&mut self, input: &KbInput) {
|
pub fn update(&mut self, input: &Input) {
|
||||||
if input.pressed(Key::F12) {
|
if input.pressed(Key::F12) {
|
||||||
self.panel ^= true;
|
self.panel ^= true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ impl ScreenRes {
|
||||||
}
|
}
|
||||||
|
|
||||||
// We assume this game won't be played above 32767*32767 resolution
|
// We assume this game won't be played above 32767*32767 resolution
|
||||||
|
#[derive(Default, Clone, Copy)]
|
||||||
pub struct ScreenPos {
|
pub struct ScreenPos {
|
||||||
pub x: ScreenPosScalar,
|
pub x: ScreenPosScalar,
|
||||||
pub y: ScreenPosScalar,
|
pub y: ScreenPosScalar,
|
||||||
|
|
|
||||||
41
src/input.rs
41
src/input.rs
|
|
@ -1,22 +1,51 @@
|
||||||
use fnv::FnvHashSet;
|
use fnv::FnvHashSet;
|
||||||
use sfml::window::{Event, Key};
|
use sfml::window::{mouse, Event, Key};
|
||||||
|
|
||||||
|
use crate::graphics::ScreenPos;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct KbInput {
|
pub struct Input {
|
||||||
down: FnvHashSet<Key>,
|
down: FnvHashSet<Key>,
|
||||||
pressed: FnvHashSet<Key>,
|
pressed: FnvHashSet<Key>,
|
||||||
|
pub lmb_down: bool,
|
||||||
|
pub rmb_down: bool,
|
||||||
|
pub mouse_down_loc: ScreenPos,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KbInput {
|
impl Input {
|
||||||
pub fn update_from_event(&mut self, ev: &Event) {
|
pub fn update_from_event(&mut self, ev: &Event) {
|
||||||
match ev {
|
match ev {
|
||||||
Event::KeyPressed { code, .. } => {
|
&Event::KeyPressed { code, .. } => {
|
||||||
self.pressed.insert(*code);
|
self.pressed.insert(code);
|
||||||
self.down.insert(*code);
|
self.down.insert(code);
|
||||||
}
|
}
|
||||||
Event::KeyReleased { code, .. } => {
|
Event::KeyReleased { code, .. } => {
|
||||||
self.down.remove(code);
|
self.down.remove(code);
|
||||||
}
|
}
|
||||||
|
&Event::MouseButtonPressed { button, x, y } => {
|
||||||
|
self.mouse_down_loc = ScreenPos {
|
||||||
|
x: x as i16,
|
||||||
|
y: y as i16,
|
||||||
|
};
|
||||||
|
if button == mouse::Button::Left {
|
||||||
|
self.lmb_down = true;
|
||||||
|
}
|
||||||
|
if button == mouse::Button::Right {
|
||||||
|
self.rmb_down = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
&Event::MouseButtonReleased { button, .. } => {
|
||||||
|
if button == mouse::Button::Left {
|
||||||
|
self.lmb_down = false;
|
||||||
|
}
|
||||||
|
if button == mouse::Button::Right {
|
||||||
|
self.rmb_down = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
&Event::MouseMoved { x, y } => {
|
||||||
|
self.mouse_down_loc.x = x as i16;
|
||||||
|
self.mouse_down_loc.y = y as i16;
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue