Skip to content

Commit

Permalink
Add start/finish rooms (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed Apr 18, 2024
1 parent 600e4bc commit af2f194
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
63 changes: 43 additions & 20 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,54 @@ impl Generator {
edge_bug
}

fn generate_room(&mut self, pos: &Position, margin: usize) {
let start_x = pos.x.saturating_sub(margin);
let start_y = pos.y.saturating_sub(margin);
let end_x = (pos.x + margin + 1).min(self.map.width);
let end_y = (pos.y + margin + 1).min(self.map.height);

let valid = start_x < end_x && start_y < end_y;

if valid {
let mut view = self.map.grid.slice_mut(s![start_x..end_x, start_y..end_y]);
view.map_inplace(|elem| *elem = BlockType::Empty);

let platform_margin = margin.saturating_sub(1);

let mut view = self.map.grid.slice_mut(s![
pos.x - platform_margin..pos.x + platform_margin + 1,
pos.y + 1..pos.y + 2
]);
view.map_inplace(|elem| *elem = BlockType::Hookable);
fn generate_room(&mut self, pos: &Position, margin: usize, zone_type: &BlockType) {
// TODO: ensure valid position?

// carve room
self.map.set_area(
&Position::new(pos.x - margin, pos.y - margin),
&Position::new(pos.x + margin + 1, pos.y + margin + 1),
&BlockType::Empty,
true,
);

// set platform
self.map.set_area(
&Position::new(pos.x - (margin - 2), pos.y),
&Position::new(pos.x + (margin - 2) + 1, pos.y + 1),
&BlockType::Hookable,
true,
);

// set spawns
if *zone_type == BlockType::Start {
self.map.set_area(
&Position::new(pos.x - (margin - 2), pos.y - 1),
&Position::new(pos.x + (margin - 2) + 1, pos.y),
&BlockType::Spawn,
true,
);
}

// set start/finish line TODO: only right/left so far
self.map.set_area(
&Position::new(pos.x + margin + 1, pos.y - margin),
&Position::new(pos.x + margin + 2, pos.y + margin + 1),
zone_type,
false,
);
self.map.set_area(
&Position::new(pos.x - margin - 1, pos.y - margin),
&Position::new(pos.x - margin, pos.y + margin + 1),
zone_type,
false,
);
}

pub fn post_processing(&mut self) {
self.fix_edge_bugs();
self.generate_room(&self.map.spawn.clone(), 3);
self.generate_room(&self.map.spawn.clone(), 4, &BlockType::Start);
self.generate_room(&self.walker.pos.clone(), 4, &BlockType::Finish);
}

/// Generates an entire map with a single function call. This function is used by the CLI.
Expand Down
19 changes: 17 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,29 @@ impl Map {
pos.x < self.width && pos.y < self.height
}

pub fn set_area(&mut self, top_left: &Position, bot_right: &Position, value: &BlockType) {
// TODO: right now override is hardcoded to overide empty AND freeze. i might need some
// distiction here in the future :)
pub fn set_area(
&mut self,
top_left: &Position,
bot_right: &Position,
value: &BlockType,
overide: bool,
) {
let valid_area = self.pos_in_bounds(top_left) && self.pos_in_bounds(bot_right);

if valid_area {
let mut view = self
.grid
.slice_mut(s![top_left.x..bot_right.x, top_left.y..bot_right.y]);
view.map_inplace(|elem| *elem = value.clone());
view.map_inplace(|current_value| {
if overide
|| *current_value == BlockType::Empty
|| *current_value == BlockType::Freeze
{
*current_value = value.clone();
}
});
}
}
}

0 comments on commit af2f194

Please sign in to comment.