- 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.
- Learning Flask Framework
- Python Deep Learning
- TypeScript實(shí)戰(zhàn)指南
- Drupal 8 Module Development
- TypeScript項目開發(fā)實(shí)戰(zhàn)
- 深入RabbitMQ
- Python Projects for Kids
- Java EE架構(gòu)設(shè)計與開發(fā)實(shí)踐
- Professional JavaScript
- 少兒編程輕松學(xué)(全2冊)
- HikariCP數(shù)據(jù)庫連接池實(shí)戰(zhàn)
- Spring Boot 2+Thymeleaf企業(yè)應(yīng)用實(shí)戰(zhàn)
- 計算機(jī)軟件項目實(shí)訓(xùn)指導(dǎo)
- Visual Basic 開發(fā)從入門到精通
- Instant OpenCV for iOS