grid logic

This commit is contained in:
2026-09-19 16:32:04 +02:00
parent 6143cac61d
commit 56d90253bd
12 changed files with 104 additions and 49 deletions
+16 -14
View File
@@ -1,33 +1,35 @@
use bevy::prelude::*;
use crate::world::Chunk;
use crate::{
config::{CHUNK_ROWS, TERRAIN_COLS},
world::{
Chunk,
grid::{GridRect, grid_to_world},
},
};
// Test checkboard background
const COLOR_A: Color = Color::srgb(0.0, 0.0, 0.0);
const COLOR_B: Color = Color::srgb(0.6, 0.2, 0.8);
const ROWS_PER_CHUNK: i64 = 2;
const CELL_SIZE: f32 = crate::world::CHUNK_HEIGHT / ROWS_PER_CHUNK as f32;
const BOARD_WIDTH: f32 = 1920.0;
const COLS: i64 = (BOARD_WIDTH / CELL_SIZE).ceil() as i64;
pub fn spawn_background_chunk(commands: &mut Commands, id: i64) {
for row in (id * ROWS_PER_CHUNK)..(id * ROWS_PER_CHUNK + ROWS_PER_CHUNK) {
for col in 0..COLS {
let color = if (row + col) % 2 == 0 {
for row in 0..CHUNK_ROWS {
for col in 0..TERRAIN_COLS {
let global_row = id * CHUNK_ROWS as i64 + row as i64;
let color = if (global_row + col as i64) % 2 == 0 {
COLOR_A
} else {
COLOR_B
};
let (center, size) = grid_to_world(id, GridRect::cell(col, row));
commands.spawn((
Chunk { id },
Sprite::from_color(color, Vec2::new(CELL_SIZE, CELL_SIZE)),
Transform::from_xyz(
(col as f32 + 0.5 - COLS as f32 / 2.0) * CELL_SIZE,
(row as f32 + 0.5) * CELL_SIZE,
0.0,
),
Sprite::from_color(color, size),
Transform::from_xyz(center.x, center.y, 0.0),
));
}
}