summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-23 22:12:21 +0100
committertroido <troido@protonmail.com>2020-02-23 22:12:21 +0100
commitf422238d7aaae0ff1b2d560a71a99b0a881ad338 (patch)
treec335cb8eee47d34953f7d4cd96c195958d223202
parent613952f918b8d72a3e397dc46be309b2320c6ad0 (diff)
players can get damage from traps
-rw-r--r--content/maps/room.json5
-rw-r--r--src/components/mod.rs29
-rw-r--r--src/componentwrapper.rs4
-rw-r--r--src/defaultencyclopedia.rs5
-rw-r--r--src/playerid.rs2
-rw-r--r--src/playerstate.rs4
-rw-r--r--src/room.rs8
-rw-r--r--src/systems/attacking.rs2
-rw-r--r--src/systems/controlinput.rs10
-rw-r--r--src/systems/create.rs11
-rw-r--r--src/systems/mod.rs4
-rw-r--r--src/systems/moving.rs22
-rw-r--r--src/systems/trapping.rs46
13 files changed, 104 insertions, 48 deletions
diff --git a/content/maps/room.json b/content/maps/room.json
index 4bfd923..c410bc1 100644
--- a/content/maps/room.json
+++ b/content/maps/room.json
@@ -13,8 +13,8 @@
" ,,,,.,,,,,,,,,,,~~~,,,,,,,,,,,,,,,,,,,,X",
"X,,,,,.,,,,,,,,,,,~~~~,,,,,,T,,,,,,,,,,,,X",
"X,,,,,.,,,,,,,,,,,,~~~,,,,,,,,,,,,,,,,,,,X",
- "X,,,,,.,,,,,,,,,,,,~~~,,,,,T,,,,######,,,X",
- "X,,,,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X",
+ "X,^,,,.,,,,,,,,,,,,~~~,,,,,T,,,,######,,,X",
+ "X,^,,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X",
"X,,,,,.............bbb...........++++#,,,X",
"X,**,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X",
"X,*,*,.,,,,,,,,,,,,~~~,,,T,,,T,,#++++#,,,X",
@@ -41,6 +41,7 @@
"*": ["grass", "pebble"],
"o": ["grass", "stone"],
"%": {"type": "portal", "kwargs": {"destination": "broom", "dest_pos": "northentry"}},
+ "^": ["grass", "spiketrap"],
" ": []
}
}
diff --git a/src/components/mod.rs b/src/components/mod.rs
index 8941abb..0c258dc 100644
--- a/src/components/mod.rs
+++ b/src/components/mod.rs
@@ -8,6 +8,7 @@ use specs::{
VecStorage,
HashMapStorage,
FlaggedStorage,
+ NullStorage,
Component
};
@@ -49,24 +50,28 @@ impl Component for Visible {
#[derive(Component, Debug)]
pub struct Controller(pub Control);
-#[derive(Component, Debug, Clone)]
+#[derive(Default, Component, Debug, Clone)]
+#[storage(NullStorage)]
pub struct Blocking;
-#[derive(Component, Debug, Clone)]
+#[derive(Default, Component, Debug, Clone)]
+#[storage(NullStorage)]
pub struct Floor;
-#[derive(Component, Debug, Clone)]
+#[derive(Default, Component, Debug, Clone)]
+#[storage(NullStorage)]
pub struct New;
-#[derive(Component, Debug, Clone)]
+#[derive(Default, Component, Debug, Clone)]
+#[storage(NullStorage)]
pub struct Removed;
-#[derive(Component, Debug, Clone)]
+#[derive(Default, Component, Debug, Clone)]
pub struct Moved {
pub from: Pos
}
-#[derive(Component, Debug, Clone)]
+#[derive(Default, Component, Debug, Clone)]
#[storage(HashMapStorage)]
pub struct Player {
pub id: PlayerId
@@ -115,3 +120,15 @@ pub struct RoomExit {
pub struct Attacked {
pub attacks: Vec<Attack>
}
+
+
+#[derive(Default, Component, Debug, Clone)]
+#[storage(NullStorage)]
+pub struct Entered;
+
+#[derive(Component, Debug, Clone)]
+#[storage(HashMapStorage)]
+pub struct Trap{
+ pub attack: Attack
+}
+
diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs
index 8ab991a..1a61728 100644
--- a/src/componentwrapper.rs
+++ b/src/componentwrapper.rs
@@ -7,7 +7,8 @@ use crate::{
RoomId,
Sprite,
playerstate::RoomPos,
- components::{Visible, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit},
+ attack::Attack,
+ components::{Visible, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit, Trap},
parameter::{Parameter, ParameterType}
};
@@ -113,6 +114,7 @@ components!(
}
}
};
+ Trap (damage: Int) {Trap{attack: Attack::new(damage)}};
);
diff --git a/src/defaultencyclopedia.rs b/src/defaultencyclopedia.rs
index 75234b8..ae2e4eb 100644
--- a/src/defaultencyclopedia.rs
+++ b/src/defaultencyclopedia.rs
@@ -127,5 +127,10 @@ pub fn default_encyclopedia() -> Encyclopedia {
"sprite": "wall",
"height": 2
},
+ "spiketrap": {
+ "components": [["Trap", {"damage": ["int", 8]}]],
+ "sprite": "spikes",
+ "height": 0.8
+ }
})).unwrap()
}
diff --git a/src/playerid.rs b/src/playerid.rs
index dcb1f40..aeaf814 100644
--- a/src/playerid.rs
+++ b/src/playerid.rs
@@ -1,5 +1,5 @@
-#[derive(Debug, PartialEq, Eq, Clone, Hash)]
+#[derive(Debug, Default, PartialEq, Eq, Clone, Hash)]
pub struct PlayerId {
pub name: String
}
diff --git a/src/playerstate.rs b/src/playerstate.rs
index 694ce60..6e41623 100644
--- a/src/playerstate.rs
+++ b/src/playerstate.rs
@@ -41,8 +41,8 @@ impl PlayerState {
pos: RoomPos::Unknown,
inventory: Vec::new(),
inventory_capacity: 10,
- health: 9,
- maximum_health: 10
+ health: 25,
+ maximum_health: 50
}
}
diff --git a/src/room.rs b/src/room.rs
index a4046b2..916d76d 100644
--- a/src/room.rs
+++ b/src/room.rs
@@ -33,7 +33,8 @@ use crate::{
Take,
Migrate,
Use,
- Attacking
+ Attacking,
+ Trapping
},
components::{
Position,
@@ -81,8 +82,9 @@ impl <'a, 'b>Room<'a, 'b> {
.with(Take, "take", &["controlinput"])
.with(Use, "use", &["controlinput"])
.with(Move, "move", &["registernew", "controlinput"])
- .with(Attacking, "attacking", &["use"])
- .with(View::default(), "view", &["move"])
+ .with(Trapping, "trapping", &["move"])
+ .with(Attacking, "attacking", &["use", "trapping"])
+ .with(View::default(), "view", &["move", "attacking"])
.with(Migrate, "migrate", &["view"])
.with(Create, "create", &["view", "controlinput"])
.with(Remove, "remove", &["view", "move"])
diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs
index 0fb5cf7..6535a75 100644
--- a/src/systems/attacking.rs
+++ b/src/systems/attacking.rs
@@ -18,13 +18,13 @@ impl <'a> System<'a> for Attacking {
WriteStorage<'a, Health>
);
fn run(&mut self, (mut victims, mut healths): Self::SystemData) {
-
for (health, attacked) in (&mut healths, &mut victims).join() {
for attack in attacked.attacks.drain(..) {
health.health -= attack.damage;
}
health.health = util::clamp(health.health, 0, health.maxhealth);
}
+ victims.clear();
}
}
diff --git a/src/systems/controlinput.rs b/src/systems/controlinput.rs
index bd8b23c..71373f5 100644
--- a/src/systems/controlinput.rs
+++ b/src/systems/controlinput.rs
@@ -21,15 +21,7 @@ impl <'a> System<'a> for ControlInput {
ReadStorage<'a, Player>
);
fn run(&mut self, (entities, mut input, mut controllers, players): Self::SystemData) {
- {
- let mut ents = Vec::new();
- for (ent, _controller) in (&*entities, &controllers).join() {
- ents.push(ent);
- }
- for ent in ents {
- controllers.remove(ent);
- }
- }
+ controllers.clear();
for (player, entity) in (&players, &entities).join() {
if let Some(control) = input.actions.get(&player.id){
diff --git a/src/systems/create.rs b/src/systems/create.rs
index 8463f9e..c995bcb 100644
--- a/src/systems/create.rs
+++ b/src/systems/create.rs
@@ -4,7 +4,6 @@ use specs::{
Write,
WriteStorage,
System,
- Join,
Entities,
LazyUpdate,
Builder
@@ -25,15 +24,7 @@ impl <'a> System<'a> for Create {
);
fn run(&mut self, (entities, mut new_entities, updater, mut new): Self::SystemData) {
- {
- let mut ents = Vec::new();
- for (ent, _new) in (&entities, &new).join() {
- ents.push(ent);
- }
- for ent in ents {
- new.remove(ent);
- }
- }
+ new.clear();
for (pos, preentity) in &new_entities.to_build {
let mut builder = updater.create_entity(&entities);
for comp in preentity {
diff --git a/src/systems/mod.rs b/src/systems/mod.rs
index f93088d..7864b1c 100644
--- a/src/systems/mod.rs
+++ b/src/systems/mod.rs
@@ -9,6 +9,7 @@ mod take;
mod migrate;
mod useitem;
mod attacking;
+mod trapping;
pub use self::{
controlinput::ControlInput,
@@ -20,5 +21,6 @@ pub use self::{
take::Take,
migrate::Migrate,
useitem::Use,
- attacking::Attacking
+ attacking::Attacking,
+ trapping::Trapping
};
diff --git a/src/systems/moving.rs b/src/systems/moving.rs
index ccd9455..daa1eb4 100644
--- a/src/systems/moving.rs
+++ b/src/systems/moving.rs
@@ -18,7 +18,8 @@ use crate::{
Blocking,
Position,
Floor,
- Moved
+ Moved,
+ Entered
},
controls::{
Control
@@ -40,19 +41,13 @@ impl <'a> System<'a> for Move {
ReadStorage<'a, Blocking>,
Write<'a, Ground>,
ReadStorage<'a, Floor>,
- WriteStorage<'a, Moved>
+ WriteStorage<'a, Moved>,
+ WriteStorage<'a, Entered>
);
- fn run(&mut self, (entities, controllers, mut positions, size, blocking, mut ground, floor, mut moved): Self::SystemData) {
- {
- let mut ents = Vec::new();
- for (ent, _moved) in (&*entities, &moved).join() {
- ents.push(ent);
- }
- for ent in ents {
- moved.remove(ent);
- }
- }
+ fn run(&mut self, (entities, controllers, mut positions, size, blocking, mut ground, floor, mut moved, mut entered): Self::SystemData) {
+ moved.clear();
+ entered.clear();
for (ent, controller, mut pos) in (&entities, &controllers, &mut positions.restrict_mut()).join(){
match &controller.0 {
Control::Move(direction) => {
@@ -74,6 +69,9 @@ impl <'a> System<'a> for Move {
ground.cells.get_mut(&pos_mut.pos).unwrap().remove(&ent);
pos_mut.pos = newpos;
ground.cells.entry(newpos).or_insert_with(HashSet::new).insert(ent);
+ for ent in ground.cells.get(&newpos).unwrap() {
+ let _ = entered.insert(*ent, Entered);
+ }
}
}
_ => {}
diff --git a/src/systems/trapping.rs b/src/systems/trapping.rs
new file mode 100644
index 0000000..144cd94
--- /dev/null
+++ b/src/systems/trapping.rs
@@ -0,0 +1,46 @@
+
+use specs::{
+ WriteStorage,
+ ReadStorage,
+ Entities,
+ Read,
+ System,
+ Join
+};
+
+use crate::{
+ components::{Health, Attacked, Moved, Entered, Trap, Position},
+ resources::Ground
+};
+
+
+pub struct Trapping;
+impl <'a> System<'a> for Trapping {
+ type SystemData = (
+ Entities<'a>,
+ WriteStorage<'a, Attacked>,
+ ReadStorage<'a, Health>,
+ ReadStorage<'a, Moved>,
+ ReadStorage<'a, Entered>,
+ ReadStorage<'a, Trap>,
+ ReadStorage<'a, Position>,
+ Read<'a, Ground>
+ );
+ fn run(&mut self, (entities, mut victims, healths, moves, entereds, traps, positions, ground): Self::SystemData) {
+
+ for (entity, _entered, trap, position) in (&entities, &entereds, &traps, &positions).join() {
+ for ent in ground.cells.get(&position.pos).unwrap(){
+ if ent != &entity && moves.contains(*ent) && healths.contains(*ent) {
+ victims
+ .entry(*ent)
+ .unwrap()
+ .or_insert_with(Attacked::default)
+ .attacks
+ .push(trap.attack.clone());
+
+ }
+ }
+ }
+ }
+}
+