aboutsummaryrefslogtreecommitdiff
path: root/Canvas.cpp
blob: 94ffe14696c16b3807e3c0e1de1fa812814204b3 (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
#include <cstdio>
#include <string>

#include "Canvas.h"
#include "util.h"
#include "Museum.h"

using namespace std;

Canvas::Canvas(Museum & museum) : museum(museum) { }

Tile & Canvas::get_tile(unsigned x, unsigned y) {
	return *this->tiles[this->pos_to_index(x, y)];
}

void Canvas::set_tile(unsigned x, unsigned y, TileData data) {
	size_t index = this->pos_to_index(x, y);
	if (this->tiles[index] != nullptr)
		delete this->tiles[index];
	this->tiles[index] = new Tile(this->museum, data);
	this->tiles[index]->x = x;
	this->tiles[index]->y = y;
}

size_t Canvas::pos_to_index(unsigned x, unsigned y) {
	size_t index = y * this->data.columns + x;
	return index;
}

void Canvas::update() {
	this->update_steps();
	this->update_tiles();
}

void Canvas::set_data(CanvasData data) {
	this->data = data;
	this->tiles.resize(this->data.rows * this->data.columns);
	for (size_t y = 0; y < this->data.rows; y++) {
		for (size_t x = 0; x < this->data.columns; x++) {
			if (this->tiles[this->pos_to_index(x, y)] != nullptr)
				continue;
			this->set_tile(x, y, {});
		}
	}
}

Canvas::~Canvas() {
	for (size_t i = 0; i < this->tiles.size(); i++) {
		if (this->tiles[i] == nullptr) continue;
		delete this->tiles[i];
		this->tiles[i] = nullptr;
	}
}

string Canvas::to_string(bool truecolor) {
	string out = "";

	for (size_t y = 0; y < this->data.rows; y++) {
		for (size_t x = 0; x < this->data.columns; x++) {
			Tile & tile = this->get_tile(x, y);
			string type_str = tile.data.type;
			if (type_str.length() == 0) type_str = ".";

			if (truecolor)
				out += stringf("\e[38;2;0;0;0;48;2;%d;%d;%dm",
						tile.color.red, tile.color.green, tile.color.blue);

			out += stringf("%-2s ", type_str.c_str());
		}
		if (truecolor) out += "\e[0m";
		out += "\n";
	}

	return out;
}

void Canvas::update_steps() {
	for (Artist * artist : this->museum.people.get_artists()) {
		if (artist->step == false) continue;
		artist->step = false;

		this->get_tile(artist->data.x, artist->data.y).step(artist);
	}
}

void Canvas::update_tiles() {
	for (Tile * tile : this->tiles) {
		if (tile == nullptr) continue;
		tile->update();
	}
}