Exemplo n.º 1
0
    ///returns the Tile at location relative to this tile, or null if tile could not be found
    ///DOES NOT physically move anything in a direction
    public TileMonoBehavior StepInDirection(int colAmount, int rowAmount)
    {
        int x = colAmount;
        int y = rowAmount;
        TileMonoBehavior givenTile = this;

        while (x > 0 && right != null)
        {
            givenTile = right;
            x++;
        }
        while (y > 0 && above != null)
        {
            givenTile = above;
            y++;
        }
        while (x < 0 && left != null)
        {
            givenTile = left;
            x--;
        }
        while (y < 0 && below != null)
        {
            givenTile = below;
            y--;
        }
        return(givenTile);
    }
Exemplo n.º 2
0
    public enum DamageType { poking, burn };                             ///the types of damage that an entity can take for the purpose of the TakeDamage function

    ///On Start, Entity attempts to bind itself to the Tile underneath it
    ///on fail, Entity destroys itself
    public virtual void Start()
    {
        if (occupyingTile == null)
        {
//            Debug.Log("Checking space beneath this entity at " + transform.position.x + "," + transform.position.y);
            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);
            if (collider != null)
            {
                TileMonoBehavior belowTile = collider.gameObject.GetComponent <TileMonoBehavior>();
                if (belowTile != null)
                {
                    if (!belowTile.ConnectToEntity(this)) //if could not connect entity to a tile, we should garbage collect entity because it can't interract with game at all
                    {
                        Debug.Log("ALERT! Found Tile Layer Object at " + transform.position.x + "," + transform.position.y + " that lacks TileMonoBehavior script!");
                        Destroy(this.gameObject);
                    }
                }
            }
            else
            {
                Debug.Log("Could not find Tile for this entity to stand on, destroying this");
                Destroy(this.gameObject);
            }
        }
    }
Exemplo n.º 3
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);
    }
Exemplo n.º 4
0
 ///cleans up connections this Tile has to other Tiles
 void DeleteTile()
 {
     below           = null;
     above           = null;
     left            = null;
     right           = null;
     occupyingEntity = null;
 }
Exemplo n.º 5
0
 ///Initializes list of changed tiles and adds tiles to list when called
 public static void AddToTileList(TileMonoBehavior tile)
 {
     Debug.Log("Adding tiles");
     if (oddTiles == null)
     {
         oddTiles = new List <TileMonoBehavior>();
     }
     oddTiles.Add(tile);
 }
Exemplo n.º 6
0
 ///Changes tile back to default material if needed and returns reverting tiles back to RemoveAll function
 private static bool getRevertingTiles(TileMonoBehavior tile)
 {
     if (tile.timeToRevert == 0)
     {
         tile.GetComponent <SpriteRenderer>().material = tile.defaultMaterial;
         tile.inList = false;
     }
     return(tile.timeToRevert == 0);
 }
Exemplo n.º 7
0
 ///initializes this Tile relative to the tile below and left of it
 ///both tiles will be "pointing" (not using pointers) to each other
 public void InitTile(TileMonoBehavior belowTile, TileMonoBehavior leftTile)
 {
     this.below = belowTile;
     this.left  = leftTile;
     if (belowTile != null)
     {
         belowTile.above = this;
     }
     if (leftTile != null)
     {
         leftTile.right = this;
     }
 }
