2dRogueLike源码分析Enemy
2018-02-11 16:34
155 查看
1.调用unity包using UnityEngine;
using System.Collections;2.定义enemy类继承于MovingObject
属性:给予玩家的伤害,动画控制,每次移动位置,是否跳过移动 //Enemy inherits from MovingObject, our base class for objects that can move, Player also inherits from this.
public class Enemy : MovingObject
{
public int playerDamage; //The amount of food points to subtract from the player when attacking.
private Animator animator; //Variable of type Animator to store a reference to the enemy's Animator component.
private Transform target; //Transform to attempt to move toward each turn.
private bool skipMove; //Boolean to determine whether or not enemy should skip a turn or move this turn.
3.将enemy加入gameManager
得到animator
找到player
执行父类的start函数 //Start overrides the virtual Start function of the base class.
protected override void Start ()
{
//Register this enemy with our instance of GameManager by adding it to a list of Enemy objects.
//This allows the GameManager to issue movement commands.
GameManager.instance.AddEnemyToList (this);
//Get and store a reference to the attached Animator component.
animator = GetComponent<Animator> ();
//Find the Player GameObject using it's tag and store a reference to its transform component.
target = GameObject.FindGameObjectWithTag ("Player").transform;
//Call the start function of our base class MovingObject.
base.Start ();
}4.重写AttemptMove函数
如果skipMove为真,则跳过 //Override the AttemptMove function of MovingObject to include functionality needed for Enemy to skip turns.
//See comments in MovingObject for more on how base AttemptMove function works.
protected override void AttemptMove <T> (int xDir, int yDir)
{
//Check if skipMove is true, if so set it to false and skip this turn.
if(skipMove)
{
skipMove = false;
return;
}
//Call the AttemptMove function from MovingObject.
base.AttemptMove <T> (xDir, yDir);
//Now that Enemy has moved, set skipMove to true to skip next move.
skipMove = true;
}5.根据player的位置,决定自己移动方向 //MoveEnemy is called by the GameManger each turn to tell each Enemy to try to move towards the player.
public void MoveEnemy ()
{
//Declare variables for X and Y axis move directions, these range from -1 to 1.
//These values allow us to choose between the cardinal directions: up, down, left and right.
int xDir = 0;
int yDir = 0;
//If the difference in positions is approximately zero (Epsilon) do the following:
if(Mathf.Abs (target.position.x - transform.position.x) < float.Epsilon)
//If the y coordinate of the target's (player) position is greater than the y coordinate of this enemy's position set y direction 1 (to move up). If not, set it to -1 (to move down).
yDir = target.position.y > transform.position.y ? 1 : -1;
//If the difference in positions is not approximately zero (Epsilon) do the following:
else
//Check if target x position is greater than enemy's x position, if so set x direction to 1 (move right), if not set to -1 (move left).
xDir = target.position.x > transform.position.x ? 1 : -1;
//Call the AttemptMove function and pass in the generic parameter Player, because Enemy is moving and expecting to potentially encounter a Player
AttemptMove <Player> (xDir, yDir);
}6.重写OnCantMove函数
player损失food
enemy执行enemyAttack动画 //OnCantMove is called if Enemy attempts to move into a space occupied by a Player, it overrides the OnCantMove function of MovingObject
//and takes a generic parameter T which we use to pass in the component we expect to encounter, in this case Player
protected override void OnCantMove <T> (T component)
{
//Declare hitPlayer and set it to equal the encountered component.
Player hitPlayer = component as Player;
//Call the LoseFood function of hitPlayer passing it playerDamage, the amount of foodpoints to be subtracted.
hitPlayer.LoseFood (playerDamage);
//Set the attack trigger of animator to trigger Enemy attack animation.
animator.SetTrigger ("enemyAttack");
}
}
using System.Collections;2.定义enemy类继承于MovingObject
属性:给予玩家的伤害,动画控制,每次移动位置,是否跳过移动 //Enemy inherits from MovingObject, our base class for objects that can move, Player also inherits from this.
public class Enemy : MovingObject
{
public int playerDamage; //The amount of food points to subtract from the player when attacking.
private Animator animator; //Variable of type Animator to store a reference to the enemy's Animator component.
private Transform target; //Transform to attempt to move toward each turn.
private bool skipMove; //Boolean to determine whether or not enemy should skip a turn or move this turn.
3.将enemy加入gameManager
得到animator
找到player
执行父类的start函数 //Start overrides the virtual Start function of the base class.
protected override void Start ()
{
//Register this enemy with our instance of GameManager by adding it to a list of Enemy objects.
//This allows the GameManager to issue movement commands.
GameManager.instance.AddEnemyToList (this);
//Get and store a reference to the attached Animator component.
animator = GetComponent<Animator> ();
//Find the Player GameObject using it's tag and store a reference to its transform component.
target = GameObject.FindGameObjectWithTag ("Player").transform;
//Call the start function of our base class MovingObject.
base.Start ();
}4.重写AttemptMove函数
如果skipMove为真,则跳过 //Override the AttemptMove function of MovingObject to include functionality needed for Enemy to skip turns.
//See comments in MovingObject for more on how base AttemptMove function works.
protected override void AttemptMove <T> (int xDir, int yDir)
{
//Check if skipMove is true, if so set it to false and skip this turn.
if(skipMove)
{
skipMove = false;
return;
}
//Call the AttemptMove function from MovingObject.
base.AttemptMove <T> (xDir, yDir);
//Now that Enemy has moved, set skipMove to true to skip next move.
skipMove = true;
}5.根据player的位置,决定自己移动方向 //MoveEnemy is called by the GameManger each turn to tell each Enemy to try to move towards the player.
public void MoveEnemy ()
{
//Declare variables for X and Y axis move directions, these range from -1 to 1.
//These values allow us to choose between the cardinal directions: up, down, left and right.
int xDir = 0;
int yDir = 0;
//If the difference in positions is approximately zero (Epsilon) do the following:
if(Mathf.Abs (target.position.x - transform.position.x) < float.Epsilon)
//If the y coordinate of the target's (player) position is greater than the y coordinate of this enemy's position set y direction 1 (to move up). If not, set it to -1 (to move down).
yDir = target.position.y > transform.position.y ? 1 : -1;
//If the difference in positions is not approximately zero (Epsilon) do the following:
else
//Check if target x position is greater than enemy's x position, if so set x direction to 1 (move right), if not set to -1 (move left).
xDir = target.position.x > transform.position.x ? 1 : -1;
//Call the AttemptMove function and pass in the generic parameter Player, because Enemy is moving and expecting to potentially encounter a Player
AttemptMove <Player> (xDir, yDir);
}6.重写OnCantMove函数
player损失food
enemy执行enemyAttack动画 //OnCantMove is called if Enemy attempts to move into a space occupied by a Player, it overrides the OnCantMove function of MovingObject
//and takes a generic parameter T which we use to pass in the component we expect to encounter, in this case Player
protected override void OnCantMove <T> (T component)
{
//Declare hitPlayer and set it to equal the encountered component.
Player hitPlayer = component as Player;
//Call the LoseFood function of hitPlayer passing it playerDamage, the amount of foodpoints to be subtracted.
hitPlayer.LoseFood (playerDamage);
//Set the attack trigger of animator to trigger Enemy attack animation.
animator.SetTrigger ("enemyAttack");
}
}
相关文章推荐
- 2dRogueLike源码分析Player
- suricata 3.1 源码分析7
- Android系统底层启动源码分析(开机启动)
- jQuery-1.9.1源码分析系列(十一)DOM操作续之克隆节点
- FreeRTOS源码分析之heap3.c
- Android源码分析-Alarm机制与Binder的交互
- libevent2源码分析之五:关键的调用链
- LinkedList源码分析
- SSH框架总结(框架分析+环境搭建+实例源码下载)
- [Abp 源码分析]五、系统设置
- IntentService源码分析
- ros源码分析(5)—rosmaster xmlrpc api
- 微信 Mars Android Sample 源码分析
- JDK源码分析之concurrent包(三) -- Future方式的实现
- Hessian源码分析--HessianProxy
- ffmpeg源码分析--15.命令说明
- java中的==、equals()、hashCode()源码分析
- 灵活且强大的RecyclerViewAdapter源码分析
- Fragment源码分析(一) 构造