-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.hpp
More file actions
78 lines (66 loc) · 1.7 KB
/
manager.hpp
File metadata and controls
78 lines (66 loc) · 1.7 KB
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
#ifndef MANAGER_HPP
#define MANAGER_HPP
#include <iostream>
#include <cstring>
#include <sstream>
#include <unordered_map>
#include <SFML/Graphics.hpp>
#include <vector>
#include "map.hpp"
template <typename T>
class Manager {
protected:
std::unordered_map<std::string, T> ent_library;
std::vector<T*> ents;
public:
std::unordered_map<std::string, int> num_ents_created;
T* lastCreatedEnt;
Map* curMap;
virtual ~Manager() {}
/*
* Moved to entity_manager
virtual void render(sf::RenderTarget* screen) {
for (T* thisEnt : ents) {
thisEnt->render(screen);
}
};*/
virtual void update(float dt) {
for (T* thisEnt : ents) {
thisEnt->update(dt);
}
};
virtual T* addNewEntByType(std::string type, float x, float y) {
// Ensure that the type exists
if (ent_library.find(type) == ent_library.end()) {
std::cerr << "ERROR: attempted to create entity of unknown type " << type << std::endl;
return NULL;
}
// Construct a name for the ent
std::ostringstream stream;
stream << type << "_" << num_ents_created[type]++;
T* newEnt = ent_library[type].clone(stream.str());
newEnt->moveToRealXY(x,y);
lastCreatedEnt = newEnt;
addEnt(newEnt);
return newEnt;
};
virtual void addNewEntType(std::string type, T newEnt) {
ent_library[type] = newEnt;
};
virtual T* removeEnt(std::string uid) {
// Find the entity and do an in-place swap with the final element, then shrink
for (typename std::vector<T*>::iterator it = ents.begin() ; it != ents.end(); ++it) {
if ((*it)->uid == uid) {
T* removedUnit = *it;
*it = ents[ents.size()-1];
ents.pop_back();
return removedUnit;
}
}
return NULL;
};
virtual void addEnt(T* ent) {
ents.push_back(ent);
};
};
#endif