Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Planet-> Vector of all Actors
- Actor-> Vector of all Behaviors
- Behaviour-> Game Logic
- class Actor;
- class Behaviour
- {
- protected:
- Actor* actor;
- public:
- string type;
- Behaviour();
- ~Behaviour();
- virtual void Init(); // virtual because every component needs to override them
- virtual void Tick();
- }
- // Actor.h
- class Planet;
- class Actor
- {
- private:
- unsigned id;
- string name;
- vector< unique_ptr< Behaviour > > allBehaviours;
- protected:
- Planet* planet;
- public:
- Actor();
- ~Actor();
- void Init(); // Go through all the behaviors and call their Inits and Ticks
- void Tick();
- template <typename T, typename ...Args>
- void AddBehaviour(Args && ...args); // Creates and adds a new Behaviour of type T to the allBehaviours vector
- template <typename T>
- T& GetBehaviour(std::string type); // Loops through all the Behaviours, and returns the Behaviour whose type is the same as type
- // Actor.cpp
- #include "Planet.h"
- #include "Behaviour.h"
- #include "Actor.h"
- // Implement Functions
- // Planet.h
- class Actor;
- class Planet
- {
- private:
- unsigned idCounter = 0;
- string name;
- vector< unique_ptr< Actor > > allActors;
- public:
- unsigned GetNewID();
- void Init();
- void Tick(); // Used to loop through all the Actors
- void AddActor(Actor* newActor); // adds a new Actor to the list of all Actors
- void KillActor(int id); // loops through all the Actors and when it finds an Actor whose unique id is matching the id variable it erases it from the allActors vector
- }
- // Planet.cpp
- #include "Public.h"
- #include "Actor.h"
- #include "Planet.h"
- Planet::KillActor(int id){
- unsigned i = 0;
- for (auto& a : allActors) {
- if (a->GetID() == id)
- allActors.erase(allActors.begin() + i);
- i++;
- }
- }
- // Other Functions
- {
- // other stuff
- win.GetCurrentPlanet()->AddActor(this);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement