Example #1
0
        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));
        }
Example #2
0
    public static PlayerHandAction CreateAction(HandActionId id, GameObject interactorGameObject, GameObject interactedGameObject)
    {
        PlayerHandAction action = null;

        switch (id)
        {
        case HandActionId.Cancel:
            action = new CancelAction(interactorGameObject, interactedGameObject);
            break;

        case HandActionId.OpenDoor:
            break;

        case HandActionId.PressButton:
            action = new PressButtonAction(interactorGameObject, interactedGameObject);
            break;

        case HandActionId.Take:
            action = new TakeAction(interactorGameObject, interactedGameObject);
            break;

        default:
            break;
        }

        return(action);
    }
Example #3
0
        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));
        }
Example #4
0
        public void CanTake_SpaceForFood_ReturnTrue()
        {
            //Setup
            var subject = new BUnit(1);
            var action  = new TakeAction(Guid.NewGuid());

            //Act
            var canAct = subject.CanAttemptAction(action);

            //Assert
            Assert.True(canAct);
        }
Example #5
0
    // Events

    void OnSelectCharacter(object sender, EventArgs args)
    {
        this.characterSelected = sender as Character;
        HexCoordinates characterCoordinates = this.characterSelected.coordinates;

        // Generate action buttons
        for (int i = 0; i < this.buttonActionsLabels.Length; i++)
        {
            Button  button     = Instantiate(this.buttonActionPrefab, this.dynamicCanvas, false);
            Vector3 tempVector = this.grid.getPositionByCoordinates(HexGrid.directions [i].X + characterCoordinates.X,
                                                                    HexGrid.directions [i].Y + characterCoordinates.Y);
            tempVector  += this.grid.transformOffset;
            tempVector.z = button.transform.position.z;
            button.transform.position = this.mainCamera.WorldToScreenPoint(tempVector);
            button.GetComponentInChildren <Text> ().text = this.buttonActionsLabels[i];
            // Bind button events & store in gamecontroller
            TakeAction actionToTake = button.GetComponent <TakeAction> ();
            actionToTake.TriggerClickButtonAction += OnTriggerClickButtonAction;
            this.buttonActions.Add(button);
        }
    }
Example #6
0
        public void Take_NonExistantTarget_FailAction()
        {
            //Setup
            var unit     = CreateUnit();
            var taker    = unit.As <ITaker>();
            var targetId = Guid.NewGuid();

            var state = CreateStateMock(CreateUnitList(unit));

            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));
        }
Example #7
0
 public AI(TakeAction actionMethod)
 {
     random            = new Random();
     this.actionMethod = actionMethod;
 }
        protected virtual void ProcessTakeAction(TakeAction action, List <StackResponseContainerInfo> stackResponses)
        {
            byte count = action.Count;
            Item sourceItem;
            Item destItem;
            StackRequestSlotInfo source      = action.Source;
            StackRequestSlotInfo destination = action.Destination;

            sourceItem = GetContainerItem(source.ContainerId, source.Slot);
            Log.Debug($"Take {sourceItem}");

            if (sourceItem.Count == count || sourceItem.Count - count <= 0)
            {
                destItem            = sourceItem;
                sourceItem          = new ItemAir();
                sourceItem.UniqueId = 0;
                SetContainerItem(source.ContainerId, source.Slot, sourceItem);
            }
            else
            {
                destItem          = (Item)sourceItem.Clone();
                sourceItem.Count -= count;
                destItem.Count    = count;
                destItem.UniqueId = Environment.TickCount & Int32.MaxValue;
            }

            SetContainerItem(destination.ContainerId, destination.Slot, destItem);

            if (source.ContainerId == 21 || source.ContainerId == 22)
            {
                if (!(GetContainerItem(21, 14) is ItemAir) && !(GetContainerItem(22, 15) is ItemAir))
                {
                    Enchantment.SendEnchantments(_player, GetContainerItem(21, 14));
                }
                else
                {
                    Enchantment.SendEmptyEnchantments(_player);
                }
            }

            stackResponses.Add(new StackResponseContainerInfo
            {
                ContainerId = source.ContainerId,
                Slots       = new List <StackResponseSlotInfo>
                {
                    new StackResponseSlotInfo()
                    {
                        Count          = sourceItem.Count,
                        Slot           = source.Slot,
                        HotbarSlot     = source.Slot,
                        StackNetworkId = sourceItem.UniqueId
                    }
                }
            });
            stackResponses.Add(new StackResponseContainerInfo
            {
                ContainerId = destination.ContainerId,
                Slots       = new List <StackResponseSlotInfo>
                {
                    new StackResponseSlotInfo()
                    {
                        Count          = destItem.Count,
                        Slot           = destination.Slot,
                        HotbarSlot     = destination.Slot,
                        StackNetworkId = destItem.UniqueId
                    }
                }
            });
        }
