-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeDoor.cpp
More file actions
71 lines (63 loc) · 2.12 KB
/
makeDoor.cpp
File metadata and controls
71 lines (63 loc) · 2.12 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
#include "makeObjects.h"
#include <iostream>
void makeDoor(hb::GameObject* go, const Tmx::Map* map, int obj_grp, int obj_id)
{
// Get Tmx object representing the new Door GameObject
const Tmx::Object* object = map->GetObjectGroup(obj_grp)->GetObject(obj_id);
// Get it's position in object space
hb::Vector3d position = go->getPosition();
// Fixe Tiled position bug for Tile objects
--position.y;
go->setPosition(position);
// Make sprite from object tile
int gid = object->GetGid();
const Tmx::Tileset* tileset = map->FindTileset(gid);
hb::Texture tex = hb::Texture::loadFromFile("res/levels/" + tileset->GetImage()->GetSource());
hb::Sprite sprite(tex, hb::Vector2d(tileset->GetTileWidth(), tileset->GetTileHeight()));
// Initialize collision component
hb::CollisionComponent* collisions = new hb::CollisionComponent(hb::Vector2d(1, 1));
// Initialize function component
hb::FunctionComponent* fc = new hb::FunctionComponent;
// Get Door target (defined as a property in Tiled)
std::string target = object->GetProperties().GetStringProperty("target");
// Define GameObject status data and instantiate it
struct Data
{
bool touching_player = false;
};
Data* data = new Data;
// Define input listener for activating Door
auto listener = hb::InputManager::instance()->listen([=](hb::KeyPressed& ev)
{
if (ev.code == hb::Keyboard::Key::Space and data->touching_player)
{
hb::Game::setScene(target);
}
});
// Create new GameObject with components previously defined
go->addComponents({
new hb::SpriteComponent(sprite, {gid - tileset->GetFirstGid()}),
collisions,
fc
});
// Function called when object is about to be destroyed
fc->addListener("destroy", [=](hb::DataRepository&)
{
// Release memory used by data
delete data;
// No longer listen to event
hb::InputManager::instance()->ignore(listener);
});
// Define Door update function
fc->addListener("update", [=](hb::DataRepository&)
{
// detect if touching player
bool t = false;
while (not collisions->empty())
{
auto c = collisions->nextCollision();
t |= (c.object->getName() == "Player");
}
data->touching_player = t;
});
}