Exemplo n.º 1
0
    void SpawnPedestrians(BoundsInt outerBoundsInt)
    {
        TileBase[] tiles       = pedestrians.GetTilesBlock(outerBoundsInt);
        int        marginWidth = maxSpawnMargin - minSpawnMargin;
        int        farMarginX  = outerBoundsInt.size.x - marginWidth;
        int        farMarginY  = outerBoundsInt.size.y - marginWidth;

        int carCount = Physics2D.OverlapAreaAll(
            (Vector3)outerBoundsInt.min, (Vector3)outerBoundsInt.max, layerMask).Length;

        foreach (int y in Enumerable.Range(0, outerBoundsInt.size.y).OrderBy(y => Random.value))
        {
            foreach (int x in Enumerable.Range(0, outerBoundsInt.size.x).OrderBy(x => Random.value))
            {
                // Only use tiles that are within the margin but not visible to the camera.
                if (carCount < targetCount &&
                    (x < marginWidth || y < marginWidth || x >= farMarginX || y >= farMarginY))
                {
                    TileBase tile  = tiles[y * outerBoundsInt.size.x + x];
                    Vector3  point = pedestrians.CellToWorld(
                        new Vector3Int(outerBoundsInt.xMin + x, outerBoundsInt.yMin + y, 0)) + halfCell;
                    int dir = tile == null ? -1 : dirs.IndexOf(tile.name);

                    if (dir > -1 && Physics2D.OverlapCircle(point, clearRadius, layerMask) == null)
                    {
                        PedestrianScript pedestrian = pedestrianPrefabs[Random.Range(0, pedestrianPrefabs.Length)];
                        Instantiate(pedestrian, point + new Vector3(0, 0, pedestrian.transform.position.z),
                                    Quaternion.Euler(0, 0, 0 * dir), transform);

                        carCount += 1;
                    }
                }
            }
        }
    }
Exemplo n.º 2
0
    void SpawnPedestrian(GameObject prefab, Vector3 spawnPos) {


        spawnPos = addToVectorAlongDirection(spawnPos, Random.Range(0, 2) == 0 ? roadWidth : -roadWidth, dirAfterTurn(PlayerScript.currentDirection, false));
        Direction dir = Random.Range(0, 2) == 0 ? PlayerScript.currentDirection : dirAfterTurn(dirAfterTurn(PlayerScript.currentDirection, true), true);

        //Try to reuse old pedestrians
        if (pedestrianList.Count > 0) {
            LinkedListNode<PedestrianScript> oldest = pedestrianList.Last;
            do {
                PedestrianScript oldestPed = oldest.Value;
                if (oldestPed.isAlive){
                    if (Mathf.Abs(differenceAlongDirection(play.transform.position, oldestPed.transform.position, dirAfterTurn(PlayerScript.currentDirection, true))) > chunkReuseDistance || differenceAlongDirection(play.transform.position, oldestPed.transform.position) > chunkReuseDistance) {
                        oldestPed.transform.position = spawnPos;
                        oldestPed.transform.eulerAngles = new Vector3(0, (int)dir, 0);
                        pedestrianList.Remove(oldest);
                        pedestrianList.AddFirst(oldestPed);
                        return;
                    }
                } 
                oldest = oldest.Previous;
            } while (oldest != null);
        }

        //If not, just make a new one
        PedestrianScript newPed = Instantiate(prefab, spawnPos, Quaternion.Euler(0, (int)dir, 0)).GetComponent<PedestrianScript>();
        pedestrianList.AddFirst(newPed);
    }
Exemplo n.º 3
0
    void Awake()
    {
        ConfigScript config = (ConfigScript)Object.FindObjectOfType(typeof(ConfigScript));

        traffic        = GameObject.Find("Pedestrians Traffic").GetComponent <Tilemap>();
        walking        = GetComponent <PedestrianScript>();
        body           = GetComponent <Rigidbody2D>();
        pedestrianMask = LayerMask.GetMask("Pedestrians");

        Vector3 frontSide  = transform.position;
        Vector3 cellCenter = traffic.CellToLocalInterpolated(traffic.WorldToCell(frontSide) +
                                                             new Vector3(0.5f, 0.5f, 0));

        goal = GetNextCell(cellCenter);
    }