#include "game.hpp" namespace game { size_t selection_cookie = 1; void state_t::start(void) { human_t *human; alien_t *alien; world.generator = worldgen; 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)); alien = new alien_t(this); alien->place(&world, v2f_t(14.5, -2.5)); alien = new alien_t(this); alien->place(&world, v2f_t(13.5, -2.5)); } void state_t::stop(void) { // FIXME /* for (unit_t *unit : units) delete unit; */ } void state_t::select(rectf_t x) { size_t old_cookie = selection_cookie; selection_cookie++; selected_units.clear(); for (world::entity_t *ent : world.get_render_entities(x)) { unit_t *unit; if (ent->type != ET_UNIT) continue; unit = (unit_t*)ent; if (unit->type != unit_t::UNIT_HUMAN) continue; if (!unit->dead && unit->selected != old_cookie) unit->say(text::get(text::SAY_READY)); unit->selected = selection_cookie; selected_units.insert(unit); } } void state_t::command(v2f_t x) { v2f_t snap; snap[0] = std::round(x[0] - 0.5f) + 0.5f; snap[1] = std::round(x[1] - 0.5f) + 0.5f; for (unit_t *unit : selected_units) { if (unit->dead) continue; if (!unit->start_moving(snap, CF_SOLID)) unit->say(text::get(text::SAY_NO_PATH)); else unit->say(text::get(text::SAY_MOVING)); } } void state_t::tick(double now_, double dt_) { now = now_; dt = dt_; 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; if (enemy->awake) continue; if (enemy->type == unit_t::UNIT_HUMAN) continue; enemy->awake = true; awake_units.insert(enemy); enemy->wake(unit); } unit->think(); } 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++; } } } bool load_assets(void) { assets::load(); return true; } } //namespace game