跳轉到內容

遊戲開發指南/理論/遊戲邏輯/建立實體類

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
    }
};
華夏公益教科書