aboutsummaryrefslogtreecommitdiff
path: root/People.cpp
blob: d11434dc8e85f7e7ea020581bbe302e24c9c3364 (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
#include <algorithm>
#include <memory>

#include "People.h"
#include "ArtistData.h"
#include "util.h"

using namespace std;

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

People::~People() {
	for (Artist * artist : this->artists)
		delete artist;
	this->artists.clear();
}

void People::add_artist(const ArtistData & data) {
	if (this->artist_count >= 10000) return;

	this->artists.push_front(new Artist(this->museum, data));
	this->artist_count++;
}

void People::remove_artist(Artist & target) {
	auto it = find(this->artists.begin(), this->artists.end(), &target);
	if (it == this->artists.end()) return;
	Artist * artist = *it;
	this->artists.remove(&target);
	this->artist_count--;
	delete artist;
}

forward_list<Artist *> People::get_artists() {
	return this->artists;
}

string People::to_string() {
	string out = "";

	out += stringf("%d artists\n", this->artist_count);
	for (Artist * artist : this->artists) {
		out += stringf("- at (%.2f,%.2f)\n", artist->data.x, artist->data.y);
	}

	return out;
}

void People::update(bool tick) {
	for (Artist * artist : this->artists) {
		artist->update(tick);
	}
}

Memories People::save() {
	Memories data;
	for (Artist * artist : this->artists) {
		data.push_back(make_unique<ArtistDataMemento>(artist->data));
	}
	return data;
}

void People::restore(const Memories & memories) {
	this->artists.clear();
	this->artist_count = 0;
	for (const unique_ptr<Memento> & memory : memories) {
		auto data = dynamic_cast<ArtistDataMemento *>(memory.get());
		if (data == nullptr) continue;
		this->add_artist(data->data);
	}
}