From 69abade8bede3507620ad857a8f82107b2110a4a Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Wed, 1 Mar 2023 09:47:24 +0100 Subject: [PATCH] Added ability to place blocks behind or below others --- README.md | 24 ++++++++++-------------- src/game/world.rs | 5 +++++ src/screens/build.rs | 17 ++++++++++++----- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 2d20334..fafceee 100644 --- a/README.md +++ b/README.md @@ -9,17 +9,6 @@ with this 2D game ## Screenshot ![img](./screenshot.png) -To try out the example world on your device, -copy the `example.world` to the applications data dir. - -On Linux, this is `~/.local/share/little_town/`, -if you are not running Linux, -have a look at the [ProjectDir Value Table](https://github.com/xdg-rs/dirs/tree/master/directories#projectdirs). - -After copying the file, -launch the game and the world should appear in the world list - - ## Roadmap - [x] Placing & Removing blocks - [x] Basic Welcome Screen @@ -32,17 +21,24 @@ launch the game and the world should appear in the world list - [x] World creation screen - [ ] (MAYBE) campaign mode (build & expand a city with limited ressources) - [ ] Touch control + - [ ] Move screen with 2 fingers + - [ ] zoom camera with pinch gesture + - [ ] place block with single click + - [ ] destroy block with long click - [ ] Settings menu (i.e for Keybindings) -- [ ] Game music? +- [ ] Background music (ambient) - [ ] Add more assets - [x] SketchTown Expansion pack - [ ] (MAYBE) [Sketch Desert](https://kenney.nl/assets/sketch-desert) +- [x] Placing blocks on the other side of a block (Shift+Mouse) + - [x] (requires) rotate block using Ctrl+Mouse ## Keymap ### in-Game | Action | Key(s) / MouseButton | |--------|----------------------| | place Block | Left mouse button | +| place Block on inverted face | `` and left mouse button | | destroy Block | right mouse button | | Save Game | `S` | | Save & Quit | `Q` or `` | @@ -54,8 +50,8 @@ launch the game and the world should appear in the world list | South | `J` | | West | `H` | | East | `L` | -| rotate CCW | `` and left mouse button | -| rotate CW | `` and right mouse button | +| rotate CCW | `` and left mouse button | +| rotate CW | `` and right mouse button | #### Camera movement | Action | Key(s) / MouseButton | diff --git a/src/game/world.rs b/src/game/world.rs index 3f17ac7..b57c7a5 100644 --- a/src/game/world.rs +++ b/src/game/world.rs @@ -81,6 +81,11 @@ impl World { /// can be used to remove items /// or place items pub fn place_block(&mut self, pos: &Pos3, block: Block, dir: Direction) -> bool { + // dont place block if there is a block here + if self.grid.contains_key(pos) { + return false; + } + if let Some(count) = self.inventory.contents.get_mut(&block) { // player has block if *count > 0 { diff --git a/src/screens/build.rs b/src/screens/build.rs index 527321d..603040a 100644 --- a/src/screens/build.rs +++ b/src/screens/build.rs @@ -293,7 +293,6 @@ impl GameComponent for BuildScreen { } fn ev_loop(&mut self) -> GameEvent { if let Some(world) = &mut self.world { - if self.inventory_widgets.shown { // process input match self.inventory_widgets.widget_back.ev_loop() { @@ -414,7 +413,7 @@ impl GameComponent for BuildScreen { // for a given pixel let mut pos = Pos3::new(pos.x, pos.y, pos.z); - if is_key_down(KeyCode::LeftShift) { + if is_key_down(KeyCode::LeftControl) { if let Some(block) = world.grid.get_mut(&pos) { if is_mouse_button_pressed(MouseButton::Left) { // rotate block CCW @@ -448,15 +447,23 @@ impl GameComponent for BuildScreen { let (x,y) = get_screen_coords(&pos, world.cam.scale, world.cam.center); let face = Face::from_xy(mx-x, my-y, world.cam.scale); + // invert coordinate when left shift is pressed + // allows placing blocks below & behind other block + let factor = if is_key_down(KeyCode::LeftShift) { + -1 + } else { + 1 + }; + match face.unwrap() { Face::Top => { - pos.z+=1; + pos.z+=factor; }, Face::Left => { - pos.x+=1; + pos.x+=factor; }, Face::Right => { - pos.y+=1; + pos.y+=factor; } } -- 2.38.5