- Game Development Patterns and Best Practices
- John P. Doran Matt Casanova
- 339字
- 2021-07-02 23:43:48
The problem with object behavior
So far, we have only considered what members the game object has. We haven't considered how each object will have its behavior updated. Right now, the game object is just data. Since it has no functions, it can't update itself. We could easily add an Update function for the game object but, in order to update each type of object correctly, we would need a switch statement:
//Create our objects
Object gameObjects[MAX_COUNT];
//initialization code here
//...
//Update loop
for(int i = 0; i < objectInUse; ++i)
{
switch(gameObjects[i].type)
{
case OT_PLAYER:
//Update based on input
break;
case OT_SUPER_RAIDER:
//Add intercept code here
break;
case OT_SUPER_BOMBER:
//Add case code here
break;
case OT_MISSILE:
//Add find target and chase code here
break;
case OT_BOMB:
//add grow to max radius code here
break;
default:
M5DEBUG_ASSERT(true, "Incorrect Object Type");
}
}
Again, this approach doesn't scale well. As we add more object types, we need to add even more cases to our switch statement. Since we only have one struct type, we need to have a switch statement, whenever we need to do something object-type-specific.
If we are adding behaviors, we will also face the decision of adding data to our object or hardcoding a value into the switch statement. For example, if our bomb grows in size, how does it grow? We could hard code scale.x *= 1.1f into our switch statement or we can add member data float bombScaleFactor to our struct.
In the end, this approach just isn't that flexible. Changing our design is very difficult because there are switch statements and public members throughout our code. If we were to make a game like this, then our code base would be a complete mess after only a few months. The worst part would be that once the game was completed, we wouldn't be able to reuse any code. The game object and all behaviors would be so gameplay-specific that unless we make a sequel, we would need to remake a brand new game object.
- Java面向對象思想與程序設計
- Apache Hive Essentials
- DevOps Automation Cookbook
- Animate CC二維動畫設計與制作(微課版)
- PLC編程及應用實戰(zhàn)
- Visual C++應用開發(fā)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(1)
- 用戶體驗可視化指南
- JSP程序設計實例教程(第2版)
- Programming Microsoft Dynamics? NAV 2015
- 零基礎學C++(升級版)
- WordPress Search Engine Optimization(Second Edition)
- Go Systems Programming
- Vue.js 3.x高效前端開發(fā)(視頻教學版)
- C語言王者歸來