-
Notifications
You must be signed in to change notification settings - Fork 11
/
HealthManager.cpp
69 lines (58 loc) · 2.15 KB
/
HealthManager.cpp
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
#include "HealthManager.h"
#include "Components.h"
#include "MessageManager.h"
#include "ScreenManager.h"
#include "MapManager.h"
using namespace entityx;
HealthManager::HealthManager()
{
}
void HealthManager::init()
{
MessageManager::getPtr()->subscribe<HitEvent>(*this);
}
void HealthManager::receive(const HitEvent &event)
{
std::cout<<"ricevuto"<<std::endl;
entityx::ptr<entityx::EntityManager> entities = ScreenManager::getPtr()->getCurrentEntities();
for(auto entity : entities->entities_with_components<Destroyable, Renderable>())
{
ptr<Destroyable> points = entity.component<Destroyable>();
std::string entName = entity.component<Name>()->name;
Ogre::String name;
if(entName != "Block"){
ptr<Children> child = entity.component<Children>();
name = child->children["body"].component<Renderable>()->sceneNode->getName();
}else{
ptr<Renderable> rend = entity.component<Renderable>();
name = rend->sceneNode->getName();
}
std::cout<<"name: "<<name<<std::endl;
int max = points->maxHealth;
if(name.compare(event.targetName)==0)
{
points->health-=event.points;
if(points->health<=0)
{
MessageManager::getPtr()->emit<ObjectDestroyed>(entity);
if(entity.valid()){
if(entity.component<Name>()->name == "Block")
{
MapManager::getPtr()->deletePosition(entity.component<Position>()->position);
RenderManager::getPtr()->getSceneManager()->destroySceneNode(entity.component<Renderable>()->sceneNode);
entity.destroy();
}else{
entity.assign<Position>(MapManager::getPtr()->getFreePos());
entity.assign<Destroyable>(max,max);
}
}
return;
}else{
std::cout<<"punti"<<points->health<<std::endl;
entity.assign<Destroyable>(points->health,max);
}
return;
}
}
return;
}