You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classAIComponent{public: void update() { /* Work with and modify state... */ }private: // Goals, mood, etc. ...};class PhysicsComponent{public: void update() { /* Work with and modify state... */ }private: // Rigid body, velocity, mass, etc. ...};class RenderComponent{public: void render() { /* Work with and modify state... */ }private: // Mesh, textures, shaders, etc. ...};
149
+
classAIComponent
150
+
{
151
+
public:
152
+
void update() { /* Work with and modify state... */ }
153
+
private:
154
+
// Goals, mood, etc. ...
155
+
};
156
+
157
+
classPhysicsComponent
158
+
{
159
+
public:
160
+
void update(){ /* Work with and modify state... */ }
161
+
162
+
private:
163
+
// Rigid body, velocity, mass, etc. ...
164
+
};
165
+
166
+
classRenderComponent
167
+
{
168
+
public:
169
+
void render() { /* Work with and modify state... */ }
170
+
171
+
private:
172
+
// Mesh, textures, shaders, etc. ...
173
+
};
130
174
```
131
175
游戏维护一个很大的指针数组,它们包含了对游戏世界中所有实体的引用。每次游戏循环我们需要做以下工作:
132
176
@@ -138,7 +182,28 @@ class AIComponent{public: void update() { /* Work with and modify state... */ }
138
182
139
183
许多游戏实体将这样进行实现:
140
184
```c++
141
-
while (!gameOver){ // Process AI. for (int i = 0; i < numEntities; i++) { entities[i]->ai()->update(); } // Update physics. for (int i = 0; i < numEntities; i++) { entities[i]->physics()->update(); } // Draw to screen. for (int i = 0; i < numEntities; i++) { entities[i]->render()->render(); } // Other game loop machinery for timing...}
AIComponent* aiComponents = new AIComponent[MAX_ENTITIES];PhysicsComponent* physicsComponents = new PhysicsComponent[MAX_ENTITIES];RenderComponent* renderComponents = new RenderComponent[MAX_ENTITIES];
while (!gameOver){ // Process AI. for (int i = 0; i < numEntities; i++) { aiComponents[i].update(); } // Update physics. for (int i = 0; i < numEntities; i++) { physicsComponents[i].update(); } // Draw to screen. for (int i = 0; i < numEntities; i++) { renderComponents[i].render(); } // Other game loop machinery for timing...}
void ParticleSystem::activateParticle(int index){ // Shouldn't already be active! assert(index >= numActive_); // Swap it with the first inactive particle // right after the active ones. Particle temp = particles_[numActive_]; particles_[numActive_] = particles_[index]; particles_[index] = temp; // Now there's one more. numActive_++;}
357
+
voidParticleSystem::activateParticle(int index)
358
+
{
359
+
// Shouldn't already be active!
360
+
assert(index >= numActive_);
361
+
362
+
// Swap it with the first inactive particle
363
+
// right after the active ones.
364
+
Particle temp = particles_[numActive_];
365
+
particles_[numActive_] = particles_[index];
366
+
particles_[index] = temp;
367
+
368
+
// Now there's one more.
369
+
numActive_++;
370
+
}
234
371
```
235
372
反激活粒子就只要反其道而行之:
236
373
237
374
```c++
238
-
voidParticleSystem::deactivateParticle(int index){ // Shouldn't already be inactive! assert(index < numActive_); // There's one fewer. numActive_--; // Swap it with the last active particle // right before the inactive ones. Particle temp = particles_[numActive_]; particles_[numActive_] = particles_[index]; particles_[index] = temp;}
- Tony Albrecht著的“[Pitfalls of Object-Oriented Programming](http://research.scee.net/files/presentations/gcapaustralia09/Pitfalls_of_Object_Oriented_Programming_GCAP_09.pdf)”一书大概是介绍关于游戏内数据结构设计来实现缓存友好性的材料中最被广泛阅读的了。它使得许多人(包括我!)意识到这样对数据结构的设计是多么地重要。
0 commit comments