summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-22 22:00:32 +0100
committertroido <troido@protonmail.com>2020-02-22 22:00:32 +0100
commitf4331041e5d906f95063f317852f32f19e6cdf9c (patch)
treee855402de7b2dcec81c91d97ab6a7e359689ecc5
parent2b0cc677f4092d94b31e95f3e9961ec6ed91327b (diff)
items now have an action enum
-rw-r--r--src/components/item.rs18
-rw-r--r--src/components/mod.rs (renamed from src/components.rs)12
-rw-r--r--src/componentwrapper.rs4
-rw-r--r--src/main.rs2
-rw-r--r--src/playerstate.rs17
-rw-r--r--src/resources/mod.rs (renamed from src/resources.rs)0
-rw-r--r--src/room.rs2
7 files changed, 39 insertions, 16 deletions
diff --git a/src/components/item.rs b/src/components/item.rs
new file mode 100644
index 0000000..76c5c1c
--- /dev/null
+++ b/src/components/item.rs
@@ -0,0 +1,18 @@
+
+use specs::{Component, DenseVecStorage};
+use crate::{Template};
+
+#[derive(Component, Debug, Clone)]
+pub struct Item {
+ pub ent: Template,
+ pub name: String,
+ pub action: ItemAction
+}
+
+#[derive(Debug, Clone)]
+pub enum ItemAction {
+ Eat{health: i64},
+ Build(Template),
+ None
+}
+
diff --git a/src/components.rs b/src/components/mod.rs
index 9d387f1..a0ccde4 100644
--- a/src/components.rs
+++ b/src/components/mod.rs
@@ -1,4 +1,8 @@
+pub mod item;
+
+pub use item::Item;
+
use specs::{
DenseVecStorage,
VecStorage,
@@ -11,7 +15,6 @@ use crate::{Pos, PlayerId, RoomId, Sprite};
use crate::controls::Control;
use crate::template::Template;
-
#[derive(Debug, Clone)]
pub struct Position{
pub pos: Pos
@@ -76,13 +79,6 @@ impl Component for Inventory {
type Storage = FlaggedStorage<Self, HashMapStorage<Self>>;
}
-
-#[derive(Component, Debug, Clone)]
-pub struct Item {
- pub ent: Template,
- pub name: String
-}
-
#[derive(Component, Debug, Clone)]
pub struct Health {
pub health: i64,
diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs
index 5b9673f..888903d 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};
+use crate::components::{Visible, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit, item::ItemAction};
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}};
+ Item (ent: Template, name: String) {Item{ent, name, action: ItemAction::None}};
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/main.rs b/src/main.rs
index def2ee7..dbc8728 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -37,6 +37,8 @@ pub use self::playerid::PlayerId;
pub use self::roomid::RoomId;
pub use self::util::Result;
pub use self::sprite::Sprite;
+pub use self::template::Template;
+pub use self::encyclopedia::Encyclopedia;
use self::gameserver::GameServer;
use self::server::unixserver::UnixServer;
diff --git a/src/playerstate.rs b/src/playerstate.rs
index 150b9eb..36c932c 100644
--- a/src/playerstate.rs
+++ b/src/playerstate.rs
@@ -9,7 +9,8 @@ use crate::{
components::{Visible, Player, Inventory, Health, Item},
Result,
aerr,
- Sprite
+ Sprite,
+ Encyclopedia
};
#[derive(Debug, Clone)]
@@ -85,14 +86,20 @@ impl PlayerState {
})
}
- pub fn construct(&self) -> PreEntity {
+ pub fn construct(&self, encyclopedia: &Encyclopedia) -> PreEntity {
vec![
ComponentWrapper::Visible(Visible{sprite: Sprite{name: "player".to_string()}, height: 1.0, name: self.id.name.clone()}),
ComponentWrapper::Player(Player::new(self.id.clone())),
ComponentWrapper::Inventory(Inventory{
- items: self.inventory.iter().map(
- |template| Item{ent: template.clone(), name: template.name.clone()}
- ).collect(),
+ items: self.inventory.iter().map( |template| {
+ let item_ent = encyclopedia.construct(template).unwrap();
+ for component in item_ent {
+ if let ComponentWrapper::Item(item) = component {
+ return item;
+ }
+ }
+ panic!("Item in inventory does not have item component")
+ }).collect(),
capacity: self.inventory_capacity
}),
ComponentWrapper::Health(Health{health: self.health, maxhealth: self.maximum_health})
diff --git a/src/resources.rs b/src/resources/mod.rs
index 0e9a1e3..0e9a1e3 100644
--- a/src/resources.rs
+++ b/src/resources/mod.rs
diff --git a/src/room.rs b/src/room.rs
index b4ae5e5..307e5dc 100644
--- a/src/room.rs
+++ b/src/room.rs
@@ -128,7 +128,7 @@ impl <'a, 'b>Room<'a, 'b> {
}
pub fn add_player(&mut self, state: &PlayerState){
- let pre_player = state.construct();
+ let pre_player = state.construct(&self.world.fetch::<Encyclopedia>());
let spawn = self.world.fetch::<Spawn>().pos;
let mut builder = self.world.create_entity();
let ent = builder.entity;