add sprites for most tiles

This commit is contained in:
Crispy 2024-10-04 01:21:52 +02:00
parent 12bd19a7f3
commit d4f9be5bff
41 changed files with 108 additions and 63 deletions

View file

@ -1,8 +1,11 @@
use std::collections::HashMap;
use raylib::{
color::Color,
drawing::{RaylibDraw, RaylibDrawHandle},
ffi::Rectangle,
math::Vector2,
texture::Texture2D,
};
use super::board::Pos;
@ -19,7 +22,6 @@ pub enum Tile {
value: MarbleValue,
dir: Direction,
},
Trigger(bool),
Digit(u8),
Mirror(MirrorType),
Arrow(Direction),
@ -28,6 +30,7 @@ pub enum Tile {
#[derive(Debug, Clone, Copy)]
pub enum PTile {
Trigger,
Wire(WireType),
Gate(GateType),
Math(MathOp),
@ -88,16 +91,73 @@ impl Tile {
}
}
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;
pub fn draw(
&self,
d: &mut RaylibDrawHandle,
textures: &HashMap<String, Texture2D>,
x: i32,
y: i32,
size: i32,
) {
let tex_name = match self {
Tile::Blank => "",
Tile::Block => "",
Tile::Comment(_) => "",
Tile::Marble { value, dir } => "todo!()",
Tile::Digit(_) => "",
Tile::Mirror(_) => "",
Tile::Arrow(dir) => match dir {
Direction::Up => "up",
Direction::Down => "down",
Direction::Left => "left",
Direction::Right => "right",
},
Tile::Powerable(tile, state) => {
let t = match tile {
PTile::Trigger => "trigger",
PTile::Wire(wire) => match wire {
WireType::Vertical => "wire_vertical",
WireType::Horizontal => "wire_horizontal",
WireType::Cross => "wire_cross",
},
PTile::Gate(gate) => match gate {
GateType::LessThan => "lt",
GateType::GreaterThan => "gt",
GateType::Equal => "eq",
GateType::NotEqual => "neq",
},
PTile::Math(math_op) => match math_op {
MathOp::Add => "add",
MathOp::Sub => "sub",
MathOp::Mul => "mul",
MathOp::Div => "div",
MathOp::Rem => "rem",
},
PTile::Bag => "bag",
PTile::Flipper => "flipper",
PTile::Input => "input",
PTile::Output => "output",
};
&format!("{t}_{}", if *state { "on" } else { "off" })
}
};
let tex_name = format!("{tex_name}.png");
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,
2.0,
Color::WHITE,
);
return;
}
match self {
Tile::Blank => (),
Tile::Block => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::DIMGRAY),
Tile::Comment(c) => {
d.draw_rectangle(x-size/2, y-size/2, size, size, Color::DIMGRAY);
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::Marble { value, dir } => {
@ -110,10 +170,6 @@ impl Tile {
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)
}
@ -132,45 +188,7 @@ impl Tile {
};
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);
}
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),
}
}
_ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::YELLOW),
}
}
}