aboutsummaryrefslogtreecommitdiff
path: root/Artist.cpp
blob: 9a9ac8e40811447805376af5b9f1134e70486ca6 (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
#include <cstdlib>

#include "Artist.h"
#include "Museum.h"

using namespace std;

static constexpr const float velocity_scale = 0.2;

Artist::Artist(Museum & museum, ArtistData data) : museum(museum) {
	this->data = data;
}

void Artist::update(bool tick) {
	if (tick) {
		this->update_edge_collision();
		this->update_movement();
	}
	// Artist<->Artist collisions are marked by CollisionChecker
	this->update_path_collision();
	this->update_color();
}

void Artist::update_edge_collision() {
	float next_x = this->data.x + this->data.vx * velocity_scale;
	float next_y = this->data.y + this->data.vy * velocity_scale;

	// +0.5 is for own size (the assignment explicitly defines the x,y position
	// of artists as the top-left corner)
	if ((next_x + 0.5) > this->museum.canvas.data.columns || next_x < 0)
		this->data.vx *= -1;
	if ((next_y + 0.5) > this->museum.canvas.data.rows || next_y < 0)
		this->data.vy *= -1;
}

void Artist::update_movement() {
	float last_x = this->data.x;
	float last_y = this->data.y;

	this->data.x += this->data.vx * velocity_scale;
	this->data.y += this->data.vy * velocity_scale;

	if (abs(int(last_x) - int(this->data.x)) > 0) this->step = true;
	if (abs(int(last_y) - int(this->data.y)) > 0) this->step = true;
}

void Artist::update_path_collision() {
	PathfindingContext & ctx = this->museum.pathfinding;
	if (!ctx.has_collision) return;
	Pathfinder & solver = ctx.get_solver();
	bool is_solution = solver.is_solution({
		static_cast<int>(this->data.x),
		static_cast<int>(this->data.y),
	});
	if (is_solution) this->colliding = true;
}

void Artist::update_color() {
	if (this->colliding) {
		this->color = {
			.red = 0xff,
			.green = 0x00,
			.blue = 0x00,
		};
		return;
	}

	// default color
	this->color = {
		.red = 0x00,
		.green = 0x00,
		.blue = 0x00,
	};
}