示例#1
0
    private void GenerateGraphFromGameObject()
    {
        tileDict  = new Dictionary <Vector2Int, TileNode>();
        spawnDict = new Dictionary <Vector2Int, int>();

        // Add nodes from gameObject children
        foreach (Transform child in transform)
        {
            BattleGridMarker marker = child.gameObject.GetComponent <BattleGridMarker>();
            Vector3          v      = child.position;

            // If marker, add as special
            if (marker)
            {
                switch (marker.type)
                {
                case MarkerType.Spawn:
                    spawnDict.Add(Vector3ToKey(v), marker.val);
                    break;

                default:
                    Debug.Log("Should not reach here");
                    break;
                }

                child.gameObject.SetActive(false);
                continue;
            }

            // Else, add as tile
            TileNode node = new TileNode(v);
            tileDict.Add(node.GetKey(), node);

            // Update boundaries
            xMin = Mathf.Min(xMin, v.x - 0.45f);
            xMax = Mathf.Max(xMax, v.x + 0.45f);
            zMin = Mathf.Min(zMin, v.z - 0.45f);
            zMax = Mathf.Max(zMax, v.z + 0.45f);
        }

        // Add edges for all adjacent nodes
        foreach (KeyValuePair <Vector2Int, TileNode> entry in tileDict)
        {
            Vector2Int k = entry.Key;
            TileNode   n = entry.Value;

            for (int i = k.x - 1; i <= k.x + 1; i++)
            {
                for (int j = k.y - 1; j <= k.y + 1; j++)
                {
                    if (i == k.x && j == k.y)
                    {
                        continue;
                    }

                    Vector2Int t = new Vector2Int(i, j);

                    if (!tileDict.ContainsKey(t))
                    {
                        continue;
                    }

                    float w = (n.GetPos() - tileDict[t].GetPos()).magnitude;
                    w = Mathf.RoundToInt(w * 10) / 10f;
                    n.AddNeighbor(tileDict[t], w);
                }
            }
        }

        // Debug printing

        /*
         * foreach (KeyValuePair<Vector2Int, TileNode> entry in tileDict) {
         * Debug.Log(entry.Value);
         * }
         * foreach (KeyValuePair<Vector2Int, int> entry in spawnDict) {
         * Debug.Log(entry.Value);
         * }
         */
    }
示例#2
0
 public override string ToString()
 {
     return("Edge to " + destination.GetKey().ToString() + " for " + weight.ToString());
 }
示例#3
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;
            }
        }
    }