/// <summary> /// This method will process an AttackEvent that the unit observed and will determine how the unit should "react" to it. /// </summary> /// <param name="unit"></param> /// <param name="gameEvent"></param> /// <param name="gw"></param> private static void handleAttackEventUnit(Unit unit, GameEvent gameEvent, GameWorld gw) { // Check if unit is in the Passive AttackStance. if (unit.getAttackStance() == Unit.AttackStance.Passive) { // Ignore the AttackEvent. return; } // If an ally Entity is being attacked. if (gameEvent.targetEntity.getOwner() == unit.getOwner() && unit.getOwner().isEnemy(gameEvent.sourceEntity.getOwner())) { // If the unit is not performing any action. if (unit.getActionQueue().Count == 0) { // Attack the sourceEnemy } } }
/// <summary> /// This method will update the visibility map to show that all Cells in a Units visibilityRange have been explored. /// </summary> /// <param name="unit"></param> public void updateVisMap(Unit unit) { // Only update the map if the unit belongs to the Human player. if (unit.getOwner() != humanPlayer) { return; } byte offset = (byte)unit.stats.visibilityRange; int xStart = (short)unit.x - offset; int xEnd = (short)unit.x + offset; int yStart = (short)unit.y - offset; int yEnd = (short)unit.y + offset; exploreMap(xStart, xEnd, yStart, yEnd); }
/// <summary> /// This function will process a MoveEvent and will make the unit "react" to that move event. /// </summary> /// <param name="unit">The Unit that observed the event.</param> /// <param name="gameEvent">The MoveEvent</param> /// <param name="gw">The GameWorld this is occurring in.</param> private static void handleMoveEventUnit(Unit unit, GameEvent gameEvent, GameWorld gw) { Console.Write("Unit at (" + unit.x + ", " + unit.y + "): "); // Check if unit caused this MoveEvent if (gameEvent.sourceEntity == (Entity)unit) { Unit target = searchCellsForEnemy(unit, gw); if (target != null) { Console.WriteLine("See Enemy at " + target.getCell().Xcoord + ", ("+ target.getCell().Ycoord + ")"); } } // unit did not cause move event else { // Check if sourceEntity is an enemy if (unit.getOwner().isEnemy(gameEvent.sourceEntity.getOwner())) { // Check if unit is in the Aggressive AttackStance if (unit.getAttackStance() == Unit.AttackStance.Agressive) { if(isInterruptable(unit)) { AttackAction attackAction = new AttackAction(unit, gameEvent.sourceEntity, gw); ActionController.Instance.giveCommand(unit, attackAction); Console.WriteLine("Aggressive attack the entity in cell: " + gameEvent.orginCell.Xcoord + ", " + gameEvent.orginCell.Ycoord + ")"); } } // Check if unit is in the Guard AttackStance else if (unit.getAttackStance() == Unit.AttackStance.Guard) { GuardAttack guardAttack = new GuardAttack(unit, gameEvent.sourceEntity, gw); ActionController.Instance.giveCommand(unit, guardAttack); Console.WriteLine("Guard attack the entity in cell: " + gameEvent.orginCell.Xcoord + ", " + gameEvent.orginCell.Ycoord + ")"); } } } }
/*** HELPER FUNCTIONS ****/ /// <summary> /// This function will search the cells that the unit can see for the closest enemy Unit. /// </summary> /// <param name="unit">The unit doing the searching</param> /// <param name="gw">The GameWorld to search</param> /// <returns>The closest enemy Unit or null if none exists</returns> private static Unit searchCellsForEnemy(Unit unit, GameWorld gw) { byte offset = (byte)unit.stats.visibilityRange; int xStart = (short)unit.x - offset; int xEnd = (short)unit.x + offset; int yStart = (short)unit.y - offset; int yEnd = (short)unit.y + offset; // Make sure that our bounds are valid. (Assumes that no Unit has a visibility range longer than the map.) if (xStart < 0) { xStart = 0; } else if (xEnd >= gw.map.width) { xEnd = gw.map.width; } if (yStart < 0) { yStart = 0; } else if (yEnd >= gw.map.height) { yEnd = gw.map.height; } Unit target = null; float distance = 10000f; // Set all cell explored flags to true. for (int i = xStart; i < xEnd; i++) { for (int j = yStart; j < yEnd; j++) { Unit temp = gw.map.getCell(i,j).getUnit(); if (temp != null && unit.getOwner().isEnemy(temp.getOwner())) { float tDis = EntityLocController.findDistance(unit.x, unit.y, temp.x, temp.y); if (tDis < distance) { target = temp; distance = tDis; } } } } return target; }