wip: added some debug chunk generators and fixed camera projection
parent
64c15be224
commit
7cc5f6dc2d
|
|
@ -30,10 +30,11 @@ pub fn init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_debug_terrain(mut terrain: ResMut<Terrain2D>) {
|
fn setup_debug_terrain(mut terrain: ResMut<Terrain2D>) {
|
||||||
terrain.add_chunk(Vector2I { x: 0, y: 0 }, Chunk2D::new());
|
for y in 0..32 {
|
||||||
terrain.add_chunk(Vector2I { x: 1, y: 0 }, Chunk2D::new());
|
for x in 0..8 {
|
||||||
terrain.add_chunk(Vector2I { x: 0, y: 1 }, Chunk2D::new());
|
terrain.add_chunk(Vector2I { x, y }, Chunk2D::new_circle());
|
||||||
terrain.add_chunk(Vector2I { x: 1, y: 1 }, Chunk2D::new());
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_debug_ground(mut commands: Commands) {
|
fn setup_debug_ground(mut commands: Commands) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use bevy::{prelude::*, render::camera::ScalingMode};
|
use bevy::{prelude::*, render::camera::{ScalingMode, WindowOrigin}};
|
||||||
use bevy_inspector_egui::{Inspectable, RegisterInspectable};
|
use bevy_inspector_egui::{Inspectable, RegisterInspectable};
|
||||||
|
|
||||||
use crate::util::{move_towards_vec3, vec3_lerp};
|
use crate::util::{move_towards_vec3, vec3_lerp};
|
||||||
|
|
@ -37,10 +37,11 @@ fn camera_setup(mut commands: Commands) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Name::new("Camera"),
|
Name::new("Camera"),
|
||||||
Camera2dBundle {
|
Camera2dBundle {
|
||||||
// projection: OrthographicProjection {
|
projection: OrthographicProjection {
|
||||||
// scaling_mode: ScalingMode::FixedHorizontal(320.0),
|
scaling_mode: ScalingMode::FixedHorizontal(512.0),
|
||||||
// ..default()
|
window_origin: WindowOrigin::BottomLeft,
|
||||||
// },
|
..default()
|
||||||
|
},
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,18 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use super::{local_to_texel_index, Terrain2D, TerrainEvent, Texel2D, TexelID, NEIGHBOUR_INDEX_MAP};
|
use super::{local_to_texel_index, Terrain2D, TerrainEvent, Texel2D, TexelID, NEIGHBOUR_INDEX_MAP};
|
||||||
use crate::util::Vector2I;
|
use crate::util::Vector2I;
|
||||||
use bevy::{prelude::*, render::render_resource::Extent3d};
|
use bevy::{prelude::*, render::{render_resource::Extent3d, texture::ImageSampler}};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref COLOR_MAP: HashMap<TexelID, [u8; 4]> = {
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
map.insert(0, [0x20, 0x20, 0x20, 0xff]);
|
||||||
|
map.insert(1, [0xff, 0xff, 0xff, 0xff]);
|
||||||
|
map
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Reflect, Component, Default)]
|
#[derive(Reflect, Component, Default)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
|
|
@ -43,6 +55,53 @@ impl Chunk2D {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_full() -> Chunk2D {
|
||||||
|
let mut chunk = Chunk2D {
|
||||||
|
texels: Self::new_texel_array(),
|
||||||
|
dirty_rect: None,
|
||||||
|
};
|
||||||
|
for y in 0..Self::SIZE_Y {
|
||||||
|
for x in 0..Self::SIZE_X {
|
||||||
|
chunk.set_texel(&Vector2I::new(x as i32, y as i32), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chunk
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_half() -> Chunk2D {
|
||||||
|
let mut chunk = Chunk2D {
|
||||||
|
texels: Self::new_texel_array(),
|
||||||
|
dirty_rect: None,
|
||||||
|
};
|
||||||
|
for y in 0..Self::SIZE_Y {
|
||||||
|
for x in 0..Self::SIZE_X {
|
||||||
|
if x <= Self::SIZE_Y - y {
|
||||||
|
chunk.set_texel(&Vector2I::new(x as i32, y as i32), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chunk
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_circle() -> Chunk2D {
|
||||||
|
let mut chunk = Chunk2D {
|
||||||
|
texels: Self::new_texel_array(),
|
||||||
|
dirty_rect: None,
|
||||||
|
};
|
||||||
|
let origin = Self::SIZE / 2;
|
||||||
|
let radius = Self::SIZE_X as i32 / 2;
|
||||||
|
for y in 0..Self::SIZE_Y {
|
||||||
|
for x in 0..Self::SIZE_X {
|
||||||
|
let dx = (x as i32 - origin.x).abs();
|
||||||
|
let dy = (y as i32 - origin.y).abs();
|
||||||
|
if dx * dx + dy * dy <= (radius - 1) * (radius - 1) {
|
||||||
|
chunk.set_texel(&Vector2I::new(x as i32, y as i32), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chunk
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new_texel_array() -> [Texel2D; Self::SIZE_X * Self::SIZE_Y] {
|
pub fn new_texel_array() -> [Texel2D; Self::SIZE_X * Self::SIZE_Y] {
|
||||||
[Texel2D::default(); Self::SIZE_X * Self::SIZE_Y]
|
[Texel2D::default(); Self::SIZE_X * Self::SIZE_Y]
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +134,7 @@ impl Chunk2D {
|
||||||
local_to_texel_index(position).map(|i| self.texels[i])
|
local_to_texel_index(position).map(|i| self.texels[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_texel_option_mut(&mut self, position: &Vector2I) -> Option<&mut Texel2D> {
|
pub fn get_texel_mut(&mut self, position: &Vector2I) -> Option<&mut Texel2D> {
|
||||||
local_to_texel_index(position).map(|i| &mut self.texels[i])
|
local_to_texel_index(position).map(|i| &mut self.texels[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -95,7 +154,7 @@ impl Chunk2D {
|
||||||
if update_neighbours {
|
if update_neighbours {
|
||||||
for offset in Texel2D::NEIGHBOUR_OFFSET_VECTORS {
|
for offset in Texel2D::NEIGHBOUR_OFFSET_VECTORS {
|
||||||
// Flip neighbour's bit
|
// Flip neighbour's bit
|
||||||
match self.get_texel_option_mut(&(*position + offset)) {
|
match self.get_texel_mut(&(*position + offset)) {
|
||||||
Some(mut neighbour) => {
|
Some(mut neighbour) => {
|
||||||
neighbour.neighbour_mask ^= 1 << NEIGHBOUR_INDEX_MAP[&-offset];
|
neighbour.neighbour_mask ^= 1 << NEIGHBOUR_INDEX_MAP[&-offset];
|
||||||
}
|
}
|
||||||
|
|
@ -116,16 +175,27 @@ pub fn chunk_spawner(
|
||||||
for terrain_event in terrain_events.iter() {
|
for terrain_event in terrain_events.iter() {
|
||||||
match terrain_event {
|
match terrain_event {
|
||||||
TerrainEvent::ChunkAdded(chunk_index) => {
|
TerrainEvent::ChunkAdded(chunk_index) => {
|
||||||
|
let chunk = terrain.index_to_chunk(chunk_index).unwrap();
|
||||||
|
|
||||||
let mut data = Vec::with_capacity(Chunk2D::SIZE_X * Chunk2D::SIZE_Y * 4);
|
let mut data = Vec::with_capacity(Chunk2D::SIZE_X * Chunk2D::SIZE_Y * 4);
|
||||||
for _y in 0..Chunk2D::SIZE_Y {
|
let fallback: [u8; 4] = [0x00, 0x00, 0x00, 0x00];
|
||||||
for _x in 0..Chunk2D::SIZE_X {
|
for y in (0..Chunk2D::SIZE_Y).rev() {
|
||||||
data.push(0x00);
|
for x in 0..Chunk2D::SIZE_X {
|
||||||
data.push(0x00);
|
data.append(
|
||||||
data.push(0x00);
|
&mut COLOR_MAP
|
||||||
data.push(0x00);
|
.get(
|
||||||
|
&chunk
|
||||||
|
.get_texel(&Vector2I::new(x as i32, y as i32))
|
||||||
|
.unwrap()
|
||||||
|
.id,
|
||||||
|
)
|
||||||
|
.unwrap_or(&fallback)
|
||||||
|
.to_vec()
|
||||||
|
.clone(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let image = Image::new(
|
let mut image = Image::new(
|
||||||
Extent3d {
|
Extent3d {
|
||||||
width: Chunk2D::SIZE_X as u32,
|
width: Chunk2D::SIZE_X as u32,
|
||||||
height: Chunk2D::SIZE_Y as u32,
|
height: Chunk2D::SIZE_Y as u32,
|
||||||
|
|
@ -136,7 +206,9 @@ pub fn chunk_spawner(
|
||||||
bevy::render::render_resource::TextureFormat::Rgba8Unorm,
|
bevy::render::render_resource::TextureFormat::Rgba8Unorm,
|
||||||
);
|
);
|
||||||
|
|
||||||
images.add(image);
|
image.sampler_descriptor = ImageSampler::nearest();
|
||||||
|
|
||||||
|
let texture = images.add(image);
|
||||||
|
|
||||||
let pos = Vec2::from(*chunk_index * Chunk2D::SIZE);
|
let pos = Vec2::from(*chunk_index * Chunk2D::SIZE);
|
||||||
commands
|
commands
|
||||||
|
|
@ -152,8 +224,10 @@ pub fn chunk_spawner(
|
||||||
0.75,
|
0.75,
|
||||||
),
|
),
|
||||||
custom_size: Some(Vec2::from(Chunk2D::SIZE)),
|
custom_size: Some(Vec2::from(Chunk2D::SIZE)),
|
||||||
|
anchor: bevy::sprite::Anchor::BottomLeft,
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
|
texture,
|
||||||
transform: Transform::from_translation(Vec3::new(pos.x, pos.y, 0.0)),
|
transform: Transform::from_translation(Vec3::new(pos.x, pos.y, 0.0)),
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,10 @@ pub struct Vector2<T: VectorComponent> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: VectorComponent> Vector2<T> {
|
impl<T: VectorComponent> Vector2<T> {
|
||||||
|
pub fn new(x: T, y: T) -> Vector2<T> {
|
||||||
|
Vector2 { x, y }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn min(&self, other: &Vector2<T>) -> Vector2<T> {
|
pub fn min(&self, other: &Vector2<T>) -> Vector2<T> {
|
||||||
Vector2 {
|
Vector2 {
|
||||||
x: Ord::min(self.x, other.x),
|
x: Ord::min(self.x, other.x),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue