Data organization in the assignment Marek Trtík PA199 2 Typical game architecture Math Graphics Physics Audio Network AI Animation Engine libraries Scripting … Game data Game mechanics Game libraries Quests/story system … Our Focus 3  General enough for games:  Game objects are often composed in an acyclic hierarchies, e.g.,:  Car – the wheels and doors are attached to the body.  Animation skeleton – bones form a tree.  Items (like weapons) are attached to an agent/player.  Simple to implement and easy to traverse (tree DFS, BFS): class GameNode { GameNodeWeakPtr parent; std::list children; … Game data: Tree structure 4 class GameNodeHierarchy { // The only owner of all nodes GameNodePtr root; public: static GameNodeHierarchy& instance(); // Singleton template std::shared_ptr push_back_child( GameNodePtr parent, ParameterTypes... args); … Game data: Tree structure 5  A component is a class instance of some Engine/Game library.  Holds specific data.  May also provide a functionality – code.  Allows for a data-driven approach:  Attaching components to a GameNode => Specification of node’s purpose in the game.  Intuitive and easy to use.  Also easy to implement: Game data: Component based 6 class Component { bool active; GameNode* node; // The node this component is attached to. public: virtual ~Component() {} // IMPORTANT: Allows for an inheritance! …  We need to extend GameNode to store attached components: Component system 7 class GameNode { … std::list components; public: template std::shared_ptr find_component() const; void push_back_component(ComponentPtr component); …  An important component for a game is a frame of reference: Component system 8 class Frame : public Component { // Frame of reference FramePtr parent; // A frame in which this one is defined. // We do not need to know about children. Vec3 origin; Quat orientation; Vec3 scale; // Optional; for uniform scaling use just: float scale; mutable Mat44* to_world; // Cached; compute on demand. mutable Mat44* from_world; // Cached; compute on demand. … Frame of reference 9  We can define a script component: class GameScript : public Component { public: virtual void update() {} // To be called by ScriptingEngine };  A simple scripting engine can then be defined as follows: Scripting in C++ 10 class ScriptingEngine { std::list scripts; public: static ScriptingEngine& instance(); // Singleton void update(); // Call ‘update’ on each script. template std::shared_ptr create_script(ParameterTypes... args) { auto script = std::make_shared(args...); scripts.push_back(script); // Keep track of all created scripts. return script; } … Scripting in C++ 11 Example: Data hierarchy of our game Ball SphereCollider SphereController … Frame Paddles PaddlesController Frame Paddle1 … Frame Wall WallController Frame Brick1 Frame … Ground Frame … … … Root 12  A sketch of an implementation of the discussed topic is in IS: game_data_hierarchy.ZIP Reference