add digit sprites, remove comment tiles

This commit is contained in:
Crispy 2024-10-05 15:01:06 +02:00
parent 636e9d04e2
commit 86548a8b0d
13 changed files with 65 additions and 75 deletions

BIN
assets/tiles/digit_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 B

BIN
assets/tiles/digit_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 B

BIN
assets/tiles/digit_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

BIN
assets/tiles/digit_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 B

BIN
assets/tiles/digit_4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 B

BIN
assets/tiles/digit_5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

BIN
assets/tiles/digit_6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 B

BIN
assets/tiles/digit_7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

BIN
assets/tiles/digit_8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 B

BIN
assets/tiles/digit_9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

View file

@ -4,7 +4,7 @@ use std::{
ops::Rem,
};
use marble_engine::{board::Board, parse, tile::Tile, Machine};
use marble_engine::{board::Board, parse, tile::Tile, tile_to_char, Machine};
use raylib::prelude::*;
mod marble_engine;
@ -26,7 +26,7 @@ struct Game {
time_since_step: f32,
}
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
enum Tool {
None,
SetTile(Tile),
@ -51,11 +51,11 @@ fn main() {
let mut textures: HashMap<String, Texture2D> = HashMap::new();
for d in read_dir("assets/tiles").unwrap().flatten() {
let name = d.file_name();
if d.path().is_file() {
let name = name.to_string_lossy();
let path = d.path();
if path.is_file() {
let name = path.file_stem().unwrap().to_string_lossy();
let texture = rl
.load_texture(&thread, &format!("assets/tiles/{name}"))
.load_texture(&thread, &format!("assets/tiles/{name}.png"))
.unwrap();
textures.insert(name.to_string(), texture);
}
@ -203,11 +203,12 @@ impl Game {
let mut input_text = String::from_utf8_lossy(self.machine.input()).to_string();
if text_input(
d,
Rectangle::new(350., footer_top + 60., 200., 25.),
Rectangle::new(5., footer_top + 60., 200., 25.),
&mut input_text,
&mut self.input_text_selected,
) {
self.machine.set_input(input_text.into_bytes());
}
}
}

View file

@ -327,57 +327,8 @@ pub fn parse(source: &str) -> Board {
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::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),
'#' => Tile::Block,
' ' => Tile::Blank,
'(' => {
in_comment = true;
Tile::Comment(b'(')
}
_ => Tile::Blank,
});
tiles.push(tile_to_char(char));
}
rows.push(tiles);
}
@ -387,3 +338,39 @@ pub fn parse(source: &str) -> Board {
Board::new(rows)
}
pub const fn tile_to_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),
'#' => Tile::Block,
' ' => Tile::Blank,
_ => Tile::Blank,
}
}

View file

@ -6,12 +6,11 @@ use super::board::Pos;
pub type MarbleValue = u32;
#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub enum Tile {
#[default]
Blank,
Block,
Comment(u8),
Marble {
value: MarbleValue,
dir: Direction,
@ -22,7 +21,7 @@ pub enum Tile {
Powerable(PTile, bool),
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PTile {
Trigger,
Wire(WireType),
@ -34,13 +33,13 @@ pub enum PTile {
Output,
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MirrorType {
Forward,
Back,
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MathOp {
Add,
Sub,
@ -49,7 +48,7 @@ pub enum MathOp {
Rem,
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GateType {
LessThan,
GreaterThan,
@ -57,7 +56,7 @@ pub enum GateType {
NotEqual,
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WireType {
Vertical,
Horizontal,
@ -97,9 +96,20 @@ impl Tile {
let tex_name = match self {
Tile::Blank => "",
Tile::Block => "block",
Tile::Comment(_) => "",
Tile::Marble { value: _, dir: _ } => "marble",
Tile::Digit(_) => "",
Tile::Digit(n) => match n {
b'0' => "digit_0",
b'1' => "digit_1",
b'2' => "digit_2",
b'3' => "digit_3",
b'4' => "digit_4",
b'5' => "digit_5",
b'6' => "digit_6",
b'7' => "digit_7",
b'8' => "digit_8",
b'9' => "digit_9",
_ => unreachable!("invalid digit"),
},
Tile::Mirror(mirror) => match mirror {
MirrorType::Forward => "mirror_forward",
MirrorType::Back => "mirror_back",
@ -139,8 +149,7 @@ impl Tile {
&format!("{t}_{}", if *state { "on" } else { "off" })
}
};
let tex_name = format!("{tex_name}.png");
if let Some(texture) = textures.get(&tex_name) {
if let Some(texture) = textures.get(tex_name) {
d.draw_texture_ex(
texture,
Vector2::new((x - size / 2) as f32, (y - size / 2) as f32),
@ -153,13 +162,6 @@ impl Tile {
match self {
Tile::Blank => (),
Tile::Comment(c) => {
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::Digit(n) => {
d.draw_text(&String::from(*n as char), x - 10, y - 10, 20, Color::ORANGE)
}
_ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::RED),
}
}