init
This commit is contained in:
commit
bf46a3e7c3
8 changed files with 1352 additions and 0 deletions
35
src/main.rs
Normal file
35
src/main.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use std::fs::read_to_string;
|
||||
|
||||
use marble_engine::parse;
|
||||
use raylib::prelude::*;
|
||||
|
||||
mod marble_engine;
|
||||
|
||||
fn main() {
|
||||
let (mut rl, thread) = raylib::init().resizable().title("Hello, World").build();
|
||||
rl.set_target_fps(60);
|
||||
|
||||
let board = parse(&read_to_string("boards/flow.mbl").unwrap());
|
||||
let mut pos_offset = Vector2::zero();
|
||||
let mut machine = marble_engine::Machine::new(board, "Vec::new()".bytes().collect());
|
||||
|
||||
while !rl.window_should_close() {
|
||||
if rl.is_key_pressed(KeyboardKey::KEY_SPACE) {
|
||||
machine.step();
|
||||
}
|
||||
if rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_MIDDLE) {
|
||||
pos_offset += rl.get_mouse_delta()
|
||||
}
|
||||
if rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_RIGHT) {
|
||||
pos_offset = Vector2::zero();
|
||||
}
|
||||
|
||||
let mut d = rl.begin_drawing(&thread);
|
||||
d.clear_background(Color::new(64, 64, 64, 255));
|
||||
|
||||
machine.draw(&mut d, pos_offset);
|
||||
d.draw_text("Hello, world!", 12, 12, 20, Color::WHITE);
|
||||
|
||||
d.draw_fps(2, 2);
|
||||
}
|
||||
}
|
364
src/marble_engine.rs
Normal file
364
src/marble_engine.rs
Normal file
|
@ -0,0 +1,364 @@
|
|||
use raylib::{drawing::RaylibDrawHandle, math::Vector2};
|
||||
|
||||
mod board;
|
||||
mod tile;
|
||||
use board::{Board, Pos};
|
||||
use tile::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Machine {
|
||||
board: Board,
|
||||
marbles: Vec<Pos>,
|
||||
|
||||
input: Vec<u8>,
|
||||
input_index: usize,
|
||||
output: Vec<u8>,
|
||||
steps: usize,
|
||||
}
|
||||
|
||||
impl Machine {
|
||||
pub fn new_empty(width: usize) -> Self {
|
||||
Self {
|
||||
board: Board::new_empty(width, width),
|
||||
marbles: Vec::new(),
|
||||
input: Vec::new(),
|
||||
input_index: 0,
|
||||
output: Vec::new(),
|
||||
steps: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(grid: Board, input: Vec<u8>) -> Self {
|
||||
// let (grid, marbles) = parse(source);
|
||||
let mut marbles = Vec::new();
|
||||
for y in 0..grid.height() {
|
||||
for x in 0..grid.width() {
|
||||
if let Some(Tile::Marble { value: _, dir: _ }) = grid.get((x, y).into()) {
|
||||
marbles.push((x, y).into());
|
||||
}
|
||||
}
|
||||
}
|
||||
Self {
|
||||
board: grid,
|
||||
marbles,
|
||||
input,
|
||||
input_index: 0,
|
||||
output: Vec::new(),
|
||||
steps: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&self, d: &mut RaylibDrawHandle, offset: Vector2) {
|
||||
let tile_size = 32;
|
||||
for x in 0..self.board.width() {
|
||||
for y in 0..self.board.height() {
|
||||
if let Some(tile) = self.board.get((x, y).into()) {
|
||||
let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2;
|
||||
let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2;
|
||||
tile.draw(d, px, py, tile_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn step(&mut self) {
|
||||
self.steps += 1;
|
||||
// reset wires
|
||||
for y in 0..self.board.height() {
|
||||
for x in 0..self.board.width() {
|
||||
match self.board.get_mut((x, y).into()) {
|
||||
Tile::Wire(_, state) => *state = false,
|
||||
Tile::Trigger(state) => *state = false,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut to_remove = Vec::new();
|
||||
let mut triggers = Vec::new();
|
||||
for i in 0..self.marbles.len() {
|
||||
let marble_pos = self.marbles[i];
|
||||
let tile = self.board.get_or_blank(marble_pos);
|
||||
|
||||
if let Tile::Marble { value, dir } = tile {
|
||||
let next_pos = dir.step(marble_pos);
|
||||
if !self.board.in_bounds(next_pos) {
|
||||
continue;
|
||||
}
|
||||
let mut new_tile = None;
|
||||
let target = self.board.get_mut(next_pos);
|
||||
match target {
|
||||
Tile::Blank => {
|
||||
*target = tile;
|
||||
self.marbles[i] = next_pos;
|
||||
new_tile = Some(Tile::Blank);
|
||||
}
|
||||
Tile::Digit(d) => {
|
||||
let new_val = value
|
||||
.wrapping_mul(10)
|
||||
.wrapping_add((*d - b'0') as MarbleValue);
|
||||
*target = Tile::Marble {
|
||||
value: new_val,
|
||||
dir,
|
||||
};
|
||||
self.marbles[i] = next_pos;
|
||||
new_tile = Some(Tile::Blank);
|
||||
}
|
||||
Tile::Bag => {
|
||||
to_remove.push(i);
|
||||
new_tile = Some(Tile::Blank);
|
||||
}
|
||||
Tile::Marble {
|
||||
value: _other_value,
|
||||
dir: other_dir,
|
||||
} => {
|
||||
// bounce off other marbles
|
||||
if *other_dir != dir {
|
||||
new_tile = Some(Tile::Marble {
|
||||
value,
|
||||
dir: dir.opposite(),
|
||||
});
|
||||
*other_dir = dir;
|
||||
}
|
||||
}
|
||||
Tile::Trigger(state) => {
|
||||
*state = true;
|
||||
triggers.push(next_pos);
|
||||
let far_pos = dir.step(next_pos);
|
||||
if let Some(Tile::Blank) = self.board.get(far_pos) {
|
||||
self.board.set(far_pos, Tile::Marble { value, dir });
|
||||
self.marbles[i] = far_pos;
|
||||
new_tile = Some(Tile::Blank);
|
||||
}
|
||||
}
|
||||
Tile::Arrow(arrow_dir) => {
|
||||
let far_pos = arrow_dir.step(next_pos);
|
||||
let arrow_dir = *arrow_dir;
|
||||
if let Some(Tile::Blank) = self.board.get(far_pos) {
|
||||
self.marbles[i] = far_pos;
|
||||
self.board.set(
|
||||
far_pos,
|
||||
Tile::Marble {
|
||||
value,
|
||||
dir: arrow_dir,
|
||||
},
|
||||
);
|
||||
new_tile = Some(Tile::Blank);
|
||||
} else if far_pos == marble_pos {
|
||||
// bounce on reverse arrow
|
||||
new_tile = Some(Tile::Marble {
|
||||
value,
|
||||
dir: dir.opposite(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Tile::Mirror(mirror) => {
|
||||
let new_dir = mirror.new_dir(dir);
|
||||
let far_pos = new_dir.step(next_pos);
|
||||
let far_target = self.board.get_mut(far_pos);
|
||||
if let Tile::Blank = far_target {
|
||||
*far_target = Tile::Marble {
|
||||
value,
|
||||
dir: new_dir,
|
||||
};
|
||||
self.marbles[i] = far_pos;
|
||||
new_tile = Some(Tile::Blank);
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if let Some(t) = new_tile {
|
||||
*self.board.get_mut(marble_pos) = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut offset = 0;
|
||||
for i in to_remove {
|
||||
self.marbles.remove(i - offset);
|
||||
offset += 1;
|
||||
}
|
||||
for pos in triggers {
|
||||
for dir in Direction::ALL {
|
||||
self.propagate_power(dir, dir.step(pos));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn propagate_power(&mut self, dir: Direction, pos: Pos) {
|
||||
if !self.board.in_bounds(pos) {
|
||||
return;
|
||||
}
|
||||
let tile = self.board.get_mut(pos);
|
||||
let front_pos = dir.step(pos);
|
||||
match tile {
|
||||
Tile::Wire(wiretype, state) => {
|
||||
if *state {
|
||||
return;
|
||||
}
|
||||
*state = true;
|
||||
let dirs = wiretype.directions();
|
||||
for d in dirs {
|
||||
self.propagate_power(*d, d.step(pos));
|
||||
}
|
||||
}
|
||||
Tile::Print => {
|
||||
let sample = self.board.get_or_blank(front_pos);
|
||||
if let Tile::Marble { value, dir: _ } = sample {
|
||||
self.output.push(value as u8);
|
||||
}
|
||||
}
|
||||
Tile::Bag => {
|
||||
if let Some(Tile::Blank) = self.board.get(front_pos) {
|
||||
*self.board.get_mut(front_pos) = Tile::Marble { value: 0, dir };
|
||||
self.marbles.push(front_pos);
|
||||
}
|
||||
}
|
||||
Tile::Input => {
|
||||
if self.input_index < self.input.len()
|
||||
&& self.board.get_or_blank(front_pos).is_blank()
|
||||
{
|
||||
let value = self.input[self.input_index] as MarbleValue;
|
||||
*self.board.get_mut(front_pos) = Tile::Marble { value, dir };
|
||||
self.marbles.push(front_pos);
|
||||
self.input_index += 1;
|
||||
}
|
||||
}
|
||||
Tile::Math(op) => {
|
||||
let op = *op;
|
||||
let pos_a = dir.left().step(pos);
|
||||
let pos_b = dir.right().step(pos);
|
||||
let val_a = self.board.get_or_blank(pos_a).read_value();
|
||||
let val_b = self.board.get_or_blank(pos_b).read_value();
|
||||
if (!self.board.get_or_blank(pos_a).is_blank()
|
||||
|| !self.board.get_or_blank(pos_b).is_blank())
|
||||
&& self.board.get_or_blank(front_pos).is_blank()
|
||||
{
|
||||
let result = match op {
|
||||
MathOp::Add => val_a.wrapping_add(val_b),
|
||||
MathOp::Sub => val_a.wrapping_sub(val_b),
|
||||
MathOp::Mul => val_a.wrapping_mul(val_b),
|
||||
MathOp::Div => val_a.checked_div(val_b).unwrap_or_default(),
|
||||
MathOp::Rem => val_a.checked_rem(val_b).unwrap_or_default(),
|
||||
};
|
||||
// println!("{op:?} a:{val_a} b:{val_b}");
|
||||
*self.board.get_mut(front_pos) = Tile::Marble { value: result, dir };
|
||||
self.marbles.push(front_pos);
|
||||
}
|
||||
}
|
||||
Tile::Flip => {
|
||||
let m = self.board.get_mut(front_pos);
|
||||
match m {
|
||||
Tile::Wire(wire_type, _) => {
|
||||
*wire_type = match *wire_type {
|
||||
WireType::Vertical => WireType::Horizontal,
|
||||
WireType::Horizontal => WireType::Vertical,
|
||||
WireType::Cross => WireType::Cross,
|
||||
};
|
||||
}
|
||||
Tile::Mirror(mirror) => {
|
||||
*mirror = match *mirror {
|
||||
MirrorType::Forward => MirrorType::Back,
|
||||
MirrorType::Back => MirrorType::Forward,
|
||||
};
|
||||
}
|
||||
Tile::Arrow(dir) => {
|
||||
*dir = dir.opposite();
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
Tile::Gate(gate) => {
|
||||
let gate = *gate;
|
||||
let pos_a = dir.left().step(pos);
|
||||
let pos_b = dir.right().step(pos);
|
||||
let val_a = self.board.get_or_blank(pos_a).read_value();
|
||||
let val_b = self.board.get_or_blank(pos_b).read_value();
|
||||
|
||||
let result = match gate {
|
||||
GateType::LessThan => val_a < val_b,
|
||||
GateType::GreaterThan => val_a > val_b,
|
||||
GateType::Equal => val_a == val_b,
|
||||
GateType::NotEqual => val_a != val_b,
|
||||
};
|
||||
if result {
|
||||
self.propagate_power(dir, dir.step(pos));
|
||||
}
|
||||
}
|
||||
Tile::Marble { value: _, dir: _ }
|
||||
| Tile::Trigger(_)
|
||||
| Tile::Digit(_)
|
||||
| Tile::Mirror(_)
|
||||
| Tile::Arrow(_)
|
||||
| Tile::Comment(_)
|
||||
| Tile::Blank => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
let mut in_comment = false;
|
||||
for char in line.chars() {
|
||||
if in_comment {
|
||||
if char == ')' {
|
||||
in_comment = false;
|
||||
}
|
||||
if char == ' ' {
|
||||
// allow marbles to pass through gaps in comments
|
||||
tiles.push(Tile::Blank);
|
||||
} else {
|
||||
tiles.push(Tile::Comment(char as u8));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
tiles.push(match char {
|
||||
'o' => Tile::Marble {
|
||||
value: 0,
|
||||
dir: Direction::Down,
|
||||
},
|
||||
'*' => Tile::Trigger(false),
|
||||
'-' => Tile::Wire(WireType::Horizontal, false),
|
||||
'|' => Tile::Wire(WireType::Vertical, false),
|
||||
'+' => Tile::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::Gate(GateType::Equal),
|
||||
'!' => Tile::Gate(GateType::NotEqual),
|
||||
'L' => Tile::Gate(GateType::LessThan),
|
||||
'G' => Tile::Gate(GateType::GreaterThan),
|
||||
'P' => Tile::Print,
|
||||
'I' => Tile::Input,
|
||||
'F' => Tile::Flip,
|
||||
'A' => Tile::Math(MathOp::Add),
|
||||
'S' => Tile::Math(MathOp::Sub),
|
||||
'M' => Tile::Math(MathOp::Mul),
|
||||
'D' => Tile::Math(MathOp::Div),
|
||||
'R' => Tile::Math(MathOp::Rem),
|
||||
'B' => Tile::Bag,
|
||||
d @ '0'..='9' => Tile::Digit(d as u8),
|
||||
'#' => Tile::Comment(b'#'),
|
||||
' ' => Tile::Blank,
|
||||
'(' => {
|
||||
in_comment = true;
|
||||
Tile::Comment(b'(')
|
||||
}
|
||||
_ => Tile::Blank,
|
||||
});
|
||||
}
|
||||
rows.push(tiles);
|
||||
}
|
||||
for line in &mut rows {
|
||||
line.resize(width, Tile::Blank);
|
||||
}
|
||||
|
||||
Board::new(rows)
|
||||
}
|
87
src/marble_engine/board.rs
Normal file
87
src/marble_engine/board.rs
Normal file
|
@ -0,0 +1,87 @@
|
|||
use super::tile::*;
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq)]
|
||||
pub struct Pos {
|
||||
pub x: isize,
|
||||
pub y: isize,
|
||||
}
|
||||
|
||||
impl From<(usize, usize)> for Pos {
|
||||
fn from(value: (usize, usize)) -> Self {
|
||||
Self {
|
||||
x: value.0 as isize,
|
||||
y: value.1 as isize,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Board {
|
||||
rows: Vec<Vec<Tile>>,
|
||||
width: usize,
|
||||
height: usize,
|
||||
}
|
||||
|
||||
impl Board {
|
||||
pub fn new_empty(width: usize, height: usize) -> Self {
|
||||
let rows = vec![vec![Tile::Blank; width]; height];
|
||||
Self {
|
||||
rows,
|
||||
width,
|
||||
height,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(rows: Vec<Vec<Tile>>) -> Self {
|
||||
Self {
|
||||
width: rows[0].len(),
|
||||
height: rows.len(),
|
||||
rows,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn in_bounds(&self, p: Pos) -> bool {
|
||||
p.x >= 0 && p.y >= 0 && p.x < self.width as isize && p.y < self.height as isize
|
||||
}
|
||||
|
||||
pub fn get(&self, p: Pos) -> Option<Tile> {
|
||||
if self.in_bounds(p) {
|
||||
Some(self.rows[p.y as usize][p.x as usize])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_or_blank(&self, p: Pos) -> Tile {
|
||||
if self.in_bounds(p) {
|
||||
self.rows[p.y as usize][p.x as usize]
|
||||
} else {
|
||||
Tile::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, p: Pos) -> &mut Tile {
|
||||
if self.in_bounds(p) {
|
||||
&mut self.rows[p.y as usize][p.x as usize]
|
||||
} else {
|
||||
panic!(
|
||||
"position {p:?} out of bounds, size is {}x{}",
|
||||
self.width, self.height
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&mut self, p: Pos, tile: Tile) {
|
||||
if self.in_bounds(p) {
|
||||
self.rows[p.y as usize][p.x as usize] = tile;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn width(&self) -> usize {
|
||||
self.width
|
||||
}
|
||||
|
||||
pub fn height(&self) -> usize {
|
||||
self.height
|
||||
}
|
||||
}
|
232
src/marble_engine/tile.rs
Normal file
232
src/marble_engine/tile.rs
Normal file
|
@ -0,0 +1,232 @@
|
|||
use raylib::{
|
||||
color::Color,
|
||||
drawing::{RaylibDraw, RaylibDrawHandle},
|
||||
ffi::Rectangle,
|
||||
math::Vector2,
|
||||
};
|
||||
|
||||
use super::board::Pos;
|
||||
|
||||
pub type MarbleValue = u32;
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub enum Tile {
|
||||
#[default]
|
||||
Blank,
|
||||
Comment(u8),
|
||||
Bag,
|
||||
Marble {
|
||||
value: MarbleValue,
|
||||
dir: Direction,
|
||||
},
|
||||
Trigger(bool),
|
||||
Digit(u8),
|
||||
Wire(WireType, bool),
|
||||
Gate(GateType),
|
||||
Print,
|
||||
Input,
|
||||
Flip,
|
||||
Math(MathOp),
|
||||
Mirror(MirrorType),
|
||||
Arrow(Direction),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum MirrorType {
|
||||
Forward,
|
||||
Back,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum MathOp {
|
||||
Add,
|
||||
Sub,
|
||||
Mul,
|
||||
Div,
|
||||
Rem,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum GateType {
|
||||
LessThan,
|
||||
GreaterThan,
|
||||
Equal,
|
||||
NotEqual,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum WireType {
|
||||
Vertical,
|
||||
Horizontal,
|
||||
Cross,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
impl Tile {
|
||||
pub fn is_blank(&self) -> bool {
|
||||
matches!(self, Tile::Blank)
|
||||
}
|
||||
|
||||
pub fn read_value(&self) -> MarbleValue {
|
||||
if let Tile::Marble { value, dir: _ } = self {
|
||||
*value
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&self, d: &mut RaylibDrawHandle, x: i32, y: i32, size: i32) {
|
||||
let up = y - size / 2 + 1;
|
||||
let down = y + size / 2 - 1;
|
||||
let left = x - size / 2 + 1;
|
||||
let right = x + size / 2 - 1;
|
||||
match self {
|
||||
Tile::Blank => (),
|
||||
Tile::Comment(c) => {
|
||||
d.draw_text(&format!("{}", *c as char), x - 10, y - 10, 20, Color::RED)
|
||||
}
|
||||
Tile::Bag => d.draw_circle_lines(x, y, size as f32 * 0.4, Color::CYAN),
|
||||
Tile::Marble { value, dir } => {
|
||||
d.draw_circle(x, y, size as f32 * 0.35, Color::new(15, 15, 15, 255));
|
||||
d.draw_text(
|
||||
&format!("{value}"),
|
||||
x - size / 2,
|
||||
y - size / 2,
|
||||
20,
|
||||
Color::MAGENTA,
|
||||
);
|
||||
}
|
||||
Tile::Trigger(state) => {
|
||||
let color = if *state { Color::RED } else { Color::LIGHTGRAY };
|
||||
d.draw_rectangle(x - size / 4, y - size / 4, size / 2, size / 2, color)
|
||||
}
|
||||
Tile::Digit(n) => {
|
||||
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) => {
|
||||
let height = size as f32 * 1.25;
|
||||
let width = (size / 4) as f32;
|
||||
let rec = Rectangle {
|
||||
x: x as f32,
|
||||
y: y as f32,
|
||||
width,
|
||||
height,
|
||||
};
|
||||
let rot = match mirror {
|
||||
MirrorType::Forward => 45.0,
|
||||
MirrorType::Back => -45.0,
|
||||
};
|
||||
d.draw_rectangle_pro(rec, Vector2::new(width, height) * 0.5, rot, Color::CYAN);
|
||||
}
|
||||
Tile::Arrow(dir) => {
|
||||
let up = Vector2::from((x as f32, up as f32));
|
||||
let down = Vector2::from((x as f32, down as f32));
|
||||
let left = Vector2::from((left as f32, y as f32));
|
||||
let right = Vector2::from((right as f32, y as f32));
|
||||
let (v1, v2, v3) = match dir {
|
||||
Direction::Up => (up, left, right),
|
||||
Direction::Down => (down, right, left),
|
||||
Direction::Left => (left, down, up),
|
||||
Direction::Right => (right, up, down),
|
||||
};
|
||||
d.draw_triangle(v1, v2, v3, Color::CYAN);
|
||||
}
|
||||
_ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::YELLOW),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
pub const ALL: [Direction; 4] = [
|
||||
Direction::Up,
|
||||
Direction::Down,
|
||||
Direction::Left,
|
||||
Direction::Right,
|
||||
];
|
||||
|
||||
pub fn opposite(&self) -> Direction {
|
||||
match self {
|
||||
Direction::Up => Direction::Down,
|
||||
Direction::Down => Direction::Up,
|
||||
Direction::Left => Direction::Right,
|
||||
Direction::Right => Direction::Left,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn right(&self) -> Direction {
|
||||
match self {
|
||||
Direction::Up => Direction::Right,
|
||||
Direction::Down => Direction::Left,
|
||||
Direction::Left => Direction::Up,
|
||||
Direction::Right => Direction::Down,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn left(&self) -> Direction {
|
||||
self.right().opposite()
|
||||
}
|
||||
|
||||
pub fn step(&self, mut pos: Pos) -> Pos {
|
||||
match self {
|
||||
Direction::Up => pos.y -= 1,
|
||||
Direction::Down => pos.y += 1,
|
||||
Direction::Left => pos.x -= 1,
|
||||
Direction::Right => pos.x += 1,
|
||||
}
|
||||
pos
|
||||
}
|
||||
}
|
||||
|
||||
impl WireType {
|
||||
pub fn directions(self) -> &'static [Direction] {
|
||||
match self {
|
||||
WireType::Vertical => &[Direction::Up, Direction::Down],
|
||||
WireType::Horizontal => &[Direction::Left, Direction::Right],
|
||||
WireType::Cross => &Direction::ALL,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MirrorType {
|
||||
pub fn new_dir(self, dir: Direction) -> Direction {
|
||||
match self {
|
||||
MirrorType::Forward => match dir {
|
||||
Direction::Up => Direction::Right,
|
||||
Direction::Down => Direction::Left,
|
||||
Direction::Left => Direction::Down,
|
||||
Direction::Right => Direction::Up,
|
||||
},
|
||||
MirrorType::Back => match dir {
|
||||
Direction::Up => Direction::Left,
|
||||
Direction::Down => Direction::Right,
|
||||
Direction::Left => Direction::Up,
|
||||
Direction::Right => Direction::Down,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue