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

Planning behaviors

Behaviors are just a fancy way of saying things or interactions that will happen in the game. Breaking down these actions or reactions in this way helps to componentize how we think our game will work. Stopping and thinking about this from the very beginning means we won't get too many surprises later on. (There are always surprises after a good night's sleep.)

For example, behaviors can take the following forms:

  • Attacking another entity
  • Taking damage
  • Collecting the loot, which could be money or items
  • Teleporting to another land

Behaviors on classes should only affect the class that it is defined on. If you are going to affect another class's attributes, it should be through another behavior on that class.

Behaviors for the common game object

As we have an existing class for common game objects (Entity), we can start to define some behaviors that are common to all the characters in our RPG game, namely the following objects:

  • TakeDamage: This is an object where a character can be damaged. Keeping this object as common ensures that the calculation of damages is the same for all.
  • Attack: This is an object where a character can attack another character; if successful, it deals with damage or, in rare occurrences, it makes characters hurt themselves. Again, having one way to calculate this helps in battle games so that attacks are balanced.

So, if we add these behaviors to our Entity object, we get something that looks like the following screenshot:

The behaviors shown in the preceding screenshot would add the following to the Entity class code:

public void TakeDamage(int Amount) { 
    health=health-Mathf.Clamp((Amount-armor),0,int.MaxValue); 
} 
 
public void Attack(Entity Entity){ 
    Entity.TakeDamage(strength); 
} 

Tip

Make sure the preceding code is within the Entity class braces.

We'll not implement these actual behaviors in the code just yet as we will cover them in more detail when we visit the battle system. For now, we are just setting the ground work for what we expect to use in the game.

For now, we won't add any further behaviors to the player; we will simply evolve it as we require it.

主站蜘蛛池模板: 富平县| 萍乡市| 仁怀市| 镇坪县| 罗定市| 巴彦淖尔市| 旌德县| 蕉岭县| 石景山区| 卫辉市| 布尔津县| 商丘市| 陇南市| 克拉玛依市| 大余县| 怀化市| 手游| 静海县| 南城县| 闽侯县| 来安县| 张掖市| 元江| 铁岭市| 开化县| 余江县| 景德镇市| 绩溪县| 九龙城区| 沙田区| 亚东县| 芦山县| 安陆市| 营口市| 邯郸县| 南溪县| 蚌埠市| 右玉县| 吴忠市| 兴宁市| 景谷|