Skip to content

Commit

Permalink
add player spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
berlingoqc committed May 11, 2024
1 parent e4d2270 commit 452e70a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 35 deletions.
3 changes: 2 additions & 1 deletion crates/map/src/game/entity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod door;
pub mod window;
pub mod room;
pub mod room;
pub mod player_spawn;
5 changes: 5 additions & 0 deletions crates/map/src/game/entity/player_spawn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use bevy::prelude::*;

#[derive(Default, Component, Reflect)]
pub struct PlayerSpawnComponent {
}
58 changes: 29 additions & 29 deletions crates/map/src/generation/imp/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,35 +190,6 @@ impl BasicMapGeneration {
}



fn get_entities(
&mut self,
) -> Vec<(EntityLocation, crate::generation::entity::door::DoorConfig)> {
// get all my level , get all the doors in each level
self.map
.rooms
.iter()
.flat_map(|x| {
x.entity_locations
.doors
.iter()
.map(|y| (
EntityLocation{
position: y.position,
size: y.size,
level_iid: x.level_iid.clone(),
},
DoorConfig {
// TODO: create the algo to fix the price of door
cost: 1000,
// TODO: create the algo to found the electrify rule
electrify: true,
})
)
.collect::<Vec<(EntityLocation, crate::generation::entity::door::DoorConfig)>>()
})
.collect()
}
}

impl IMapGeneration for BasicMapGeneration {
Expand Down Expand Up @@ -318,4 +289,33 @@ impl IMapGeneration for BasicMapGeneration {
}).collect()
}

fn get_player_spawn(&mut self) -> Vec<(EntityLocation, ())> {
self.map
.rooms
.iter()
.filter(|x| {
let property = x.properties.get(LEVEL_PROPERTIES_SPAWN_NAME);
if let Some(property) = property {
if let Value::Bool(b) = property {
return *b;
}
}
return false;
}).flat_map(|roor| {
roor.entity_locations
.player_spawns
.iter()
.map(|y| (
EntityLocation{
position: y.position,
size: y.size,
level_iid: roor.level_iid.clone()
},
()
))
.collect::<Vec<(EntityLocation, ())>>()

}).collect()
}

}
5 changes: 5 additions & 0 deletions crates/map/src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ trait IMapGeneration {

fn get_doors(&mut self) -> Vec<(EntityLocation, DoorConfig)>;
fn get_windows(&mut self) -> Vec<(EntityLocation, WindowConfig)>;
fn get_player_spawn(&mut self) -> Vec<(EntityLocation, ())>;
}

pub trait IMapGenerator {
Expand All @@ -37,6 +38,7 @@ pub trait IMapGenerator {
);
fn add_doors(&mut self, doors: &Vec<(EntityLocation, DoorConfig)>);
fn add_windows(&mut self, windows: &Vec<(EntityLocation, WindowConfig)>);
fn add_player_spawns(&mut self, player_spawns: &Vec<(EntityLocation, ())>);
}

pub fn map_generation(
Expand Down Expand Up @@ -67,5 +69,8 @@ pub fn map_generation(
let windows = generator.get_windows();
map_generator.add_windows(&windows);

let player_spawns = generator.get_player_spawn();
map_generator.add_player_spawns(&player_spawns);

Ok(())
}
3 changes: 2 additions & 1 deletion crates/map/src/ldtk/game/entity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod door;
pub mod window;
pub mod window;
pub mod player_spawn;
12 changes: 12 additions & 0 deletions crates/map/src/ldtk/game/entity/player_spawn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use bevy::prelude::*; use bevy_ecs_ldtk::prelude::*;

use crate::game::entity::player_spawn::PlayerSpawnComponent;


#[derive(Default, Bundle, LdtkEntity)]
pub struct PlayerSpawnBundle {
player_spawn: PlayerSpawnComponent,
#[sprite_sheet_bundle]
sprite_sheet: SpriteSheetBundle

}
6 changes: 6 additions & 0 deletions crates/map/src/ldtk/generation/to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,11 @@ impl IMapGenerator for GeneratedMap {
self.add_entity_to_level(location, map_const::ENTITY_WINDOW_LOCATION,vec![]);
}
}

fn add_player_spawns(&mut self, player_spawns: &Vec<(EntityLocation, ())>) {
for (location, _) in player_spawns.iter() {
self.add_entity_to_level(location, map_const::ENTITY_PLAYER_SPAWN_LOCATION, vec![]);
}
}
}

4 changes: 2 additions & 2 deletions crates/map/src/ldtk/map_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub const LAYER_CONNECTION: &str = "LevelConnection";
pub const LAYER_ENTITY: &str = "Entities";

pub const ENTITY_DOOR_LOCATION: &str = "Door";
pub const ENTITY_PLAYER_SPAWN_LOCATION: &str = "PlayerSpawnLocation";
pub const ENTITY_ZOMBIE_SPAWN_LOCATION: &str = "ZombieSpawnLocation";
pub const ENTITY_PLAYER_SPAWN_LOCATION: &str = "PlayerSpawn";
pub const ENTITY_ZOMBIE_SPAWN_LOCATION: &str = "ZombieSpawn";
pub const ENTITY_CRATE_LOCATION: &str = "CrateLocation";
pub const ENTITY_WEAPON_LOCATION: &str = "WeaponLocation";
pub const ENTITY_WINDOW_LOCATION: &str = "Window";
Expand Down
6 changes: 4 additions & 2 deletions crates/map/src/ldtk/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bevy::prelude::*;
use bevy_ecs_ldtk::prelude::*;

use crate::game::entity::{door::DoorComponent, room::RoomComponent, window::WindowComponent};
use crate::game::entity::{door::DoorComponent, player_spawn::PlayerSpawnComponent, room::RoomComponent, window::WindowComponent};

use super::{game::{entity::{door::DoorBundle, window::WindowBundle}, system::add_level_components::add_room_component_to_ldtk_level}, map_const};
use super::{game::{entity::{door::DoorBundle, player_spawn::PlayerSpawnBundle, window::WindowBundle}, system::add_level_components::add_room_component_to_ldtk_level}, map_const};


use bevy_inspector_egui::quick::WorldInspectorPlugin;
Expand All @@ -17,6 +17,7 @@ impl Plugin for EntityPlugin {
Update,
add_room_component_to_ldtk_level.run_if(on_event::<LevelEvent>()),
)
.register_ldtk_entity::<PlayerSpawnBundle>(map_const::ENTITY_PLAYER_SPAWN_LOCATION)
.register_ldtk_entity::<WindowBundle>(map_const::ENTITY_WINDOW_LOCATION)
.register_ldtk_entity::<DoorBundle>(map_const::ENTITY_DOOR_LOCATION);
}
Expand Down Expand Up @@ -50,6 +51,7 @@ impl Plugin for MyWorldInspectorPlugin {
.register_type::<RoomComponent>()
.register_type::<DoorComponent>()
.register_type::<WindowComponent>()
.register_type::<PlayerSpawnComponent>()
.add_plugins(WorldInspectorPlugin::new());
}
}

0 comments on commit 452e70a

Please sign in to comment.