Skip to main content

starbloom_map/
chunk.rs

1use bevy_ecs::prelude::*;
2use egor::render::*;
3use egor::math::*;
4
5use starbloom_base::prelude::*;
6use starbloom_tiles::*;
7use starbloom_camera::*;
8
9pub const CHUNK_DIM: usize = 16;
10
11pub const CHUNK_SIZE: f32 = CHUNK_DIM as f32 * TILE_SIZE;
12
13#[derive(Component)]
14pub struct Chunk {
15    x: u64, // No overflows for you any time soon
16    y: u64,
17    tiles: [[TileRepr; CHUNK_DIM]; CHUNK_DIM],
18}
19
20impl Chunk {
21    pub fn load(x: u64, y: u64) -> Self {
22        Self {
23            x,
24            y,
25            tiles: [[1; CHUNK_DIM]; CHUNK_DIM],
26        }
27    }
28
29    pub fn render(&self, gfx: &mut NonSendMut<GfxCmds>, main_camera: &Res<MainCamera>, tile_regestry: &TileRegestry) {
30        for (x, row) in self.tiles.iter().enumerate() {
31            for (y, tile_idx) in row.iter().enumerate() {
32                let tile = tile_regestry.get_tile_by_idx(tile_idx);
33                if !tile.renderable {
34                    continue;
35                }
36		let pos = main_camera.cam.world_to_screen(vec2(x as f32, y as f32)*TILE_SIZE);
37		gfx.draw(Box::new(move |gfx: &mut Graphics<'_>| {
38		    gfx.rect().size(vec2(TILE_SIZE, TILE_SIZE)).at(pos);
39		}));
40            }
41        }
42    }
43
44    pub fn get(&self, x: usize, y: usize) -> TileRepr {
45        self.tiles[x][y]
46    }
47}