Skip to content

Commit

Permalink
Added down stair walk
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac8668 committed Dec 30, 2023
1 parent e3d7c93 commit 79f30b8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 81 deletions.
59 changes: 34 additions & 25 deletions src/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,44 @@ pub fn update_actors(mut chunk_manager: Query<&mut ChunkManager>, mut actors: Qu

for mut actor in actors.iter_mut() {
let mut prev = actor.pos;
'pos: for v in Line::new(actor.pos, actor.vel.as_ivec2()) {
let move_hor;
match (prev.x != v.x, prev.y != v.y) {
(true, false) => move_hor = true,
(false, true) => move_hor = false,
(true, true) => move_hor = fastrand::bool(),
for v in Line::new(actor.pos, actor.vel.as_ivec2()) {
let move_hor = match (prev.x != v.x, prev.y != v.y) {
(true, false) => true,
(false, true) => false,
(true, true) => fastrand::bool(),
_ => unreachable!(),
}
};

if move_hor {
if !move_x(&mut chunk_manager, &mut actor, v.x - prev.x) {
//If we can't move to the left or right
//Check if we can get up a stair-like structure
//TODO for now we only can get up a atom, maybe it is good to smoothly go down a atom too
let moved_x = move_x(&mut chunk_manager, &mut actor, v.x - prev.x);
if on_ground(&chunk_manager, &actor) {
let starting_y = actor.pos.y;
for i in 0..STAIR_WALK_HEIGHT {
if move_y(&mut chunk_manager, &mut actor, -1) {
if i == STAIR_WALK_HEIGHT - 1 {
//Walk horizontaly after height adjustments on the last step
if move_x(&mut chunk_manager, &mut actor, v.x - prev.x) {
//We climbed up or down a stair!
break 'pos;
match moved_x {
//If we can't move to the left or right
//Check if we can get up a stair-like structure
false => {
for i in 1..=UP_WALK_HEIGHT {
let moved_y = move_y(&mut chunk_manager, &mut actor, -1);
//Abort if we couldn't move up, or if we moved up but couldn't move sideways on the last step
if !moved_y
|| i == UP_WALK_HEIGHT
&& !move_x(&mut chunk_manager, &mut actor, v.x - prev.x)
{
abort_stair(&mut chunk_manager, &mut actor, starting_y, 1);
break;
}
}
}
//If we can move to the left or right
//Check if we can snap back to the ground
true => {
for i in 1..=DOWN_WALK_HEIGHT {
if !move_y(&mut chunk_manager, &mut actor, 1) {
break;
} else if i == DOWN_WALK_HEIGHT {
abort_stair(&mut chunk_manager, &mut actor, starting_y, -1);
}
abort_stair(&mut chunk_manager, &mut actor, starting_y);
break;
}
} else {
abort_stair(&mut chunk_manager, &mut actor, starting_y);
break;
}
}
}
Expand All @@ -80,9 +89,9 @@ pub fn update_actors(mut chunk_manager: Query<&mut ChunkManager>, mut actors: Qu
}
}

pub fn abort_stair(chunk_manager: &mut ChunkManager, actor: &mut Actor, starting_y: i32) {
pub fn abort_stair(chunk_manager: &mut ChunkManager, actor: &mut Actor, starting_y: i32, dir: i32) {
for _ in 0..(starting_y - actor.pos.y) {
move_y(chunk_manager, actor, 1);
move_y(chunk_manager, actor, dir);
}
}

Expand Down
21 changes: 4 additions & 17 deletions src/chunk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub fn chunk_manager_update(

// Loop through deferred tasks
while let Ok(update) = dirty_update_rects_recv.recv().await {
update_dirty_rects(new_dirty_rects, update.chunk_pos);
update_dirty_rects_3x3(new_dirty_rects, update.chunk_pos);
}
});

Expand All @@ -264,15 +264,7 @@ pub fn chunk_manager_update(

// Loop through deferred tasks
while let Ok(update) = dirty_render_rects_recv.recv().await {
let pos = update.chunk_pos;
if let Some(rect) = render_dirty_rects.get_mut(&pos.chunk) {
extend_rect_if_needed(rect, &pos.atom)
} else {
render_dirty_rects.insert(
pos.chunk,
URect::new(pos.atom.x, pos.atom.y, pos.atom.x, pos.atom.y),
);
}
update_dirty_rects(render_dirty_rects, update.chunk_pos);
}
});

Expand Down Expand Up @@ -526,15 +518,10 @@ impl Plugin for ChunkManagerPlugin {
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app
.init_resource::<ExtractedTextureUpdates>()
.add_systems(
ExtractSchedule,
extract_chunk_texture_updates.after(chunk_manager_update),
);
.add_systems(ExtractSchedule, extract_chunk_texture_updates);
Image::register_system(
render_app,
prepare_chunk_gpu_textures
.in_set(RenderSet::PrepareAssets)
.after(extract_chunk_texture_updates),
prepare_chunk_gpu_textures.in_set(RenderSet::PrepareAssets),
)
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ pub const CHUNK_LEN: usize = CHUNK_LENGHT * CHUNK_LENGHT;
pub const HALF_CHUNK_LEN: usize = CHUNK_LEN / 2;
pub const QUARTER_CHUNK_LEN: usize = CHUNK_LEN / 4;

pub const STAIR_WALK_HEIGHT: usize = 3;
pub const UP_WALK_HEIGHT: usize = 3;
pub const DOWN_WALK_HEIGHT: usize = 6;
pub const ATOM_SIZE: usize = 3;
pub const CAMERA_SPEED: f32 = 10.;
pub const _CAMERA_SPEED: f32 = 10.;
pub const GRAVITY: u8 = 1;
pub const TERM_VEL: u8 = 10;
pub const FRAMES_SLEEP: u8 = 4;
Expand Down
2 changes: 1 addition & 1 deletion src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn _camera(keys: Res<Input<KeyCode>>, mut camera_q: Query<&mut Transform, With<C
let y = -(keys.pressed(KeyCode::S) as u8 as f32) + keys.pressed(KeyCode::W) as u8 as f32;

let v = Vec2::new(x, y).normalize_or_zero().extend(0.);
camera_q.single_mut().translation += v * CAMERA_SPEED;
camera_q.single_mut().translation += v * _CAMERA_SPEED;
}

pub struct DebugPlugin;
Expand Down
34 changes: 20 additions & 14 deletions src/manager_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,21 @@ pub fn get_mutable_references<'a>(
});
}

pub fn update_dirty_rects(dirty_rects: &mut HashMap<IVec2, URect>, pos: ChunkPos) {
if let Some(dirty_rects) = dirty_rects.get_mut(&pos.chunk) {
extend_rect_if_needed(dirty_rects, &pos.atom)
} else {
dirty_rects.insert(
pos.chunk,
URect::new(pos.atom.x, pos.atom.y, pos.atom.x, pos.atom.y),
);
}
}

//This function gets a single ChunkPos and makes sure that we update the 3x3 surrounding atoms
pub fn update_dirty_rects(dirty_rects: &mut HashMap<IVec2, URect>, chunk_pos: ChunkPos) {
let atom = chunk_pos.atom;
let mut chunk = chunk_pos.chunk;
pub fn update_dirty_rects_3x3(dirty_rects: &mut HashMap<IVec2, URect>, pos: ChunkPos) {
let atom = pos.atom;
let mut chunk = pos.chunk;

if (1..62).contains(&atom.x) && (1..62).contains(&atom.y) {
// Case where the 3x3 position area is within a chunk
Expand Down Expand Up @@ -464,21 +475,16 @@ pub fn update_dirty_rects(dirty_rects: &mut HashMap<IVec2, URect>, chunk_pos: Ch
} else {
// Case where the 3x3 position is in the corner of a chunk
for (x, y) in (-1..=1).cartesian_product(-1..=1) {
let mut global = chunk_to_global(chunk_pos);
let mut global = chunk_to_global(pos);
global += ivec2(x, y);
let chunk_pos = global_to_chunk(global);
let pos = global_to_chunk(global);

if let Some(rect) = dirty_rects.get_mut(&chunk_pos.chunk) {
extend_rect_if_needed(rect, &chunk_pos.atom)
if let Some(rect) = dirty_rects.get_mut(&pos.chunk) {
extend_rect_if_needed(rect, &pos.atom)
} else {
dirty_rects.insert(
chunk_pos.chunk,
URect::new(
chunk_pos.atom.x,
chunk_pos.atom.y,
chunk_pos.atom.x,
chunk_pos.atom.y,
),
pos.chunk,
URect::new(pos.atom.x, pos.atom.y, pos.atom.x, pos.atom.y),
);
}
}
Expand Down
27 changes: 5 additions & 22 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ pub fn player_setup(

/// Updates player
pub fn update_player(
mouse: Res<Input<MouseButton>>,
keys: ResMut<Input<KeyCode>>,
input: (Res<Input<MouseButton>>, ResMut<Input<KeyCode>>),
window: Query<&Window>,
mut player: Query<(
&mut Actor,
Expand All @@ -101,6 +100,7 @@ pub fn update_player(
) {
let (mut actor, mut player, mut textatlas_sprite, mut anim_idxs) = player.single_mut();
let (mut tool_transform, tool_gtransform, mut tool_sprite, mut tool) = tool.single_mut();
let (mouse, keys) = input;

let mut chunk_manager = chunk_manager.single_mut();

Expand Down Expand Up @@ -226,7 +226,7 @@ pub fn update_player(
let chunk_pos = global_to_chunk(vec);
if let Some(atom) = chunk_manager.get_mut_atom(chunk_pos) {
if atom.state != State::Void {
tool.atoms.push(atom.clone());
tool.atoms.push(*atom);
*atom = Atom::new();
pos_to_update.push(chunk_pos);
break;
Expand All @@ -238,25 +238,8 @@ pub fn update_player(

let mut dirty_rects = dirty_rects.single_mut();
for pos in pos_to_update {
// Update simultation rect
if let Some(dirty_rect) = dirty_rects.current.get_mut(&pos.chunk) {
extend_rect_if_needed(dirty_rect, &pos.atom)
} else {
dirty_rects.current.insert(
pos.chunk,
URect::new(pos.atom.x, pos.atom.y, pos.atom.x, pos.atom.y),
);
}

// Update render rect
if let Some(dirty_rect) = dirty_rects.render.get_mut(&pos.chunk) {
extend_rect_if_needed(dirty_rect, &pos.atom)
} else {
dirty_rects.render.insert(
pos.chunk,
URect::new(pos.atom.x, pos.atom.y, pos.atom.x, pos.atom.y),
);
}
update_dirty_rects_3x3(&mut dirty_rects.current, pos);
update_dirty_rects(&mut dirty_rects.render, pos);
}
}
}
Expand Down

0 comments on commit 79f30b8

Please sign in to comment.