攻击多个目标
来源:互联网 发布:公务员数学题软件 编辑:程序博客网 时间: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;
}
}
}
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;
}
}
}
阅读全文
0 0
- 攻击多个目标
- makefile编译多个目标
- 使用射线碰撞攻击目标
- 攻击目标叫什么名字
- 多个深度设备跟踪、定位目标
- cygwin *** 多个目标匹配。 停止。
- cygwin *** 多个目标匹配。 停止
- Makefile生成多个目标文件
- Makefile规则(一):生成多个目标
- cygwin *** 多个目标匹配。 停止
- Makefile:1: *** 多个目标匹配。 停止。
- makefile:1: *** 多个目标匹配。 停止。
- 物联网成为黑客攻击目标,2013年全球有超过5.52亿个身份信息被泄露
- 多个C文件生成多个目标的makefile
- 2012年目标攻击和APT高级持续性攻击
- 【APT 攻击 】防护基础设施对抗目标攻击重要的一步
- 判定攻击目标是否在攻击者扇形攻击范围类
- 社交网站被目标攻击的人为因素
- 删除双系统(Ubuntu+Win7)中的Ubuntu之后,win7不能启动的问题
- 写一个ssm项目练练手吧
- codeblocks快捷键(转载)
- [技巧分享-打印机设置]在局域网内设置共享打印机
- angular2_引入第三方文件之swiper的引入
- 攻击多个目标
- Ubuntu 常用软件安装和配置
- Maven之阿里云镜像仓库-yellowcong
- 基于SDP的提议/应答(offer/answer)模型简介
- Spring Boot搭建web服务
- 基于bootstrap的提示弹窗实现
- [leetcode] 112. Path Sum
- Use Cases for SIP and SDP Offer/Answer
- 判断字符串是否为空