跳转到内容

游戏开发指南/理论/游戏逻辑/创建实体类

50% developed
来自维基教科书,开放的书籍,开放的世界

注意:以下代码使用 Vector3粒子

这是一个用 C++ 编写的实体类可能看起来像的概要

Entity.h

#include "Vector3.h"
#include "Particle.h"
using namespace std;
 
class Entity: public Particle { 
public:
    //These functions are going to be overloaded, they are there as a template for the for loops above to call them.
    void Tick();
    void Render();

    Entity();
    ~Entity();

    Vector3 Scale;
};

Entity.cpp

#include "Entity.h"

void Entity::Tick(){
	Particle::Tick();
}

Entity::Entity(){}
Entity::~Entity(){}

示例子类

[编辑 | 编辑源代码]

car.cpp

#include "Entity.h"
class Car: public Entity { //I've used a car as an example, but you'll need one for every type of distinct object.
public:
    //Polymorphism used
    void Car::Tick(){
        Entity::Tick(); //Call parent function
 
        //Handle other things if necessary like AI
    }
    void Car::Render(){
        //Draw the geometry
    }
};
华夏公益教科书