Exemplo n.º 8
0
 void Start()
 {
     //Debug.Log("Started");
     transform.position.Set(transform.position.x, transform.position.y, tileZLayer);
     defaultMaterial = GetComponent <SpriteRenderer>().material;
     ///get all tiles L,R,U,D relative to this one, and point them together (first acting tile connects both ways, so no need to connect back later, and makes later tiles start faster)
     if (right == null) //if tile hasn't been already defined (because we don't want to define the same tile's same value twice)
     //look for a tile right of this
     {
         Vector2    topRightLoc = new Vector2(transform.position.x + 1.25f, transform.position.y + 0.25f);
         Vector2    botLeftLoc  = new Vector2(transform.position.x + 0.75f, transform.position.y - 0.25f);
         Collider2D collider    = Physics2D.OverlapArea(topRightLoc, botLeftLoc, tileLayerMask);
         if (collider != null)
         {
             TileMonoBehavior newRight = collider.gameObject.GetComponent <TileMonoBehavior>();
             if (newRight != null)
             {
                 //Debug.Log("Tile at " + transform.position.x + "," + transform.position.y + " found a right Tile");
                 right      = newRight;
                 right.left = this;
             }
         }
     }
     if (left == null) //look for a tile left of this
     {
         Vector2    topRightLoc = new Vector2(transform.position.x - 0.75f, transform.position.y + 0.25f);
         Vector2    botLeftLoc  = new Vector2(transform.position.x - 1.25f, transform.position.y - 0.25f);
         Collider2D collider    = Physics2D.OverlapArea(topRightLoc, botLeftLoc, tileLayerMask);
         if (collider != null)
         {
             TileMonoBehavior newTile = collider.gameObject.GetComponent <TileMonoBehavior>();
             if (newTile != null)
             {
                 left       = newTile;
                 left.right = this;
             }
         }
     }
     if (above == null) //look for a tile above this
     {
         Vector2    topRightLoc = new Vector2(transform.position.x + 0.25f, transform.position.y + 1.25f);
         Vector2    botLeftLoc  = new Vector2(transform.position.x - 0.25f, transform.position.y + 0.75f);
         Collider2D collider    = Physics2D.OverlapArea(topRightLoc, botLeftLoc, tileLayerMask);
         if (collider != null)
         {
             TileMonoBehavior newTile = collider.gameObject.GetComponent <TileMonoBehavior>();
             if (newTile != null)
             {
                 above       = newTile;
                 above.below = this;
             }
         }
     }
     if (below == null) //look for a tile below this
     {
         Vector2    topRightLoc = new Vector2(transform.position.x + 0.25f, transform.position.y - 0.75f);
         Vector2    botLeftLoc  = new Vector2(transform.position.x - 0.25f, transform.position.y - 1.25f);
         Collider2D collider    = Physics2D.OverlapArea(topRightLoc, botLeftLoc, tileLayerMask);
         if (collider != null)
         {
             TileMonoBehavior newTile = collider.gameObject.GetComponent <TileMonoBehavior>();
             if (newTile != null)
             {
                 below       = newTile;
                 below.above = this;
             }
         }
     }
     if (occupyingEntity == null) //if there is no entity already defined, see if there is one standing on top of this tile
     {
         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, entitiesLayerMask);
         if (collider != null)
         {
             Entity foundEntity = collider.gameObject.GetComponent <Entity>();
             if (foundEntity != null)
             {
                 occupyingEntity = foundEntity;
                 foundEntity.goToTile(this);
             }
         }
     }
 }
Exemplo n.º 9
0
    void Update()
    {
        if (testingMode >= 0) //testing using the clock, but only run terrain testing after initial testing finished
        {
            delayClock += Time.deltaTime;
            if (delayClock >= 1) //1 second has passed
            {
                delayClock = 0;
                switch (testingMode)
                {
                case 0:     //should find new tile 5 left of player, connected to right of it
                {
                    Vector2    topRightLoc = new Vector2(transform.position.x - 4.75f, transform.position.y + 0.25f);
                    Vector2    botLeftLoc  = new Vector2(transform.position.x - 5.25f, transform.position.y - 0.25f);
                    Collider2D collider    = Physics2D.OverlapArea(topRightLoc, botLeftLoc, TileMonoBehavior.tileLayerMask);
                    if (collider != null)
                    {
                        print("Found tile at location, success! Checking tile's connections");
                        TileMonoBehavior tileScript = collider.gameObject.GetComponent <TileMonoBehavior>();
                        if (tileScript == null)
                        {
                            print("ALERT! GameObject we found is not a Tile, aborting tests");
                            testingMode = -2;
                            return;
                        }
                        bool testsFailed = false;
                        if (tileScript.getLeft() != null)
                        {
                            print("ALERT! Found left tile that shouldn't be there");
                            testsFailed = true;
                        }

                        if (tileScript.getRight() == null)
                        {
                            print("ALERT! Found no right tile, but there should be one there...");
                            testsFailed = true;
                        }

                        if (tileScript.getAbove() != null)
                        {
                            print("ALERT! Found above tile that shouldn't be there");
                            testsFailed = true;
                        }

                        if (tileScript.getBelow() != null)
                        {
                            print("ALERT! Found below tile that shouldn't be there");
                            testsFailed = true;
                        }
                        if (testsFailed)
                        {
                            print("Tests failed! Aborting");
                            testingMode = -2;
                            return;
                        }
                        TerrainTesting();         //now testingMode is 0
                    }
                    break;
                }

                case 1:     //see if monster still exists in game
                {
                    if (DungeonManager.turnOrder.Count != lastTurnOrderCount)
                    {
                        print("ALERT! Expected invalid created monster to be deleted, but turn order does not reflect this! Aborting!");
                        print("Turn Order was actually " + DungeonManager.turnOrder.Count);
                        testingMode = -2;
                        return;
                    }
                    print("Entity count remained the same as expected");
                    TerrainTesting();
                    break;
                }

                case 2:     //see if monster still exists in game
                {
                    if (DungeonManager.turnOrder.Count != (lastTurnOrderCount + 1))
                    {
                        print("ALERT! Expected created monster to not be deleted, but turn order does not reflect this! Aborting!");
                        print("Turn Order was actually " + DungeonManager.turnOrder.Count);
                        testingMode = -2;
                        return;
                    }
                    print("Entity count went up by 1 as expected");
                    TerrainTesting();
                    break;
                }

                case 3:     //see if monster still exists in game
                {
                    if (DungeonManager.turnOrder.Count != (lastTurnOrderCount + 1))
                    {
                        print("ALERT! Expected created monster to not be deleted, but turn order does not reflect this! Aborting!");
                        print("Turn Order was actually " + DungeonManager.turnOrder.Count);
                        testingMode = -2;
                        return;
                    }
                    print("Entity count went up by 1 as expected");
                    TerrainTesting();
                    break;
                }

                case 4:
                {
                    print("finished with Terrain testing!");
                    print("Testing finished!");
                    testingMode = -1;         //prevent looping through Update
                    return;
                }

                default:    //no defined testing case to do... "We're done here" (Cave Johnson)
                {
                    if (testingMode < 5)
                    {
                        testingMode++;
                    }
                    return;
                }
                }
            }
        }
    }
