public void notify(GameEvent gameEvent) { foreach (GameEventObserver o in observers) { o.notify(gameEvent); } }
/// <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 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 + ")"); } } } }