group wires into one tool option
This commit is contained in:
parent
ea02eff82b
commit
465b5c40d1
4 changed files with 89 additions and 78 deletions
57
src/main.rs
57
src/main.rs
|
@ -2,9 +2,8 @@ use std::{fs::read_to_string, ops::Rem};
|
||||||
|
|
||||||
use marble_engine::{
|
use marble_engine::{
|
||||||
board::Board,
|
board::Board,
|
||||||
parse,
|
tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile, WireType},
|
||||||
tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile},
|
Machine,
|
||||||
tile_from_char, Machine,
|
|
||||||
};
|
};
|
||||||
use raylib::prelude::*;
|
use raylib::prelude::*;
|
||||||
|
|
||||||
|
@ -26,6 +25,7 @@ struct Game {
|
||||||
tool_menu_gate: GateType,
|
tool_menu_gate: GateType,
|
||||||
tool_menu_arrow: Direction,
|
tool_menu_arrow: Direction,
|
||||||
tool_menu_mirror: MirrorType,
|
tool_menu_mirror: MirrorType,
|
||||||
|
tool_menu_wire: WireType,
|
||||||
input_text_selected: bool,
|
input_text_selected: bool,
|
||||||
sim_speed: f32,
|
sim_speed: f32,
|
||||||
time_since_step: f32,
|
time_since_step: f32,
|
||||||
|
@ -37,6 +37,7 @@ enum Tool {
|
||||||
SetTile(Tile),
|
SetTile(Tile),
|
||||||
Math,
|
Math,
|
||||||
Gate,
|
Gate,
|
||||||
|
Wire,
|
||||||
Arrow,
|
Arrow,
|
||||||
Mirror,
|
Mirror,
|
||||||
Number,
|
Number,
|
||||||
|
@ -65,7 +66,7 @@ fn main() {
|
||||||
textures.load_dir("assets/tiles", &mut rl, &thread);
|
textures.load_dir("assets/tiles", &mut rl, &thread);
|
||||||
|
|
||||||
let mut game = Game::new_sandbox();
|
let mut game = Game::new_sandbox();
|
||||||
let board = parse(&read_to_string("boards/adder.mbl").unwrap());
|
let board = Board::parse(&read_to_string("boards/adder.mbl").unwrap());
|
||||||
game.load_board(board);
|
game.load_board(board);
|
||||||
|
|
||||||
while !rl.window_should_close() {
|
while !rl.window_should_close() {
|
||||||
|
@ -94,6 +95,7 @@ impl Game {
|
||||||
tool_menu_gate: GateType::Equal,
|
tool_menu_gate: GateType::Equal,
|
||||||
tool_menu_arrow: Direction::Right,
|
tool_menu_arrow: Direction::Right,
|
||||||
tool_menu_mirror: MirrorType::Forward,
|
tool_menu_mirror: MirrorType::Forward,
|
||||||
|
tool_menu_wire: WireType::Vertical,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,6 +140,13 @@ impl Game {
|
||||||
MirrorType::Back => MirrorType::Forward,
|
MirrorType::Back => MirrorType::Forward,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Tool::Wire => {
|
||||||
|
self.tool_menu_wire = match self.tool_menu_wire {
|
||||||
|
WireType::Vertical => WireType::Horizontal,
|
||||||
|
WireType::Horizontal => WireType::Cross,
|
||||||
|
WireType::Cross => WireType::Vertical,
|
||||||
|
}
|
||||||
|
}
|
||||||
Tool::None => (),
|
Tool::None => (),
|
||||||
Tool::SetTile(_) => (),
|
Tool::SetTile(_) => (),
|
||||||
Tool::Number => (),
|
Tool::Number => (),
|
||||||
|
@ -272,45 +281,40 @@ impl Game {
|
||||||
border,
|
border,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
tool_button((0, -1), "eraser", Tool::SetTile(tile_from_char(' ')));
|
tool_button((0, -1), "eraser", Tool::SetTile(Tile::from_char(' ')));
|
||||||
tool_button((1, -1), "transparent", Tool::None);
|
tool_button((1, -1), "transparent", Tool::None);
|
||||||
|
|
||||||
tool_button((0, 0), "marble", Tool::SetTile(tile_from_char('o')));
|
tool_button((0, 0), "block", Tool::SetTile(Tile::from_char('#')));
|
||||||
tool_button((0, 1), "block", Tool::SetTile(tile_from_char('#')));
|
tool_button((0, 1), "bag_off", Tool::SetTile(Tile::from_char('B')));
|
||||||
tool_button((0, 2), "bag_off", Tool::SetTile(tile_from_char('B')));
|
tool_button((0, 2), "trigger_off", Tool::SetTile(Tile::from_char('*')));
|
||||||
tool_button((0, 3), "trigger_off", Tool::SetTile(tile_from_char('*')));
|
tool_button((0, 3), "input_off", Tool::SetTile(Tile::from_char('I')));
|
||||||
tool_button((0, 4), "input_off", Tool::SetTile(tile_from_char('I')));
|
tool_button((0, 4), "output_off", Tool::SetTile(Tile::from_char('P')));
|
||||||
tool_button((0, 5), "output_off", Tool::SetTile(tile_from_char('P')));
|
tool_button((0, 5), "flipper_off", Tool::SetTile(Tile::from_char('F')));
|
||||||
tool_button((0, 6), "flipper_off", Tool::SetTile(tile_from_char('F')));
|
|
||||||
|
|
||||||
tool_button(
|
tool_button((1, 0), "marble", Tool::SetTile(Tile::from_char('o')));
|
||||||
(1, 0),
|
|
||||||
"wire_horizontal_off",
|
|
||||||
Tool::SetTile(tile_from_char('-')),
|
|
||||||
);
|
|
||||||
tool_button(
|
tool_button(
|
||||||
(1, 1),
|
(1, 1),
|
||||||
"wire_vertical_off",
|
&Tile::Powerable(PTile::Wire(self.tool_menu_wire), false).texture(),
|
||||||
Tool::SetTile(tile_from_char('|')),
|
Tool::Wire,
|
||||||
);
|
);
|
||||||
tool_button((1, 2), "wire_cross_off", Tool::SetTile(tile_from_char('+')));
|
|
||||||
tool_button(
|
tool_button(
|
||||||
(1, 3),
|
(1, 2),
|
||||||
&Tile::Arrow(self.tool_menu_arrow).texture(),
|
&Tile::Arrow(self.tool_menu_arrow).texture(),
|
||||||
Tool::Arrow,
|
Tool::Arrow,
|
||||||
);
|
);
|
||||||
tool_button(
|
tool_button(
|
||||||
(1, 4),
|
(1, 3),
|
||||||
&Tile::Mirror(self.tool_menu_mirror).texture(),
|
&Tile::Mirror(self.tool_menu_mirror).texture(),
|
||||||
Tool::Mirror,
|
Tool::Mirror,
|
||||||
);
|
);
|
||||||
tool_button(
|
tool_button(
|
||||||
(1, 5),
|
(1, 4),
|
||||||
&Tile::Powerable(PTile::Math(self.tool_menu_math), false).texture(),
|
&Tile::Powerable(PTile::Math(self.tool_menu_math), false).texture(),
|
||||||
Tool::Math,
|
Tool::Math,
|
||||||
);
|
);
|
||||||
tool_button(
|
tool_button(
|
||||||
(1, 6),
|
(1, 5),
|
||||||
&Tile::Powerable(PTile::Gate(self.tool_menu_gate), false).texture(),
|
&Tile::Powerable(PTile::Gate(self.tool_menu_gate), false).texture(),
|
||||||
Tool::Gate,
|
Tool::Gate,
|
||||||
);
|
);
|
||||||
|
@ -339,6 +343,7 @@ impl Game {
|
||||||
}
|
}
|
||||||
Tool::Math => format!("{}_off", self.tool_menu_math.texture_name()),
|
Tool::Math => format!("{}_off", self.tool_menu_math.texture_name()),
|
||||||
Tool::Gate => format!("{}_off", self.tool_menu_gate.texture_name()),
|
Tool::Gate => format!("{}_off", self.tool_menu_gate.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::Number => todo!(),
|
||||||
|
@ -364,6 +369,10 @@ impl Game {
|
||||||
tile_pos.into(),
|
tile_pos.into(),
|
||||||
Tile::Powerable(PTile::Gate(self.tool_menu_gate), false),
|
Tile::Powerable(PTile::Gate(self.tool_menu_gate), false),
|
||||||
),
|
),
|
||||||
|
Tool::Wire => self.source_board.set(
|
||||||
|
tile_pos.into(),
|
||||||
|
Tile::Powerable(PTile::Wire(self.tool_menu_wire), false),
|
||||||
|
),
|
||||||
Tool::Arrow => self
|
Tool::Arrow => self
|
||||||
.source_board
|
.source_board
|
||||||
.set(tile_pos.into(), Tile::Arrow(self.tool_menu_arrow)),
|
.set(tile_pos.into(), Tile::Arrow(self.tool_menu_arrow)),
|
||||||
|
|
|
@ -315,57 +315,3 @@ impl Machine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(source: &str) -> Board {
|
|
||||||
let mut rows = Vec::new();
|
|
||||||
|
|
||||||
let mut width = 0;
|
|
||||||
for line in source.lines() {
|
|
||||||
width = width.max(line.len());
|
|
||||||
let mut tiles = Vec::new();
|
|
||||||
for char in line.chars() {
|
|
||||||
tiles.push(tile_from_char(char));
|
|
||||||
}
|
|
||||||
rows.push(tiles);
|
|
||||||
}
|
|
||||||
for line in &mut rows {
|
|
||||||
line.resize(width, Tile::Blank);
|
|
||||||
}
|
|
||||||
|
|
||||||
Board::new(rows)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const fn tile_from_char(c: char) -> Tile {
|
|
||||||
match c {
|
|
||||||
'o' => Tile::Marble {
|
|
||||||
value: 0,
|
|
||||||
dir: Direction::Down,
|
|
||||||
},
|
|
||||||
'*' => Tile::Powerable(PTile::Trigger, false),
|
|
||||||
'-' => Tile::Powerable(PTile::Wire(WireType::Horizontal), false),
|
|
||||||
'|' => Tile::Powerable(PTile::Wire(WireType::Vertical), false),
|
|
||||||
'+' => Tile::Powerable(PTile::Wire(WireType::Cross), false),
|
|
||||||
'/' => Tile::Mirror(MirrorType::Forward),
|
|
||||||
'\\' => Tile::Mirror(MirrorType::Back),
|
|
||||||
'^' => Tile::Arrow(Direction::Up),
|
|
||||||
'v' => Tile::Arrow(Direction::Down),
|
|
||||||
'<' => Tile::Arrow(Direction::Left),
|
|
||||||
'>' => Tile::Arrow(Direction::Right),
|
|
||||||
'=' => Tile::Powerable(PTile::Gate(GateType::Equal), false),
|
|
||||||
'!' => Tile::Powerable(PTile::Gate(GateType::NotEqual), false),
|
|
||||||
'L' => Tile::Powerable(PTile::Gate(GateType::LessThan), false),
|
|
||||||
'G' => Tile::Powerable(PTile::Gate(GateType::GreaterThan), false),
|
|
||||||
'P' => Tile::Powerable(PTile::Output, false),
|
|
||||||
'I' => Tile::Powerable(PTile::Input, false),
|
|
||||||
'F' => Tile::Powerable(PTile::Flipper, false),
|
|
||||||
'A' => Tile::Powerable(PTile::Math(MathOp::Add), false),
|
|
||||||
'S' => Tile::Powerable(PTile::Math(MathOp::Sub), false),
|
|
||||||
'M' => Tile::Powerable(PTile::Math(MathOp::Mul), false),
|
|
||||||
'D' => Tile::Powerable(PTile::Math(MathOp::Div), false),
|
|
||||||
'R' => Tile::Powerable(PTile::Math(MathOp::Rem), false),
|
|
||||||
'B' => Tile::Powerable(PTile::Bag, false),
|
|
||||||
d @ '0'..='9' => Tile::Digit(d as u8 - b'0'),
|
|
||||||
'#' => Tile::Block,
|
|
||||||
' ' => Tile::Blank,
|
|
||||||
_ => Tile::Blank,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -35,6 +35,25 @@ pub struct Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
|
pub fn parse(source: &str) -> Self {
|
||||||
|
let mut rows = Vec::new();
|
||||||
|
|
||||||
|
let mut width = 0;
|
||||||
|
for line in source.lines() {
|
||||||
|
width = width.max(line.len());
|
||||||
|
let mut tiles = Vec::new();
|
||||||
|
for char in line.chars() {
|
||||||
|
tiles.push(Tile::from_char(char));
|
||||||
|
}
|
||||||
|
rows.push(tiles);
|
||||||
|
}
|
||||||
|
for line in &mut rows {
|
||||||
|
line.resize(width, Tile::Blank);
|
||||||
|
}
|
||||||
|
|
||||||
|
Board::new(rows)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new_empty(width: usize, height: usize) -> Self {
|
pub fn new_empty(width: usize, height: usize) -> Self {
|
||||||
let rows = vec![vec![Tile::Blank; width]; height];
|
let rows = vec![vec![Tile::Blank; width]; height];
|
||||||
Self {
|
Self {
|
||||||
|
|
|
@ -68,6 +68,42 @@ pub enum Direction {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tile {
|
impl Tile {
|
||||||
|
pub const fn from_char(c: char) -> Tile {
|
||||||
|
match c {
|
||||||
|
'o' => Tile::Marble {
|
||||||
|
value: 0,
|
||||||
|
dir: Direction::Down,
|
||||||
|
},
|
||||||
|
'*' => Tile::Powerable(PTile::Trigger, false),
|
||||||
|
'-' => Tile::Powerable(PTile::Wire(WireType::Horizontal), false),
|
||||||
|
'|' => Tile::Powerable(PTile::Wire(WireType::Vertical), false),
|
||||||
|
'+' => Tile::Powerable(PTile::Wire(WireType::Cross), false),
|
||||||
|
'/' => Tile::Mirror(MirrorType::Forward),
|
||||||
|
'\\' => Tile::Mirror(MirrorType::Back),
|
||||||
|
'^' => Tile::Arrow(Direction::Up),
|
||||||
|
'v' => Tile::Arrow(Direction::Down),
|
||||||
|
'<' => Tile::Arrow(Direction::Left),
|
||||||
|
'>' => Tile::Arrow(Direction::Right),
|
||||||
|
'=' => Tile::Powerable(PTile::Gate(GateType::Equal), false),
|
||||||
|
'!' => Tile::Powerable(PTile::Gate(GateType::NotEqual), false),
|
||||||
|
'L' => Tile::Powerable(PTile::Gate(GateType::LessThan), false),
|
||||||
|
'G' => Tile::Powerable(PTile::Gate(GateType::GreaterThan), false),
|
||||||
|
'P' => Tile::Powerable(PTile::Output, false),
|
||||||
|
'I' => Tile::Powerable(PTile::Input, false),
|
||||||
|
'F' => Tile::Powerable(PTile::Flipper, false),
|
||||||
|
'A' => Tile::Powerable(PTile::Math(MathOp::Add), false),
|
||||||
|
'S' => Tile::Powerable(PTile::Math(MathOp::Sub), false),
|
||||||
|
'M' => Tile::Powerable(PTile::Math(MathOp::Mul), false),
|
||||||
|
'D' => Tile::Powerable(PTile::Math(MathOp::Div), false),
|
||||||
|
'R' => Tile::Powerable(PTile::Math(MathOp::Rem), false),
|
||||||
|
'B' => Tile::Powerable(PTile::Bag, false),
|
||||||
|
d @ '0'..='9' => Tile::Digit(d as u8 - b'0'),
|
||||||
|
'#' => Tile::Block,
|
||||||
|
' ' => Tile::Blank,
|
||||||
|
_ => Tile::Blank,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_blank(&self) -> bool {
|
pub fn is_blank(&self) -> bool {
|
||||||
matches!(self, Tile::Blank)
|
matches!(self, Tile::Blank)
|
||||||
}
|
}
|
||||||
|
@ -223,3 +259,4 @@ impl GateType {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue