Skip to content

Commit d7e3a2a

Browse files
author
Alessio Linares
committed
Added map of component factories to Game
1 parent 33a0b4b commit d7e3a2a

30 files changed

+845
-54
lines changed

Core.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef HUMMINGBIRD_BASE
2+
#define HUMMINGBIRD_BASE
3+
#include "Core/Clock.h"
4+
#include "Core/Color.h"
5+
#include "Core/DataComponent.h"
6+
#include "Core/DataRepository.h"
7+
#include "Core/EventManager.h"
8+
#include "Core/FunctionComponent.h"
9+
#include "Core/Game.h"
10+
#include "Core/GameObject.h"
11+
#include "Core/Plugin.h"
12+
#include "Core/Resource.h"
13+
#include "Core/ResourceManager.h"
14+
#include "Core/Time.h"
15+
#include "Core/Transform.h"
16+
#include "Core/Vector2d.h"
17+
#include "Core/Vector3d.h"
18+
#endif
File renamed without changes.

Clock.h renamed to Core/Clock.h

File renamed without changes.

Core/Color.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef HB_COLOR_h
2+
#define HB_COLOR_h
3+
4+
namespace hb
5+
{
6+
struct Color {
7+
float r;
8+
float g;
9+
float b;
10+
float a;
11+
12+
Color() {
13+
this->r = 0.0f;
14+
this->g = 0.0f;
15+
this->b = 0.0f;
16+
this->a = 1.0f;
17+
}
18+
19+
Color(float r, float g, float b, float a = 1.0f) {
20+
this->r = r;
21+
this->g = g;
22+
this->b = b;
23+
this->a = a;
24+
}
25+
26+
Color(int r, int g, int b, int a = 255) {
27+
this->r = (float)r/255.0f;
28+
this->g = (float)g/255.0f;
29+
this->b = (float)b/255.0f;
30+
this->a = (float)a/255.0f;
31+
}
32+
};
33+
}
34+
35+
#endif
File renamed without changes.

Core/DataRepository.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "DataRepository.h"
2+
using namespace hb;
3+
4+
5+
DataRepository::DataRepository()
6+
{
7+
m_data = std::unordered_map<std::string, Data>();
8+
}
9+
10+
11+
DataRepository::~DataRepository()
12+
{
13+
14+
}
15+
16+
17+
void DataRepository::setInt(const std::string& name, int value)
18+
{
19+
m_data[name].i = value;
20+
}
21+
22+
23+
int DataRepository::getInt(const std::string& name, int default_value)
24+
{
25+
auto it = m_data.find(name);
26+
if (it != m_data.end())
27+
return it->second.i;
28+
return default_value;
29+
}
30+
31+
32+
void DataRepository::setUInt(const std::string& name, unsigned int value)
33+
{
34+
m_data[name].ui = value;
35+
}
36+
37+
38+
int DataRepository::getUInt(const std::string& name, unsigned int default_value)
39+
{
40+
auto it = m_data.find(name);
41+
if (it != m_data.end())
42+
return it->second.ui;
43+
return default_value;
44+
}
45+
46+
47+
void DataRepository::setDouble(const std::string& name, double value)
48+
{
49+
m_data[name].d = value;
50+
}
51+
52+
53+
double DataRepository::getDouble(const std::string& name, double default_value)
54+
{
55+
auto it = m_data.find(name);
56+
if (it != m_data.end())
57+
return it->second.d;
58+
return default_value;
59+
}
60+
61+
62+
void DataRepository::setFloat(const std::string& name, float value)
63+
{
64+
m_data[name].f = value;
65+
}
66+
67+
68+
float DataRepository::getFloat(const std::string& name, float default_value)
69+
{
70+
auto it = m_data.find(name);
71+
if (it != m_data.end())
72+
return it->second.f;
73+
return default_value;
74+
}
75+
76+
77+
void DataRepository::setChar(const std::string& name, char value)
78+
{
79+
m_data[name].c = value;
80+
}
81+
82+
83+
char DataRepository::getChar(const std::string& name, char default_value)
84+
{
85+
auto it = m_data.find(name);
86+
if (it != m_data.end())
87+
return it->second.c;
88+
return default_value;
89+
}
90+
91+
92+
void DataRepository::setBool(const std::string& name, bool value)
93+
{
94+
m_data[name].b = value;
95+
}
96+
97+
98+
bool DataRepository::getBool(const std::string& name, bool default_value)
99+
{
100+
auto it = m_data.find(name);
101+
if (it != m_data.end())
102+
return it->second.b;
103+
return default_value;
104+
}
105+
106+
107+
void DataRepository::setPointer(const std::string& name, void* value)
108+
{
109+
m_data[name].p = value;
110+
}

Core/DataRepository.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef HB_DATA_REPOSITORY_H
2+
#define HB_DATA_REPOSITORY_H
3+
#include <string>
4+
#include <unordered_map>
5+
6+
namespace hb
7+
{
8+
class DataRepository
9+
{
10+
public:
11+
DataRepository();
12+
~DataRepository();
13+
14+
void setInt(const std::string& name, int value);
15+
int getInt(const std::string& name, int default_value = -1);
16+
void setUInt(const std::string& name, unsigned int value);
17+
int getUInt(const std::string& name, unsigned int default_value = 0);
18+
void setDouble(const std::string& name, double value);
19+
double getDouble(const std::string& name, double default_value = -1.);
20+
void setFloat(const std::string& name, float value);
21+
float getFloat(const std::string& name, float default_value = -1.f);
22+
void setChar(const std::string& name, char value);
23+
char getChar(const std::string& name, char default_value = 0x00);
24+
void setBool(const std::string& name, bool value);
25+
bool getBool(const std::string& name, bool default_value = false);
26+
void setPointer(const std::string& name, void* value);
27+
template <typename T>
28+
T* getPointer(const std::string& name, T* default_value = nullptr)
29+
{
30+
auto it = m_data.find(name);
31+
if (it != m_data.end())
32+
return static_cast<T*>(m_data[name].p);
33+
return default_value;
34+
}
35+
36+
private:
37+
union Data
38+
{
39+
int i;
40+
unsigned int ui;
41+
double d;
42+
float f;
43+
char c;
44+
bool b;
45+
void* p;
46+
};
47+
std::unordered_map<std::string, Data> m_data;
48+
};
49+
}
50+
#endif

Core/EventListener.h

Whitespace-only changes.

EventManager.h renamed to Core/EventManager.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@ namespace hb
1414
typedef std::function<void(const Event&)> Listener;
1515
int listen(Listener&& listener)
1616
{
17-
m_listeners.push_back(std::move(listener));
18-
m_is_active.push_back(true);
19-
return m_listeners.size()-1;
17+
if (m_disabled.size() != 0)
18+
{
19+
int i = m_disabled.back();
20+
m_disabled.pop_back();
21+
m_listeners[i] = std::move(listener);
22+
m_is_active[i] = true;
23+
return i;
24+
}
25+
else
26+
{
27+
m_listeners.push_back(std::move(listener));
28+
m_is_active.push_back(true);
29+
return m_listeners.size()-1;
30+
}
2031
}
2132

2233
void message(const Event& event)

Core/FunctionComponent.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "FunctionComponent.h"
2+
using namespace hb;
3+
4+
FunctionComponent::FunctionComponent()
5+
{
6+
m_pre_update = [&] () {};
7+
m_update = [&] () {};
8+
m_post_update = [&] () {};
9+
m_destroy = [&] () {};
10+
}
11+
12+
13+
FunctionComponent::~FunctionComponent()
14+
{
15+
m_destroy();
16+
}
17+
18+
19+
void FunctionComponent::setPreUpdateFunction(std::function<void(void)> pre_update)
20+
{
21+
m_pre_update = pre_update;
22+
}
23+
24+
25+
void FunctionComponent::setUpdateFunction(std::function<void(void)> update)
26+
{
27+
m_update = update;
28+
}
29+
30+
31+
void FunctionComponent::setPostUpdateFunction(std::function<void(void)> post_update)
32+
{
33+
m_post_update = post_update;
34+
}
35+
36+
37+
void FunctionComponent::setDestroyFunction(std::function<void(void)> destroy)
38+
{
39+
m_destroy = destroy;
40+
}
41+
42+
43+
void FunctionComponent::preUpdate()
44+
{
45+
m_pre_update();
46+
}
47+
48+
49+
void FunctionComponent::update()
50+
{
51+
m_update();
52+
}
53+
54+
55+
void FunctionComponent::postUpdate()
56+
{
57+
m_post_update();
58+
}

Core/FunctionComponent.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef HB_FUNCTION_COMPONENT_H
2+
#define HB_FUNCTION_COMPONENT_H
3+
#include <functional>
4+
#include "GameObject.h"
5+
#include "DataRepository.h"
6+
7+
namespace hb
8+
{
9+
class FunctionComponent : public GameObject::Component
10+
{
11+
public:
12+
FunctionComponent();
13+
virtual ~FunctionComponent() override;
14+
void setPreUpdateFunction(std::function<void(void)> pre_update);
15+
void setUpdateFunction(std::function<void(void)> update);
16+
void setPostUpdateFunction(std::function<void(void)> post_update);
17+
void setDestroyFunction(std::function<void(void)> destroy);
18+
virtual void preUpdate() override;
19+
virtual void update() override;
20+
virtual void postUpdate() override;
21+
22+
private:
23+
std::function<void(void)> m_pre_update;
24+
std::function<void(void)> m_update;
25+
std::function<void(void)> m_post_update;
26+
std::function<void(void)> m_destroy;
27+
};
28+
}
29+
#endif

0 commit comments

Comments
 (0)