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

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

It is also important to note that visual effects as the result of a behavior or action are different to those that affect the characteristics of the game's object. You may have a visual representation as the result of a behavior or action (for example, a particle effect when taking damage or the character swinging their sword to attack), but in Unity especially, you have to keep these separate. We will cover more on this later.

Note

In traditional games that are built using other systems, bundling visual interactions or audio with behaviors is quite common. However, Unity forces you to think differently; it recommends you to work with the system, not against it.

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.

It is far too easy to simply perform player.health = 10 rather than player.damage(10); this way any side effects or saves from damage can be taken into account within the class itself. Read on!

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 (just an example):

public void TakeDamage(int Amount) { Health –= Mathf.Clamp((Amount - Armor),0,int.MaxValue);}

public void Attack(Entity Entity)
  { Entity.TakeDamage(Strength); }

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.

Behaviors for the player's character

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

Note

If you are feeling adventurous using what was previously detailed, try sketching out what kind of behaviors a player's character might have and then compare/test them against future chapters.

主站蜘蛛池模板: 灌云县| 渝北区| 永城市| 万源市| 宜兰市| 仲巴县| 张家港市| 环江| 海盐县| 昆明市| 若羌县| 灵宝市| 米泉市| 泰兴市| 夏津县| 江安县| 台湾省| 河间市| 漳平市| 隆尧县| 宜春市| 永州市| 黄龙县| 靖边县| 庄浪县| 沙河市| 朝阳市| 鄂伦春自治旗| 建水县| 互助| 镇康县| 龙里县| 建始县| 五大连池市| 乐山市| 桐柏县| 奉化市| 和田县| 龙门县| 池州市| 镇原县|