123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824 |
- using UnityEngine;
- 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 LinkedList<AStarNode> path;
- private Vector3 nextMovePosition;
- private bool _isMoving;
- public override bool isMoving
- {
- get{return _isMoving;}
- }
- public AStarPoint targetMovePosition = new AStarPoint();
- public bool forceMove;
- private float rotateSpeed = 200f;
- public float rotateTargetAngle;
- private bool _isTargetRotating;
- public float rotateMoveTargetAngle;
- private bool _isMoveRotating;
- public Transform bodyTrans;
- public Transform topPartTrans;
- public Transform bottomPartTrans;
- private Animation animationTop;
- private Animation animationBottom;
-
- public Material materialTop;
- public Material materialBottom;
- private PowerManager powerManager;
- private SwapManager swapManager;
- private ShotManager shotManager;
- 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);
- }
- 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;
- bodyTrans = transform.Find("Body");
-
- topPartTrans = bodyTrans.Find("Top");
- if(topPartTrans != null)
- {
- animationTop = topPartTrans.GetComponent<Animation>();
- materialTop = topPartTrans.GetComponentInChildren<Renderer>().material;
- material = materialTop;
- }
-
- bottomPartTrans = bodyTrans.Find("Bottom");
- if(bottomPartTrans != null)
- {
- animationBottom = bottomPartTrans.GetComponent<Animation>();
- materialBottom = bottomPartTrans.GetComponentInChildren<Renderer>().material;
- }
- if(topPartTrans == null)
- animation = bodyTrans.GetComponentInChildren<Animation>();
- 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;
- }
- shotManager = GetComponentInChildren<ShotManager>();
-
- 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;
- if(topPartTrans == null)
- {
- SkinnedMeshRenderer[] renderers = bodyTrans.GetComponentsInChildren<SkinnedMeshRenderer>();
- for(int i=0; i<renderers.Length; i++)
- {
- renderers[i].material.mainTexture = textureArr[index];
- }
- }
- else
- {
- SkinnedMeshRenderer[] renderers = topPartTrans.GetComponentsInChildren<SkinnedMeshRenderer>();
- for(int i=0; i<renderers.Length; i++)
- {
- renderers[i].material.mainTexture = textureArr[index];
- }
- renderers = bottomPartTrans.GetComponentsInChildren<SkinnedMeshRenderer>();
- for(int i=0; i<renderers.Length; i++)
- {
- renderers[i].material.mainTexture = textureArr[index];
- }
- }
- }
- }
- }
- public SwapManager GetSwapManager()
- {
- return swapManager;
- }
- public ShotManager GetShotManager()
- {
- return shotManager;
- }
- 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)
- {
- SkinnedMeshRenderer[] rendererArr = bodyTrans.GetComponentsInChildren<SkinnedMeshRenderer>();
- for(int i=0; i<rendererArr.Length; i++)
- {
- rendererArr[i].material.mainTexture = 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(topPartTrans != null)
- return topPartTrans;
- return transform;
- }
- public void SetState(State state)
- {
- if(this.state != state)
- {
- this.state = state;
- string animName = state.ToString();
- if(animation != null)
- {
- if(animation.GetClip(animName) != null)
- animation.Play(animName);
- }
- if(animationTop == null)
- return;
- if(state == State.Idle)
- {
- if(animationTop.GetClip(animName) != null)
- {
- animationTop.Play(animName);
- animationBottom.Play(animName);
- }
- }
- else if(state == State.Run)
- {
- if(animationTop.GetClip(animName) != null)
- {
- animationTop.Play(animName);
- animationBottom.Play(animName);
- }
- }
- else if(state == State.Attack)
- {
- if(animationTop.GetClip(animName) != null)
- animationTop.Play(animName);
- if(isMoving)
- {
- if(animationBottom.GetClip(State.Run.ToString()) != null)
- animationBottom.Play(State.Run.ToString());
- }
- else
- {
- if(animationBottom.GetClip(State.Idle.ToString()) != null)
- animationBottom.Play(State.Idle.ToString());
- }
- }
- }
- }
- public bool IsPlayingState(State state, Part part = Part.All)
- {
- string animName = state.ToString();
- if(animation != null)
- {
- return animation.IsPlaying(animName);
- }
- if(animationTop != null && (part == Part.Top || part == Part.All))
- {
- return animationTop.IsPlaying(animName);
- }
- if(animationBottom != null && (part == Part.Bottom || part == Part.All))
- {
- return animationBottom.IsPlaying(animName);
- }
- return false;
- }
- 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(col, row);
- }
- public void MoveTo(float x, float y)
- {
- int col = Map.XToColumn(x);
- int row = Map.ZToRow(y);
- MoveTo(col, row);
- }
- protected void StartMove(int col, int row)
- {
- targetMovePosition.x = col;
- targetMovePosition.y = row;
-
- Vector3 position = transform.position;
- int startCol = (int)(position.x/Map.TILE_WIDTH);
- int startRow = (int)(position.z/Map.TILE_LENGTH);
-
- map.PathSearchEnqueue(new AStarPoint(startCol, startRow), new AStarPoint(col, row), this);
- }
- public void SetPath(LinkedList<AStarNode> path)
- {
- //long startTime = System.DateTime.Now.ToFileTime();
- //path = map.GetPath(new AStarPoint(startCol, startRow), new AStarPoint(col, row), this);
- //long endTime = System.DateTime.Now.ToFileTime();
- //Debuger.Log("search path spend time : "+(endTime-startTime));
- this.path = path;
- if(!IsDead() && path != null)
- {
- path.RemoveFirst();
- _isMoving = true;
- if(state == State.Idle)
- SetState(State.Run);
- if(_teleportStep > 0)
- Teleport(_teleportStep);
- else
- setNextMovePosition();
- OnMove();
- swapManager.AbortSwap();
- }
- }
- public void StopMove()
- {
- if(holdAction)
- {
- if(target != null)
- {
- for(int i=0; i<buffList.Count; i++)
- {
- if(buffList[i].hasCloseTarget)
- {
- int col = Map.XToColumn(target.position.x);
- int row = Map.ZToRow(target.position.z);
- StartMove(col, row);
- break;
- }
- }
- }
- return;
- }
- path = null;
- _isMoving = false;
- forceMove = false;
- if(state == State.Run)
- SetState(State.Idle);
- }
- // Update is called once per frame
- void Update () {
- 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())
- {
- Vector3 targetPosition = target.position;
- float theta = NumberUtil.getRadianByATan(targetPosition.x, targetPosition.z, position.x, position.z);
- float rotateAngle = 90-NumberUtil.radianToAngle(theta);
- if(!rotateTargetAngle.Equals(rotateAngle))
- {
- rotateTargetAngle = rotateAngle;
- _isTargetRotating = true;
- }
- }
- rotating();
- CheckHpRecover();
- DealBuff();
- if((state == State.Attack || state == State.Power) && !IsPlayingState(state, Part.Top))
- {
- if(isMoving)
- {
- SetState(State.Run);
- }
- else
- {
- SetState(State.Idle);
- }
- }
- if(swapManager.isPreparingSwap && !swapManager.requestedSwap)
- {
- headBar.SetSpellProgress(swapManager.GetPreparingSwapTime(), SwapManager.PREPARING_SWAP_DURATION);
- }
- }
- private void moving()
- {
- float timeLeft = updateMovePosition(GameTime.deltaTime);
- if(_isMoving && timeLeft > 0)
- {
- updateMovePosition(timeLeft);
- }
- }
- private float updateMovePosition(float deltaTime)
- {
- Vector3 position = transform.position;
-
- float deltaX = nextMovePosition.x-position.x;
- float deltaY = nextMovePosition.z-position.z;
- float theta = Mathf.Atan2(deltaY, deltaX);
-
- float distance = Mathf.Sqrt(deltaX*deltaX+deltaY*deltaY);
- float moveDistance = moveSpeed*deltaTime;
-
- float timeLeft = 0;
- if(moveDistance >= distance)
- {
- timeLeft = deltaTime*(1f-distance/moveDistance);
- DealMoveStepComplete();
- setNextMovePosition();
- }
- else
- {
- float movX = moveDistance*Mathf.Cos(theta);
- float movZ = moveDistance*Mathf.Sin(theta);
- position.x += movX;
- position.z += movZ;
- transform.position = position;
- }
- return timeLeft;
- }
- private void DealMoveStepComplete()
- {
- Vector3 position = transform.position;
- position.x = nextMovePosition.x;
- position.z = nextMovePosition.z;
- transform.position = position;
- if(MoveStepComplete != null)
- MoveStepComplete();
- }
- private void setNextMovePosition()
- {
- if(path != null)
- {
- if(path.Count > 0)
- {
- AStarNode astarNode = path.First.Value;
- path.RemoveFirst();
- nextMovePosition = AstarNodeToPosition(astarNode);
- Vector3 position = transform.position;
-
- float theta = NumberUtil.getRadianByATan(nextMovePosition.x, nextMovePosition.z, position.x, position.z);
- float rotateAngle = 90-NumberUtil.radianToAngle(theta);
- Rotate (rotateAngle);
- }
- else
- {
- StopMove();
- }
- if(miniCraft != null)
- miniCraft.UpdatePos();
- }
- }
- public void Rotate(float rotateAngle)
- {
- rotateMoveTargetAngle = rotateAngle;
- _isMoveRotating = true;
- if(target == null)
- {
- if(!rotateTargetAngle.Equals(rotateMoveTargetAngle))
- {
- rotateTargetAngle = rotateMoveTargetAngle;
- _isTargetRotating = true;
- }
- }
- }
- private Vector3 AstarNodeToPosition(AStarNode astarNode)
- {
- return new Vector3((astarNode.X+0.5f)*Map.TILE_WIDTH, 0, (astarNode.Y+0.5f)*Map.TILE_LENGTH);
- }
-
- public void setRotateTargetAngle(float value)
- {
- rotateTargetAngle = value;
- _isTargetRotating = true;
- }
- private void rotating()
- {
- if(!enableMove || swapManager.isPreparingSwap)
- return;
- if(bottomPartTrans == null)
- {
- if(_isMoveRotating || _isTargetRotating)
- {
- float targetRotation = 0;
- if(_isMoving)
- {
- targetRotation = rotateMoveTargetAngle;
- }
- else
- {
- targetRotation = rotateTargetAngle;
- }
- Vector3 eulerAngles = bodyTrans.eulerAngles;
- float currentRotation = eulerAngles.y;
- float rotateStep = rotateSpeed*GameTime.deltaTime;
-
- currentRotation = NumberUtil.getCloseToTargetAngle(currentRotation, targetRotation, rotateStep);
- eulerAngles.y = currentRotation;
- bodyTrans.eulerAngles = eulerAngles;
- if(_isMoving)
- {
- if(rotateMoveTargetAngle.Equals(currentRotation))
- {
- _isMoveRotating = false;
- }
- }
- else
- {
- if(rotateTargetAngle.Equals(currentRotation))
- {
- _isTargetRotating = false;
- }
- }
- }
- }
- else
- {
- Vector3 eulerAngles;
- float rotateStep = rotateSpeed*GameTime.deltaTime;
- if(_isTargetRotating)
- {
- eulerAngles = topPartTrans.localEulerAngles;
- float currentRotation = eulerAngles.y;
-
- currentRotation = NumberUtil.getCloseToTargetAngle(currentRotation, rotateTargetAngle, rotateStep);
- eulerAngles.y = currentRotation;
- topPartTrans.localEulerAngles = eulerAngles;
- if(shotManager != null)
- shotManager.transform.localEulerAngles = eulerAngles;
- if(rotateTargetAngle.Equals(currentRotation))
- {
- _isTargetRotating = false;
- }
- }
- if(_isMoveRotating)
- {
- eulerAngles = bottomPartTrans.localEulerAngles;
- float currentBottomRotation = eulerAngles.y;
-
- currentBottomRotation = NumberUtil.getCloseToTargetAngle(currentBottomRotation, rotateMoveTargetAngle, rotateStep);
- eulerAngles.y = currentBottomRotation;
- bottomPartTrans.localEulerAngles = eulerAngles;
- if(rotateMoveTargetAngle.Equals(currentBottomRotation))
- {
- _isMoveRotating = false;
- }
- }
- }
- }
- 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+"]";
- }
- }
|