summaryrefslogtreecommitdiff
path: root/src/game/game.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game/game.cpp')
-rw-r--r--src/game/game.cpp137
1 files changed, 74 insertions, 63 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp
index 993f450..35cb90c 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -4,6 +4,59 @@ namespace game {
size_t selection_cookie = 1;
+entity_t::entity_t(game::state_t *game_, int type_) : world::entity_t(type_)
+{
+ game = game_;
+}
+
+void entity_t::destroy(void)
+{
+ unlink();
+ game->awake_entities.erase(this);
+ delete this;
+}
+
+void entity_t::place(world::world_t *world_)
+{
+ bool do_spawn = false;
+
+ if (!world)
+ do_spawn = true;
+
+ link(world_);
+
+ if (do_spawn)
+ on_spawn();
+
+ if (always_awake)
+ wake();
+}
+
+void entity_t::place(world::world_t *world, v2f_t x_)
+{
+ x = x_;
+ cmodel.bounds = size + x;
+ render_bounds = render_size + x;
+
+ place(world);
+}
+
+void entity_t::wake(void)
+{
+ awake = true;
+ wake_time = game->now;
+ game->awake_entities.insert(this);
+
+ if (!always_awake)
+ on_wake();
+}
+
+void entity_t::sleep(void)
+{
+ awake = false;
+ game->awake_entities.erase(this);
+}
+
void state_t::start(void)
{
human_t *human;
@@ -14,13 +67,10 @@ void state_t::start(void)
human = new human_t(this);
human->place(&world, v2f_t(0.5, 0.5));
- units.insert(human);
human = new human_t(this);
human->place(&world, v2f_t(1.5, 0.5));
- units.insert(human);
human = new human_t(this);
human->place(&world, v2f_t(2.5, 0.5));
- units.insert(human);
alien = new alien_t(this);
alien->place(&world, v2f_t(15.5, -2.5));
@@ -32,11 +82,6 @@ void state_t::start(void)
void state_t::stop(void)
{
- // FIXME
- /*
- for (unit_t *unit : units)
- delete unit;
- */
}
void state_t::select(rectf_t x)
@@ -83,12 +128,6 @@ void state_t::command(v2f_t x)
}
}
-void state_t::add_effect(effect_t *effect)
-{
- effects.insert(effect);
- effect->link(&world);
-}
-
void state_t::tick(double now_, double dt_)
{
union {
@@ -105,61 +144,33 @@ void state_t::tick(double now_, double dt_)
u.d = dt;
dice_prng.seed(dice_prng.next() ^ u.i);
- for (unit_t *unit : units) {
- rectf_t wake_range;
-
- if (unit->dead)
- continue;
-
- wake_range[0] = unit->x - v2f_t(10, 10);
- wake_range[1] = unit->x + v2f_t(10, 10);
-
- for (world::entity_t *ent : world.get_entities(wake_range, -1)) {
- unit_t *enemy;
-
- if (ent->type != ET_UNIT)
- continue;
-
- enemy = (unit_t*)ent;
-
- if (enemy->dead)
- continue;
-
- enemy->wake_time = now;
+ // NOTE: on_think can erase the entity from awake_entities.
+ for (auto i = awake_entities.begin(); i != awake_entities.end(); ) {
+ auto next = i;
+ next++;
+ (*i)->on_think();
+ i = next;
+ }
+}
- if (enemy->awake)
- continue;
+void state_t::wake_everything(v2f_t x, float range)
+{
+ rectf_t wake_range;
- if (enemy->type == unit_t::UNIT_HUMAN)
- continue;
+ wake_range[0] = x - v2f_t(range, range);
+ wake_range[1] = x + v2f_t(range, range);
- enemy->awake = true;
- awake_units.insert(enemy);
- enemy->wake(unit);
- }
+ for (world::entity_t *went : world.get_entities(wake_range, -1)) {
+ auto ent = dynamic_cast<game::entity_t*>(went);
- unit->think();
- }
+ // WTF?
+ if (!ent)
+ continue;
- for (auto i = std::begin(awake_units); i != std::end(awake_units);) {
- if (now - (*i)->wake_time >= 5.0) {
- (*i)->awake = false;
- (*i)->sleep();
- i = awake_units.erase(i);
- } else {
- if (!(*i)->dead)
- (*i)->think();
- i++;
- }
- }
+ if (ent->always_awake || ent->awake)
+ continue;
- for (auto i = std::begin(effects); i != std::end(effects);) {
- if (now > (*i)->ttl) {
- (*i)->unlink();
- delete *i;
- i = effects.erase(i);
- } else
- i++;
+ ent->wake();
}
}