summaryrefslogtreecommitdiff
path: root/src/game.cpp
blob: bfc698daa12b7259955375b7eebece61fe7bde34 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "common.hpp"

namespace game {

static size_t selection_cookie = 1;

enum {
	ET_UNIT
};

class unit_t : public world::entity_t {
	world::world_t *world;

	world::cmodel_t make_cmodel(v2f_t at)
	{
		world::cmodel_t cmodel;

		cmodel.bounds[0] = at + size[0];
		cmodel.bounds[1] = at + size[1];
		cmodel.cflags = cflags;

		return cmodel;
	}

	void compute_bounds()
	{
		render_bounds[0] = x + render_size[0];
		render_bounds[1] = x + render_size[1];
	}

public:
	v2f_t x;
	rectf_t size, render_size;
	world::cflags_t cflags;
	size_t selected = 0;

	unit_t() : entity_t(ET_UNIT)
	{
	}

	struct {
		bool moving = false;
		v2f_t dst;
		float angle = 0.0f;

		std::list<v2f_t> path;

		bool blocked;
		size_t attempts_left;
		float next_attempt;
	} move;

	const wchar_t *say_text;
	double say_time = -INFINITY;

	void say(const wchar_t *wstr, double now)
	{
		say_text = wstr;
		say_time = now;
		std::cout << (void*)this << ": " << wstr << "\n";
	}

	void place(world::world_t *world_, v2f_t x_)
	{
		world = world_;
		x = x_;
		move.moving = false;
		cflags = 1;

		unlink();
		cmodel = make_cmodel(x);
		compute_bounds();
		link(world);
	}

	void keep_moving(double now)
	{
		float time;

		if (!move.moving)
			return;

		if (move.blocked && now < move.next_attempt)
			return;

		time = 0.15; // FIXME

		while (time > 0.0f) {
			v2f_t delta, next, x_new;
			world::cmodel_t cmodel_next;

			if (!move.path.size()) {
				move.moving = false;
				break;
			}

			next = *move.path.begin();
			delta = next - x;
			move.angle = delta.angle();

			if (delta.len() >= time) {
				x_new = x + delta * time / delta.len();
				time -= delta.len();
			} else {
				x_new = next;
				time -= delta.len();
				move.path.pop_front();
			}

			cmodel_next = make_cmodel(x_new);
			if (!world->test_rect(&cmodel_next, this)) {
				x = x_new;
				cmodel = cmodel_next;
				continue;
			}

			if (move.attempts_left) {
				move.blocked = true;
				move.attempts_left--;
				move.next_attempt = now + 0.2f;
			} else {
				if ((x - move.dst).len() > 1.5f)
					say(text::unit_blocked, now);
				move.moving = false;
			}
			break;
		}


		unlink();
		compute_bounds();
		link(world);
	}

	bool start_moving(v2f_t dst_, double now)
	{
		if (!world) {
			printf("unit_t::start_moving: entity is not linked\n");
			return false;
		}

		move.dst = dst_;
		move.path.clear();

		if (!world->find_path(x, move.dst, &cmodel, this, &move.path)) {
			say(text::unit_no_path, now);
			move.moving = false;
			return false;
		}

		move.moving = true;

		move.blocked = false;
		move.attempts_left = 10;
		move.next_attempt = -INFINITY;

		return true;
	}
};

class human_t : public unit_t {
public:
	human_t()
	{
		size[0] = v2f_t(-0.4f, -0.4f);
		size[1] = v2f_t(+0.4f, +0.4f);
		render_size[0] = v2f_t(-0.5f, -1.0f);
		render_size[1] = v2f_t(+0.5f, +0.5f);
	}

	void render_to(render::state_t *render)
	{
		bool moving;

		if (selected == selection_cookie)
			render->render_hlrect(render_bounds, sf::Color::Blue);

		moving = move.moving && !move.blocked;

		render->render((moving ? &assets::human.legs_walking :
		               &assets::human.legs_idle), render_bounds, move.angle);
		render->render(&assets::human.body_idle, render_bounds, move.angle);
		render->render(&assets::human.head_idle, render_bounds, move.angle);

		if (move.moving && debug_draw_paths)
			render->debug_path(&move.path);

		if (say_time + 5.0 > render->now) {
			v2f_t text_pos;
			float height;

			text_pos = render_bounds[0] + v2f_t(render_bounds.dim(0) / 2, -render_bounds.dim(1) * 0.1);
			height = size.dim_min() * 0.20f;
			render->render_text(text_pos, height, say_text,
			                    render::ALIGN_CENTER_BOTTOM,
			                    sf::Color::White);
		}
	}
};

void state_t::start(void)
{
	for (size_t i = 0; i < 10; i++) {
		human_t *human;

		human = new human_t;
		human->place(&world, v2f_t(0.5, 0.5) + i * v2f_t(0.2, -1.2f));
		units.insert(human);
	}
}

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;
		unit->selected = selection_cookie;
		selected_units.insert(unit);
	}
}

void state_t::command(v2f_t x)
{
	for (unit_t *unit : selected_units)
		unit->start_moving(x, now);
}

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

	for (unit_t *unit : units)
		unit->keep_moving(now);
}

} //namespace game