summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-22 23:41:51 +0100
committertroido <troido@protonmail.com>2020-02-22 23:41:51 +0100
commit522aad7889cd62e96af7c420789507ccbf5b7aaa (patch)
treebfb3c21d627552bcda6f2743854cee920722fd3b
parentf4331041e5d906f95063f317852f32f19e6cdf9c (diff)
it is now possible to use items
-rw-r--r--src/components/item.rs30
-rw-r--r--src/components/mod.rs8
-rw-r--r--src/componentwrapper.rs4
-rw-r--r--src/controls.rs4
-rw-r--r--src/defaultencyclopedia.rs19
-rw-r--r--src/parameter.rs17
-rw-r--r--src/playerstate.rs2
-rw-r--r--src/room.rs6
-rw-r--r--src/systems/mod.rs1
-rw-r--r--src/systems/useitem.rs57
10 files changed, 132 insertions, 16 deletions
diff --git a/src/components/item.rs b/src/components/item.rs
index 76c5c1c..bcc672a 100644
--- a/src/components/item.rs
+++ b/src/components/item.rs
@@ -9,10 +9,36 @@ pub struct Item {
pub action: ItemAction
}
-#[derive(Debug, Clone)]
+
+
+use serde_json::{json, Value};
+
+#[derive(Debug, Clone, PartialEq)]
pub enum ItemAction {
- Eat{health: i64},
+ Eat(i64),
Build(Template),
None
}
+use ItemAction::{Eat, Build, None};
+
+impl ItemAction {
+ pub fn to_json(&self) -> Value {
+ match self {
+ Eat(health) => json!(["eat", health]),
+ Build(template) => json!(["build", template.to_json()]),
+ None => json!(["none", null])
+ }
+ }
+
+ pub fn from_json(val: &Value) -> Option<Self> {
+ let typ = val.get(0)?;
+ let arg = val.get(1)?;
+ Some(match typ.as_str()? {
+ "eat" => Eat(arg.as_i64()?),
+ "build" => Build(Template::from_json(arg).ok()?),
+ "none" => None,
+ _ => {return Option::None}
+ })
+ }
+}
diff --git a/src/components/mod.rs b/src/components/mod.rs
index a0ccde4..459590b 100644
--- a/src/components/mod.rs
+++ b/src/components/mod.rs
@@ -84,6 +84,14 @@ pub struct Health {
pub health: i64,
pub maxhealth: i64
}
+impl Health {
+ pub fn heal(&mut self, amount: i64) {
+ self.health += amount;
+ if self.health > self.maxhealth {
+ self.health = self.maxhealth;
+ }
+ }
+}
#[derive(Component, Debug, Clone)]
pub struct Serialise {
diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs
index 888903d..fb2a80a 100644
--- a/src/componentwrapper.rs
+++ b/src/componentwrapper.rs
@@ -3,7 +3,7 @@ use std::collections::HashMap;
use specs::Builder;
use crate::{PlayerId, RoomId, Sprite};
-use crate::components::{Visible, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit, item::ItemAction};
+use crate::components::{Visible, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit};
use crate::parameter::{Parameter, ParameterType};
@@ -94,7 +94,7 @@ components!(
Blocking () {Blocking};
Floor () {Floor};
Player (name: String) {Player::new(PlayerId{name})};
- Item (ent: Template, name: String) {Item{ent, name, action: ItemAction::None}};
+ Item (ent: Template, name: String, action: Action) {Item{ent, name, action}};
Inventory (capacity: Int) {Inventory{items: Vec::new(), capacity: capacity as usize}};
Health (health: Int, maxhealth: Int) {Health{health, maxhealth}};
Serialise (template: Template) {Serialise{template}};
diff --git a/src/controls.rs b/src/controls.rs
index 36d7c77..29d45a0 100644
--- a/src/controls.rs
+++ b/src/controls.rs
@@ -43,7 +43,8 @@ impl Direction {
pub enum Control {
Move(Direction),
Take(Option<usize>),
- Drop(usize)
+ Drop(usize),
+ Use(usize)
}
@@ -57,6 +58,7 @@ impl Control {
},
"take" => Some(Control::Take(val.get(1)?.as_u64().map(|idx| idx as usize))),
"drop" => Some(Control::Drop(val.get(1)?.as_u64().unwrap_or(0) as usize)),
+ "use" => Some(Control::Use(val.get(1)?.as_u64().unwrap_or(0) as usize)),
_ => None
}
} else {None}
diff --git a/src/defaultencyclopedia.rs b/src/defaultencyclopedia.rs
index cce8931..260c5f7 100644
--- a/src/defaultencyclopedia.rs
+++ b/src/defaultencyclopedia.rs
@@ -80,14 +80,18 @@ pub fn default_encyclopedia() -> Encyclopedia {
},
"pebble": {
"components": [
- ["Item", {"ent": ["template", "pebble"], "name": ["string", "pebble"]}]
+ ["Item", {
+ "ent": ["template", "pebble"],
+ "name": ["string", "pebble"],
+ "action": ["action", ["eat", 1]]
+ }]
],
"sprite": "pebble",
"height": 0.3
},
"stone": {
"components": [
- ["Item", {"ent": ["template", "stone"], "name": ["string", "stone"]}]
+ ["Item", {"ent": ["template", "stone"], "name": ["string", "stone"], "action": ["action", ["build", "builtwall"]]}]
],
"sprite": "stone",
"height": 0.4
@@ -113,6 +117,15 @@ pub fn default_encyclopedia() -> Encyclopedia {
["RoomExit", {"destination": ["arg", "destination"]}],
"Floor"
]
- }
+ },
+ "builtwall": {
+ "arguments": [["health", "int", 100]],
+ "components": [
+ "Blocking",
+ ["Health", {"health": ["arg", "health"], "maxhealth": ["int", 100]}]
+ ],
+ "sprite": "wall",
+ "height": 2
+ },
})).unwrap()
}
diff --git a/src/parameter.rs b/src/parameter.rs
index e82915b..65dd4d0 100644
--- a/src/parameter.rs
+++ b/src/parameter.rs
@@ -1,6 +1,7 @@
use serde_json::{Value, json};
use crate::template::Template;
+use crate::components::item::ItemAction;
#[derive(Debug, PartialEq, Clone)]
pub enum Parameter {
@@ -8,7 +9,8 @@ pub enum Parameter {
Int(i64),
// Pos(Pos),
Float(f64),
- Template(Template)
+ Template(Template),
+ Action(ItemAction)
}
impl Parameter {
@@ -23,7 +25,8 @@ impl Parameter {
ParameterType::String => Some(Self::String(val.as_str()?.to_string())),
ParameterType::Int => Some(Self::Int(val.as_i64()?)),
ParameterType::Float => Some(Self::Float(val.as_f64()?)),
- ParameterType::Template => Some(Self::Template(Template::from_json(val).ok()?))
+ ParameterType::Template => Some(Self::Template(Template::from_json(val).ok()?)),
+ ParameterType::Action => Some(Self::Action(ItemAction::from_json(val)?))
}
}
@@ -32,7 +35,8 @@ impl Parameter {
Self::String(_) => ParameterType::String,
Self::Int(_) => ParameterType::Int,
Self::Float(_) => ParameterType::Float,
- Self::Template(_) => ParameterType::Template
+ Self::Template(_) => ParameterType::Template,
+ Self::Action(_) => ParameterType::Action
}
}
@@ -58,7 +62,8 @@ impl Parameter {
Self::String(s) => json!(s),
Self::Int(i) => json!(i),
Self::Float(f) => json!(f),
- Self::Template(t) => t.to_json()
+ Self::Template(t) => t.to_json(),
+ Self::Action(a) => a.to_json()
}
}
}
@@ -68,7 +73,8 @@ pub enum ParameterType {
String,
Int,
Float,
- Template
+ Template,
+ Action
}
impl ParameterType {
@@ -79,6 +85,7 @@ impl ParameterType {
"int" => Some(Self::Int),
"float" => Some(Self::Float),
"template" => Some(Self::Template),
+ "action" => Some(Self::Action),
_ => None
}
}
diff --git a/src/playerstate.rs b/src/playerstate.rs
index 36c932c..ff9565b 100644
--- a/src/playerstate.rs
+++ b/src/playerstate.rs
@@ -6,7 +6,7 @@ use crate::{
componentwrapper::{ComponentWrapper, PreEntity},
PlayerId,
RoomId,
- components::{Visible, Player, Inventory, Health, Item},
+ components::{Visible, Player, Inventory, Health},
Result,
aerr,
Sprite,
diff --git a/src/room.rs b/src/room.rs
index 307e5dc..2d48363 100644
--- a/src/room.rs
+++ b/src/room.rs
@@ -30,7 +30,8 @@ use super::systems::{
remove::Remove,
create::Create,
take::Take,
- migrate::Migrate
+ migrate::Migrate,
+ useitem::Use
};
use crate::components::{
Position,
@@ -71,6 +72,7 @@ impl <'a, 'b>Room<'a, 'b> {
.with(RegisterNew::default(), "registernew", &[])
.with(ControlInput, "controlinput", &[])
.with(Take, "take", &["controlinput"])
+ .with(Use, "use", &["controlinput"])
.with(Move, "move", &["registernew", "controlinput"])
.with(View::default(), "view", &["move"])
.with(Migrate, "migrate", &["view"])
@@ -128,7 +130,7 @@ impl <'a, 'b>Room<'a, 'b> {
}
pub fn add_player(&mut self, state: &PlayerState){
- let pre_player = state.construct(&self.world.fetch::<Encyclopedia>());
+ let pre_player = state.construct(&self.world.fetch::<NewEntities>().encyclopedia);
let spawn = self.world.fetch::<Spawn>().pos;
let mut builder = self.world.create_entity();
let ent = builder.entity;
diff --git a/src/systems/mod.rs b/src/systems/mod.rs
index 68de813..546669b 100644
--- a/src/systems/mod.rs
+++ b/src/systems/mod.rs
@@ -7,3 +7,4 @@ pub mod remove;
pub mod create;
pub mod take;
pub mod migrate;
+pub mod useitem;
diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs
new file mode 100644
index 0000000..d77239c
--- /dev/null
+++ b/src/systems/useitem.rs
@@ -0,0 +1,57 @@
+
+
+use specs::{
+ ReadStorage,
+ WriteStorage,
+ System,
+ Join,
+ Write
+};
+
+use crate::components::{
+ Controller,
+ Position,
+ Inventory,
+ Health
+};
+
+use crate::resources::{NewEntities};
+use crate::components::item::ItemAction::{None, Build, Eat};
+use crate::controls::Control;
+
+
+pub struct Use;
+impl <'a> System<'a> for Use {
+ type SystemData = (
+ ReadStorage<'a, Controller>,
+ WriteStorage<'a, Position>,
+ WriteStorage<'a, Inventory>,
+ Write<'a, NewEntities>,
+ WriteStorage<'a, Health>
+ );
+
+ fn run(&mut self, (controllers, positions, mut inventories, mut new, mut healths): Self::SystemData) {
+ for (controller, position, inventory, maybe_health) in (&controllers, &positions, &mut inventories, (&mut healths).maybe()).join(){
+ match &controller.0 {
+ Control::Use(rank) => {
+ if let Some(item) = inventory.items.get(*rank) {
+ match &item.action {
+ Build(template) => {
+ let _ = new.create(position.pos, template.clone());
+ inventory.items.remove(*rank);
+ }
+ Eat(health_diff) => {
+ if let Some(health) = maybe_health {
+ health.heal(*health_diff);
+ }
+ inventory.items.remove(*rank);
+ }
+ None => {}
+ }
+ }
+ }
+ _ => {}
+ }
+ }
+ }
+}