From f4331041e5d906f95063f317852f32f19e6cdf9c Mon Sep 17 00:00:00 2001 From: troido Date: Sat, 22 Feb 2020 22:00:32 +0100 Subject: items now have an action enum --- src/components.rs | 100 ------------------------------------------------ src/components/item.rs | 18 +++++++++ src/components/mod.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++++ src/componentwrapper.rs | 4 +- src/main.rs | 2 + src/playerstate.rs | 17 +++++--- src/resources.rs | 92 -------------------------------------------- src/resources/mod.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++ src/room.rs | 2 +- 9 files changed, 223 insertions(+), 200 deletions(-) delete mode 100644 src/components.rs create mode 100644 src/components/item.rs create mode 100644 src/components/mod.rs delete mode 100644 src/resources.rs create mode 100644 src/resources/mod.rs diff --git a/src/components.rs b/src/components.rs deleted file mode 100644 index 9d387f1..0000000 --- a/src/components.rs +++ /dev/null @@ -1,100 +0,0 @@ - -use specs::{ - DenseVecStorage, - VecStorage, - HashMapStorage, - FlaggedStorage, - Component -}; - -use crate::{Pos, PlayerId, RoomId, Sprite}; -use crate::controls::Control; -use crate::template::Template; - - -#[derive(Debug, Clone)] -pub struct Position{ - pub pos: Pos -} -impl Position { - pub fn new(pos: Pos) -> Position { - Position{pos} - } -} - -impl Component for Position { - type Storage = FlaggedStorage>; -} - -#[derive(Debug, Clone)] -pub struct Visible { - pub sprite: Sprite, - pub height: f64, - pub name: String -} -impl Component for Visible { - type Storage = FlaggedStorage>; -} - -#[derive(Component, Debug)] -pub struct Controller(pub Control); - -#[derive(Component, Debug, Clone)] -pub struct Blocking; - -#[derive(Component, Debug, Clone)] -pub struct Floor; - -#[derive(Component, Debug, Clone)] -pub struct New; - -#[derive(Component, Debug, Clone)] -pub struct Removed; - -#[derive(Component, Debug, Clone)] -pub struct Moved { - pub from: Pos -} - -#[derive(Component, Debug, Clone)] -#[storage(HashMapStorage)] -pub struct Player { - pub id: PlayerId -} -impl Player { - pub fn new(id: PlayerId) -> Self { - Self{id} - } -} - -#[derive(Debug, Clone, Default)] -pub struct Inventory { - pub items: Vec, - pub capacity: usize -} -impl Component for Inventory { - type Storage = FlaggedStorage>; -} - - -#[derive(Component, Debug, Clone)] -pub struct Item { - pub ent: Template, - pub name: String -} - -#[derive(Component, Debug, Clone)] -pub struct Health { - pub health: i64, - pub maxhealth: i64 -} - -#[derive(Component, Debug, Clone)] -pub struct Serialise { - pub template: Template -} - -#[derive(Component, Debug, Clone)] -pub struct RoomExit { - pub destination: RoomId -} 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/mod.rs b/src/components/mod.rs new file mode 100644 index 0000000..a0ccde4 --- /dev/null +++ b/src/components/mod.rs @@ -0,0 +1,96 @@ + +pub mod item; + +pub use item::Item; + +use specs::{ + DenseVecStorage, + VecStorage, + HashMapStorage, + FlaggedStorage, + Component +}; + +use crate::{Pos, PlayerId, RoomId, Sprite}; +use crate::controls::Control; +use crate::template::Template; + +#[derive(Debug, Clone)] +pub struct Position{ + pub pos: Pos +} +impl Position { + pub fn new(pos: Pos) -> Position { + Position{pos} + } +} + +impl Component for Position { + type Storage = FlaggedStorage>; +} + +#[derive(Debug, Clone)] +pub struct Visible { + pub sprite: Sprite, + pub height: f64, + pub name: String +} +impl Component for Visible { + type Storage = FlaggedStorage>; +} + +#[derive(Component, Debug)] +pub struct Controller(pub Control); + +#[derive(Component, Debug, Clone)] +pub struct Blocking; + +#[derive(Component, Debug, Clone)] +pub struct Floor; + +#[derive(Component, Debug, Clone)] +pub struct New; + +#[derive(Component, Debug, Clone)] +pub struct Removed; + +#[derive(Component, Debug, Clone)] +pub struct Moved { + pub from: Pos +} + +#[derive(Component, Debug, Clone)] +#[storage(HashMapStorage)] +pub struct Player { + pub id: PlayerId +} +impl Player { + pub fn new(id: PlayerId) -> Self { + Self{id} + } +} + +#[derive(Debug, Clone, Default)] +pub struct Inventory { + pub items: Vec, + pub capacity: usize +} +impl Component for Inventory { + type Storage = FlaggedStorage>; +} + +#[derive(Component, Debug, Clone)] +pub struct Health { + pub health: i64, + pub maxhealth: i64 +} + +#[derive(Component, Debug, Clone)] +pub struct Serialise { + pub template: Template +} + +#[derive(Component, Debug, Clone)] +pub struct RoomExit { + pub destination: RoomId +} 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.rs deleted file mode 100644 index 0e9a1e3..0000000 --- a/src/resources.rs +++ /dev/null @@ -1,92 +0,0 @@ - -use std::collections::{HashMap, HashSet}; -use specs::{Entity, ReadStorage, Component}; - -use crate::{ - pos::Pos, - controls::Control, - worldmessages::WorldMessage, - componentwrapper::PreEntity, - encyclopedia::Encyclopedia, - PlayerId, - RoomId, - util::Result, - template::Template, - components::Visible -}; - - -#[derive(Default)] -pub struct Input { - pub actions: HashMap -} - -#[derive(Default)] -pub struct Output { - pub output: HashMap -} - -#[derive(Default)] -pub struct Size { - pub width: i64, - pub height: i64 -} - -#[derive(Default)] -pub struct Spawn { - pub pos: Pos -} - -#[derive(Default)] -pub struct Ground { - pub cells: HashMap> -} -impl Ground { - pub fn components_on<'a, C: Component>(&self, pos: Pos, component_type: &'a ReadStorage) -> Vec<&'a C> { - self.cells.get(&pos).unwrap_or(&HashSet::new()).iter().filter_map(|e| component_type.get(*e)).collect() - } - - pub fn by_height(&self, pos: &Pos, visibles: &ReadStorage, ignore: &Entity) -> Vec { - let mut entities: Vec = self.cells - .get(&pos).unwrap_or(&HashSet::new()) - .iter() - .cloned() - .filter(|e| e != ignore && visibles.contains(*e)) - .collect(); - entities.sort_by(|a, b| - visibles.get(*b).unwrap().height.partial_cmp(&visibles.get(*a).unwrap().height).unwrap() - ); - entities - } -} - - -#[derive(Default)] -pub struct NewEntities { - pub to_build: Vec<(Pos, PreEntity)>, - pub encyclopedia: Encyclopedia -} -impl NewEntities { - pub fn new(encyclopedia: Encyclopedia) -> Self { - Self{ - to_build: Vec::new(), - encyclopedia - } - } - pub fn create(&mut self, pos: Pos, template: Template) -> Result<()> { - let components = self.encyclopedia.construct(&template)?; - self.to_build.push((pos, components)); - Ok(()) - } -} - -#[derive(Default)] -pub struct Players { - pub entities: HashMap -} - -#[derive(Default)] -pub struct Emigration { - pub emigrants: Vec<(PlayerId, RoomId)> -} - diff --git a/src/resources/mod.rs b/src/resources/mod.rs new file mode 100644 index 0000000..0e9a1e3 --- /dev/null +++ b/src/resources/mod.rs @@ -0,0 +1,92 @@ + +use std::collections::{HashMap, HashSet}; +use specs::{Entity, ReadStorage, Component}; + +use crate::{ + pos::Pos, + controls::Control, + worldmessages::WorldMessage, + componentwrapper::PreEntity, + encyclopedia::Encyclopedia, + PlayerId, + RoomId, + util::Result, + template::Template, + components::Visible +}; + + +#[derive(Default)] +pub struct Input { + pub actions: HashMap +} + +#[derive(Default)] +pub struct Output { + pub output: HashMap +} + +#[derive(Default)] +pub struct Size { + pub width: i64, + pub height: i64 +} + +#[derive(Default)] +pub struct Spawn { + pub pos: Pos +} + +#[derive(Default)] +pub struct Ground { + pub cells: HashMap> +} +impl Ground { + pub fn components_on<'a, C: Component>(&self, pos: Pos, component_type: &'a ReadStorage) -> Vec<&'a C> { + self.cells.get(&pos).unwrap_or(&HashSet::new()).iter().filter_map(|e| component_type.get(*e)).collect() + } + + pub fn by_height(&self, pos: &Pos, visibles: &ReadStorage, ignore: &Entity) -> Vec { + let mut entities: Vec = self.cells + .get(&pos).unwrap_or(&HashSet::new()) + .iter() + .cloned() + .filter(|e| e != ignore && visibles.contains(*e)) + .collect(); + entities.sort_by(|a, b| + visibles.get(*b).unwrap().height.partial_cmp(&visibles.get(*a).unwrap().height).unwrap() + ); + entities + } +} + + +#[derive(Default)] +pub struct NewEntities { + pub to_build: Vec<(Pos, PreEntity)>, + pub encyclopedia: Encyclopedia +} +impl NewEntities { + pub fn new(encyclopedia: Encyclopedia) -> Self { + Self{ + to_build: Vec::new(), + encyclopedia + } + } + pub fn create(&mut self, pos: Pos, template: Template) -> Result<()> { + let components = self.encyclopedia.construct(&template)?; + self.to_build.push((pos, components)); + Ok(()) + } +} + +#[derive(Default)] +pub struct Players { + pub entities: HashMap +} + +#[derive(Default)] +pub struct Emigration { + pub emigrants: Vec<(PlayerId, RoomId)> +} + 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::()); let spawn = self.world.fetch::().pos; let mut builder = self.world.create_entity(); let ent = builder.entity; -- cgit