wip commit

fix/collision-refresh
hheik 2022-12-12 18:09:38 +02:00
parent 4968e5a802
commit 1135766cac
5 changed files with 58 additions and 23 deletions

View File

@ -47,8 +47,11 @@ fn debug_controls(
fn setup_debug_terrain(mut commands: Commands, mut terrain: ResMut<Terrain2D>) { fn setup_debug_terrain(mut commands: Commands, mut terrain: ResMut<Terrain2D>) {
let terrain_gen = TerrainGen2D::new(432678); let terrain_gen = TerrainGen2D::new(432678);
for y in 0..(WORLD_WIDTH / Chunk2D::SIZE_Y as i32) { // for y in 0..(WORLD_WIDTH / Chunk2D::SIZE_Y as i32) {
for x in 0..(WORLD_WIDTH / Chunk2D::SIZE_X as i32) { // for x in 0..(WORLD_WIDTH / Chunk2D::SIZE_X as i32) {
// DEBUG:
for y in 1..2 {
for x in 8..9 {
let position = Vector2I { x, y }; let position = Vector2I { x, y };
terrain.add_chunk(position, terrain_gen.gen_chunk(&position)); terrain.add_chunk(position, terrain_gen.gen_chunk(&position));
} }

View File

@ -217,8 +217,12 @@ fn kinematic_movement(
// name = name_query.get(coll_entity).unwrap(), // name = name_query.get(coll_entity).unwrap(),
// ); // );
kinematic_state.on_ground = true; kinematic_state.on_ground = true;
// DEBUG:
println!("grounded");
} else { } else {
kinematic_state.on_ground = false; kinematic_state.on_ground = false;
// DEBUG:
println!("no ground");
} }
} }
} }

View File

@ -61,7 +61,9 @@ pub fn player_spawn(mut commands: Commands) {
let kinematic = KinematicBundle { let kinematic = KinematicBundle {
collider: Collider::round_cuboid(4.0, 8.0, 1.0), collider: Collider::round_cuboid(4.0, 8.0, 1.0),
transform: TransformBundle::from_transform(Transform::from_translation(Vec3::new( transform: TransformBundle::from_transform(Transform::from_translation(Vec3::new(
256.0, 128.0, 0.0, // 256.0, 128.0, 0.0,
// DEBUG:
256.0, 72.996, 0.0,
))), ))),
..default() ..default()
}; };
@ -84,6 +86,7 @@ pub fn player_spawn(mut commands: Commands) {
.insert(KinematicInput::default()) .insert(KinematicInput::default())
.insert(CameraFollow { .insert(CameraFollow {
priority: 1, priority: 1,
movement: FollowMovement::Smooth(7.0), // movement: FollowMovement::Smooth(7.0),
movement: FollowMovement::Instant,
}); });
} }

View File

@ -5,7 +5,7 @@ use std::collections::{
use bevy::{ use bevy::{
prelude::*, prelude::*,
render::{camera::RenderTarget, view::window}, render::{camera::RenderTarget},
}; };
use bevy_prototype_debug_lines::DebugLines; use bevy_prototype_debug_lines::DebugLines;
@ -13,6 +13,7 @@ mod chunk2d;
mod terrain_gen2d; mod terrain_gen2d;
mod texel2d; mod texel2d;
use bevy_rapier2d::prelude::*;
pub use chunk2d::*; pub use chunk2d::*;
pub use terrain_gen2d::*; pub use terrain_gen2d::*;
pub use texel2d::*; pub use texel2d::*;
@ -29,15 +30,48 @@ impl Plugin for Terrain2DPlugin {
app.register_type::<TerrainChunk2D>() app.register_type::<TerrainChunk2D>()
.insert_resource(Terrain2D::new()) .insert_resource(Terrain2D::new())
.add_event::<TerrainEvent2D>() .add_event::<TerrainEvent2D>()
.add_system(debug_painter) .add_system(debug_painter)
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
dirty_rect_visualizer.before(emit_terrain_events), dirty_rect_visualizer.before(emit_terrain_events),
) )
.add_system_to_stage(CoreStage::PostUpdate, emit_terrain_events)
.add_system(chunk_spawner) // DEBUG:
.add_system(chunk_sprite_sync) .add_system_to_stage(CoreStage::First, first_log)
.add_system(chunk_collision_sync); .add_system_to_stage(CoreStage::Last, last_log)
.add_system_to_stage(CoreStage::PostUpdate, chunk_spawner.before(emit_terrain_events))
.add_system_to_stage(CoreStage::PostUpdate, chunk_sprite_sync.after(chunk_spawner))
.add_system_to_stage(CoreStage::PostUpdate, chunk_collision_sync.after(chunk_spawner))
.add_system_to_stage(CoreStage::PostUpdate, emit_terrain_events);
}
}
// DEBUG:
fn first_log() {
println!("start <");
}
// DEBUG:
fn last_log(
chunk_query: Query<(Entity, &TerrainChunk2D)>,
child_query: Query<&Children>,
collider_query: Query<&Collider>,
mut commands: Commands,
) {
println!("> end");
for (entity, chunk) in chunk_query.iter() {
// if chunk.index == Vector2I::new(8, 1) {
// }
println!("chunk! {entity:?} {:?}", chunk.index);
for children in child_query.get(entity).iter() {
for child in children.iter() {
print!("\t");
commands.entity(*child).log_components()
}
}
} }
} }

