攻击多个目标

来源:互联网 发布:公务员数学题软件 编辑:程序博客网 时间:2024/05/17 17:43
using System.Collections;
using System.Collections.Generic;
using UnityEngine;




public class Attribute : MonoBehaviour {
public string name;
public float HP;
public float MP;
public float Atk;
public float Def;
public float Speed;
public bool isDead;
public int attackType;
public float bloodPercent;
public float intialBlood;
public Vector2 initialPosition;
public Quaternion initialRotation;
public int targetWeights;


public int row;
public int column;


public bool faction; //阵营
BattleManager battleManager;


private Talant talant;
public Talant talant0;
public Talant talant1;
public Talant talant2;
public Talant talant3;


private bool isMoveToTarget;
private bool isMoveBack;


private float distanceToTarget;
private float distanceToInitial;


public List<GameObject> targetObjs;
public List<GameObject> enemyObjs;
private GameObject targetObj;
private Animator animator;
private float moveSpeed = 5;


private Vector2 stopPosition;




void Start(){
intialBlood = HP;
bloodPercent = HP/intialBlood;
animator = this.GetComponentInChildren<Animator> ();
battleManager = GameObject.Find ("battleManager").GetComponent<BattleManager> ();
targetObjs = new List<GameObject>();
}




public void receiveDamage(float damage){
HP -= damage;
bloodPercent = HP / intialBlood;
if(HP <= 0){
isDead = true;
gameObject.tag = "DeadUnit";
}
}


public void startToAct(){
chooseTanlat ();
attackAllTarget ();
}
void chooseTanlat(){
int sumTalantWeights = talant0.TriggerWeights + talant1.TriggerWeights + talant2.TriggerWeights + talant3.TriggerWeights;
int randomTalantWeights = Random.Range (0,sumTalantWeights);
if(randomTalantWeights <= talant1.TriggerWeights){
talant = talant1;
}else if(randomTalantWeights <= talant1.TriggerWeights + talant2.TriggerWeights){
talant = talant2;
}else if(randomTalantWeights <= talant1.TriggerWeights + talant2.TriggerWeights + talant3.TriggerWeights){
talant = talant2;
}else{
talant = talant0;
}
getTargetObjs (1);
//getTargetObjs (talant.TargetType);
}


public void attackAllTarget(){

Debug.Log ("当前目标总量为" + targetObjs.Count);
if (targetObjs.Count > 0) {
targetObj = targetObjs [0];
Debug.Log ("当前目标为" + targetObj.GetComponent<Attribute>().name);
targetObjs.Remove (targetObj);
if (targetObj.GetComponent<Attribute> ().isDead == false) {
isMoveToTarget = true;
} else {
isMoveToTarget = false;
}


} else {
isMoveBack = true;
}


}
void attack(){

animator.SetTrigger ("Atk");
float attackAnimationTime = animator.runtimeAnimatorController.animationClips[3].length;
float damageAnimationTime = attackAnimationTime * 0.4f;
StartCoroutine (targetObj.GetComponent<Attribute>().WaitForTakeDamage(damageAnimationTime));
StartCoroutine (WaitForRunBack(attackAnimationTime));
}
void Update(){
//移动到目标
if (isMoveToTarget) {
Debug.Log ("开始移动");
distanceToTarget = Vector2.Distance (targetObj.transform.localPosition, this.transform.position);


if (distanceToTarget > 1f) {
animator.SetBool ("Move", true);
this.transform.localPosition = Vector2.MoveTowards (this.transform.localPosition, targetObj.transform.position, moveSpeed * Time.deltaTime);
} else {
animator.SetBool ("Move", false);
isMoveToTarget = false;
stopPosition = this.transform.position;
Debug.Log ("攻击目标为" + targetObj.GetComponent<Attribute>().name);
attack ();
}
}
if(isMoveBack){
//判定返回时的朝向
if (this.initialPosition.x > this.transform.position.x) {
this.transform.rotation = Quaternion.AngleAxis (180, Vector3.up);
} else {
this.transform.rotation = Quaternion.AngleAxis (0, Vector3.up);
}
//返回时的距离
distanceToInitial = Vector2.Distance (this.transform.position,this.initialPosition);


if (distanceToInitial > 0) {


animator.SetBool ("Move", true);
this.transform.localPosition = Vector2.MoveTowards (this.transform.localPosition,initialPosition,moveSpeed * Time.deltaTime);
} else {
animator.SetBool ("Move", false);
isMoveBack = false;
//修正初始位置和朝向
this.transform.position = initialPosition ;
this.transform.rotation = initialRotation ;
battleManager.toBattle ();
}
}
}
IEnumerator WaitForTakeDamage(float time){




yield return new WaitForSeconds (time);




if (this.isDead) {
//先跑回去再死
animator.SetBool ("Death",true);
} else {
animator.SetTrigger ("Damaged");
}
}




IEnumerator WaitForRunBack(float time){




yield return new WaitForSeconds (time +0.25f);
attackAllTarget ();




}


public void getTargetObjs(int targetType){

switch(targetType){
case 1:
if (this.faction) {
enemyObjs = GameObject.Find ("battleManager").GetComponent<BattleManager> ().battleNpcList;
} else {
enemyObjs = GameObject.Find ("battleManager").GetComponent<BattleManager> ().battleUserList;
}
int sumTargetWeights = 0;
int sumTargetRange = 0;
foreach(GameObject enemy in enemyObjs){
sumTargetWeights += enemy.GetComponent<Attribute> ().targetWeights;
}
int randomTargetWeights = Random.Range (0,sumTargetWeights);
foreach(GameObject enemy in enemyObjs){
sumTargetRange += enemy.GetComponent<Attribute> ().targetWeights;
if(randomTargetWeights <= sumTargetRange){
targetObjs.Add (enemy);
break;
}
}

break;
}
}
}
原创粉丝点击