#pragma once #include #include #define GO_DEBUG 0 /** * Simple class to demonstrate Interceptors, Accessors, Handles, and object * creation in v8 * * NOTE: I've used GameObject to help you think about the functionality as it * relates to scripting, but this could be any kind of objet (i.e., Events).. */ class GameObject { public: GameObject(); ~GameObject(); std::string guid; int x; int y; /** * This function will make this class instance accessible to scripts in * the given context. * * IMPORTANT: Please read this definition of this function in * GameObject.cpp. The helper function I've provided expects certain * parameters which you must use in order to take advance of this * convinience. */ v8::Local exposeToV8(v8::Isolate *isolate, v8::Local &context, std::string context_name="default"); /** * Static function to keep track of current total number of * gameobjects, to facilitate creating GUIDs by incrementing a counter. */ static int getCurrentGUID(); /** * Shared list of all instances of game objects...auto populated by the * constructor. */ static std::vector game_objects; /** * Factor methods for creating new instances of GameObjects. * * This is primarily a demonstration of how you can create new objects * in javascript. NOTE: this is embedded in this GameObject class, but * could (and likely should) be used for other functionality as well * (i.e., Events). */ static GameObject* GameObjectFactory(std::string context_name="default"); static void ScriptedGameObjectFactory(const v8::FunctionCallbackInfo& args); private: /** Static variable to keep count of current number of GameObject instances */ static int current_guid; v8::Isolate* isolate; v8::Global* context; /** * NOTE: These "Accessors" have to be **static** * * You will need to implement a setter and getter for every class * member variable you want accessible to javascript. */ static void setGameObjectX(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info); static void getGameObjectX(v8::Local property, const v8::PropertyCallbackInfo& info); // note return type static void setGameObjectY(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info); static void getGameObjectY(v8::Local property, const v8::PropertyCallbackInfo& info); // note return type static void setGameObjectGUID(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info); static void getGameObjectGUID(v8::Local property, const v8::PropertyCallbackInfo& info); // note return type };