123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class AI : MonoBehaviour
- {
- public enum AIType
- {
- None,
- Manual,
- Hero,
- Soldier,
- Station,
- Door,
- Remote,
- Show,
- }
- protected BattleController battleController;
- protected Map map;
- protected BattleObject _owner;
- protected Player player;
-
- protected float lastCheckTime = 0;
- protected float checkTimeInterval = 0.5f;
- protected bool sendDeathHold = false;
- protected float lastSendDeathTime = 0f;
- public void init(BattleController battleController)
- {
- this.battleController = battleController;
- this.map = battleController.GetMap();
- }
- // Use this for initialization
- protected virtual void Start ()
- {
- _owner = GetComponent<BattleObject>();
- if(battleController.isReplay)
- {
- enabled = false;
- }
- }
- public virtual void SetPlayer(Player player)
- {
- this.player = player;
- }
- public virtual Player GetPlayer()
- {
- return player;
- }
- public bool CanSendMessage()
- {
- if(player == null)
- return false;
- if(!battleController.IsAITaker() || battleController.isGameOver || !battleController.isBattleStart || _owner.IsDead())
- {
- return false;
- }
- else
- {
- return true;
- }
- if(player.isMe)
- return true;
- return false;
- }
- protected virtual bool CheckDead()
- {
- if(sendDeathHold && GameTime.time-lastSendDeathTime < 5f)
- return true;
- if(!sendDeathHold || GameTime.time-lastSendDeathTime>5f)//wait 5s to get response
- {
- if(_owner.hp == 0)
- {
- sendDeathHold = true;
- lastSendDeathTime = GameTime.time;
- battleController.GetMessageManager().Dead(_owner, _owner.lastShooterId, _owner.lastShooterCraftId);
- return true;
- }
- }
- return false;
- }
- protected virtual void FindTarget()
- {
- BattleObject nearestCraft = null;
- float nearestDistance = _owner.viewRange;
- List<BattleObject> list = map.GetBattleObjectByRange(_owner.position, _owner.viewRange, TeamUtil.GetOpponentTeam(_owner.team));
- for (int i=0; i<list.Count; i++)
- {
- BattleObject battleObj = list[i];
- if(!battleObj.CanShoot() || battleObj.overwhelming)
- continue;
- float distance = NumberUtil.distanceVector3(_owner.position, battleObj.position);
- if(distance < nearestDistance)
- {
- nearestDistance = distance;
- nearestCraft = battleObj;
- }
- }
- _owner.target = nearestCraft;
- }
- protected virtual void FindTargetAllMap()
- {
- BattleObject nearestCraft = null;
- float nearestDistance = float.MaxValue;
- List<BattleObject> list = map.GetBattleObjectByRange(_owner.position, nearestDistance, TeamUtil.GetOpponentTeam(_owner.team));
- for (int i=0; i<list.Count; i++)
- {
- BattleObject battleObj = list[i];
- if(!battleObj.CanShoot() || battleObj.overwhelming)
- continue;
-
- float distance = NumberUtil.distanceVector3(_owner.position, battleObj.position);
- if(distance < nearestDistance)
- {
- nearestDistance = distance;
- nearestCraft = battleObj;
- }
- }
- _owner.target = nearestCraft;
- }
- }
|