-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeSwitch.cpp
More file actions
78 lines (73 loc) · 2.26 KB
/
makeSwitch.cpp
File metadata and controls
78 lines (73 loc) · 2.26 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
#include "makeObjects.h"
#include <iostream>
void makeSwitch(hb::GameObject* go, const Tmx::Map* map, int obj_grp, int obj_id)
{
// Define GameObject status data and instantiate it
struct Data
{
bool player_touching = false;
};
Data* data = new Data;
// Get Tmx object representing the new Switch GameObject
const Tmx::Object* obj = map->GetObjectGroup(obj_grp)->GetObject(obj_id);
// Get z position from s-index of ObjectGroup
auto collisions = new hb::CollisionComponent(hb::Vector2d(1, 1));
// Make sprite from tile
int gid = obj->GetGid();
const Tmx::Tileset *tileset = map->FindTileset(gid);
hb::Texture tex = hb::Texture::loadFromFile("res/levels/" + tileset->GetImage()->GetSource(), hb::Rect(0, 0, tileset->GetImage()->GetWidth(), tileset->GetImage()->GetHeight()));
hb::Sprite sprite = hb::Sprite(tex, hb::Vector2d(tileset->GetTileWidth(), tileset->GetTileHeight()), hb::Vector2d(tileset->GetMargin(), tileset->GetMargin()));
// Define Function component
hb::FunctionComponent* fc = new hb::FunctionComponent();
// Define Switch component
std::string tmp;
std::stringstream ss;
ss << obj->GetProperties().GetStringProperty("target");
std::vector<int> targets;
int target_id;
while(ss >> target_id)
{
targets.push_back(target_id);
}
SwitchComponent* sc = new SwitchComponent(targets);
sc->setAction([=](hb::GameObject* target)
{
target->setActive(sc->isOn());
});
// Create new GameObject
go->addComponents({
collisions,
new hb::SpriteComponent(sprite, {gid - tileset->GetFirstGid()}),
fc,
sc
});
hb::Vector3d v = go->getPosition();
v.z += obj->GetProperties().GetIntProperty("z");
v.y -= 1;
go->setPosition(v);
// Input listener to activate switch
auto listener = hb::InputManager::instance()->listen([=](hb::KeyPressed& e)
{
if (e.code == hb::Keyboard::Key::Space and data->player_touching)
{
sc->doSwitch();
}
});
// GameObject update function
fc->addListener("update", [=](hb::DataRepository&)
{
bool t = false;
while (not collisions->empty())
{
auto c = collisions->nextCollision();
t |= (c.object->getName() == "Player");
}
data->player_touching = t;
});
// GameObject destroy function
fc->addListener("destroy", [=](hb::DataRepository&)
{
delete data;
hb::InputManager::instance()->ignore(listener);
});
}