Orxonox  0.0.5 Codename: Arcturus
scriptable_controller_api.h
Go to the documentation of this file.
1 #ifndef SCRIPTABLE_CONTROLLER_API_H
2 #define SCRIPTABLE_CONTROLLER_API_H
3 
4 #include <functional>
5 #include "core/CoreIncludes.h"
6 #include "tools/Timer.h"
7 #include "OgreVector3.h"
8 
9 struct lua_State;
10 
11 namespace orxonox
12 {
13 
14 class ScriptableController;
15 class WorldEntity;
16 class Pawn;
17 
26 {
27 friend class ScriptableController;
28 
29 public:
37  ScriptableControllerAPI(lua_State *lua, ScriptableController *controller);
38 
43 
44 // ### API ####################################################################
45 
53  void orxPrint(std::string msg);
54 
60  void registerAfterTimeout(std::function<void (void)> callback, double timeout);
61 
72  void registerAtNearObject(std::function<void(std::string, std::string)> callback, std::string id1, std::string id2, double distance);
73 
86  void registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance);
87 
101  void registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz);
102 
116  void registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz);
117 
125  void registerAtPawnKilled(std::function<void (std::string)> callback, std::string id);
126 
134  void registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id);
135 
142  void killPawn(std::string id);
143 
152  void spawn(std::string type, std::string id);
153 
154  void spawnTest(std::string id);
155 
163  void setPosition(std::string id, double x, double y, double z);
164 
178  void setOrientation(std::string id, double x, double y, double z, double angle);
179 
189  void setVelocity(std::string id, double x, double y, double z);
190 
198  void setAngularVelocity(std::string id, double x, double y, double z);
199 
200 
208  double myTestFunction(double x, double y);
209 
210  void moveControllableEntity(std::string id, double x, double y, double z);
211 
212 // ### API END ################################################################
213 
214 private:
219  {
220  NearObjectHandler(WorldEntity *entity1, WorldEntity *entity2, std::string id1, std::string id2, double distance, std::function<void (std::string, std::string)> callback)
221  : entity1_(entity1), entity2_(entity2), id1_(id1), id2_(id2), distance_(distance), callback_(callback)
222  {}
223 
226  double distance_;
227  std::function<void (std::string, std::string)> callback_;
228  };
229 
234  {
235  NearPointHandler(WorldEntity *entity, std::string id, double x, double y, double z, double distance, std::function<void (std::string)> callback)
236  : entity_(entity), id_(id), point_(x, y, z), distance_(distance), callback_(callback)
237  {}
238 
241  Vector3 point_;
242  double distance_;
243  std::function<void (std::string)> callback_;
244  };
245 
249  struct AreaHandler
250  {
251  AreaHandler(WorldEntity *entity, std::string id, double x, double y, double z, double dx, double dy, double dz, bool atEnter, std::function<void (std::string)> callback)
252  : entity_(entity), id_(id), start_point_(x, y, z), atEnter_(atEnter), callback_(callback)
253  { this-> end_point_ = this->start_point_ + Vector3(dx, dy, dz); }
254 
257  Vector3 start_point_, end_point_;
258  bool atEnter_;
259  std::function<void (std::string)> callback_;
260  };
261 
262  lua_State *lua_;
264  std::list<NearObjectHandler> nearObjectHandlers_;
265  std::list<NearPointHandler> nearPointHandlers_;
266  std::list<AreaHandler> areaHandlers_;
267  std::map<std::string, std::list<std::function<void (std::string)> > > pawnDestroyedHandlers_;
268  std::map<std::string, std::list<std::function<void (std::string, std::string, double, double)> > > pawnHitHandlers_;
270  static const double periodic_interval;
271 
278  void pawnKilled(std::string id, Pawn *pawn);
279 
287  void pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield);
288 
295  void periodic(void);
296 };
297 
298 }
299 
300 #endif // SCRIPTABLE_CONTROLLER_API_H
WorldEntity * entity1_
Definition: scriptable_controller_api.h:224
std::function< void(std::string, std::string)> callback_
Definition: scriptable_controller_api.h:227
void setPosition(std::string id, double x, double y, double z)
Set the position of an object.
Definition: scriptable_controller_api.cc:296
Everything in Orxonox that has a health attribute is a Pawn.
Definition: Pawn.h:56
Runs a scripts on a per-level basis and handles the connection to orxonox.
Definition: scriptable_controller.h:31
static const double periodic_interval
Definition: scriptable_controller_api.h:270
Vector3 point_
Definition: scriptable_controller_api.h:241
void registerAfterTimeout(std::function< void(void)> callback, double timeout)
Register a function that will be called after a timeout.
Definition: scriptable_controller_api.cc:65
void setAngularVelocity(std::string id, double x, double y, double z)
Set the angular velocity of an object.
Definition: scriptable_controller_api.cc:358
std::list< NearObjectHandler > nearObjectHandlers_
Definition: scriptable_controller_api.h:264
void registerAtNearPoint(std::function< void(std::string)> callback, std::string id, double x, double y, double z, double distance)
Register a function that will be called when an object is close to a certain point.
Definition: scriptable_controller_api.cc:93
std::map< std::string, std::list< std::function< void(std::string, std::string, double, double)> > > pawnHitHandlers_
Definition: scriptable_controller_api.h:268
Groups everything together that is needed to handle a near-poinb event.
Definition: scriptable_controller_api.h:233
::std::string string
Definition: gtest-port.h:756
ScriptableControllerAPI(lua_State *lua, ScriptableController *controller)
Constructs the API with the given lua state.
Definition: scriptable_controller_api.cc:19
lua_State * lua_
Definition: scriptable_controller_api.h:262
void registerAtAreaLeave(std::function< void(std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
Register a function that will be called when an object leaves a cubic area.
Definition: scriptable_controller_api.cc:109
NearObjectHandler(WorldEntity *entity1, WorldEntity *entity2, std::string id1, std::string id2, double distance, std::function< void(std::string, std::string)> callback)
Definition: scriptable_controller_api.h:220
std::string id1_
Definition: scriptable_controller_api.h:225
void registerAtPawnHit(std::function< void(std::string, std::string, double, double)> callback, std::string id)
Register a function that will be called when a Pawn is hit.
Definition: scriptable_controller_api.cc:122
double distance_
Definition: scriptable_controller_api.h:242
double distance_
Definition: scriptable_controller_api.h:226
bool atEnter_
Definition: scriptable_controller_api.h:258
The WorldEntity represents everything that can be put in a Scene at a certain location.
Definition: WorldEntity.h:72
ScriptableController * controller_
Definition: scriptable_controller_api.h:263
void periodic(void)
Called every 0.5s.
Definition: scriptable_controller_api.cc:419
void moveControllableEntity(std::string id, double x, double y, double z)
Definition: scriptable_controller_api.cc:497
Declaration of the Timer class, used to call functions after a given time-interval.
Timer periodicTimer
Definition: scriptable_controller_api.h:269
AreaHandler(WorldEntity *entity, std::string id, double x, double y, double z, double dx, double dy, double dz, bool atEnter, std::function< void(std::string)> callback)
Definition: scriptable_controller_api.h:251
void spawnTest(std::string id)
Definition: scriptable_controller_api.cc:202
~ScriptableControllerAPI()
Destructs the API and closes the lua state.
Definition: scriptable_controller_api.cc:55
void killPawn(std::string id)
Kill a pawn.
Definition: scriptable_controller_api.cc:127
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
void spawn(std::string type, std::string id)
Spawn an object.
Definition: scriptable_controller_api.cc:136
double myTestFunction(double x, double y)
Set the angular velocity of an object.
Definition: scriptable_controller_api.cc:486
std::list< AreaHandler > areaHandlers_
Definition: scriptable_controller_api.h:266
WorldEntity * entity_
Definition: scriptable_controller_api.h:255
Defines several very important macros used to register objects, register classes, and to work with id...
Groups everything together that is needed to handle a near-object event.
Definition: scriptable_controller_api.h:218
API for ScriptableController&#39;s lua-scripts.
Definition: scriptable_controller_api.h:25
void orxPrint(std::string msg)
Print a message.
Definition: scriptable_controller_api.cc:60
void registerAtNearObject(std::function< void(std::string, std::string)> callback, std::string id1, std::string id2, double distance)
Register a function that will be called when two object are close to eachother.
Definition: scriptable_controller_api.cc:84
std::function< void(std::string)> callback_
Definition: scriptable_controller_api.h:243
std::function< void(std::string)> callback_
Definition: scriptable_controller_api.h:259
std::string id2_
Definition: scriptable_controller_api.h:225
void registerAtPawnKilled(std::function< void(std::string)> callback, std::string id)
Register a function that will be called when a Pawn is killed.
Definition: scriptable_controller_api.cc:117
void setOrientation(std::string id, double x, double y, double z, double angle)
Set the orientation of an object.
Definition: scriptable_controller_api.cc:315
WorldEntity * entity2_
Definition: scriptable_controller_api.h:224
Groups everything together that is needed to handle an area enter/leave event.
Definition: scriptable_controller_api.h:249
The Scripts&#39; code is executed through lua.
std::map< std::string, std::list< std::function< void(std::string)> > > pawnDestroyedHandlers_
Definition: scriptable_controller_api.h:267
void pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield)
Called by ScriptableController when a Pawn is hit.
Definition: scriptable_controller_api.cc:413
std::string id_
Definition: scriptable_controller_api.h:240
std::list< NearPointHandler > nearPointHandlers_
Definition: scriptable_controller_api.h:265
void registerAtAreaEnter(std::function< void(std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
Register a function that will be called when an object enters a cubic area.
Definition: scriptable_controller_api.cc:101
WorldEntity * entity_
Definition: scriptable_controller_api.h:239
void setVelocity(std::string id, double x, double y, double z)
Set the velocity of an object.
Definition: scriptable_controller_api.cc:339
Timer is a helper class that executes a function after a given amount of seconds in game-time...
Definition: Timer.h:105
NearPointHandler(WorldEntity *entity, std::string id, double x, double y, double z, double distance, std::function< void(std::string)> callback)
Definition: scriptable_controller_api.h:235
std::string id_
Definition: scriptable_controller_api.h:256
void pawnKilled(std::string id, Pawn *pawn)
Called by ScriptableController when a pawn is killed.
Definition: scriptable_controller_api.cc:377
Vector3 start_point_
Definition: scriptable_controller_api.h:257