Example #9
0
        /// <summary>
        /// CoreItem should always be the lowest item priority in the inventory, so we can assume
        /// that there will be no more commands that could match after this method. Thus, if we can't
        /// match the string at the beginning of commands, GetAction will still skip the commands
        /// array anyway.
        /// </summary>
        /// <param name="player">
        /// The player trying to execute the action.</param>
        /// <param name="commands">A string of action commands to be parsed in order. This should contain quoted strings and
        /// core action handles (SAY, GIVE, IF, etc) only.</param>
        /// <returns>The action built from the commands array.</returns>
        override public bool DoAction(Player player, ref string[] commands, ref bool lastAction)
        {
            Action action = null;

            if (commands != null && commands.Length > 0)
            {
                int index = 0;
                switch (commands[0].ToUpper())
                {
                case "SAY":
                {
                    SayAction sayAction = new SayAction(player);
                    index++;
                    if (commands.Length > 1)
                    {
                        sayAction.Message = commands[1];
                        index++;
                    }
                    action = sayAction;
                }
                break;

                case "IF":
                {
                    IfElseAction ifAction = new IfElseAction(player);
                    index++;
                    if (commands.Length > 1)
                    {
                        index += ifAction.Parse(commands.Skip(1).ToArray());
                    }
                    action = ifAction;
                }
                break;

                case "NOT":
                {
                    NotAction notAction = new NotAction(player);
                    commands = commands.Skip(1).ToArray();          // skip to next action
                    player.Inventory.DoAction(ref commands, ref lastAction);
                    notAction.Op = lastAction;
                    // no index increment, the ref is our increment
                    action = notAction;
                }
                break;

                case "AND":
                {
                    AndAction andAction = new AndAction(player);
                    commands      = commands.Skip(1).ToArray();
                    andAction.Op1 = lastAction;
                    player.Inventory.DoAction(ref commands, ref lastAction);
                    andAction.Op2 = lastAction;
                    // no index increment, the ref is our increment
                    action = andAction;
                }
                break;

                case "OR":
                {
                    OrAction orAction = new OrAction(player);
                    commands     = commands.Skip(1).ToArray();
                    orAction.Op1 = lastAction;
                    player.Inventory.DoAction(ref commands, ref lastAction);
                    orAction.Op2 = lastAction;
                    // no index increment, the ref is our increment
                    action = orAction;
                }
                break;

                case "GIVE":
                {
                    GiveAction giveAction = new GiveAction(player);
                    index++;
                    if (commands.Length > 1)
                    {
                        giveAction.ItemName = commands[1];
                        index++;
                    }
                    if (commands.Length > 3 && commands[2].ToUpper() == "TO")
                    {
                        giveAction.ToItemName = commands[3];
                        index += 2;
                    }
                    action = giveAction;
                }
                break;

                case "TAKE":
                {
                    TakeAction takeAction = new TakeAction(player);
                    index++;
                    if (commands.Length > 1)
                    {
                        takeAction.ItemName = commands[1];
                        index++;
                    }
                    if (commands.Length > 3 && commands[2].ToUpper() == "FROM")
                    {
                        takeAction.FromItemName = commands[3];
                        index += 2;
                    }
                    action = takeAction;
                }
                break;

                case "HAS":
                {
                    HasAction hasAction = new HasAction(player);
                    index++;
                    if (commands.Length > 1)
                    {
                        hasAction.ItemName = commands[1];
                        index++;
                    }
                    if (commands.Length > 3 && commands[2].ToUpper() == "IN")
                    {
                        hasAction.InItemName = commands[3];
                        index += 2;
                    }
                    action = hasAction;
                }
                break;

                case "GO":
                {
                    GoAction goAction = new GoAction(player);
                    index++;
                    if (commands.Length > 1)
                    {
                        goAction.GiveItemName = commands[1];
                        goAction.TakeItemName = GoItemString;
                        index++;
                    }
                    if (goAction.ExecuteWhenCreated())
                    {
                        GoItemString = goAction.GiveItemName;
                    }
                    action = goAction;
                }
                break;

                case "INV":
                {
                    GroupAction groupAction = new GroupAction(player);
                    index++;
                    foreach (Item item in player.Inventory.Items
                             .Where(item => !item.Hidden))
                    {
                        SayAction sayAction = new SayAction(player);
                        sayAction.Message = item.DisplayName;
                        groupAction.Add(sayAction);
                    }
                    if (groupAction.Actions.Count < 1)
                    {
                        SayAction sayAction = new SayAction(player);
                        sayAction.Message = "You have no items.";
                        groupAction.Add(sayAction);
                    }
                    action = groupAction;
                }
                break;

                default:            // unrecognized? This is the base item, so there's no other items that could have matching actions.
                    index++;        // ignore it.
                    break;
                }

                commands = commands.Skip(index).ToArray();
            }

            if (action != null)
            {
                lastAction = action.Execute();
                return(true);
            }
            else
            {
                return(false);
            }
        }