summaryrefslogtreecommitdiff
path: root/src/game/game.cpp
blob: b9a46db443f74d8433a8ac4f36e4e8d9d948fd5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#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;
	human->place(&world, v2f_t(0.5, 0.5));
	units.insert(human);

	alien = new alien_t;
	alien->place(&world, v2f_t(15.5, -2.5));
}

void state_t::stop(void)
{
	// FIXME
	/*
	for (unit_t *unit : units)
		delete unit;
	*/
}

void state_t::select(rectf_t x)
{
	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;

		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)
		unit->start_moving(snap, now);
}

void state_t::tick(double now_, double dt_)
{
	now = now_;
	dt = dt_;

	for (unit_t *unit : units) {
		rectf_t wake_range;

		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;
			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(now, unit);
		}

		unit->think(now, dt);
	}

	for (auto i = std::begin(awake_units); i != std::end(awake_units);) {
		if (now - (*i)->wake_time >= 5.0) {
			(*i)->awake = false;
			(*i)->sleep(now);
			i = awake_units.erase(i);
		} else {
			(*i)->think(now, dt);
			i++;
		}
	}
}

bool load_assets(void)
{
	assets::load();
	return true;
}

} //namespace game