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

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.

主站蜘蛛池模板: 宜黄县| 勃利县| 陆丰市| 阳山县| 夏河县| 岗巴县| 大冶市| 辽宁省| 晴隆县| 阿拉善盟| 高碑店市| 洪湖市| 惠来县| 深水埗区| 浮山县| 广东省| 会东县| 栾城县| 习水县| 万荣县| 徐水县| 井陉县| 云南省| 邯郸县| 武夷山市| 长汀县| 昌图县| 广西| 民勤县| 吉木乃县| 灵石县| 乌兰县| 楚雄市| 金华市| 大庆市| 哈巴河县| 桂东县| 枣庄市| 内丘县| 景东| 麻阳|