public void Attack_TargetHealthBelowZero_TargetDeletedFromState() { //Setup var unit = CreateUnit(); var attacker = unit.As <IAttacker>(); var attack = new Random().Next(1, 10); attacker.Setup(a => a.AttackLevel).Returns(attack); var target = new Mock <WorldObject>(); target.Setup(t => t.ShouldRemove()).Returns(true); var targetId = Guid.NewGuid(); var state = CreateStateMock(CreateUnitList(unit)); state.Setup(s => s.FindObject(targetId)).Returns(target.Object); AddUnitToState(unit, state); var action = new AttackAction(targetId); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var subject = new ActionExecutor(state.Object); //Act subject.Execute(actions); //Assert target.Verify(t => t.ShouldRemove()); state.Verify(s => s.Destroy(target.Object)); }
public void Move_RoughTerrain_UnitStationary(MoveAction.Direction direction, int x, int y) { //Setup var unit = CreateUnit(); unit.Setup(u => u.Location).Returns(new Point(5, 5)); var newLocation = new Point(x, y); var state = CreateStateMock(CreateUnitList(unit)); state.Setup(s => s.Geography.GetTerrainAt(newLocation)).Returns(Geography.Terrain.ROUGH); AddUnitToState(unit, state); var action = new MoveAction(direction); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var subject = new ActionExecutor(state.Object); //Act subject.Execute(actions); //Assert //Moved in the correct direction unit.Verify(u => u.Move(newLocation), Times.Never); }
public void Drop_UnownedObject_FailAction() { //Setup var unit = CreateUnit(); var dropper = unit.As <IDropper>(); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var subject = new ActionExecutor(state.Object); var transferObject = new Mock <WorldObject>(); var transferable = transferObject.As <ITransferable>(); //Has no owner transferable.Setup(tr => tr.Owner).Returns(null); var transferGuid = Guid.NewGuid(); var action = new DropAction(transferGuid); state.Setup(s => s.FindObject(transferGuid)).Returns(transferObject.Object); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); //Act & Assert Assert.Throws <BadActionException>(() => subject.Execute(actions)); }
public void Attack_AdjacentTarget_ReducesTargetHealth() { //Setup var unit = CreateUnit(); var attacker = unit.As <IAttacker>(); var attack = new Random().Next(1, 10); attacker.Setup(a => a.AttackLevel).Returns(attack); var target = new Mock <WorldObject>(); var targetId = Guid.NewGuid(); var state = CreateStateMock(CreateUnitList(unit)); state.Setup(s => s.FindObject(targetId)).Returns(target.Object); AddUnitToState(unit, state); var action = new AttackAction(targetId); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var subject = new ActionExecutor(state.Object); //Act subject.Execute(actions); //Assert target.Verify(t => t.TakeDamage(attack)); }
public void Take_DistantTarget_FailsAction() { //Setup var unit = CreateUnit(); var taker = unit.As <ITaker>(); var target = new Mock <WorldObject>(); var transfer = target.As <ITransferable>(); var targetId = Guid.NewGuid(); target.Setup(t => t.Location).Returns(new System.Drawing.Point(10, 10)); var state = CreateStateMock(CreateUnitList(unit)); state.Setup(s => s.FindObject(targetId)).Returns(target.Object); AddUnitToState(unit, state); var action = new TakeAction(targetId); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var subject = new ActionExecutor(state.Object); //Act & Assert Assert.Throws <BadActionException>(() => subject.Execute(actions)); }
public void Create_ValidCreateAction_NewObjectCreatedAndAddedToState() { //Setup var unit = CreateUnit(); var creator = unit.As <ICreator>(); var action = new CreateAction(0); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var obj = new Mock <WorldObject>().Object; creator.Setup(c => c.CanCreate(action)).Returns(true); creator.Setup(c => c.Create(action)).Returns(obj); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var subject = new ActionExecutor(state.Object); //Act subject.Execute(actions); //Assert creator.Verify(c => c.CanCreate(action)); creator.Verify(c => c.Create(action)); state.Verify(s => s.Add(obj)); }
public void Drop_NonTransferableObject_FailsAction() { //Setup var unit = CreateUnit(); var dropper = unit.As <IDropper>(); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var transferObject = new Mock <WorldObject>(); //Object not given the ITransferable interface so it will fail var transferGuid = Guid.NewGuid(); var action = new DropAction(transferGuid); state.Setup(s => s.FindObject(transferGuid)).Returns(transferObject.Object); var subject = new ActionExecutor(state.Object); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); //Act & Assert Assert.Throws <BadActionException>(() => subject.Execute(actions)); }
public void Take_AdjacentAvailableTarget_UnitOwnsTarget() { //Setup var unit = CreateUnit(); var taker = unit.As <ITaker>(); var target = new Mock <WorldObject>(); var transfer = target.As <ITransferable>(); var targetId = Guid.NewGuid(); var state = CreateStateMock(CreateUnitList(unit)); state.Setup(s => s.FindObject(targetId)).Returns(target.Object); AddUnitToState(unit, state); var action = new TakeAction(targetId); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var subject = new ActionExecutor(state.Object); //Act subject.Execute(actions); //Assert transfer.Verify(t => t.TransferTo(unit.Object.Id)); taker.Verify(t => t.Take(transfer.Object)); }
public void Drop_UnitCannotDropObject_NoActionTaken() { //Setup var unit = CreateUnit(); var dropper = unit.As <IDropper>(); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var subject = new ActionExecutor(state.Object); var transferObject = new Mock <WorldObject>(); var transferable = transferObject.As <ITransferable>(); transferable.Setup(tr => tr.Owner).Returns(unit.Object.Id); var transferGuid = Guid.NewGuid(); var action = new DropAction(transferGuid); state.Setup(s => s.FindObject(transferGuid)).Returns(transferObject.Object); //Unit will not drop on its end dropper.Setup(d => d.Drop(transferable.Object)).Returns(false); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); //Act subject.Execute(actions); //Assert //Dropper asked to drop but transfer object not asked to drop dropper.Verify(d => d.Drop(transferable.Object)); transferable.Verify(t => t.Drop(), Times.Never); }
public void Display_DisplayUpdateCalled() { //Setup var unit = CreateUnit(); SensoryDisplay display = new SensoryDisplay(); display.Taste.ResetTo(1, 5); unit.Setup(u => u.Display.UpdateFrom(display)); var action = new DisplayAction(display); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var subject = new ActionExecutor(state.Object); //Act subject.Execute(actions); //Assert unit.Verify(unit => unit.Display.UpdateFrom(display)); }
public void Drop_NotIDropper_FailsAction() { //Setup var unit = CreateUnit(); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var transferGuid = Guid.NewGuid(); var action = new DropAction(transferGuid); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var subject = new ActionExecutor(state.Object); //Act & Assert Assert.Throws <BadActionException>(() => subject.Execute(actions)); }
private UnitActionCollection DecideNextActions(WorldState state) { var actions = new UnitActionCollection(); if (Players.Count == 0) { var units = state.Units; actions.SetDefaultActions(units); return(actions); } foreach (var player in Players) { actions.Merge(player.Step(state)); } return(actions); }
public void Create_NotACreatingUnit_FailsAction() { //Setup var unit = CreateUnit(); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var action = new CreateAction(0); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var subject = new ActionExecutor(state.Object); //Act & Assert Assert.Throws <BadActionException>(() => subject.Execute(actions)); }
public void Attack_NoSuchTarget_FailsAction() { //Setup var unit = CreateUnit(); var targetId = Guid.NewGuid(); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var action = new AttackAction(targetId); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var subject = new ActionExecutor(state.Object); //Act & Assert Assert.Throws <BadActionException>(() => subject.Execute(actions)); }
public void Drop_InvalidObject_FailAction() { //Setup var unit = CreateUnit(); var dropper = unit.As <IDropper>(); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var subject = new ActionExecutor(state.Object); var action = new DropAction(Guid.NewGuid()); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); //Act & Assert //No object setup so it is invalid Assert.Throws <BadActionException>(() => subject.Execute(actions)); }
public void GiveAway_InvalidObject_FailAction() { //Setup var first = CreateUnit(); var dropper = first.As <IDropper>(); var second = CreateUnit(); var state = CreateStateMock(CreateUnitList(first, second)); AddUnitToState(first, state); AddUnitToState(second, state); var transferGuid = Guid.NewGuid(); var action = new GiveAction(transferGuid, second.Object.Id); var subject = new ActionExecutor(state.Object); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(first.Object.Id, action); //Assertion Assert.Throws <BadActionException>(() => subject.Execute(actions)); }
public void Display_Reset_UnitResetDisplayCalled() { //Setup var unit = CreateUnit(); var action = new DisplayAction(); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var subject = new ActionExecutor(state.Object); //Act subject.Execute(actions); //Assert unit.Verify(unit => unit.ResetDisplay()); }
public void GiveAway_ValidObjectAndRecepient_OwnerChanged() { //Setup var first = CreateUnit(); var dropper = first.As <IDropper>(); var second = CreateUnit(); var receiver = second.As <IReceiver>(); var state = CreateStateMock(CreateUnitList(first, second)); AddUnitToState(first, state); AddUnitToState(second, state); var subject = new ActionExecutor(state.Object); var transferObject = new Mock <WorldObject>(); var transferable = transferObject.As <ITransferable>(); transferable.Setup(tr => tr.Owner).Returns(first.Object.Id); var transferGuid = Guid.NewGuid(); var action = new GiveAction(transferGuid, second.Object.Id); dropper.Setup(d => d.Drop(transferable.Object)).Returns(true); receiver.Setup(t => t.Receive(transferable.Object)).Returns(true); state.Setup(s => s.FindObject(transferGuid)).Returns(transferObject.Object); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(first.Object.Id, action); //Act subject.Execute(actions); //Assert //Dropper dropped, reciever received, and transfer object knows its new owner dropper.Verify(d => d.Drop(transferable.Object)); receiver.Verify(r => r.Receive(transferable.Object)); transferable.Verify(t => t.Drop()); transferable.Verify(t => t.TransferTo(second.Object.Id)); }
public void Drop_ValidObject_OwnerChangedAndObjectMoved() { //Setup var unit = CreateUnit(); var dropper = unit.As <IDropper>(); var point = new Point(5, 5); unit.Setup(u => u.Location).Returns(point); var state = CreateStateMock(CreateUnitList(unit)); AddUnitToState(unit, state); var subject = new ActionExecutor(state.Object); var transferObject = new Mock <WorldObject>(); var transferable = transferObject.As <ITransferable>(); transferable.Setup(tr => tr.Owner).Returns(unit.Object.Id); var transferGuid = Guid.NewGuid(); var action = new DropAction(transferGuid); dropper.Setup(d => d.Drop(transferable.Object)).Returns(true); state.Setup(s => s.FindObject(transferGuid)).Returns(transferObject.Object); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); //Act subject.Execute(actions); //Assert //Dropper dropped and transfer object knows it has been dropped dropper.Verify(d => d.Drop(transferable.Object)); transferable.Verify(t => t.Drop()); transferObject.Verify(t => t.Move(point)); }
public void Take_InvalidTarget_FailAction() { //Setup var unit = CreateUnit(); var taker = unit.As <ITaker>(); var target = new Mock <WorldObject>(); var targetId = Guid.NewGuid(); var state = CreateStateMock(CreateUnitList(unit)); state.Setup(s => s.FindObject(targetId)).Returns(target.Object); AddUnitToState(unit, state); var action = new TakeAction(targetId); UnitActionCollection actions = new UnitActionCollection(); actions.AddAction(unit.Object.Id, action); var subject = new ActionExecutor(state.Object); //Act & Assert Assert.Throws <BadActionException>(() => subject.Execute(actions)); }