From 49562d62a605dbb1119fbdfc2bfdf8abf019444e Mon Sep 17 00:00:00 2001 From: benjamyn Date: Sat, 3 Dec 2022 21:06:41 +1100 Subject: [PATCH] syncies --- src/main.rs | 35 +++++++++++++++--------- src/spawner.rs | 73 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 67 insertions(+), 41 deletions(-) diff --git a/src/main.rs b/src/main.rs index 171d197..927665a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,6 @@ pub use gamelog::*; mod spawner; - mod inventory_system; use inventory_system::*; @@ -52,8 +51,13 @@ pub enum RunState { ShowInventory, ShowDropItem, ShowSpawnMenu, - ShowTargeting { range : i32, item : Entity}, - MainMenu { menu_selection : gui::MainMenuSelection }, + ShowTargeting { + range: i32, + item: Entity, + }, + MainMenu { + menu_selection: gui::MainMenuSelection, + }, SaveGame, } @@ -216,20 +220,24 @@ impl GameState for State { RunState::MainMenu { .. } => { let result = gui::main_menu(self, ctx); match result { - gui::MainMenuResult::NoSelection { selected } => newrunstate = RunState::MainMenu{menu_selection: selected}, - gui::MainMenuResult::Selected { selected } => { - match selected { - gui::MainMenuSelection::NewGame => newrunstate = RunState::PreRun, - gui::MainMenuSelection::LoadGame => newrunstate = RunState::PreRun, - gui::MainMenuSelection::Quit => { ::std::process::exit(0)} + gui::MainMenuResult::NoSelection { selected } => { + newrunstate = RunState::MainMenu { + menu_selection: selected, } } + gui::MainMenuResult::Selected { selected } => match selected { + gui::MainMenuSelection::NewGame => newrunstate = RunState::PreRun, + gui::MainMenuSelection::LoadGame => newrunstate = RunState::PreRun, + gui::MainMenuSelection::Quit => ::std::process::exit(0), + }, } } RunState::SaveGame => { let data = serde_json::to_string(&*self.ecs.fetch::()).unwrap(); println!("{}", data); - newrunstate = RunState::MainMenu { menu_selection: gui::MainMenuSelection::LoadGame }; + newrunstate = RunState::MainMenu { + menu_selection: gui::MainMenuSelection::LoadGame, + }; } } @@ -272,6 +280,8 @@ fn main() -> rltk::BError { gs.ecs.register::(); gs.ecs.register::>(); + gs.ecs.insert(SimpleMarkerAllocator::::new()); + let map = Map::new_map_rooms_and_corridors(); let (player_x, player_y) = map.rooms[0].center(); @@ -286,11 +296,12 @@ fn main() -> rltk::BError { gs.ecs.insert(map); gs.ecs.insert(player_entity); - gs.ecs.insert(RunState::MainMenu { menu_selection: MainMenuSelection::NewGame }); + gs.ecs.insert(RunState::MainMenu { + menu_selection: MainMenuSelection::NewGame, + }); gs.ecs.insert(gamelog::GameLog { entries: vec!["Welcome to my rougelike".to_string()], }); - gs.ecs.insert(SimpleMarkerAllocator::::new()); // Main loop runner rltk::main_loop(context, gs) diff --git a/src/spawner.rs b/src/spawner.rs index 07d970b..c3b619a 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -1,11 +1,14 @@ -use crate::AreaOfEffect; +use crate::{AreaOfEffect, SerializeMe}; use super::{ - BlocksTile, CombatStats, Item, Monster, Name, Player, Position, ProvidesHealing, Rect, - Renderable, Viewshed, InflictsDamage, MAPWIDTH, Ranged, Consumable, Confusion + BlocksTile, CombatStats, Confusion, Consumable, InflictsDamage, Item, Monster, Name, Player, + Position, ProvidesHealing, Ranged, Rect, Renderable, Viewshed, MAPWIDTH, }; use rltk::{RandomNumberGenerator, RGB}; -use specs::prelude::*; +use specs::{ + prelude::*, + saveload::{MarkedBuilder, SimpleMarker}, +}; const MAX_MONSTERS: i32 = 4; const MAX_ITEMS: i32 = 2; @@ -38,6 +41,7 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity { defence: 2, power: 5, }) + .marked::>() .build() } @@ -54,16 +58,16 @@ pub fn random_monster(ecs: &mut World, x: i32, y: i32) { } fn random_item(ecs: &mut World, x: i32, y: i32) { - let roll :i32; + let roll: i32; { let mut rng = ecs.write_resource::(); roll = rng.roll_dice(1, 4); } match roll { - 1 => { health_potion(ecs, x, y) } - 2 => { fireball_scroll(ecs, x, y) } - 3 => { confusion_scroll(ecs, x, y)} - _ => { magic_missile_scroll(ecs, x, y) } + 1 => health_potion(ecs, x, y), + 2 => fireball_scroll(ecs, x, y), + 3 => confusion_scroll(ecs, x, y), + _ => magic_missile_scroll(ecs, x, y), } } @@ -99,6 +103,7 @@ fn monster(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharTy defence: 1, power: 4, }) + .marked::>() .build(); } @@ -166,23 +171,27 @@ pub fn health_potion(ecs: &mut World, x: i32, y: i32) { .with(Item {}) .with(Consumable {}) .with(ProvidesHealing { heal_amount: 8 }) + .marked::>() .build(); } pub fn magic_missile_scroll(ecs: &mut World, x: i32, y: i32) { ecs.create_entity() - .with(Position{ x, y }) - .with(Renderable{ + .with(Position { x, y }) + .with(Renderable { glyph: rltk::to_cp437(')'), fg: RGB::named(rltk::CYAN), bg: RGB::named(rltk::BLACK), - render_order: 2 + render_order: 2, }) - .with(Name{ name: "Magic Missile Scroll".to_string() }) - .with(Item{}) - .with(Consumable{}) - .with(Ranged{ range: 6 }) - .with(InflictsDamage{ damage: 8 }) + .with(Name { + name: "Magic Missile Scroll".to_string(), + }) + .with(Item {}) + .with(Consumable {}) + .with(Ranged { range: 6 }) + .with(InflictsDamage { damage: 8 }) + .marked::>() .build(); } @@ -193,30 +202,36 @@ pub fn fireball_scroll(ecs: &mut World, x: i32, y: i32) { glyph: rltk::to_cp437(')'), fg: RGB::named(rltk::ORANGE), bg: RGB::named(rltk::BLACK), - render_order: 2 + render_order: 2, + }) + .with(Name { + name: "Fireball Scroll".to_string(), }) - .with(Name{ name: "Fireball Scroll".to_string() }) .with(Item {}) .with(Consumable {}) .with(Ranged { range: 6 }) - .with(InflictsDamage{ damage: 20 }) + .with(InflictsDamage { damage: 20 }) .with(AreaOfEffect { radius: 3 }) + .marked::>() .build(); } pub fn confusion_scroll(ecs: &mut World, x: i32, y: i32) { ecs.create_entity() - .with(Position{ x, y }) - .with(Renderable{ + .with(Position { x, y }) + .with(Renderable { glyph: rltk::to_cp437(')'), fg: RGB::named(rltk::PINK), bg: RGB::named(rltk::BLACK), - render_order: 2 + render_order: 2, }) - .with(Name{ name: "Confusion Scroll".to_string() }) - .with(Item{}) - .with(Consumable{}) - .with(Ranged{ range: 6 }) - .with(Confusion{ turns: 4}) + .with(Name { + name: "Confusion Scroll".to_string(), + }) + .with(Item {}) + .with(Consumable {}) + .with(Ranged { range: 6 }) + .with(Confusion { turns: 4 }) + .marked::>() .build(); -} \ No newline at end of file +}