///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); }
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; } } } } }
///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"); } }