AnnScriptManager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <systemMacro.h>
4 #include <AnnScriptFile.hpp>
5 #include <AnnSubsystem.hpp>
6 #include <AnnEventManager.hpp>
7 #include <AnnLightObject.hpp>
8 
9 #include <chaiscript.hpp>
10 #include <chaiscript_stdlib.hpp>
11 #include <AnnTypes.h>
12 
13 namespace Annwvyn
14 {
15  class AnnGameObject;
16 
26 
27  enum {
36  };
37 
40  {
41  public:
44 
46  AnnBehaviorScript(const std::string& name,
47  std::function<void(chaiscript::Boxed_Value&)> updateHook,
49  chaiscript::Boxed_Value scriptObjectInstance);
50 
52  virtual ~AnnBehaviorScript();
53 
55  void update();
56 
58  bool isValid() const;
59 
61  void registerAsListener();
62 
64  void unregisterAsListener();
65 
67  void KeyEvent(AnnKeyEvent e) override;
69  void MouseEvent(AnnMouseEvent e) override;
71  void ControllerEvent(AnnControllerEvent e) override;
73  void TimeEvent(AnnTimeEvent e) override;
75  void TriggerEvent(AnnTriggerEvent e) override;
77  void HandControllerEvent(AnnHandControllerEvent e) override;
79  void CollisionEvent(AnnCollisionEvent e) override;
81  void PlayerCollisionEvent(AnnPlayerCollisionEvent e) override;
82 
83  private:
85  const bool valid;
86 
89 
91  chaiscript::Boxed_Value ScriptObjectInstance;
92 
111 
113  bool cannotKey, cannotMouse, cannotStick, cannotTime, cannotTrigger, cannotHand, cannotCollision, cannotPlayerCollision;
114 
116  void callUpdateOnScript() { callUpdateOnScriptInstance(ScriptObjectInstance); }
117  };
118 
121  {
122  public:
123  using AnnScriptID = uID;
124 
127 
129  ~AnnScriptManager();
130 
132  bool needUpdate() override { return false; }
133 
135  void update() override {}
136 
138  bool evalFile(const std::string& file);
139 
141  std::shared_ptr<AnnBehaviorScript> getBehaviorScript(const std::string& scriptName, AnnGameObject* owner = nullptr);
142 
144  void evalString(const std::string& chaiCode);
145 
147  chaiscript::ChaiScript* _getEngine();
148 
149  private:
151  chaiscript::ChaiScript chai;
152 
155 
157  void registerApi();
158 
160  void registerResourceManager();
161 
163  void unregisterResourceManager();
164 
166  static constexpr const char* const scriptExtension{ ".chai" };
167 
169  static constexpr const char* const fileErrorPrefix{ "Script File ERROR - " };
170 
172  static constexpr const char* const logFromScript{ "Script - " };
173 
175  static constexpr const char* const scriptTemplate{
176  R"chaiscript(def create__SCRIPT_NAME____OBJECT_SCRIPT_ID__(owner)
177  {
178  var ScriptInstance__OBJECT_SCRIPT_ID__ = __SCRIPT_NAME__(owner);
179  return ScriptInstance__OBJECT_SCRIPT_ID__;
180  })chaiscript"
181  };
182 
184  static constexpr const char* const scriptNameMarker{ "__SCRIPT_NAME__" };
186  static constexpr const char* const scriptObjectID{ "__OBJECT_SCRIPT_ID__" };
188  static constexpr const char* const scriptOwnerMarker{ "__SCRIPT_OWNER__" };
190  static constexpr const char* const scriptInstanceMarker{ "ScriptInstance" };
192  static constexpr const char* const scriptOwnerPrefix{ "ScriptOwner" };
193 
195  static constexpr const size_t nameMarkerLen{ 15 };
197  static constexpr const size_t scriptIDMarkerLen{ 20 };
199  static constexpr const size_t scriptOwnerMarkerLen{ 16 };
200 
202  static AnnScriptID ID;
203 
213 
215  void tryAndGetEventHooks();
216  };
217 
219 }
bool needUpdate() override
This subsystem doesn&#39;t need to be updated.
std::function< void(chaiscript::Boxed_Value &, AnnHandControllerEvent)> callHandControllertOnScriptInstance
Hook for Hand Controller event.
file for defining macros used for symbol export regarding the way the OS does things.
Object that represent a light source.
Collision between the player and another object.
Definition: AnnEvents.hpp:395
std::function< void(chaiscript::Boxed_Value &, AnnTriggerEvent)> callTriggerEventOnScriptInstance
std::function< void(chaiscript::Boxed_Value &, AnnPlayerCollisionEvent)> callPlayerCollisionEventOnScriptInstance
Hook for PlayerCollision event.
std::function< void(chaiscript::Boxed_Value &, AnnTimeEvent)> callTimeEventOnScriptInstance
Hook for Time event.
AnnScriptFileResourceManager * scriptFileManager
Pointer to the script manager.
Chaiscript File ResourceManager.
std::function< void(chaiscript::Boxed_Value &, AnnControllerEvent)> callStickEventOnScriptInstance
Hook for Stick event.
std::function< void(chaiscript::Boxed_Value &, AnnKeyEvent)> callKeyEventOnScriptInstance
Hook for key event.
std::function< void(chaiscript::Boxed_Value &, AnnCollisionEvent)> callCollisionEventOnScriptInstance
void update() override
This method does nothing.
std::function< void(chaiscript::Boxed_Value &, AnnControllerEvent)> callStickEventOnScriptInstance
std::function< void(chaiscript::Boxed_Value &, AnnMouseEvent)> callMouseEventOnScriptInstance
Namespace containing the totality of Annwvyn components.
Definition: AnnGetter.cpp:8
A joystick event.
Definition: AnnEvents.hpp:240
std::function< void(chaiscript::Boxed_Value &, AnnTriggerEvent)> callTriggerEventOnScriptInstance
Hook for Trigger event.
An object that exist in the game. Graphically and Potentially Physically.
A timer timeout event.
Definition: AnnEvents.hpp:342
chaiscript::ChaiScript chai
ChaiScript engine.
Object that reprenset a script defining an object "behavior".
std::string name
Name of this script. Name of the "class" defined in the script.
std::tuple< std::function< void(chaiscript::Boxed_Value &, AnnKeyEvent)>, std::function< void(chaiscript::Boxed_Value &, AnnMouseEvent)>, std::function< void(chaiscript::Boxed_Value &, AnnControllerEvent)>, std::function< void(chaiscript::Boxed_Value &, AnnTimeEvent)>, std::function< void(chaiscript::Boxed_Value &, AnnTriggerEvent)>, std::function< void(chaiscript::Boxed_Value &, AnnHandControllerEvent)>, std::function< void(chaiscript::Boxed_Value &, AnnCollisionEvent)>, std::function< void(chaiscript::Boxed_Value &, AnnPlayerCollisionEvent)> > AnnBehaviorScriptHooks
std::function< void(chaiscript::Boxed_Value &, AnnPlayerCollisionEvent)> callPlayerCollisionEventOnScriptInstance
unsigned long long uID
Definition: AnnTypes.h:39
std::function< void(chaiscript::Boxed_Value &, AnnTimeEvent)> callTimeEventOnScriptInstance
A hand controller event.
Definition: AnnEvents.hpp:300
Script Manager, serve as an interface between ChaiScript and the rest of the engine.
std::function< void(chaiscript::Boxed_Value &, AnnHandControllerEvent)> callHandControllertOnScriptInstance
Parent class of all Annwvyn SubSystem.
static AnnScriptID ID
Static counter that will be incremented at each script creation.
const bool valid
Validity state of this object. Cannot change.
event management for Annwvyn
A keyboard event.
Definition: AnnEvents.hpp:37
void callUpdateOnScript()
Just call the update on the instance.
std::function< void(chaiscript::Boxed_Value &)> callUpdateOnScriptInstance
The "update" function. ChaiScript "object.update()" is like calling "update(object)".
std::function< void(chaiscript::Boxed_Value &, AnnCollisionEvent)> callCollisionEventOnScriptInstance
Hook for Collision event.
Data types definition for Annwvyn.
#define AnnDllExport
Definition: systemMacro.h:24
std::function< void(chaiscript::Boxed_Value &, AnnKeyEvent)> callKeyEventOnScriptInstance
To create the event hooks for the scripts :
std::function< void(chaiscript::Boxed_Value &, AnnMouseEvent)> callMouseEventOnScriptInstance
Hook for Mouse event.
A mouse event information object.
Definition: AnnEvents.hpp:124
Trigger in/out event.
Definition: AnnEvents.hpp:409
#define LISTENER
Macro for declaring a listener.
Collision event between 2 game objects.
Definition: AnnEvents.hpp:362
chaiscript::Boxed_Value ScriptObjectInstance
The ChaiScript object that is an instance of "name" class. BlackBox to find what "update" to call on ...