Exemple #1
0
    ///returns true if this entity moving in given direction would initiate an attack instead of shifting spaces
    ///returns false otherwise
    public virtual bool WillAttack(MoveDirection givenDirection)
    {
        TileMonoBehavior targetTile = null;

        switch (givenDirection) //finds tile in given direction, tries to move there
        {
        case (MoveDirection.up):
            targetTile = occupyingTile.getAbove();
            break;

        case (MoveDirection.left):
            targetTile = occupyingTile.getLeft();
            break;

        case (MoveDirection.down):
            targetTile = occupyingTile.getBelow();
            break;

        default:
            targetTile = occupyingTile.getRight();
            break;
        }
        if (targetTile == null || !targetTile.IsOccupied())
        {
            return(false);
        }
        return(true);
    }
    /// Update is called once per frame
    ///Change color of spell based on type
    ///Gets the tile beneath the spell and changes tile material based on spell type
    ///Spell is destroyed after hitting enemy or going off the board
    void Update()
    {
        if (type == spellType.regular)
        {
            GetComponent <SpriteRenderer>().color = Color.white;
        }
        else if (type == spellType.fire)
        {
            GetComponent <SpriteRenderer>().color = Color.red;
            damageCaused = 5;
        }
        else if (type == spellType.ice)
        {
            GetComponent <SpriteRenderer>().color = Color.blue;
            damageCaused = 5;
        }
        else
        {
            GetComponent <SpriteRenderer>().color = Color.yellow;
            damageCaused = 5;
        }

        Vector2    topRightLoc = new Vector2(transform.position.x + 0.25f, transform.position.y + 0.25f);
        Vector2    botLeftLoc  = new Vector2(transform.position.x - 0.25f, transform.position.y - 0.25f);
        Collider2D collider    = Physics2D.OverlapArea(topRightLoc, botLeftLoc, TileMonoBehavior.tileLayerMask);      //test if a floor tile is under this tile, if so then grab it and check what occupies it

        if (collider != null)
        {
            //Debug.Log("Testing floor collision");
            TileMonoBehavior tileBeneathUs = collider.gameObject.GetComponent <TileMonoBehavior>();
            if (tileBeneathUs != null)
            {
                if (!tileBeneathUs.IsWalkable())
                {
                    Destroy(this.gameObject);
                }
                else if (tileBeneathUs.IsOccupied())
                {
                    Debug.Log(tileBeneathUs.occupyingEntity + " takes " + damageCaused + " poking damage from the spell");
                    tileBeneathUs.occupyingEntity.TakeDamage(damageCaused, Entity.DamageType.poking);
                    Destroy(this.gameObject);
                }
                else
                {
                    this.occupyingTile = tileBeneathUs;
                    if (type == spellType.regular)
                    {
                        tileBeneathUs.GetComponent <SpriteRenderer>().material = defaultMaterial;
                        tileBeneathUs.timeToRevert = 0;
                    }
                    else if (type == spellType.fire)
                    {
                        tileBeneathUs.GetComponent <SpriteRenderer>().material = Resources.Load("Materials/Red") as Material;
                        if (!tileBeneathUs.inList)
                        {
                            tileBeneathUs.timeToRevert = DungeonManager.turnOrder.Count * 5;
                            DungeonManager.AddToTileList(tileBeneathUs);
                            tileBeneathUs.inList = true;
                        }
                    }
                    else if (type == spellType.ice)
                    {
                        tileBeneathUs.GetComponent <SpriteRenderer>().material = Resources.Load("Materials/Blue") as Material;
                        if (!tileBeneathUs.inList)
                        {
                            tileBeneathUs.timeToRevert = DungeonManager.turnOrder.Count * 5;
                            DungeonManager.AddToTileList(tileBeneathUs);
                            tileBeneathUs.inList = true;
                        }
                    }
                    else
                    {
                        tileBeneathUs.GetComponent <SpriteRenderer>().material = Resources.Load("Materials/Yellow") as Material;
                        if (!tileBeneathUs.inList)
                        {
                            tileBeneathUs.timeToRevert = DungeonManager.turnOrder.Count * 5;
                            DungeonManager.AddToTileList(tileBeneathUs);
                            tileBeneathUs.inList = true;
                        }
                    }
                }
            }
            else
            {
                //Debug.Log("Destroying spell by lack of Tile");
                Destroy(this.gameObject);
            }
        }
        else
        {
            //Debug.Log("Destroying spell by lack of Collider");
            Destroy(this.gameObject);
        }
    }
    ///populates random dungeon rooms with stuff, including placing player character, stairs, enemies
    void PopulateDungeon()
    {
        ///create dungeon controller that all entities will reference

        Instantiate(dungeonController, new Vector3(), transform.rotation); //places dungeon controller first to take advantage of automatic turn order tracking

        //Instantiate(playerObject, GetRandomRoomFromQueue().getCoords(), transform.rotation); //places player character in a random room

        dungeon_room targetRoom = GetRandomRoomFromQueue(); //pick a random room to place the stairs in

        if (createdRoomsQueue.Count >= 2)                   //if we can have stairs in separate room from Player (if we grab top room, there is another room after for player to go into)
        {
//            Debug.Log("Preventing Stairs appearing in player's room");
            tappedRoomsStack.Push(createdRoomsQueue.Dequeue()); //prevent the stairs from appearing in the same room as the player (we already grabbed the stairs room, now we're just keeping it separate from random rooms list)
        }
        //clear the tile that the stairs occupy
        Vector3 stairsPos   = targetRoom.getRandomTileInRoom(); //grab a random position in the room, TODO: remove that tile, and place the stairs tile in its place
        Vector2 topRightLoc = new Vector2(stairsPos.x + 0.25f, stairsPos.y + 0.25f);
        Vector2 botLeftLoc  = new Vector2(stairsPos.x - 0.25f, stairsPos.y - 0.25f);



        Collider2D collider = Physics2D.OverlapArea(topRightLoc, botLeftLoc, TileMonoBehavior.tileLayerMask);//build a collider at stairs position to see if there is already a tile there

        if (collider != null)
        {
            Destroy(collider.gameObject);
        }
        //place stairs into the dungeon
        Instantiate(stairsTilePrefab, stairsPos, transform.rotation);
        targetRoom = GetRandomRoomFromQueue();

        //playe player into the dungeon
        Vector3 playerPos = targetRoom.getRandomTileInRoom();

        while (playerPos.x == stairsPos.x && playerPos.y == stairsPos.y) //if player directly overlaps stairs, move player someplace else
        {
            playerPos = targetRoom.getRandomTileInRoom();
        }
        Instantiate(playerObject, playerPos, transform.rotation);

        if (tappedRoomsStack.Count > 0) //shift all of the removed elements back onto the queue, preparation for placing in rest of the dungeon
        {
            createdRoomsQueue.Enqueue(tappedRoomsStack.Pop());
        }
        //TODO: insert spectacular monster/item spawning algorithm here!
        int failCount = 0; //used to kill while loop after too many failed cases

        while (numberOfMonsters > 0 && (failCount < 10))
        {
            //pick a tile, any tile...
            TileMonoBehavior tileAtSpawnPos   = null;
            dungeon_room     monsterSpawnRoom = GetRandomRoomFromQueue();
            Vector3          monsterSpawnPos  = monsterSpawnRoom.getRandomTileInRoom();
            //check if tile already occupied
            topRightLoc = new Vector2(monsterSpawnPos.x + 0.25f, monsterSpawnPos.y + 0.25f); //build a collider to fetch the tile there
            botLeftLoc  = new Vector2(monsterSpawnPos.x - 0.25f, monsterSpawnPos.y - 0.25f);
            collider    = Physics2D.OverlapArea(topRightLoc, botLeftLoc, TileMonoBehavior.tileLayerMask);
            if (collider != null) //we find a tile there
            {
                tileAtSpawnPos = collider.gameObject.GetComponent <TileMonoBehavior>();
            }
            if (tileAtSpawnPos != null && !tileAtSpawnPos.IsOccupied()) //if we found a tile and it is not occupied, spawn a monster there
            {
                Debug.Log("Creating monster at " + monsterSpawnPos.x + "," + monsterSpawnPos.y);
                GameObject spawnedEnemy = (GameObject)Instantiate(basicMonster, monsterSpawnPos, transform.rotation); //place a monster there, connect it to the tile
                tileAtSpawnPos.ConnectToEntity(spawnedEnemy.GetComponent <Entity>());
                numberOfMonsters--;
                failCount = 0;
            }
            else
            {
                failCount++;
            }
        }
        if (failCount == 10)
        {
            Debug.Log("Failed to spawn all monsters, remaining monsters: " + numberOfMonsters);
        }
    }