enter house (#10)
Reviewed-on: #10 Co-authored-by: luckyturtledev <git@lukas1818.de> Co-committed-by: luckyturtledev <git@lukas1818.de>
This commit is contained in:
parent
1c897d6712
commit
bb37f613ad
5 changed files with 52 additions and 12 deletions
|
@ -11,13 +11,16 @@ pub struct HouseState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) {
|
pub fn draw(state: &crate::State, _engine: &comfy::EngineContext<'_>) {
|
||||||
|
if let Some(house) = state.house() {
|
||||||
//Draw Grid
|
//Draw Grid
|
||||||
state.house.grid.draw();
|
house.grid.draw();
|
||||||
|
|
||||||
//Draw Player
|
//Draw Player
|
||||||
state.house.player.draw();
|
house.player.draw();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext<'_>) {
|
pub fn update(state: &mut crate::State, _engine: &mut comfy::EngineContext<'_>) {
|
||||||
state.house.player.update(&state.house.grid);
|
let house = state.house_mut();
|
||||||
|
house.player.update(&house.grid);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
|
use comfy::IVec2;
|
||||||
|
|
||||||
pub mod house;
|
pub mod house;
|
||||||
pub mod overworld;
|
pub mod overworld;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub enum Activity {
|
pub enum Activity {
|
||||||
#[allow(dead_code)]
|
House(IVec2),
|
||||||
House,
|
|
||||||
#[default]
|
#[default]
|
||||||
Overworld
|
Overworld
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
use crate::{game::ZLayer, State};
|
use crate::{
|
||||||
|
activities::{
|
||||||
|
house::{self, HouseState},
|
||||||
|
Activity
|
||||||
|
},
|
||||||
|
game::ZLayer,
|
||||||
|
State
|
||||||
|
};
|
||||||
use comfy::{
|
use comfy::{
|
||||||
draw_circle, draw_rect_outline, draw_sprite, error, info, is_key_down,
|
draw_circle, draw_rect_outline, draw_sprite, error, info, is_key_down,
|
||||||
main_camera_mut, EngineContext, IVec2, KeyCode, Vec2, RED, WHITE
|
main_camera_mut, EngineContext, IVec2, KeyCode, Vec2, RED, WHITE
|
||||||
|
@ -146,6 +153,16 @@ fn update_move_player(state: &mut State) {
|
||||||
y: requested_pos_diff.y as _
|
y: requested_pos_diff.y as _
|
||||||
};
|
};
|
||||||
state.ghost.overworld_pos_last_update = now;
|
state.ghost.overworld_pos_last_update = now;
|
||||||
|
|
||||||
|
//move into house if player stepp at door
|
||||||
|
{
|
||||||
|
let current_tile = state.overworld.get_or_generate_tile(tile_pos);
|
||||||
|
if current_tile.can_enter_house() {
|
||||||
|
info!("enter house at {tile_pos}");
|
||||||
|
state.activity = Activity::House(tile_pos);
|
||||||
|
state.house_mut(); // gen new house
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(state: &mut State, _ctx: &mut EngineContext<'_>) {
|
pub fn update(state: &mut State, _ctx: &mut EngineContext<'_>) {
|
||||||
|
|
|
@ -88,14 +88,14 @@ impl Sub<i32> for ZLayer {
|
||||||
pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {
|
pub fn update(state: &mut State, engine: &mut EngineContext<'_>) {
|
||||||
state.score += engine.delta * 10.0;
|
state.score += engine.delta * 10.0;
|
||||||
match state.activity {
|
match state.activity {
|
||||||
Activity::House => house::update(state, engine),
|
Activity::House(_) => house::update(state, engine),
|
||||||
Activity::Overworld => overworld::update(state, engine)
|
Activity::Overworld => overworld::update(state, engine)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(state: &State, engine: &EngineContext<'_>) {
|
pub fn draw(state: &State, engine: &EngineContext<'_>) {
|
||||||
match state.activity {
|
match state.activity {
|
||||||
Activity::House => house::draw(state, engine),
|
Activity::House(_) => house::draw(state, engine),
|
||||||
Activity::Overworld => overworld::draw(state, engine)
|
Activity::Overworld => overworld::draw(state, engine)
|
||||||
}
|
}
|
||||||
crate::ui::draw(state, engine);
|
crate::ui::draw(state, engine);
|
||||||
|
|
23
src/main.rs
23
src/main.rs
|
@ -17,7 +17,7 @@ use self::{
|
||||||
};
|
};
|
||||||
use comfy::{
|
use comfy::{
|
||||||
init_game_config, pollster, run_comfy_main_async, EngineContext, EngineState,
|
init_game_config, pollster, run_comfy_main_async, EngineContext, EngineState,
|
||||||
GameConfig, GameLoop
|
GameConfig, GameLoop, HashMap, IVec2
|
||||||
};
|
};
|
||||||
|
|
||||||
const GAME_NAME: &str = "Powercreep";
|
const GAME_NAME: &str = "Powercreep";
|
||||||
|
@ -28,10 +28,29 @@ struct State {
|
||||||
activity: Activity,
|
activity: Activity,
|
||||||
ghost: Ghost,
|
ghost: Ghost,
|
||||||
overworld: Overworld,
|
overworld: Overworld,
|
||||||
house: HouseState,
|
houses: HashMap<IVec2, HouseState>,
|
||||||
score: f32
|
score: f32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
fn get_house_pos(&self) -> Option<IVec2> {
|
||||||
|
match self.activity {
|
||||||
|
Activity::House(pos) => Some(pos),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn house(&self) -> Option<&HouseState> {
|
||||||
|
self.houses.get(&self.get_house_pos().unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn house_mut(&mut self) -> &mut HouseState {
|
||||||
|
self.houses
|
||||||
|
.entry(self.get_house_pos().unwrap())
|
||||||
|
.or_insert_with(HouseState::default)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl GameLoop for State {
|
impl GameLoop for State {
|
||||||
fn new(_c: &mut EngineState) -> Self {
|
fn new(_c: &mut EngineState) -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
|
|
Loading…
Reference in a new issue