summaryrefslogtreecommitdiff
path: root/src/game.cpp
blob: f4b505747529ec7cd122d09cd26893dbe070b25a (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
#include "common.hpp"

namespace game {

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

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

public:
	rectf_t size, render_size;

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

		std::list<v2f_t> path;
	} move;

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

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

	void keep_moving(void)
	{
		float time;

		if (!move.moving)
			return;

		time = 0.15;

		while (time > 0.0f) {
			v2f_t delta, 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 += delta * time / delta.len();
				break;
			} else {
				x = next;
				time -= delta.len();
				move.path.pop_front();
			}
		}

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

	bool start_moving(v2f_t dst_)
	{
		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, size, this, &move.path))
			move.moving = true;
		else
			move.moving = false;

		return move.moving;
	}
};

class human_t : public unit_t {
public:
	void render_to(render::state_t *render)
	{
		render->render((move.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)
			render->debug_path(&move.path);
	}
};

static human_t human, human2;

void state_t::start(void)
{
	human.size[0] = v2f_t(-0.4f, -0.4f);
	human.size[1] = v2f_t(+0.4f, +0.4f);
	human.render_size[0] = v2f_t(-0.5f, -1.0f);
	human.render_size[1] = v2f_t(+0.5f, +0.5f);
	human.place(&world, v2f_t(0.5f, 0.5f));

	human2.size[0] = v2f_t(-0.4f, -0.4f);
	human2.size[1] = v2f_t(+0.4f, +0.4f);
	human2.render_size[0] = v2f_t(-0.5f, -1.0f);
	human2.render_size[1] = v2f_t(+0.5f, +0.5f);
	human2.place(&world, v2f_t(3.5f, 0.5f));

}

void state_t::debug_click(v2f_t x)
{
	human.start_moving(x);
}

void state_t::tick(void)
{
	human.keep_moving();
}

} //namespace game