make all powerable tiles also store a powered state that can be rendered

This commit is contained in:
Crispy 2024-10-03 23:34:16 +02:00
parent bf46a3e7c3
commit 12bd19a7f3
3 changed files with 152 additions and 144 deletions

View file

@ -6,10 +6,10 @@ use raylib::prelude::*;
mod marble_engine; mod marble_engine;
fn main() { fn main() {
let (mut rl, thread) = raylib::init().resizable().title("Hello, World").build(); let (mut rl, thread) = raylib::init().resizable().title("good window title").build();
rl.set_target_fps(60); rl.set_target_fps(60);
let board = parse(&read_to_string("boards/flow.mbl").unwrap()); let board = parse(&read_to_string("boards/adder.mbl").unwrap());
let mut pos_offset = Vector2::zero(); let mut pos_offset = Vector2::zero();
let mut machine = marble_engine::Machine::new(board, "Vec::new()".bytes().collect()); let mut machine = marble_engine::Machine::new(board, "Vec::new()".bytes().collect());
@ -28,7 +28,6 @@ fn main() {
d.clear_background(Color::new(64, 64, 64, 255)); d.clear_background(Color::new(64, 64, 64, 255));
machine.draw(&mut d, pos_offset); machine.draw(&mut d, pos_offset);
d.draw_text("Hello, world!", 12, 12, 20, Color::WHITE);
d.draw_fps(2, 2); d.draw_fps(2, 2);
} }

View file

@ -67,7 +67,7 @@ impl Machine {
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() {
match self.board.get_mut((x, y).into()) { match self.board.get_mut((x, y).into()) {
Tile::Wire(_, state) => *state = false, Tile::Powerable(_, state) => *state = false,
Tile::Trigger(state) => *state = false, Tile::Trigger(state) => *state = false,
_ => (), _ => (),
} }
@ -103,7 +103,7 @@ impl Machine {
self.marbles[i] = next_pos; self.marbles[i] = next_pos;
new_tile = Some(Tile::Blank); new_tile = Some(Tile::Blank);
} }
Tile::Bag => { Tile::Powerable(PTile::Bag, _) => {
to_remove.push(i); to_remove.push(i);
new_tile = Some(Tile::Blank); new_tile = Some(Tile::Blank);
} }
@ -190,30 +190,31 @@ impl Machine {
} }
let tile = self.board.get_mut(pos); let tile = self.board.get_mut(pos);
let front_pos = dir.step(pos); let front_pos = dir.step(pos);
match tile { if let Tile::Powerable(tile, state) = tile {
Tile::Wire(wiretype, state) => {
if *state { if *state {
return; return;
} }
*state = true; *state = true;
match tile {
PTile::Wire(wiretype) => {
let dirs = wiretype.directions(); let dirs = wiretype.directions();
for d in dirs { for d in dirs {
self.propagate_power(*d, d.step(pos)); self.propagate_power(*d, d.step(pos));
} }
} }
Tile::Print => { PTile::Output => {
let sample = self.board.get_or_blank(front_pos); let sample = self.board.get_or_blank(front_pos);
if let Tile::Marble { value, dir: _ } = sample { if let Tile::Marble { value, dir: _ } = sample {
self.output.push(value as u8); self.output.push(value as u8);
} }
} }
Tile::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(front_pos) = Tile::Marble { value: 0, dir };
self.marbles.push(front_pos); self.marbles.push(front_pos);
} }
} }
Tile::Input => { PTile::Input => {
if self.input_index < self.input.len() if self.input_index < self.input.len()
&& self.board.get_or_blank(front_pos).is_blank() && self.board.get_or_blank(front_pos).is_blank()
{ {
@ -223,7 +224,7 @@ impl Machine {
self.input_index += 1; self.input_index += 1;
} }
} }
Tile::Math(op) => { PTile::Math(op) => {
let op = *op; let op = *op;
let pos_a = dir.left().step(pos); let pos_a = dir.left().step(pos);
let pos_b = dir.right().step(pos); let pos_b = dir.right().step(pos);
@ -245,10 +246,10 @@ impl Machine {
self.marbles.push(front_pos); self.marbles.push(front_pos);
} }
} }
Tile::Flip => { PTile::Flipper => {
let m = self.board.get_mut(front_pos); let m = self.board.get_mut(front_pos);
match m { match m {
Tile::Wire(wire_type, _) => { Tile::Powerable(PTile::Wire(wire_type), _) => {
*wire_type = match *wire_type { *wire_type = match *wire_type {
WireType::Vertical => WireType::Horizontal, WireType::Vertical => WireType::Horizontal,
WireType::Horizontal => WireType::Vertical, WireType::Horizontal => WireType::Vertical,
@ -267,7 +268,7 @@ impl Machine {
_ => (), _ => (),
}; };
} }
Tile::Gate(gate) => { PTile::Gate(gate) => {
let gate = *gate; let gate = *gate;
let pos_a = dir.left().step(pos); let pos_a = dir.left().step(pos);
let pos_b = dir.right().step(pos); let pos_b = dir.right().step(pos);
@ -284,13 +285,7 @@ impl Machine {
self.propagate_power(dir, dir.step(pos)); self.propagate_power(dir, dir.step(pos));
} }
} }
Tile::Marble { value: _, dir: _ } }
| Tile::Trigger(_)
| Tile::Digit(_)
| Tile::Mirror(_)
| Tile::Arrow(_)
| Tile::Comment(_)
| Tile::Blank => (),
} }
} }
} }
@ -322,30 +317,30 @@ pub fn parse(source: &str) -> Board {
dir: Direction::Down, dir: Direction::Down,
}, },
'*' => Tile::Trigger(false), '*' => Tile::Trigger(false),
'-' => Tile::Wire(WireType::Horizontal, false), '-' => Tile::Powerable(PTile::Wire(WireType::Horizontal), false),
'|' => Tile::Wire(WireType::Vertical, false), '|' => Tile::Powerable(PTile::Wire(WireType::Vertical), false),
'+' => Tile::Wire(WireType::Cross, false), '+' => Tile::Powerable(PTile::Wire(WireType::Cross), false),
'/' => Tile::Mirror(MirrorType::Forward), '/' => Tile::Mirror(MirrorType::Forward),
'\\' => Tile::Mirror(MirrorType::Back), '\\' => Tile::Mirror(MirrorType::Back),
'^' => Tile::Arrow(Direction::Up), '^' => Tile::Arrow(Direction::Up),
'v' => Tile::Arrow(Direction::Down), 'v' => Tile::Arrow(Direction::Down),
'<' => Tile::Arrow(Direction::Left), '<' => Tile::Arrow(Direction::Left),
'>' => Tile::Arrow(Direction::Right), '>' => Tile::Arrow(Direction::Right),
'=' => Tile::Gate(GateType::Equal), '=' => Tile::Powerable(PTile::Gate(GateType::Equal), false),
'!' => Tile::Gate(GateType::NotEqual), '!' => Tile::Powerable(PTile::Gate(GateType::NotEqual), false),
'L' => Tile::Gate(GateType::LessThan), 'L' => Tile::Powerable(PTile::Gate(GateType::LessThan), false),
'G' => Tile::Gate(GateType::GreaterThan), 'G' => Tile::Powerable(PTile::Gate(GateType::GreaterThan), false),
'P' => Tile::Print, 'P' => Tile::Powerable(PTile::Output, false),
'I' => Tile::Input, 'I' => Tile::Powerable(PTile::Input, false),
'F' => Tile::Flip, 'F' => Tile::Powerable(PTile::Flipper, false),
'A' => Tile::Math(MathOp::Add), 'A' => Tile::Powerable(PTile::Math(MathOp::Add), false),
'S' => Tile::Math(MathOp::Sub), 'S' => Tile::Powerable(PTile::Math(MathOp::Sub), false),
'M' => Tile::Math(MathOp::Mul), 'M' => Tile::Powerable(PTile::Math(MathOp::Mul), false),
'D' => Tile::Math(MathOp::Div), 'D' => Tile::Powerable(PTile::Math(MathOp::Div), false),
'R' => Tile::Math(MathOp::Rem), 'R' => Tile::Powerable(PTile::Math(MathOp::Rem), false),
'B' => Tile::Bag, 'B' => Tile::Powerable(PTile::Bag, false),
d @ '0'..='9' => Tile::Digit(d as u8), d @ '0'..='9' => Tile::Digit(d as u8),
'#' => Tile::Comment(b'#'), '#' => Tile::Block,
' ' => Tile::Blank, ' ' => Tile::Blank,
'(' => { '(' => {
in_comment = true; in_comment = true;

View file

@ -13,22 +13,28 @@ pub type MarbleValue = u32;
pub enum Tile { pub enum Tile {
#[default] #[default]
Blank, Blank,
Block,
Comment(u8), Comment(u8),
Bag,
Marble { Marble {
value: MarbleValue, value: MarbleValue,
dir: Direction, dir: Direction,
}, },
Trigger(bool), Trigger(bool),
Digit(u8), Digit(u8),
Wire(WireType, bool),
Gate(GateType),
Print,
Input,
Flip,
Math(MathOp),
Mirror(MirrorType), Mirror(MirrorType),
Arrow(Direction), Arrow(Direction),
Powerable(PTile, bool),
}
#[derive(Debug, Clone, Copy)]
pub enum PTile {
Wire(WireType),
Gate(GateType),
Math(MathOp),
Bag,
Flipper,
Input,
Output,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -89,16 +95,17 @@ impl Tile {
let right = x + size / 2 - 1; let right = x + size / 2 - 1;
match self { match self {
Tile::Blank => (), Tile::Blank => (),
Tile::Block => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::DIMGRAY),
Tile::Comment(c) => { Tile::Comment(c) => {
d.draw_text(&format!("{}", *c as char), x - 10, y - 10, 20, Color::RED) d.draw_rectangle(x-size/2, y-size/2, size, size, Color::DIMGRAY);
d.draw_text(&format!("{}", *c as char), x - 10, y - 10, 20, Color::WHITE);
} }
Tile::Bag => d.draw_circle_lines(x, y, size as f32 * 0.4, Color::CYAN),
Tile::Marble { value, dir } => { Tile::Marble { value, dir } => {
d.draw_circle(x, y, size as f32 * 0.35, Color::new(15, 15, 15, 255)); d.draw_circle(x, y, size as f32 * 0.35, Color::new(15, 15, 15, 255));
d.draw_text( d.draw_text(
&format!("{value}"), &format!("{value}"),
x - size / 2, x - size / 2 + 2,
y - size / 2, y - size / 2 + 2,
20, 20,
Color::MAGENTA, Color::MAGENTA,
); );
@ -110,24 +117,6 @@ impl Tile {
Tile::Digit(n) => { Tile::Digit(n) => {
d.draw_text(&String::from(*n as char), x - 10, y - 10, 20, Color::ORANGE) d.draw_text(&String::from(*n as char), x - 10, y - 10, 20, Color::ORANGE)
} }
Tile::Wire(wire, state) => {
let color = if *state { Color::RED } else { Color::WHITE };
let vertical = !matches!(wire, WireType::Horizontal);
let horizontal = !matches!(wire, WireType::Vertical);
if vertical {
// d.draw_line(x, up, x, down, color);
d.draw_rectangle(x - size / 8, y - size / 2, size / 4, size, color)
}
if horizontal {
// d.draw_line(left, y, right, y, color);
d.draw_rectangle(x - size / 2, y - size / 8, size, size / 4, color)
}
}
// Tile::Gate(_) => todo!(),
// Tile::Print => todo!(),
// Tile::Input => todo!(),
// Tile::Flip => todo!(),
// Tile::Math(_) => todo!(),
Tile::Mirror(mirror) => { Tile::Mirror(mirror) => {
let height = size as f32 * 1.25; let height = size as f32 * 1.25;
let width = (size / 4) as f32; let width = (size / 4) as f32;
@ -156,7 +145,32 @@ impl Tile {
}; };
d.draw_triangle(v1, v2, v3, Color::CYAN); d.draw_triangle(v1, v2, v3, Color::CYAN);
} }
_ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::YELLOW), Tile::Powerable(tile, state) => {
let color = if *state { Color::RED } else { Color::LIGHTGRAY };
match tile {
PTile::Bag => {
d.draw_circle(x, y, size as f32 * 0.4, color);
d.draw_circle(x, y, size as f32 * 0.2, Color::GRAY);
}
PTile::Wire(wire) => {
let vertical = !matches!(wire, WireType::Horizontal);
let horizontal = !matches!(wire, WireType::Vertical);
if vertical {
d.draw_rectangle(x - size / 8, y - size / 2, size / 4, size, color)
}
if horizontal {
d.draw_rectangle(x - size / 2, y - size / 8, size, size / 4, color)
}
}
// PTile::Gate(_) => todo!(),
// PTile::Math(_) => todo!(),
// PTile::Print => todo!(),
// PTile::Input => todo!(),
// PTile::Flip => todo!(),
_ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, color),
}
}
} }
} }
} }