View File

@ -477,9 +477,7 @@ pub fn chunk_spawner(
*/ */
pub fn chunk_sprite_sync( pub fn chunk_sprite_sync(
mut terrain_events: EventReader<TerrainEvent2D>, mut terrain_events: EventReader<TerrainEvent2D>,
mut commands: Commands,
mut images: ResMut<Assets<Image>>, mut images: ResMut<Assets<Image>>,
mut sprite_query: Query<&mut Sprite>,
terrain: Res<Terrain2D>, terrain: Res<Terrain2D>,
added_chunk_query: Query< added_chunk_query: Query<
(Entity, &TerrainChunk2D), (Entity, &TerrainChunk2D),
@ -519,21 +517,12 @@ pub fn chunk_sprite_sync(
// Update sprite // Update sprite
for (entity, chunk, rect) in updated_chunks { for (entity, chunk, rect) in updated_chunks {
let chunk = terrain.index_to_chunk(&chunk.index).unwrap(); let chunk = terrain.index_to_chunk(&chunk.index).unwrap();
let rect = rect.unwrap_or(ChunkRect { // TODO: Update only the rect
let _rect = rect.unwrap_or(ChunkRect {
min: Vector2I::ZERO, min: Vector2I::ZERO,
max: Chunk2D::SIZE - Vector2I::ONE, max: Chunk2D::SIZE - Vector2I::ONE,
}); });
let mut sprite = match sprite_query.get_mut(entity) {
Ok(sprite) => sprite,
Err(err) => {
println!("[chunk_sprite_sync] Sprite component not found for entity:");
commands.entity(entity).log_components();
println!("{err:?}");
continue;
}
};
let handle = texture_query.get(entity).unwrap(); let handle = texture_query.get(entity).unwrap();
let mut image = images.get_mut(handle).unwrap(); let mut image = images.get_mut(handle).unwrap();
let image_data = chunk.create_texture_data(); let image_data = chunk.create_texture_data();
@ -556,7 +545,7 @@ pub fn chunk_collision_sync(
child_query: Query<&Children>, child_query: Query<&Children>,
collider_query: Query<&Collider>, collider_query: Query<&Collider>,
) { ) {
let mut updated_chunks = vec![]; let mut updated_chunks: Vec<(Entity, &TerrainChunk2D)> = vec![];
// Check for added components // Check for added components
for (added_entity, added_chunk) in added_chunk_query.iter() { for (added_entity, added_chunk) in added_chunk_query.iter() {
@ -585,6 +574,8 @@ pub fn chunk_collision_sync(
} }
for (entity, chunk) in updated_chunks.iter() { for (entity, chunk) in updated_chunks.iter() {
// DEBUG:
println!("update chunk {:?}", chunk.index);
// Remove old colliders // Remove old colliders
for children in child_query.get(*entity) { for children in child_query.get(*entity) {
for child in children { for child in children {