Example #1
0
    void Update()
    {
        if (isMovingUnit)
        {
            moveTime += Time.deltaTime * 2.6f;

            if (moveTime >= 1f)
            {
                movingUnit.body.transform.position                  = moveDest.GetPos();
                movingUnit.body.transform.GetChild(0).rotation      = movingUnit.GetFaceRotation();
                movingUnit.body.transform.GetChild(0).localPosition = 0.8f * Vector3.up;
                movingUnit.SetPosKey(moveDest.GetKey());

                if (movingPath.Count == 0)
                {
                    isMovingUnit = false;
                }
                else
                {
                    moveSrc  = moveDest;
                    moveDest = movingPath[0];
                    movingPath.RemoveAt(0);
                    moveTime = 0f;

                    Vector3    moveDir = (moveDest.GetPos() - moveSrc.GetPos()).normalized;
                    Vector2Int faceDir = new Vector2Int(Mathf.RoundToInt(moveDir.x), Mathf.RoundToInt(moveDir.z));
                    movingUnit.SetFaceDirection(faceDir);
                }
            }
            else
            {
                // Vector3 pos = moveSrc.GetPos() * (1 - moveTime) + moveDest.GetPos() * moveTime;
                // movingUnit.body.transform.position = pos;

                Vector3 moveDir = (moveDest.GetPos() - moveSrc.GetPos()).normalized;

                float stepSize = 1f / 4f;

                float stepTime = Mathf.FloorToInt(moveTime / stepSize) * stepSize;
                // stepTime = moveTime;
                float   rotAngle = stepTime * 360;
                Vector3 rotVec   = Vector3.Cross(Vector3.up, Vector3.forward);
                Vector3 pos      = moveSrc.GetPos() * (1 - stepTime) + moveDest.GetPos() * stepTime;
                movingUnit.body.transform.position                  = pos;
                movingUnit.body.transform.GetChild(0).rotation      = movingUnit.GetFaceRotation() * Quaternion.AngleAxis(rotAngle, rotVec);
                movingUnit.body.transform.GetChild(0).localPosition = (Mathf.Abs(Mathf.Cos(rotAngle * Mathf.Deg2Rad)) * 0.4f + 0.4f) * Vector3.up;

                // movingUnit.SetPosKey(moveDest.GetKey());
            }
        }

        if (isControllingUnit)
        {
            controlTime += Time.deltaTime;

            if (currentUnit.unit.apCur == 2 && controlTime >= 1f)
            {
                HashSet <TileNode> targets = GetMoveTiles(currentUnit);
                TileNode           end     = targets.ToArray()[UnityEngine.Random.Range(0, targets.Count)];
                List <TileNode>    path    = GetMovePath(currentUnit, end);
                MoveUnit(currentUnit, path);
                controlTime -= 1f;
            }

            if (currentUnit.unit.apCur == 1 && !isMovingUnit && controlTime >= 1f)
            {
                List <Action> actions = currentUnit.unit.actions;
                Action        action  = actions[UnityEngine.Random.Range(0, actions.Count)];

                HashSet <TileNode> targets = action.GetSelectionTiles(currentUnit, this);

                if (action.GetActionType() == ActionType.Targeted)
                {
                    TileNode target = targets.ToArray()[UnityEngine.Random.Range(0, targets.Count)];
                    ActUnit(currentUnit, action, target);
                }

                if (action.GetActionType() == ActionType.Fixed)
                {
                    List <TileNode> target = new List <TileNode>(targets.ToArray());
                    ActUnit(currentUnit, action, target);
                }

                controlTime -= 1f;
            }

            if (currentUnit.unit.apCur == 0 && !isActingUnit && controlTime >= 1f)
            {
                isControllingUnit = false;
                controlTime      -= 1f;
            }
        }
    }