init
This commit is contained in:
commit
bf46a3e7c3
8 changed files with 1352 additions and 0 deletions
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