官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 巢湖市| 政和县| 乌恰县| 增城市| 开平市| 新郑市| 建平县| 雷山县| 通许县| 白河县| 古丈县| 定兴县| 鹤壁市| 苏尼特右旗| 奉新县| 长宁县| 宜川县| 石台县| 嵩明县| 陵川县| 广德县| 遂溪县| 江北区| 嘉义市| 曲阜市| 台东县| 金华市| 衡南县| 仪陇县| 深水埗区| 迁安市| 湘潭市| 西和县| 家居| 玉龙| 莎车县| 平南县| 伊金霍洛旗| 崇州市| 宣威市| 沭阳县|