Speed with TURN_COST and slight randomisation to reduce predictability
This commit is contained in:
parent
dce40ac741
commit
aac6e0ad02
12 changed files with 200 additions and 145 deletions
|
|
@ -1,15 +1,16 @@
|
|||
use crate::{Energy, Position, RunState, TakingTurn};
|
||||
use crate::{Clock, Energy, Position, RunState, TakingTurn};
|
||||
use rltk::prelude::*;
|
||||
use specs::prelude::*;
|
||||
|
||||
pub struct EnergySystem {}
|
||||
|
||||
const NORMAL_SPEED: i32 = 12;
|
||||
pub const NORMAL_SPEED: i32 = 12;
|
||||
const TURN_COST: i32 = NORMAL_SPEED * 4;
|
||||
|
||||
impl<'a> System<'a> for EnergySystem {
|
||||
#[allow(clippy::type_complexity)]
|
||||
type SystemData = (
|
||||
ReadStorage<'a, Clock>,
|
||||
WriteStorage<'a, Energy>,
|
||||
ReadStorage<'a, Position>,
|
||||
WriteStorage<'a, TakingTurn>,
|
||||
|
|
@ -20,29 +21,70 @@ impl<'a> System<'a> for EnergySystem {
|
|||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (mut energies, positions, mut turns, entities, mut rng, mut runstate, player) = data;
|
||||
|
||||
if *runstate != RunState::MonsterTurn {
|
||||
let (clock, mut energies, positions, mut turns, entities, mut rng, mut runstate, player) = data;
|
||||
// If not ticking, do nothing.
|
||||
if *runstate != RunState::Ticking {
|
||||
return;
|
||||
}
|
||||
|
||||
for (_entity, _clock, energy) in (&entities, &clock, &mut energies).join() {
|
||||
energy.current += NORMAL_SPEED;
|
||||
if energy.current >= TURN_COST {
|
||||
energy.current -= TURN_COST;
|
||||
crate::gamelog::record_event("turns", 1);
|
||||
}
|
||||
}
|
||||
// Clear TakingTurn{} from every entity.
|
||||
turns.clear();
|
||||
|
||||
for (entity, energy, _pos) in (&entities, &mut energies, &positions).join() {
|
||||
// Every entity has a POTENTIAL equal to their speed.
|
||||
let mut energy_potential: i32 = energy.speed;
|
||||
if entity == *player {
|
||||
console::log(format!(
|
||||
"TICK for Player with speed {}: [current energy: {}]",
|
||||
energy.speed, energy.current
|
||||
));
|
||||
}
|
||||
// Increment current energy by NORMAL_SPEED for every
|
||||
// whole number of NORMAL_SPEEDS in their POTENTIAL.
|
||||
while energy_potential >= NORMAL_SPEED {
|
||||
energy_potential -= NORMAL_SPEED;
|
||||
energy.current += NORMAL_SPEED;
|
||||
if entity == *player {
|
||||
console::log(format!(
|
||||
"Gained 12 energy. [current: {}, potential: {}]",
|
||||
energy.current, energy_potential
|
||||
));
|
||||
}
|
||||
}
|
||||
// Roll a NORMAL_SPEED-sided die. If less than their
|
||||
// remaining POTENTIAL, increment current energy by
|
||||
// NORMAL_SPEED.
|
||||
// i.e. An entity with a speed of 3/4ths NORMAL_SPEED
|
||||
// will gain NORMAL_SPEED energy in 75% of ticks.
|
||||
if energy_potential > 0 {
|
||||
if rng.roll_dice(1, NORMAL_SPEED) <= energy_potential {
|
||||
energy.current += NORMAL_SPEED;
|
||||
if entity == *player {
|
||||
console::log(format!(
|
||||
"Rolled for remainder! Gained 12 energy. [current energy: {}]",
|
||||
energy.current
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
// TURN_COST is equal to 4 * NORMAL_SPEED. If the current entity
|
||||
// has enough energy, they take a turn and decrement their energy
|
||||
// by TURN_COST. If the current entity is the player, await input.
|
||||
if energy.current >= TURN_COST {
|
||||
turns.insert(entity, TakingTurn {}).expect("Unable to insert turn.");
|
||||
energy.current -= TURN_COST;
|
||||
if entity == *player {
|
||||
if entity == *player {
|
||||
console::log(format!(
|
||||
"Player has >=TURN_COST energy, granting a turn. [remaining energy: {}]",
|
||||
energy.current
|
||||
));
|
||||
}
|
||||
*runstate = RunState::AwaitingInput;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
mod energy_system;
|
||||
pub use energy_system::EnergySystem;
|
||||
pub use energy_system::{EnergySystem, NORMAL_SPEED};
|
||||
mod turn_status;
|
||||
pub use turn_status::TurnStatusSystem;
|
||||
|
|
|
|||
45
src/ai/turn_status.rs
Normal file
45
src/ai/turn_status.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use crate::{gamelog, Confusion, Name, ParticleBuilder, Position, RunState, TakingTurn};
|
||||
use rltk::prelude::*;
|
||||
use specs::prelude::*;
|
||||
|
||||
pub struct TurnStatusSystem {}
|
||||
|
||||
impl<'a> System<'a> for TurnStatusSystem {
|
||||
#[allow(clippy::type_complexity)]
|
||||
type SystemData = (
|
||||
WriteStorage<'a, TakingTurn>,
|
||||
WriteStorage<'a, Confusion>,
|
||||
Entities<'a>,
|
||||
ReadExpect<'a, RunState>,
|
||||
ReadStorage<'a, Name>,
|
||||
ReadStorage<'a, Position>,
|
||||
WriteExpect<'a, ParticleBuilder>,
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (mut turns, mut confusion, entities, runstate, names, positions, mut particle_builder) = data;
|
||||
if *runstate != RunState::Ticking {
|
||||
return;
|
||||
}
|
||||
let mut remove_turn: Vec<Entity> = Vec::new();
|
||||
let mut remove_confusion: Vec<Entity> = Vec::new();
|
||||
for (entity, _turn, confused, name, pos) in (&entities, &mut turns, &mut confusion, &names, &positions).join() {
|
||||
confused.turns -= 1;
|
||||
if confused.turns < 1 {
|
||||
remove_confusion.push(entity);
|
||||
gamelog::Logger::new().npc_name(&name.name).colour(WHITE).append("snaps out of it.").log();
|
||||
particle_builder.request(pos.x, pos.y, RGB::named(LIGHT_BLUE), RGB::named(BLACK), to_cp437('!'), 200.0);
|
||||
} else {
|
||||
remove_turn.push(entity);
|
||||
gamelog::Logger::new().npc_name(&name.name).colour(WHITE).append("is confused.").log();
|
||||
particle_builder.request(pos.x, pos.y, RGB::named(MAGENTA), RGB::named(BLACK), to_cp437('?'), 200.0);
|
||||
}
|
||||
}
|
||||
for e in remove_turn {
|
||||
turns.remove(e);
|
||||
}
|
||||
for e in remove_confusion {
|
||||
confusion.remove(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue