Exemple #1
0
    private void PlaceNpcs(Island island)
    {
        int            npcsToSpawn = (int)npcQuantity.GetRandom();
        int            npcsSpawned = 0;
        List <Vector2> tileList    = new List <Vector2>(island.tiles.Keys);

        while (npcsSpawned < npcsToSpawn && npcsSpawned < tileList.Count)
        {
            // Create NPC and place it onto a random tile
            Vector2 randTileVector = tileList[Random.Range(0, tileList.Count)];
            Tile    chosenTile     = island.tiles[randTileVector];
            // Don't spawn NPCs on tiles with entities
            if (!chosenTile.HasEntity())
            {
                GameObject chosenNpc = npcPrefabs[Random.Range(0, npcPrefabs.Length)];
                GameObject npc       = Instantiate(chosenNpc, chosenTile.transform.position, Quaternion.identity, chosenTile.transform);
                ShiftSpriteToTile(npc.transform, chosenTile);
                // Flip animal half the time
                if (Random.value < 0.5f)
                {
                    npc.GetComponent <NPCAnimator>().FlipSprite();
                }
                npc.GetComponent <Animal>().Sleep();
            }
            // Increment even if tile is unsuitable for NPC
            npcsSpawned++;
        }
    }
Exemple #2
0
 private void FixedUpdate()
 {
     if (RandomValue.GetRandom() < chanceToChangeDirections)
     {
         speed *= -1;
     }
 }
Exemple #3
0
    public IEnumerator WakeUp()
    {
        state = AnimalState.ACTIVE;
        npcAnimator.Trigger("Wake");
        yield return(new WaitForSeconds(wakeTransitionTime.GetRandom()));

        StartCoroutine(Wander());
    }
Exemple #4
0
    // Create shape of island by generating tiles and the entities on top of them.
    private void GrowTiles(Island island)
    {
        int      chosenTileIndex = Random.Range(0, tileInfo.Length);
        TileInfo chosenTileInfo  = tileInfo[chosenTileIndex];
        Tile     chosenTile      = Instantiate(chosenTileInfo.tilePrefab, island.gameObject.transform).GetComponent <Tile>();

        RandomizeDecor(chosenTile);
        chosenTile.SetIsland(island, Vector2.zero);
        chosenTile.SetSortingOrder();
        island.tiles.Add(Vector2.zero, chosenTile);
        int tileQuantity = (int)tileQuantityRange.GetRandom();

        while (island.tiles.Count < tileQuantity)
        {
            List <Vector2> tileList       = new List <Vector2>(island.tiles.Keys);
            Vector2        randTileVector = tileList[Random.Range(0, tileList.Count)];
            Tile           existingTile   = island.tiles[randTileVector];
            Vector2        newTileVect    = GetRandAdjacentVector(randTileVector);
            if (!island.tiles.ContainsKey(newTileVect))
            {
                // Initialize tile
                Tile newTile = Instantiate(chosenTileInfo.tilePrefab, Vector2.zero, Quaternion.identity, island.gameObject.transform).GetComponent <Tile>();
                RandomizeDecor(newTile);
                newTile.SetIsland(island, newTileVect);
                // Move tile to position in island
                newTile.transform.position += new Vector3(newTileVect.x * newTile.GetComponent <Collider2D>().bounds.size.x, newTileVect.y * newTile.GetComponent <Collider2D>().bounds.size.y, 0);
                newTile.SetSortingOrder();
                // Random chance to place entity
                if (Random.value < entitySpawnChance)
                {
                    if (chosenTileInfo.uniqueTileEntities.Length > 0)
                    {
                        Entity     chosenEntity = chosenTileInfo.uniqueTileEntities[Random.Range(0, chosenTileInfo.uniqueTileEntities.Length)].GetComponentsInChildren <Entity>()[0];
                        GameObject entityObj    = chosenEntity.gameObject;
                        // Set this entity to be on the FloatingIsland layer while it's unhooked
                        //entityObj.layer = LayerMask.NameToLayer("FloatingIsland");
                        if (entityObj.transform.parent != null)
                        {
                            entityObj = entityObj.transform.parent.gameObject;
                            chosenEntity.transform.parent.gameObject.layer = LayerMask.NameToLayer("FloatingIsland");
                        }
                        Entity spawnedEntity = Instantiate(entityObj, newTile.transform).GetComponentsInChildren <Entity>()[0];
                        if (spawnedEntity.GetComponent <SoilEntity>() != null)
                        {
                            spawnedEntity.GetComponent <SoilEntity>().PlantNow();
                        }

                        newTile.PlaceEntity(spawnedEntity, newTileVect);
                    }
                }
                island.tiles.Add(newTileVect, newTile);
            }
        }
    }
Exemple #5
0
    private void Move()
    {
        float xForce = moveForce.GetRandom();

        if (Random.value < 0.5f)
        {
            xForce *= -1;
        }
        float yForce = moveForce.GetRandom();

        if (Random.value < 0.5f)
        {
            yForce *= -1;
        }
        if (npcAnimator.flipped && xForce < 0)
        {
            npcAnimator.FlipSprite();
        }
        else if (!npcAnimator.flipped && xForce > 0)
        {
            npcAnimator.FlipSprite();
        }
        rb.AddForce(new Vector2(xForce, yForce));
    }
Exemple #6
0
    IEnumerator Wander()
    {
        while (true)
        {
            Move();
            yield return(new WaitForSeconds(moveTime.GetRandom()));

            rb.velocity = Vector2.zero;
            yield return(new WaitForSeconds(moveDelay.GetRandom()));

            if (state == AnimalState.SLEEPING || isDead)
            {
                break;
            }
        }
    }
    IEnumerator SpawnIslands()
    {
        while (true)
        {
            float halfCamHeight = cam.orthographicSize;
            float halfCamWidth  = halfCamHeight * cam.aspect;

            // Get generated island object
            GameObject island = islandGenerator.GenerateIsland();
            island.GetComponent <Island>().map = map;

            // Check if homeIsland exists
            if (!homeIsland)
            {
                Destroy(island);
                // Generate wait time until next island spawning
                yield return(new WaitForSeconds(spawnCooldown.GetRandom()));

                continue;
            }

            // Randomly choose whether this island is vertically-moving or horizontally-moving
            if (Random.value < 0.5f)
            {
                // Vertically-moving island
                RandomValue spawnXRange = new RandomValue(cam.transform.position.x - halfCamWidth, cam.transform.position.x + halfCamWidth);
                spawnXRange.SetExcludedRange(new Range(GetChildBounds(homeIsland).min.x - homeBuffer - GetChildBounds(island).extents.x, GetChildBounds(homeIsland).max.x + homeBuffer + GetChildBounds(island).extents.x));
                float newIslandX = spawnXRange.GetRandom();
                // Randomly choose whether this island spawns from top or bottom
                if (Random.value < 0.5f)
                {
                    // Island spawns from top and moves down
                    float   newIslandY        = cam.transform.position.y + halfCamHeight + GetChildBounds(island).extents.y;
                    Vector3 newIslandPosition = new Vector3(newIslandX, newIslandY, island.transform.position.z);
                    // Move island to randomly generated position
                    island.transform.position = newIslandPosition;
                    island.GetComponent <Rigidbody2D>().velocity = new Vector2(0, -islandVelocity.GetRandom());
                }
                else
                {
                    // Island spawns from bottom and moves up
                    float   newIslandY        = cam.transform.position.y - halfCamHeight - GetChildBounds(island).extents.y;
                    Vector3 newIslandPosition = new Vector3(newIslandX, newIslandY, island.transform.position.z);
                    island.transform.position = newIslandPosition;
                    island.GetComponent <Rigidbody2D>().velocity = new Vector2(0, islandVelocity.GetRandom());
                }
            }
            else
            {
                // Horizontally-moving island
                RandomValue spawnYRange = new RandomValue(cam.transform.position.y - halfCamHeight, cam.transform.position.y + halfCamHeight);
                spawnYRange.SetExcludedRange(new Range(GetChildBounds(homeIsland).min.y - homeBuffer - GetChildBounds(island).extents.y, GetChildBounds(homeIsland).max.y + homeBuffer + GetChildBounds(island).extents.y));
                float newIslandY = spawnYRange.GetRandom();
                // Randomly choose whether this island spawns from left or right
                if (Random.value < 0.5f)
                {
                    // Island spawns from left and moves right
                    float   newIslandX        = cam.transform.position.x + halfCamWidth + GetChildBounds(island).extents.x;
                    Vector3 newIslandPosition = new Vector3(newIslandX, newIslandY, island.transform.position.z);
                    island.transform.position = newIslandPosition;
                    island.GetComponent <Rigidbody2D>().velocity = new Vector2(-islandVelocity.GetRandom(), 0);
                }
                else
                {
                    // Island spawns from right and moves left
                    float   newIslandX        = cam.transform.position.x - halfCamWidth - GetChildBounds(island).extents.x;
                    Vector3 newIslandPosition = new Vector3(newIslandX, newIslandY, island.transform.position.z);
                    island.transform.position = newIslandPosition;
                    island.GetComponent <Rigidbody2D>().velocity = new Vector2(islandVelocity.GetRandom(), 0);
                }
            }
            // Generate wait time until next island spawning
            yield return(new WaitForSeconds(spawnCooldown.GetRandom()));
        }
    }