Skip to content

Commit

Permalink
improve chunk generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Cannon Tuttle committed Sep 6, 2023
1 parent 5c48d6d commit 0981a8a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/game/game_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// const N_BUILDINGS_PER_CHUNK: usize = 30;
// const USING_DOORS: bool = true;

pub const MAP_CHUNK_MIN_SIDE_LEN: usize = 7;
pub const MAP_CHUNK_MIN_SIDE_LEN: usize = 6;
pub const MAP_CHUNK_MAX_SIDE_LEN: usize = 50;

pub const MAP_CHUNK_MAX_N_TILES: usize = 800;
pub const MAP_CHUNK_MAX_N_TILES: usize = 400;

pub const TOTAL_TILES_IN_MAP: usize = 30000;
pub const TOTAL_TILES_IN_MAP: usize = 120000;

pub const N_NPCS: i32 = 10;

Expand Down
43 changes: 43 additions & 0 deletions src/game/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,44 @@ impl GameState<'static> {
}
}
let mut is_viable_spot = true;

fn shares_enough_axes_with_other_bounds(potential_bound: &TileAlignedBoundingBox, source_bound: &TileAlignedBoundingBox) -> bool {
let b1: &TileAlignedBoundingBox = potential_bound;
let b2: &TileAlignedBoundingBox = source_bound;

fn do_for_one_side (b1: &TileAlignedBoundingBox, b2: &TileAlignedBoundingBox) -> bool {
if
b1.y + b1.height as i32 == b2.y
{
if (b1.x + b1.width as i32 - b2.x).min(b2.x + b2.width as i32 - b1.x) >= MAP_CHUNK_MIN_SIDE_LEN as i32 {
return true
} else {
return false
}
}

if
b1.x + b1.width as i32 == b2.x
{
if (b1.y + b1.height as i32 - b2.y).min(b2.y + b2.height as i32 - b1.y) >= MAP_CHUNK_MIN_SIDE_LEN as i32 {
return true
} else {
return false
}
}
true
}

do_for_one_side(&b1, &b2) && do_for_one_side(&b2, b1)
}

// ensure it shares enough adjacency with source chunk
if !shares_enough_axes_with_other_bounds(&rand_bound, &new_chunk_location) {
is_viable_spot = false;
}



for other_bound in &current_chunk_locations {
// if it collides with existing chunk, disallow
if new_chunk_location.y + new_chunk_location.height as i32 > other_bound.y {
Expand All @@ -174,6 +212,11 @@ impl GameState<'static> {
}
}
}
// if it doesn't collide, but it share too little with any adjacent chunks, it's also invalid
if !shares_enough_axes_with_other_bounds(&other_bound, &new_chunk_location) {
is_viable_spot = false;
}

}

if is_viable_spot {
Expand Down
25 changes: 17 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mod wasm4;
use game::{game_constants::{TILE_WIDTH_PX, TILE_HEIGHT_PX, X_LEFT_BOUND, X_RIGHT_BOUND, Y_LOWER_BOUND, Y_UPPER_BOUND, GameMode}, game_state::GameState, entities::{MovingEntity, Character}, camera::Camera, collision::update_pos};
use num;
mod game;
use core::cell::RefCell;
use wasm4::*;

use crate::game::entities::OptionallyEnabledPlayer;
Expand Down Expand Up @@ -80,7 +79,7 @@ fn drawmap(game_state: &GameState) {



static mut GAME_STATE_HOLDER: Option<RefCell<GameState<'static>>> = None;
static mut GAME_STATE_HOLDER: Option<GameState<'static>> = None;


fn drawcharacter(spritesheet: &[u8], spritesheet_stride: &usize, camera: &Camera, character: MovingEntity) {
Expand Down Expand Up @@ -121,20 +120,30 @@ static mut PREVIOUS_GAMEPAD: [u8; 4] = [0, 0, 0, 0];
#[no_mangle]
fn update() {


let game_state: &mut GameState;

unsafe {
match GAME_STATE_HOLDER {
match &mut GAME_STATE_HOLDER {
None => {
GAME_STATE_HOLDER = Some()
let new_game_state = GameState::new();
GAME_STATE_HOLDER = Some(new_game_state);

},
Some(_) => {

}
}
match &mut GAME_STATE_HOLDER {
Some(game_state_holder) => {
game_state = game_state_holder;
},
None => unreachable!()
}
}


unsafe {
game_state = &mut GAME_STATE_HOLDER.borrow_mut();
}

*game_state = RefCell::new(GameState::new());

let gamepads: [u8; 4] = unsafe { [*GAMEPAD1, *GAMEPAD2, *GAMEPAD3, *GAMEPAD4] };
let mut btns_pressed_this_frame: [u8; 4] = [0; 4];
Expand Down

0 comments on commit 0981a8a

Please sign in to comment.