The Entity Class

Let’s start with a new class called Entity. This simple class is more of a placeholder with a few minor properties used to identify the type of entity being subclassed. Some of the methods in Entity are declared as pure virtual, meaning you must subclass Entity into a new class; you cannot use an Entity alone. The properties are all important and are used by the entity manager to process the entities. Actually, the manager doesn’t really care whether your entity is a sprite, a mesh, or a Hobgoblin; it will just process the virtual methods and use the properties you provide it.

Although a strongly typed engine might define specific entity types with an enumeration or some constant values (such as ObjectType), I did not want to regulate the engine too much—it’s up to you to set the properties when you create your entity objects and add them to the manager, and then write the code to respond to the events based on object type. One very interesting property is lifetime (composed of two variables—lifetimeLength and lifetimeTimer). Using this property, you can set an entity to auto-expire after a fixed amount of time (measured in milliseconds). If you want an entity to participate in the game for only 10 seconds, you can set its lifetime to 10000, and it will be automatically removed when the time expires. This can be extremely handy for many types of games in which you would otherwise have to add logic to terminate things such as bullets and explosions manually.

However, there is one property that we must set in order to perform the correct type of rendering, either 2D or 3D. You cannot render 2D and 3D objects together because 2D sprites must be rendered by D3DXSprite within the 3D rendering pipeline. The Entity class, defined in a moment, includes an enumeration called RenderType that also falls inside the overall Advanced2D namespace (so it’s visible to the Entity class). We need to use this simple enumeration to determine whether an entity should be rendered in 2D or 3D. That will be done by modifying the Sprite and Mesh constructors later. The Entity class has a constructor with a mandatory parameter that is called by the constructors of Sprite and Mesh with the appropriate 2D or 3D setting. It’s automatic once the classes are defined.

#include "Advanced2D.h"
#pragma once
namespace Advanced2D {

    enum RenderType {
        RENDER2D = 0,
        RENDER3D = 1
    };

    class Entity {
    private:
        int id;
        std::string name;
        bool visible;
        bool alive;
        enum RenderType renderType;
        int objectType;
        int lifetimeLength;
        Timer lifetimeTimer;

    public:
        Entity(enum RenderType renderType);
        virtual ~Entity() { };
        virtual void move() = 0;
        virtual void animate() = 0;
        virtual void draw() = 0;
        void setID(int value) { id = value; }
        int getID() { return id; }
        void setRenderType(enum RenderType type) { renderType = type; }
        enum RenderType getRenderType() { return renderType; }
        std::string getName() { return name; }
        void setName(std::string value) { name = value; }
        bool getVisible() { return visible; }
        void setVisible(bool value) { visible = value; }
        bool getAlive() { return alive; }
        void setAlive(bool value) { alive = value; }

        int getLifetime() { return lifetimeLength; }
        void setLifetime(int milliseconds) {
            lifetimeLength = milliseconds; lifetimeTimer.reset();
        }
        bool lifetimeExpired() {
            return lifetimeTimer.stopwatch(lifetimeLength);
        }
        int getObjectType() { return objectType; }
        void setObjectType(int value) { objectType = value; }
    };
};

Here is the Entity class implementation. All we need here is the constructor to initialize the property variables; otherwise, the Entity class is mostly made up of accessor and mutator methods in the header. Note that Entity does not have a default constructor, only one with the RenderType parameter. You must tell an entity whether it should be rendered in 2D or 3D, and this takes care of that requirement.

#include "Advanced2D.h"
namespace Advanced2D {
    Entity::Entity(enum RenderType renderType)
    {
        this->renderType = renderType;
        this->id = -1;
        this->name = "";
        this->visible = true;
        this->alive = true;
        this->objectType = 0;
        this->lifetimeLength = 0;
        this->lifetimeTimer.reset();
    }
};

The three pure virtual methods are Entity:move(), Entity::animate(), and Entity::draw(), which means these three must, at minimum, be implemented in a subclass. We’ll get to that in a bit, with the Sprite and Mesh classes. First, we need to make some changes to the core engine.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset