2024-10-05 17:53:23 +02:00
|
|
|
use crate::Textures;
|
2024-10-04 22:10:00 +02:00
|
|
|
|
2024-10-03 22:59:49 +02:00
|
|
|
use super::tile::*;
|
2024-10-04 22:35:15 +02:00
|
|
|
use raylib::prelude::*;
|
2024-10-03 22:59:49 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone, Copy, PartialEq)]
|
|
|
|
pub struct Pos {
|
|
|
|
pub x: isize,
|
|
|
|
pub y: isize,
|
|
|
|
}
|
|
|
|
|
2024-10-05 20:22:18 +02:00
|
|
|
impl Pos {
|
|
|
|
pub const fn to_vec(&self) -> Vector2 {
|
|
|
|
Vector2 {
|
|
|
|
x: self.x as f32,
|
|
|
|
y: self.y as f32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 22:59:49 +02:00
|
|
|
impl From<(usize, usize)> for Pos {
|
|
|
|
fn from(value: (usize, usize)) -> Self {
|
|
|
|
Self {
|
|
|
|
x: value.0 as isize,
|
|
|
|
y: value.1 as isize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 15:34:58 +02:00
|
|
|
impl From<Vector2> for Pos {
|
|
|
|
fn from(vec: Vector2) -> Self {
|
|
|
|
Self {
|
|
|
|
x: vec.x as isize,
|
|
|
|
y: vec.y as isize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-04 22:10:00 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-10-03 22:59:49 +02:00
|
|
|
pub struct Board {
|
|
|
|
rows: Vec<Vec<Tile>>,
|
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Board {
|
2024-10-05 19:45:25 +02:00
|
|
|
pub fn parse(source: &str) -> Self {
|
|
|
|
let mut rows = Vec::new();
|
|
|
|
|
|
|
|
let mut width = 0;
|
|
|
|
for line in source.lines() {
|
|
|
|
width = width.max(line.len());
|
|
|
|
let mut tiles = Vec::new();
|
|
|
|
for char in line.chars() {
|
|
|
|
tiles.push(Tile::from_char(char));
|
|
|
|
}
|
|
|
|
rows.push(tiles);
|
|
|
|
}
|
|
|
|
for line in &mut rows {
|
|
|
|
line.resize(width, Tile::Blank);
|
|
|
|
}
|
|
|
|
|
|
|
|
Board::new(rows)
|
|
|
|
}
|
|
|
|
|
2024-10-03 22:59:49 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 20:22:18 +02:00
|
|
|
pub fn get_mut(&mut self, p: Pos) -> Option<&mut Tile> {
|
|
|
|
if self.in_bounds(p) {
|
|
|
|
Some(&mut self.rows[p.y as usize][p.x as usize])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut_unchecked(&mut self, p: Pos) -> &mut Tile {
|
2024-10-03 22:59:49 +02:00
|
|
|
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
|
|
|
|
}
|
2024-10-04 22:10:00 +02:00
|
|
|
|
2024-10-05 17:53:23 +02:00
|
|
|
pub fn draw(&self, d: &mut RaylibDrawHandle, textures: &Textures, offset: Vector2, zoom: i32) {
|
2024-10-04 22:10:00 +02:00
|
|
|
let tile_size = 16 << zoom;
|
|
|
|
for x in 0..self.width {
|
|
|
|
for y in 0..self.height {
|
|
|
|
if let Some(tile) = self.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;
|
2024-10-05 17:53:23 +02:00
|
|
|
let texture = textures.get(&tile.texture());
|
2024-10-05 15:18:41 +02:00
|
|
|
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,
|
|
|
|
);
|
2024-10-04 22:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|