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 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(); materialTop = topPartTrans.GetComponentInChildren().material; material = materialTop; } bottomPartTrans = bodyTrans.Find("Bottom"); if(bottomPartTrans != null) { animationBottom = bottomPartTrans.GetComponent(); materialBottom = bottomPartTrans.GetComponentInChildren().material; } if(topPartTrans == null) animation = bodyTrans.GetComponentInChildren(); 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(); 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(); for(int i=0; i(); for(int i=0; i(); for(int i=0; i().SetColor(color); } else { Color color = TeamUtil.GetTeamColor(team.GetHashCode()); haloObj.GetComponent().SetColor(color); } } public int GetHalo() { return haloId; } public void SetSkin(int id, Texture texture) { this.skinId = id; if(texture != null) { SkinnedMeshRenderer[] rendererArr = bodyTrans.GetComponentsInChildren(); for(int i=0; i 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 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; i5f)//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