add digit tool
This commit is contained in:
parent
465b5c40d1
commit
fc6c66ff31
5 changed files with 130 additions and 65 deletions
BIN
assets/digit_tool.png
Normal file
BIN
assets/digit_tool.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 179 B |
Binary file not shown.
Before Width: | Height: | Size: 116 B After Width: | Height: | Size: 116 B |
66
src/main.rs
66
src/main.rs
|
@ -1,7 +1,7 @@
|
||||||
use std::{fs::read_to_string, ops::Rem};
|
use std::{fs::read_to_string, ops::Rem};
|
||||||
|
|
||||||
use marble_engine::{
|
use marble_engine::{
|
||||||
board::Board,
|
board::{Board, Pos},
|
||||||
tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile, WireType},
|
tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile, WireType},
|
||||||
Machine,
|
Machine,
|
||||||
};
|
};
|
||||||
|
@ -31,16 +31,16 @@ struct Game {
|
||||||
time_since_step: f32,
|
time_since_step: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone)]
|
||||||
enum Tool {
|
enum Tool {
|
||||||
None,
|
None,
|
||||||
SetTile(Tile),
|
SetTile(Tile),
|
||||||
|
Digits(Option<Pos>),
|
||||||
Math,
|
Math,
|
||||||
Gate,
|
Gate,
|
||||||
Wire,
|
Wire,
|
||||||
Arrow,
|
Arrow,
|
||||||
Mirror,
|
Mirror,
|
||||||
Number,
|
|
||||||
// SelectArea,
|
// SelectArea,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ impl Game {
|
||||||
}
|
}
|
||||||
Tool::None => (),
|
Tool::None => (),
|
||||||
Tool::SetTile(_) => (),
|
Tool::SetTile(_) => (),
|
||||||
Tool::Number => (),
|
Tool::Digits(_) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,7 +271,7 @@ impl Game {
|
||||||
texture_button(
|
texture_button(
|
||||||
d,
|
d,
|
||||||
Vector2 {
|
Vector2 {
|
||||||
x: 300. + col as f32 * bound_offset - if col < 0 { 15. } else { 0. },
|
x: 320. + col as f32 * bound_offset - if col < 0 { 15. } else { 0. },
|
||||||
y: footer_top + 5. + row as f32 * bound_offset,
|
y: footer_top + 5. + row as f32 * bound_offset,
|
||||||
},
|
},
|
||||||
textures.get(texture),
|
textures.get(texture),
|
||||||
|
@ -281,7 +281,8 @@ impl Game {
|
||||||
border,
|
border,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
tool_button((0, -1), "eraser", Tool::SetTile(Tile::from_char(' ')));
|
tool_button((0, -2), "eraser", Tool::SetTile(Tile::from_char(' ')));
|
||||||
|
tool_button((0, -1), "digit_tool", Tool::Digits(None));
|
||||||
tool_button((1, -1), "transparent", Tool::None);
|
tool_button((1, -1), "transparent", Tool::None);
|
||||||
|
|
||||||
tool_button((0, 0), "block", Tool::SetTile(Tile::from_char('#')));
|
tool_button((0, 0), "block", Tool::SetTile(Tile::from_char('#')));
|
||||||
|
@ -325,7 +326,35 @@ impl Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mouse_pos = d.get_mouse_position();
|
let mouse_pos = d.get_mouse_position();
|
||||||
if self.sim_state == SimState::Editing && mouse_pos.y < footer_top {
|
if self.sim_state == SimState::Editing {
|
||||||
|
if let Tool::Digits(Some(pos)) = &mut self.active_tool {
|
||||||
|
let tile_screen_pos = pos.to_vec() * (16 << self.zoom) as f32 + self.view_offset;
|
||||||
|
d.draw_texture_ex(
|
||||||
|
textures.get("selection"),
|
||||||
|
tile_screen_pos,
|
||||||
|
0.,
|
||||||
|
(1 << self.zoom) as f32,
|
||||||
|
Color::new(255, 180, 20, 255),
|
||||||
|
);
|
||||||
|
for n in 0..10 {
|
||||||
|
if d.is_key_pressed(unsafe { std::mem::transmute(KeyboardKey::KEY_ZERO as u32 + n) }) {
|
||||||
|
self.source_board.set(*pos, Tile::Digit(n as u8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if d.is_key_pressed(KeyboardKey::KEY_LEFT) {
|
||||||
|
pos.x -= 1;
|
||||||
|
}
|
||||||
|
if d.is_key_pressed(KeyboardKey::KEY_RIGHT) {
|
||||||
|
pos.x += 1;
|
||||||
|
}
|
||||||
|
if d.is_key_pressed(KeyboardKey::KEY_UP) {
|
||||||
|
pos.y -= 1;
|
||||||
|
}
|
||||||
|
if d.is_key_pressed(KeyboardKey::KEY_DOWN) {
|
||||||
|
pos.y += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mouse_pos.y < footer_top {
|
||||||
let tile_pos = (mouse_pos - self.view_offset) / (16 << self.zoom) as f32;
|
let tile_pos = (mouse_pos - self.view_offset) / (16 << self.zoom) as f32;
|
||||||
let tile_pos = Vector2::new(tile_pos.x.floor(), tile_pos.y.floor());
|
let tile_pos = Vector2::new(tile_pos.x.floor(), tile_pos.y.floor());
|
||||||
|
|
||||||
|
@ -346,7 +375,7 @@ impl Game {
|
||||||
Tool::Wire => format!("{}_off", self.tool_menu_wire.texture_name()),
|
Tool::Wire => format!("{}_off", self.tool_menu_wire.texture_name()),
|
||||||
Tool::Arrow => self.tool_menu_arrow.arrow_texture_name().into(),
|
Tool::Arrow => self.tool_menu_arrow.arrow_texture_name().into(),
|
||||||
Tool::Mirror => self.tool_menu_mirror.texture_name().into(),
|
Tool::Mirror => self.tool_menu_mirror.texture_name().into(),
|
||||||
Tool::Number => todo!(),
|
Tool::Digits(_) => "selection".into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
d.draw_texture_ex(
|
d.draw_texture_ex(
|
||||||
|
@ -379,9 +408,28 @@ impl Game {
|
||||||
Tool::Mirror => self
|
Tool::Mirror => self
|
||||||
.source_board
|
.source_board
|
||||||
.set(tile_pos.into(), Tile::Mirror(self.tool_menu_mirror)),
|
.set(tile_pos.into(), Tile::Mirror(self.tool_menu_mirror)),
|
||||||
Tool::Number => todo!(),
|
Tool::Digits(_pos) => {
|
||||||
|
self.active_tool = Tool::Digits(Some(tile_pos.into()));
|
||||||
|
if let Some(tile) = self.source_board.get_mut(tile_pos.into()) {
|
||||||
|
if let Tile::Digit(_) = tile {
|
||||||
|
} else {
|
||||||
|
*tile = Tile::Digit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Tool{
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
match (self, other) {
|
||||||
|
(Self::SetTile(l0), Self::SetTile(r0)) => l0 == r0,
|
||||||
|
(Self::Digits(_), Self::Digits(_)) => true,
|
||||||
|
_ => ::core::mem::discriminant(self) == ::core::mem::discriminant(other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -89,7 +89,7 @@ impl Machine {
|
||||||
// reset wires
|
// reset wires
|
||||||
for y in 0..self.board.height() {
|
for y in 0..self.board.height() {
|
||||||
for x in 0..self.board.width() {
|
for x in 0..self.board.width() {
|
||||||
if let Tile::Powerable(_, state) = self.board.get_mut((x, y).into()) {
|
if let Tile::Powerable(_, state) = self.board.get_mut_unchecked((x, y).into()) {
|
||||||
*state = false;
|
*state = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,7 @@ impl Machine {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut new_tile = None;
|
let mut new_tile = None;
|
||||||
let target = self.board.get_mut(next_pos);
|
let target = self.board.get_mut_unchecked(next_pos);
|
||||||
match target {
|
match target {
|
||||||
Tile::Blank => {
|
Tile::Blank => {
|
||||||
*target = tile;
|
*target = tile;
|
||||||
|
@ -175,7 +175,7 @@ impl Machine {
|
||||||
Tile::Mirror(mirror) => {
|
Tile::Mirror(mirror) => {
|
||||||
let new_dir = mirror.new_dir(dir);
|
let new_dir = mirror.new_dir(dir);
|
||||||
let far_pos = new_dir.step(next_pos);
|
let far_pos = new_dir.step(next_pos);
|
||||||
let far_target = self.board.get_mut(far_pos);
|
let far_target = self.board.get_mut_unchecked(far_pos);
|
||||||
if let Tile::Blank = far_target {
|
if let Tile::Blank = far_target {
|
||||||
*far_target = Tile::Marble {
|
*far_target = Tile::Marble {
|
||||||
value,
|
value,
|
||||||
|
@ -189,7 +189,7 @@ impl Machine {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(t) = new_tile {
|
if let Some(t) = new_tile {
|
||||||
*self.board.get_mut(marble_pos) = t;
|
*self.board.get_mut_unchecked(marble_pos) = t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -209,7 +209,7 @@ impl Machine {
|
||||||
if !self.board.in_bounds(pos) {
|
if !self.board.in_bounds(pos) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let tile = self.board.get_mut(pos);
|
let tile = self.board.get_mut_unchecked(pos);
|
||||||
let front_pos = dir.step(pos);
|
let front_pos = dir.step(pos);
|
||||||
if let Tile::Powerable(tile, state) = tile {
|
if let Tile::Powerable(tile, state) = tile {
|
||||||
if let PTile::Trigger = tile {
|
if let PTile::Trigger = tile {
|
||||||
|
@ -234,7 +234,7 @@ impl Machine {
|
||||||
}
|
}
|
||||||
PTile::Bag => {
|
PTile::Bag => {
|
||||||
if let Some(Tile::Blank) = self.board.get(front_pos) {
|
if let Some(Tile::Blank) = self.board.get(front_pos) {
|
||||||
*self.board.get_mut(front_pos) = Tile::Marble { value: 0, dir };
|
*self.board.get_mut_unchecked(front_pos) = Tile::Marble { value: 0, dir };
|
||||||
self.marbles.push(front_pos);
|
self.marbles.push(front_pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -243,7 +243,7 @@ impl Machine {
|
||||||
&& self.board.get_or_blank(front_pos).is_blank()
|
&& self.board.get_or_blank(front_pos).is_blank()
|
||||||
{
|
{
|
||||||
let value = self.input[self.input_index] as MarbleValue;
|
let value = self.input[self.input_index] as MarbleValue;
|
||||||
*self.board.get_mut(front_pos) = Tile::Marble { value, dir };
|
*self.board.get_mut_unchecked(front_pos) = Tile::Marble { value, dir };
|
||||||
self.marbles.push(front_pos);
|
self.marbles.push(front_pos);
|
||||||
self.input_index += 1;
|
self.input_index += 1;
|
||||||
}
|
}
|
||||||
|
@ -266,12 +266,12 @@ impl Machine {
|
||||||
MathOp::Rem => val_a.checked_rem(val_b).unwrap_or_default(),
|
MathOp::Rem => val_a.checked_rem(val_b).unwrap_or_default(),
|
||||||
};
|
};
|
||||||
// println!("{op:?} a:{val_a} b:{val_b}");
|
// println!("{op:?} a:{val_a} b:{val_b}");
|
||||||
*self.board.get_mut(front_pos) = Tile::Marble { value: result, dir };
|
*self.board.get_mut_unchecked(front_pos) = Tile::Marble { value: result, dir };
|
||||||
self.marbles.push(front_pos);
|
self.marbles.push(front_pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PTile::Flipper => {
|
PTile::Flipper => {
|
||||||
let m = self.board.get_mut(front_pos);
|
let m = self.board.get_mut_unchecked(front_pos);
|
||||||
match m {
|
match m {
|
||||||
Tile::Powerable(PTile::Wire(wire_type), _) => {
|
Tile::Powerable(PTile::Wire(wire_type), _) => {
|
||||||
*wire_type = match *wire_type {
|
*wire_type = match *wire_type {
|
||||||
|
|
|
@ -9,6 +9,15 @@ pub struct Pos {
|
||||||
pub y: isize,
|
pub y: isize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Pos {
|
||||||
|
pub const fn to_vec(&self) -> Vector2 {
|
||||||
|
Vector2 {
|
||||||
|
x: self.x as f32,
|
||||||
|
y: self.y as f32,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<(usize, usize)> for Pos {
|
impl From<(usize, usize)> for Pos {
|
||||||
fn from(value: (usize, usize)) -> Self {
|
fn from(value: (usize, usize)) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -91,7 +100,15 @@ impl Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut(&mut self, p: Pos) -> &mut Tile {
|
pub fn get_mut(&mut self, p: Pos) -> Option<&mut Tile> {
|
||||||
|
if self.in_bounds(p) {
|
||||||
|
Some(&mut self.rows[p.y as usize][p.x as usize])
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_mut_unchecked(&mut self, p: Pos) -> &mut Tile {
|
||||||
if self.in_bounds(p) {
|
if self.in_bounds(p) {
|
||||||
&mut self.rows[p.y as usize][p.x as usize]
|
&mut self.rows[p.y as usize][p.x as usize]
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue