Add hunger: Reformat

This commit is contained in:
Kasper Juul Hermansen 2022-01-29 00:31:16 +01:00
parent 74c05f97af
commit fdeabefd75
Signed by: kjuulh
GPG Key ID: DCD9397082D97069
10 changed files with 110 additions and 51 deletions

View File

@ -127,7 +127,6 @@ impl Heals {
}
}
#[derive(Component, Debug, Clone, ConvertSaveload)]
pub struct Ranged {
pub range: i32,
@ -212,6 +211,5 @@ pub struct HungerClock {
pub duration: i32,
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct ProvidesFood {}

View File

@ -1,14 +1,14 @@
use rltk::{Point, VirtualKeyCode};
use rltk::RGB;
use rltk::Rltk;
use rltk::RGB;
use rltk::{Point, VirtualKeyCode};
use specs::prelude::*;
use crate::{HungerClock, HungerState, HungerSystem, Name};
use crate::{CombatStats, InBackpack, RunState, State, Viewshed};
use crate::{Equipped, Map};
use crate::gamelog::GameLog;
use crate::Player;
use crate::Position;
use crate::{CombatStats, InBackpack, RunState, State, Viewshed};
use crate::{Equipped, Map};
use crate::{HungerClock, HungerState, HungerSystem, Name};
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
ctx.draw_box(
@ -46,14 +46,31 @@ pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
);
match hc.state {
HungerState::WellFed => ctx.print_color(71, 42, RGB::named(rltk::GREEN), RGB::named(rltk::BLACK), "Well Fed"),
HungerState::WellFed => ctx.print_color(
71,
42,
RGB::named(rltk::GREEN),
RGB::named(rltk::BLACK),
"Well Fed",
),
HungerState::Normal => {}
HungerState::Hungry => ctx.print_color(71, 42, RGB::named(rltk::ORANGE), RGB::named(rltk::BLACK), "Hungry"),
HungerState::Starving => ctx.print_color(71, 42, RGB::named(rltk::RED), RGB::named(rltk::BLACK), "Starving"),
HungerState::Hungry => ctx.print_color(
71,
42,
RGB::named(rltk::ORANGE),
RGB::named(rltk::BLACK),
"Hungry",
),
HungerState::Starving => ctx.print_color(
71,
42,
RGB::named(rltk::RED),
RGB::named(rltk::BLACK),
"Starving",
),
}
}
let mut y = 44;
for s in log.entries.iter().rev() {
if y < 49 {

View File

@ -5,10 +5,7 @@ use crate::{CombatStats, GameLog, Heals};
pub struct HealingSystem {}
impl<'a> System<'a> for HealingSystem {
type SystemData = (
WriteStorage<'a, Heals>,
WriteStorage<'a, CombatStats>,
);
type SystemData = (WriteStorage<'a, Heals>, WriteStorage<'a, CombatStats>);
fn run(&mut self, data: Self::SystemData) {
let (mut heals, mut stats) = data;

View File

@ -1,7 +1,7 @@
use specs::prelude::*;
use crate::{GameLog, Heals, HungerClock, HungerState, ProvidesHealing, RunState, SufferDamage};
use crate::spawner::player;
use crate::{GameLog, Heals, HungerClock, HungerState, ProvidesHealing, RunState, SufferDamage};
pub struct HungerSystem {}
@ -13,19 +13,36 @@ impl<'a> System<'a> for HungerSystem {
ReadExpect<'a, RunState>,
WriteStorage<'a, SufferDamage>,
WriteExpect<'a, GameLog>,
WriteStorage<'a, Heals>
WriteStorage<'a, Heals>,
);
fn run(&mut self, data: Self::SystemData) {
let (entities, mut hunger_clock, player_entity, run_state, mut suffer_damage, mut game_log, mut heals) = data;
let (
entities,
mut hunger_clock,
player_entity,
run_state,
mut suffer_damage,
mut game_log,
mut heals,
) = data;
for (entity, mut clock) in (&entities, &mut hunger_clock).join() {
let mut proceed = false;
match *run_state {
RunState::PlayerTurn => {
if entity == *player_entity { proceed = true; } }
RunState::MonsterTurn => { if entity != *player_entity { proceed = true; } }
_ => { proceed = false; }
if entity == *player_entity {
proceed = true;
}
}
RunState::MonsterTurn => {
if entity != *player_entity {
proceed = true;
}
}
_ => {
proceed = false;
}
}
if !proceed {
@ -42,7 +59,9 @@ impl<'a> System<'a> for HungerSystem {
clock.state = HungerState::Normal;
clock.duration = 200;
if entity == *player_entity {
game_log.entries.push("You are no longer well-feed".to_string())
game_log
.entries
.push("You are no longer well-feed".to_string())
}
}
HungerState::Normal => {
@ -61,7 +80,9 @@ impl<'a> System<'a> for HungerSystem {
}
HungerState::Starving => {
if entity == *player_entity {
game_log.entries.push("Your hunger pangs are getting painful".to_string())
game_log
.entries
.push("Your hunger pangs are getting painful".to_string())
}
SufferDamage::new_damage(&mut suffer_damage, entity, 1);
}

View File

@ -1,8 +1,12 @@
use specs::prelude::*;
use crate::{AreaOfEffect, CombatStats, Confusion, Consumable, Equippable, Equipped, HungerClock, HungerState, InBackpack, InflictsDamage, Map, Name, Position, ProvidesFood, ProvidesHealing, Ranged, SufferDamage, WantsToDropItem, WantsToPickupItem, WantsToRemoveItem, WantsToUseItem};
use crate::gamelog::GameLog;
use crate::particle_system::ParticleBuilder;
use crate::{
AreaOfEffect, CombatStats, Confusion, Consumable, Equippable, Equipped, HungerClock,
HungerState, InBackpack, InflictsDamage, Map, Name, Position, ProvidesFood, ProvidesHealing,
Ranged, SufferDamage, WantsToDropItem, WantsToPickupItem, WantsToRemoveItem, WantsToUseItem,
};
pub struct ItemCollectionSystem {}
@ -94,7 +98,7 @@ impl<'a> System<'a> for ItemUseSystem {
mut particle_builder,
positions,
provides_food,
mut hunger_clocks
mut hunger_clocks,
) = data;
for (entity, use_item) in (&entities, &wants_use).join() {
@ -144,7 +148,10 @@ impl<'a> System<'a> for ItemUseSystem {
if let Some(hc) = hunger_clocks.get_mut(target) {
hc.state = HungerState::WellFed;
hc.duration = 20;
game_log.entries.push(format!("You eat the {}.", names.get(use_item.item).unwrap().name));
game_log.entries.push(format!(
"You eat the {}.",
names.get(use_item.item).unwrap().name
));
}
}

View File

@ -25,6 +25,8 @@ mod components;
mod damage_system;
mod gamelog;
mod gui;
mod healing_system;
mod hunger_system;
mod inventory_system;
mod map;
mod map_indexing_system;
@ -37,8 +39,6 @@ mod rect;
mod save_load_system;
mod spawner;
mod visibility_system;
mod hunger_system;
mod healing_system;
#[derive(PartialEq, Copy, Clone)]
pub enum RunState {

View File

@ -1,9 +1,9 @@
use specs::prelude::*;
use crate::{DefenseBonus, Equipped, HungerClock, HungerState, MeleePowerBonus, Position};
use crate::components::{CombatStats, Name, SufferDamage, WantsToMelee};
use crate::gamelog::GameLog;
use crate::particle_system::ParticleBuilder;
use crate::{DefenseBonus, Equipped, HungerClock, HungerState, MeleePowerBonus, Position};
pub struct MeleeCombatSystem {}
@ -21,7 +21,7 @@ impl<'a> System<'a> for MeleeCombatSystem {
ReadStorage<'a, Equipped>,
WriteExpect<'a, ParticleBuilder>,
ReadStorage<'a, Position>,
ReadStorage<'a, HungerClock>
ReadStorage<'a, HungerClock>,
);
fn run(&mut self, data: Self::SystemData) {
@ -37,7 +37,7 @@ impl<'a> System<'a> for MeleeCombatSystem {
equipped,
mut particle_builder,
positions,
hunger_clock
hunger_clock,
) = data;
for (entity, wants_melee, name, stats) in

View File

@ -1,12 +1,12 @@
use rltk::Point;
use specs::prelude::*;
use crate::particle_system::ParticleBuilder;
use crate::{
components::{Monster, Position, Viewshed, WantsToMelee},
Confusion,
map::Map, RunState,
map::Map,
Confusion, RunState,
};
use crate::particle_system::ParticleBuilder;
pub struct MonsterAI {}
@ -23,7 +23,7 @@ impl<'a> System<'a> for MonsterAI {
WriteStorage<'a, Position>,
WriteStorage<'a, WantsToMelee>,
WriteStorage<'a, Confusion>,
WriteExpect<'a, ParticleBuilder>
WriteExpect<'a, ParticleBuilder>,
);
fn run(&mut self, data: Self::SystemData) {
@ -38,7 +38,7 @@ impl<'a> System<'a> for MonsterAI {
mut position,
mut wants_to_melee,
mut confused,
mut particle_builder
mut particle_builder,
) = data;
if *runstate != RunState::MonsterTurn {
@ -57,7 +57,14 @@ impl<'a> System<'a> for MonsterAI {
}
can_act = false;
particle_builder.request(pos.x, pos.y, rltk::RGB::named(rltk::MAGENTA), rltk::RGB::named(rltk::BLACK), rltk::to_cp437('?'), 200.);
particle_builder.request(
pos.x,
pos.y,
rltk::RGB::named(rltk::MAGENTA),
rltk::RGB::named(rltk::BLACK),
rltk::to_cp437('?'),
200.,
);
}
if !can_act {

View File

@ -3,9 +3,13 @@ use std::cmp::{max, min};
use rltk::{Point, Rltk, VirtualKeyCode};
use specs::prelude::*;
use crate::{components::{CombatStats, Player, Position, Viewshed, WantsToMelee}, HungerClock, HungerState, Item, map::Map, Monster, RunState, State, TileType, WantsToPickupItem};
use crate::gamelog::GameLog;
use crate::spawner::player;
use crate::{
components::{CombatStats, Player, Position, Viewshed, WantsToMelee},
map::Map,
HungerClock, HungerState, Item, Monster, RunState, State, TileType, WantsToPickupItem,
};
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
let mut positions = ecs.write_storage::<Position>();
@ -139,8 +143,12 @@ fn skip_turn(ecs: &mut World) -> RunState {
let mut heal_bonus = 0;
if let Some(hc) = hunger_clock.get(*player_entity) {
match hc.state {
HungerState::Hungry | HungerState::Starving => {can_heal = false;}
HungerState::WellFed => {heal_bonus = 1;}
HungerState::Hungry | HungerState::Starving => {
can_heal = false;
}
HungerState::WellFed => {
heal_bonus = 1;
}
_ => {}
}
}

View File

@ -4,9 +4,14 @@ use rltk::{FontCharType, RandomNumberGenerator, RGB};
use specs::prelude::*;
use specs::saveload::{MarkedBuilder, SimpleMarker};
use crate::{AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, DefenseBonus, EquipmentSlot, Equippable, HungerClock, HungerState, InflictsDamage, Item, MAP_WIDTH, MAX_MONSTER, MeleePowerBonus, Monster, Name, Player, Position, ProvidesFood, ProvidesHealing, Ranged, Renderable, SerializeMe, Viewshed};
use crate::random_table::RandomTable;
use crate::rect::Rect;
use crate::{
AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, DefenseBonus, EquipmentSlot,
Equippable, HungerClock, HungerState, InflictsDamage, Item, MeleePowerBonus, Monster, Name,
Player, Position, ProvidesFood, ProvidesHealing, Ranged, Renderable, SerializeMe, Viewshed,
MAP_WIDTH, MAX_MONSTER,
};
pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
ecs.create_entity()
@ -400,7 +405,6 @@ fn sabatons(ecs: &mut World, x: i32, y: i32) {
.build();
}
fn rations(ecs: &mut World, x: i32, y: i32) {
ecs.create_entity()
.with(Position { x, y })