clean up tile rendering

This commit is contained in:
Crispy 2024-10-05 15:18:41 +02:00
parent 86548a8b0d
commit 5a23dde43a
5 changed files with 14 additions and 35 deletions

BIN
assets/missing.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 B

BIN
assets/transparent.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 B

View file

@ -1,5 +1,3 @@
use std::collections::HashMap;
use raylib::prelude::*; use raylib::prelude::*;
pub mod board; pub mod board;

View file

@ -101,7 +101,14 @@ impl Board {
if let Some(tile) = self.get((x, y).into()) { if let Some(tile) = self.get((x, y).into()) {
let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2; 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; let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2;
tile.draw(d, textures, px, py, tile_size, zoom); let texture = textures.get(&tile.texture()).unwrap_or_else(||textures.get("missing").unwrap());
d.draw_texture_ex(
texture,
Vector2::new((px - tile_size / 2) as f32, (py - tile_size / 2) as f32),
0.0,
(1 << zoom) as f32,
Color::WHITE,
);
} }
} }
} }

View file

@ -1,7 +1,3 @@
use std::collections::HashMap;
use raylib::prelude::*;
use super::board::Pos; use super::board::Pos;
pub type MarbleValue = u32; pub type MarbleValue = u32;
@ -84,17 +80,9 @@ impl Tile {
} }
} }
pub fn draw( pub fn texture(&self) -> String {
&self, match self {
d: &mut RaylibDrawHandle, Tile::Blank => "transparent",
textures: &HashMap<String, Texture2D>,
x: i32,
y: i32,
size: i32,
zoom: i32,
) {
let tex_name = match self {
Tile::Blank => "",
Tile::Block => "block", Tile::Block => "block",
Tile::Marble { value: _, dir: _ } => "marble", Tile::Marble { value: _, dir: _ } => "marble",
Tile::Digit(n) => match n { Tile::Digit(n) => match n {
@ -121,7 +109,7 @@ impl Tile {
Direction::Right => "right", Direction::Right => "right",
}, },
Tile::Powerable(tile, state) => { Tile::Powerable(tile, state) => {
let t = match tile { let root = match tile {
PTile::Trigger => "trigger", PTile::Trigger => "trigger",
PTile::Wire(wire) => match wire { PTile::Wire(wire) => match wire {
WireType::Vertical => "wire_vertical", WireType::Vertical => "wire_vertical",
@ -146,24 +134,10 @@ impl Tile {
PTile::Input => "input", PTile::Input => "input",
PTile::Output => "output", PTile::Output => "output",
}; };
&format!("{t}_{}", if *state { "on" } else { "off" }) return format!("{root}_{}", if *state { "on" } else { "off" });
} }
};
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),
0.0,
(1 << zoom) as f32,
Color::WHITE,
);
return;
}
match self {
Tile::Blank => (),
_ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::RED),
} }
.to_owned()
} }
} }