feat: add map struct, overhaul stored map representation

This commit is contained in:
2023-09-11 03:01:50 -05:00
parent bf8370ef35
commit 2c6045aa1b
4 changed files with 111 additions and 70 deletions

View File

@@ -1,18 +1,23 @@
use std::rc::Rc;
use sdl2::image::LoadTexture;
use sdl2::keyboard::Keycode;
use sdl2::render::{Texture, TextureCreator};
use sdl2::video::WindowContext;
use sdl2::{pixels::Color, render::Canvas, video::Window};
use tracing::event;
use crate::constants::{MapTile, BOARD, BOARD_HEIGHT, BOARD_WIDTH};
use crate::constants::{MapTile, BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD};
use crate::direction::Direction;
use crate::entity::Entity;
use crate::map::Map;
use crate::pacman::Pacman;
pub struct Game<'a> {
canvas: &'a mut Canvas<Window>,
map_texture: Texture<'a>,
pacman: Pacman<'a>,
map: Rc<Map>,
debug: bool,
}
@@ -21,15 +26,17 @@ impl Game<'_> {
canvas: &'a mut Canvas<Window>,
texture_creator: &'a TextureCreator<WindowContext>,
) -> Game<'a> {
let map = Rc::new(Map::new(RAW_BOARD));
let pacman_atlas = texture_creator
.load_texture("assets/32/pacman.png")
.expect("Could not load pacman texture");
let pacman = Pacman::new(Some(Game::cell_to_pixel((1, 4))), pacman_atlas);
let pacman = Pacman::new((1, 1), pacman_atlas, Rc::clone(&map));
Game {
canvas,
pacman: pacman,
debug: false,
map: map,
map_texture: texture_creator
.load_texture("assets/map.png")
.expect("Could not load pacman texture"),
@@ -81,7 +88,7 @@ impl Game<'_> {
if self.debug {
for x in 0..BOARD_WIDTH {
for y in 0..BOARD_HEIGHT {
let tile = BOARD[x as usize][y as usize];
let tile = self.map.get_tile((x as i32, y as i32)).unwrap_or(MapTile::Empty);
let mut color = None;
if (x, y) == self.pacman.cell_position() {
@@ -111,11 +118,12 @@ impl Game<'_> {
}
fn draw_cell(&mut self, cell: (u32, u32), color: Color) {
let position = Map::cell_to_pixel(cell);
self.canvas.set_draw_color(color);
self.canvas
.draw_rect(sdl2::rect::Rect::new(
cell.0 as i32 * 24,
cell.1 as i32 * 24,
position.0 as i32,
position.1 as i32,
24,
24,
))