123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- using UnityEngine;
- using UnityEngine.AI;
- using System.Collections;
- using System.Collections.Generic;
- using Sfs2X.Entities.Data;
- public class Craft : BattleObject, IPowerOwner
- {
- public enum State
- {
- None,
- Idle,
- Run,
- Attack,
- Power,
- }
- public enum Part
- {
- All,
- Top,
- Bottom,
- }
- public string nick;
- private bool m_Moving;
- public override bool isMoving
- {
- get{return m_Moving;}
- }
- public bool forceMove;
- private NavMeshAgent meshAgent;
- public Vector3 moveDestination = Vector3.zero;
- private PowerManager powerManager;
- private SwapManager swapManager;
- private CraftModel craftModel;
- public State state;
- private CraftData craftData;
- private CraftEquipModify equipModify;
- public MiniCraft miniCraft;
- private GameObject haloObj;
- private int haloId = -1;
- private int skinId;
- public event EventUtil.SimpleEventDelegate MoveStepComplete;
- public event EventUtil.SimpleEventDelegate OnSwapped;
- public event EventUtil.SimpleEventDelegate OnCtrlChanged;
- void Awake()
- {
- swapManager = new SwapManager(this);
- meshAgent = GetComponent<NavMeshAgent> ();
- }
- public virtual void Init(Map map, CraftData craftData, CraftEquipModify equipModify)
- {
- base.Init(map);
- this.craftData = craftData;
- SetEquipModify (equipModify);
- id = craftData.id;
- userId = craftData.userId;
- nick = craftData.nick;
- PositionTo(craftData.position);
- Init();
- hp = maxHp;
- headBar.UpdateColor();
- headBar.text.text = nick;
- }
- public void Init()
- {
- _originMaxHp = maxHp = craftData.GetMaxHp() + equipModify.hp;
- if(craftData != null)
- {
- if(powerManager == null)
- powerManager = new PowerManager(this, craftData.GetAttackId(), craftData.GetPowerIds(), craftData.extraPowerIds);
- else
- powerManager.UpdatePower(craftData.GetAttackId(), craftData.GetPowerIds());
- team = craftData.team;
- }
-
- SetState (State.Idle);
- Debuger.LogError (this.name+" maxhp "+maxHp+" damage "+GetDamage()+" mov "+moveSpeed);
- }
- public void SetEquipModify(CraftEquipModify equipModify)
- {
- if (equipModify == null)
- this.equipModify = new CraftEquipModify ();
- else
- this.equipModify = equipModify;
- }
- public ISFSObject GetEquipModifyData()
- {
- return equipModify.GetData ();
- }
- public float GetDamage()
- {
- return craftData.GetDamage () + equipModify.damage;
- }
- public override TeamUtil.Team team {
- get {
- return _team;
- }
- set {
- _team = value;
- if(textureArr.Length > 0)
- {
- int index = value.GetHashCode() - 1;
- craftModel.SetSkin(textureArr[index]);
- }
- }
- }
- public SwapManager GetSwapManager()
- {
- return swapManager;
- }
- public ShotManager GetShotManager()
- {
- return craftModel.shotManager;
- }
- public CraftModel GetCraftModel()
- {
- return craftModel;
- }
- public void SetCraftModel(CraftModel craftModel)
- {
- if (this.craftModel != null) {
- Destroy (this.craftModel.gameObject);
- }
- this.craftModel = craftModel;
- this.craftModel.transform.SetParent (this.transform);
- this.craftModel.transform.localEulerAngles = Vector3.zero;
- this.craftModel.transform.localPosition = Vector3.zero;
- this.craftModel.transform.localScale = Vector3.one;
- }
- private bool isCtrl = false;
- public void SetCtrl(bool value)
- {
- isCtrl = value;
- UpdateHaloColor ();
- headBar.UpdateColor ();
- if(OnCtrlChanged != null)
- OnCtrlChanged();
- }
- public bool IsCtrl()
- {
- return isCtrl;
- }
- public void SetHalo(int id)
- {
- if(haloId != id)
- {
- haloId = id;
- if(haloObj != null)
- Destroy(haloObj);
- haloObj = HaloManager.GetInstance().CreateHaloObjById(id);
- haloObj.transform.SetParent(transform);
- haloObj.transform.localPosition = new Vector3(0, 0.01f, 0);
- haloObj.transform.localScale = new Vector3(1f, 1f, 1f);
- haloObj.transform.localEulerAngles = new Vector3(0, 0, 0);
- UpdateHaloColor ();
- }
- }
- private void UpdateHaloColor()
- {
- if (haloObj == null || craftData == null)
- return;
- if(isCtrl)
- {
- Color color = TeamUtil.GetTeamColor(TeamUtil.Team.Yellow.GetHashCode());
- haloObj.GetComponent<HaloObj>().SetColor(color);
- }
- else
- {
- Color color = TeamUtil.GetTeamColor(team.GetHashCode());
- haloObj.GetComponent<HaloObj>().SetColor(color);
- }
- }
- public int GetHalo()
- {
- return haloId;
- }
- public void SetSkin(int id, Texture texture)
- {
- this.skinId = id;
- if(texture != null)
- craftModel.SetSkin (texture);
- }
- public int GetSkin()
- {
- return skinId;
- }
- public bool IsHero()
- {
- if (craftData != null)
- return craftData.isHero;
- return false;
- }
- public void SetCraftId(int id)
- {
- craftData.SetCraftId(id);
- }
- public int GetCraftId()
- {
- return craftData.GetCraftId();
- }
- public string GetIcon()
- {
- return craftData.GetIcon();
- }
- public override Transform GetBaseTransform()
- {
- if(craftModel.topAnim != null)
- return craftModel.topAnim.transform;
- return transform;
- }
- public void SetState(State state)
- {
- this.state = state;
- if (state == State.Attack)
- craftModel.Attack ();
- }
- public void PositionTo(Vector3 position)
- {
- StopMove();
- transform.position = position;
- }
- public override bool enableMove
- {
- set{
- base.enableMove = value;
- if(!value && isMoving)
- {
- StopMove();
- }
- }
- get{
- return _enableMove;
- }
- }
- public float moveSpeed
- {
- get{
- float speed = originMoveSpeed + modifyMoveSpeed;
- return speed > 0 ? speed + originMoveSpeed * equipModify.move / 100f : speed;
- }
- }
- public void MoveTo(int col, int row)
- {
- if(!enableMove || holdAction)
- return;
- StartMove(Map.GetCenterX(col), Map.GetCenterZ(row));
- }
- public void MoveTo(float x, float y)
- {
- int col = Map.XToColumn(x);
- int row = Map.ZToRow(y);
- MoveTo(col, row);
- }
- protected void StartMove(float x, float y)
- {
- moveDestination.Set (x, 0, y);
- SetIsMoving(true);
- OnMove();
- swapManager.AbortSwap();
- }
- private void SetIsMoving(bool value)
- {
- m_Moving = value;
- craftModel.moving = value;
- }
- public void StopMove()
- {
- if(holdAction)
- {
- if(target != null)
- {
- for(int i=0; i<buffList.Count; i++)
- {
- if(buffList[i].hasCloseTarget)
- {
- StartMove(target.position.x, target.position.z);
- break;
- }
- }
- }
- return;
- }
- SetIsMoving (false);
- forceMove = false;
- }
- // Update is called once per frame
- // public override void EnterFrame ()
- void FixedUpdate () {
- if(IsDead())
- {
- if(GameTime.time-deadTime>5f)//do not remove immediately
- {
- Destroy(this.miniCraft.gameObject);
- Destroy(this.gameObject);
- }
- return;
- }
- if(isMoving)
- {
- moving();
- }
- if (target != null && target.CanShoot ()) {
- craftModel.LookTo (target.position);
- } else if(craftModel != null){
- craftModel.LookToForward ();
- }
- CheckHpRecover();
- DealBuff();
- if(swapManager.isPreparingSwap && !swapManager.requestedSwap)
- {
- headBar.SetSpellProgress(swapManager.GetPreparingSwapTime(), SwapManager.PREPARING_SWAP_DURATION);
- }
- }
- private void moving()
- {
- meshAgent.SetDestination (moveDestination);
- if (meshAgent.remainingDistance > 0) {
- if(miniCraft != null)
- miniCraft.UpdatePos();
- } else {
- if (MoveStepComplete != null)
- MoveStepComplete ();
- StopMove ();
- }
- }
- public void SetRotateTargetAngle(float angle)
- {
- Vector3 targetPos = transform.position;
- float radian = angle * Mathf.Deg2Rad;
- float x = Mathf.Cos (radian);
- float y = Mathf.Sin (radian);
- targetPos.x += x;
- targetPos.z += y;
- craftModel.LookAt (targetPos);
- }
- private int _teleportStep = 0;
- public void Teleport(int step)
- {
- _teleportStep = Mathf.Max(_teleportStep, step);
- if(!isMoving)
- {
- return;
- }
- GameObject disappearObj = EffectUtil.CreateDisappear();
- disappearObj.transform.position = position;
- for(int i=0; i<step; i++)
- {
- // setNextMovePosition();
- if(!isMoving)
- {
- break;
- }
- }
- // DealMoveStepComplete();
- _teleportStep = 0;
- GameObject landingObj = EffectUtil.CreateLanding();
- landingObj.transform.position = position;
- }
- public override void MakeDamage (Bullet bullet)
- {
- base.MakeDamage (bullet);
- swapManager.AbortSwap();
- }
- override public void SetInvisible(bool invisible, bool isAlly)
- {
- if(invisible == this.invisible)
- {
- return;
- }
- base.SetInvisible(invisible, isAlly);
- if(invisible)
- {
- haloObj.gameObject.SetActive(isAlly);
- }
- else
- {
- haloObj.gameObject.SetActive(true);
- }
- }
- public PowerManager GetPowerManager()
- {
- return powerManager;
- }
- public void RadioAlert()
- {
- headBar.RadioAlert();
- if(miniCraft != null)
- miniCraft.RadioAlert();
- }
- public void SwapComplete()
- {
- if(OnSwapped != null)
- OnSwapped();
- }
- public UAVehicle GetUAV()
- {
- return map.GetUAVehicle (userId);
- }
- override public void Remove()
- {
- map.RemoveBattleObject(this);
- }
- public override string ToString()
- {
- return "User["+userId+"] Id["+id+"] Type["+typeId+"]";
- }
- }
|