Exemple #1
0
    void Update()
    {
        if (lastTile != null && destinationTile != null)
        {
            // Tween move
            float progress = (Time.time - walkStart) / (1f / walkRate);
            transform.position = Vector3.Lerp(lastTile.GetCoordinate(), destinationTile.GetCoordinate(), LeanTween.easeInOutCubic(0f, 1f, progress));

            // Tween rotate
            transform.rotation = Quaternion.Lerp(lastRotation, targetRotation, LeanTween.easeOutCirc(0f, 1f, progress));
        }

        if (Time.time > walkStart + 1f / walkRate)
        {
            if (!map)
            {
                GameObject go = GameObject.FindGameObjectWithTag("Map");
                if (go != null)
                {
                    map = GameObject.FindGameObjectWithTag("Map").GetComponent <Map>();
                }
            }


            if (steps >= maxSteps * 2 || (steps > 0 && memory.Count == 0))
            {
                Destroy(gameObject);
            }

            TryWalk();
        }
    }
Exemple #2
0
    void TrySpawnWalker()
    {
        ITile tile = FindSpawnTile();

        if (tile != null)
        {
            walker = Instantiate(walkerPrefab, tile.GetCoordinate(), Quaternion.identity);
        }
    }
Exemple #3
0
    public void OnNeighborChanged(ITile neighbor)
    {
        int dx = neighbor.GetCoordinate().x - GetCoordinate().x;
        int dz = neighbor.GetCoordinate().z - GetCoordinate().z;

        bool isRight    = (dx == 1 && dz == 0);
        bool isLeft     = (dx == -1 && dz == 0);
        bool isForward  = (dx == 0 && dz == 1);
        bool isBackward = (dx == 0 && dz == -1);

        IAttachment attachment         = GetAttachment();
        IAttachment neighborAttachment = neighbor.GetAttachment();

        if (attachment is Road)
        {
            Road road = (Road)attachment;

            bool isNeighborRoad = (neighborAttachment is Road);
            if (isNeighborRoad && isRight)
            {
                road.connectRight = true;
            }
            if (isNeighborRoad && isLeft)
            {
                road.connectLeft = true;
            }
            if (isNeighborRoad && isForward)
            {
                road.connectForward = true;
            }
            if (isNeighborRoad && isBackward)
            {
                road.connectBackward = true;
            }
        }
    }
Exemple #4
0
    void TryWalk()
    {
        if (map)
        {
            ITile roadTile = GetDestinationRoadTile();
            if (roadTile != null)
            {
                Vector3Int pos = Vector3Int.FloorToInt(transform.position);
                lastTile        = map.GetTile(pos.x, pos.z);
                destinationTile = roadTile;
                walkStart       = Time.time;

                bool backtraced = false;
                for (int i = 0; i < memory.Count; i++)
                {
                    ITile tile = memory[i];
                    if (tile == destinationTile)
                    {
                        Debug.Log("Already been here");
                        memory     = memory.Take(i).ToList();
                        backtraced = true;
                        break;
                    }
                }

                if (!backtraced)
                {
                    if (steps < maxSteps)
                    {
                        memory.Add(lastTile);
                    }
                }

                Vector3 dir = (destinationTile.GetCoordinate() - transform.position).normalized;
                lastRotation   = transform.rotation;
                targetRotation = Quaternion.LookRotation(dir);

                steps++;
            }
        }
    }