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