use crate::{BACKGROUND_MAX_SIZE, EMPTY_TILE}; use glam::IVec2; /// Tilemap which will be rendered on screen pub struct Background { /// Tiles in idx, accessible via \[x\]\[y\], x and y 0..[BACKGROUND_MAX_SIZE] pub tiles: Vec>, /// Camera scroll (negative draw offset) for rendering pub scroll: IVec2, /// Rotation, 0-255 equals one full turn pub rot: u8, /// Origin (used for rotation and scroll and such) pub origin: IVec2, /// Size (used for rendering) pub size: IVec2, /// Are tiles indices half-tile indices? pub half_tile: bool, /// Is this background being displayed? pub active: bool, } impl Default for Background { fn default() -> Self { let row = vec![0u16; BACKGROUND_MAX_SIZE]; let tiles = vec![row; BACKGROUND_MAX_SIZE]; Self { tiles, scroll: IVec2::ZERO, half_tile: false, active: true, size: IVec2 { x: 19, y: 11 }, origin: IVec2::ZERO, rot: 0, } } } impl Background { /// Clears all tiles of given bg pub fn clear(&mut self) { self.fill(EMPTY_TILE); } /// Sets all tiles to val pub fn fill(&mut self, val: u16) { for x in 0..BACKGROUND_MAX_SIZE { for y in 0..BACKGROUND_MAX_SIZE { self.tiles[x][y] = val; } } } /// Shifts all tiles to the left, wrapping pub fn shift_left(&mut self) { for y in 0..self.size.y as usize { let tmp = self.tiles[0][y]; for x in 0..self.size.x as usize - 1 { self.tiles[x][y] = self.tiles[x+1][y]; } self.tiles[self.size.x as usize - 1][y] = tmp; } } /// Shifts all tiles to the right, wrapping pub fn shift_right(&mut self) { for y in 0..self.size.y as usize { let tmp = self.tiles[self.size.x as usize - 1][y]; for x in (1..self.size.x as usize).rev() { self.tiles[x][y] = self.tiles[x-1][y]; } self.tiles[0][y] = tmp; } } /// Shifts all tiles up, wrapping pub fn shift_up(&mut self) { for x in 0..self.size.x as usize { let tmp = self.tiles[x][0]; for y in 0..self.size.y as usize - 1 { self.tiles[x][y] = self.tiles[x][y+1]; } self.tiles[x][self.size.y as usize - 1] = tmp; } } /// Shifts all tiles down, wrapping pub fn shift_down(&mut self) { for x in 0..self.size.x as usize { let tmp = self.tiles[x][self.size.y as usize - 1]; for y in (1..self.size.y as usize).rev() { self.tiles[x][y] = self.tiles[x][y-1]; } self.tiles[x][0] = tmp; } } }