Exemplo n.º 10
0
    ///precondition: newTile does not have an occupying Entity, or occupying Entity must be garbage collected
    ///moves this Enity to the specified Tile and begins tracking it
    ///assumes it is already possible for Entity to move to designated Tile (tile is walkable, not occupied)
    public void goToTile(TileMonoBehavior newTile)
    {
        if (occupyingTile != null)
        {
            occupyingTile.occupyingEntity = null; //makes old tile not think this Entity is still there
        }
        occupyingTile = newTile;
        occupyingTile.occupyingEntity = this; //may forcefully evict existing entity from tile
        transform.position            = new Vector3(occupyingTile.transform.position.x, occupyingTile.transform.position.y, transform.position.z);
        //entitites that go to tiles have status effects applied to them
        if (occupyingTile.GetComponent <SpriteRenderer>().sharedMaterial == Resources.Load("Materials/Red"))   //standing in fire
        {
            Debug.Log(this + " takes 2 damage for standing in the fire");
            this.TakeDamage(2, DamageType.burn);
        }
        else if (occupyingTile.GetComponent <SpriteRenderer>().sharedMaterial == Resources.Load("Materials/Yellow"))   //standing in... wind?
        {
            if (this.speed > 1)
            {
                this.speed--;
            }
        }
        else if (occupyingTile.GetComponent <SpriteRenderer>().sharedMaterial == Resources.Load("Materials/Blue"))
        {
            while (occupyingTile.GetComponent <SpriteRenderer>().sharedMaterial == Resources.Load("Materials/Blue"))
            {
                TileMonoBehavior targetTile;
                switch (lastDirection)
                {
                case (MoveDirection.up):
                    targetTile = occupyingTile.getAbove();
                    break;

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

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

                default:
                    targetTile = occupyingTile.getRight();
                    break;
                }
                if (targetTile == null)
                {
                    break;
                }
                if (targetTile.IsWalkable())
                {
                    if (!targetTile.IsOccupied())
                    {
                        goToTile(targetTile);
                    }
                    else                 //ALERT! Potential issue here: if occupying entity is shoved into another entity, the other entity will take damage until it dies or its occupying tile changes
                    //thus this spell makes the pushing entity kill any entities it is pushed into
                    {
                        Debug.Log(targetTile.occupyingEntity + " takes " + damageAmount + " " + attackType + " damage from the shoved " + this);
                        targetTile.occupyingEntity.TakeDamage(damageAmount, attackType);
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            //Debug.Log("Finished sliding");
        }
    }
    /// 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);
        }
    }