Program Listing for File node.h

Return to documentation for file (simple2dengine/nodes/node.h)

#ifndef _SIMPLE2DENGINE_NODES_NODE_H_
#define _SIMPLE2DENGINE_NODES_NODE_H_

#include <memory>
#include <string>
#include <vector>

#include "SFML/Graphics/Rect.hpp"
#include "SFML/System/Vector2.hpp"
#include "SFML/Window/Event.hpp"

namespace simple2dengine
{
    enum class NodeState : unsigned int
    {
        None,
        Creating,
        Entering,
        Updating,
        Exiting,
        Destroying
    };

    class Engine;
    class Node : public std::enable_shared_from_this<Node>
    {
      public:
        Node(const std::string& nodeName) : name(nodeName){};

        virtual ~Node(){};

        virtual void onCreate(){};
        virtual void onEnter(){};
        virtual void onUpdate(int /*deltaInMs*/){};
        virtual void onInput(sf::Event /*event*/){};
        virtual void onExit(){};
        virtual void onDestroy(){};
        bool addChild(std::shared_ptr<Node> child);
        bool removeChild(const std::string& childName);
        void clear();
        int getIndex() const;
        const std::string& getName() const;

        std::shared_ptr<Node> getParent() const;
        std::shared_ptr<Node> getRoot();
        const std::vector<std::shared_ptr<Node>>& getChildren() const;
        std::shared_ptr<Node> getNode(const std::string& path);

      protected:
        virtual void update(int deltaInMs);
        virtual void render();

        Engine* engine = nullptr; // engine pointer

      private:
        void notifyCreate();
        void notifyEnter();
        void notifyInput(sf::Event event);
        void notifyExit();
        void notifyDestroy();

      private:
        std::vector<std::shared_ptr<Node>> children; // all child nodes
        std::weak_ptr<Node> parent;                  // parent node

        std::string name; // name of node
        int index = 0;    // index of node in its parent

        NodeState state = NodeState::None; // current state of node

        friend class SceneManager;
    };
} // namespace simple2dengine

#endif // _SIMPLE2DENGINE_NODES_NODE_H_