draw "tiles"

This commit is contained in:
Llywelwyn 2023-09-23 23:33:50 +01:00
parent 2967cddf7b
commit be1c7aa1c7

View file

@ -19,6 +19,7 @@ fn main() -> Result<(), String> {
.add_config(win_config) .add_config(win_config)
.add_config(notan::draw::DrawConfig) .add_config(notan::draw::DrawConfig)
.draw(draw) .draw(draw)
.update(update)
.build() .build()
} }
@ -146,19 +147,26 @@ fn setup(gfx: &mut Graphics) -> State {
gs gs
} }
fn draw(app: &mut App, gfx: &mut Graphics, state: &mut State) { fn draw(app: &mut App, gfx: &mut Graphics, gs: &mut State) {
let mut draw = gfx.create_draw(); let mut draw = gfx.create_draw();
draw.clear(Color::BLACK); draw.clear(Color::BLACK);
let mut x = 10.0; // Draw map
let mut y = 10.0; let map = gs.ecs.fetch::<Map>();
// Draw base texture for (i, _tile) in map.tiles.iter().enumerate() {
state.atlas.iter().for_each(|(k, tex)| { let px = idx_to_px(i, &map);
if y + tex.height() > (gfx.size().1 as f32) * 0.8 { draw.image(gs.atlas.get("floor_grass_d").unwrap()).position(px.0, px.1);
y = 10.0; }
x += 17.0; // Render batch
}
draw.image(tex).position(x, y);
y += tex.height() + 1.0;
});
gfx.render(&draw); gfx.render(&draw);
} }
fn idx_to_px(idx: usize, map: &Map) -> (f32, f32) {
(
((idx % (map.width as usize)) as i32 as f32) * (TILESIZE as f32),
((idx / (map.width as usize)) as i32 as f32) * (TILESIZE as f32),
)
}
fn update(app: &mut App, state: &mut State) {
// game loop
}