Peoplemon  0.1.0
Peoplemon 3 game source documentation
MapEventContext.cpp
Go to the documentation of this file.
2 
5 
6 namespace core
7 {
8 namespace script
9 {
10 namespace
11 {
12 using bl::script::Function;
13 using bl::script::SymbolTable;
14 using bl::script::Value;
15 
16 typedef void (*Builtin)(bl::ecs::Entity, const bl::tmap::Position&, const map::Event&, Value&);
17 
18 void triggeringEntity(bl::ecs::Entity entity, const bl::tmap::Position& pos,
19  const map::Event& event, Value&);
20 
21 void triggerPosition(bl::ecs::Entity entity, const bl::tmap::Position& pos,
22  const map::Event& event, Value&);
23 
24 void eventType(bl::ecs::Entity entity, const bl::tmap::Position& pos, const map::Event& event,
25  Value&);
26 
27 void eventPosition(bl::ecs::Entity entity, const bl::tmap::Position& pos, const map::Event& event,
28  Value&);
29 
30 void eventSize(bl::ecs::Entity entity, const bl::tmap::Position& pos, const map::Event& event,
31  Value&);
32 
33 Value bind(bl::ecs::Entity entity, const bl::tmap::Position& pos, const map::Event& event,
34  Builtin func) {
35  return Value(Function(std::bind(func, entity, pos, event, std::placeholders::_3)));
36 }
37 
38 } // namespace
39 
40 MapEventContext::MapEventContext(system::Systems& systems, bl::ecs::Entity entity,
41  const map::Event& event, const bl::tmap::Position& tile)
42 : systems(systems)
43 , entity(entity)
44 , event(event)
45 , tile(tile) {}
46 
47 void MapEventContext::addCustomSymbols(SymbolTable& table) const {
48  BaseFunctions::addDefaults(table, systems);
49 
50  table.set("triggeringEntity", bind(entity, tile, event, &triggeringEntity));
51  table.set("triggerPosition", bind(entity, tile, event, &triggerPosition));
52  table.set("eventType", bind(entity, tile, event, &eventType));
53  table.set("eventPosition", bind(entity, tile, event, &eventPosition));
54  table.set("eventSize", bind(entity, tile, event, &eventSize));
55 }
56 
57 namespace
58 {
59 void triggeringEntity(bl::ecs::Entity entity, const bl::tmap::Position&, const map::Event&,
60  Value& result) {
61  result = static_cast<float>(entity);
62 }
63 
64 void triggerPosition(bl::ecs::Entity, const bl::tmap::Position& pos, const map::Event&,
65  Value& result) {
66  result = BaseFunctions::makePosition(pos);
67 }
68 
69 void eventType(bl::ecs::Entity, const bl::tmap::Position&, const map::Event& event,
70  Value& result) {
71  switch (event.trigger) {
73  result = "OnEnter";
74  break;
76  result = "OnExit";
77  break;
79  result = "onEnterOrExit";
80  break;
82  result = "WhileIn";
83  break;
85  result = "OnInteract";
86  break;
87  default:
88  result = "Unknown";
89  }
90 }
91 
92 void eventPosition(bl::ecs::Entity, const bl::tmap::Position&, const map::Event& event,
93  Value& coord) {
94  coord.setProperty("x", {static_cast<float>(event.position.x)});
95  coord.setProperty("y", {static_cast<float>(event.position.y)});
96 }
97 
98 void eventSize(bl::ecs::Entity, const bl::tmap::Position&, const map::Event& event, Value& size) {
99  size.setProperty("x", {static_cast<float>(event.areaSize.x)});
100  size.setProperty("y", {static_cast<float>(event.areaSize.y)});
101 }
102 
103 } // namespace
104 
105 } // namespace script
106 } // namespace core
Core classes and functionality for both the editor and game.
Represents an event in a Map. A script that is run on a trigger within a given region.
Definition: Event.hpp:19
@ OnInteract
The event triggers when the player interacts with the zone.
@ OnExit
The event triggers when the player steps out of the zone.
@ onEnterOrExit
The event triggers when the player either enters or exits the zone.
@ WhileIn
The event triggers repeatedly while the player is in the zone.
@ OnEnter
The event triggers when the player steps into the zone.
static bl::script::Value makePosition(const bl::tmap::Position &pos)
Helper function for other script functions to make script position values from the position component...
static void addDefaults(bl::script::SymbolTable &table, system::Systems &systems)
Adds the universal built-in functions to the given symbol table.
MapEventContext(system::Systems &systems, bl::ecs::Entity entity, const map::Event &event, const bl::tmap::Position &tile)
Construct a new MapEventContext.
virtual void addCustomSymbols(bl::script::SymbolTable &table) const override
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47