Ejemplo n.º 1
0
 // Determine whether we can use `itemKind` in a slot based on the state
 // of inventory.
 private bool IsUsableInSlots(Item.Kind itemKind)
 {
     return
         (IsUsable(itemKind) &&
          Contains(itemKind) &&
          !slots.Contains(itemKind.Some()));
 }
Ejemplo n.º 2
0
        // Pre-conditions: `IsUsable(itemKind) && Contains(itemKind)`
        private void LoadInEmptySlot(Item.Kind itemKind)
        {
            if (slots.Contains(itemKind.Some()))
            {
                Console.WriteLine(
                    "[DEBUG] A slot already contains item {0}",
                    itemKind
                    );
                return;
            }

            for (int i = 0; i < slots.Count; i++)
            {
                if (!slots[i].HasValue)
                {
                    slots[i] = itemKind.Some();
                    Console.WriteLine(
                        "Loaded item {0} in slot {1}",
                        itemKind, i
                        );
                    return;
                }
            }

            Console.WriteLine("[DEBUG] All slots full");
        }
Ejemplo n.º 3
0
 public void Remove(Item.Kind itemKind)
 {
     data[itemKind].Count--;
     if (data[itemKind].Count == 0)
     {
         RemoveFromSlots(itemKind);
     }
 }
Ejemplo n.º 4
0
 public bool TryTakeItem(Item.Kind itemKind)
 {
     if (data[itemKind].IsFull)
     {
         return(false);
     }
     else
     {
         TakeItem(itemKind);
         return(true);
     }
 }
Ejemplo n.º 5
0
        // ---
        // TODO: Can't use `Serialization.Collections` utilities because can't
        // implement `INetSerializable` on `Item.Kind` or `Option<Item.Kind>`.
        // For now we'll just do it manually.

        private void SerializeData(NetDataWriter writer)
        {
            int count = data.Count;

            writer.Put(count);
            foreach (KeyValuePair <Item.Kind, StorageInfo> pair in data)
            {
                Item.Kind   itemKind    = pair.Key;
                StorageInfo storageInfo = pair.Value;
                writer.Put((int)pair.Key);
                storageInfo.Serialize(writer);
            }
        }
Ejemplo n.º 6
0
        // Load item in `slot` with the next or previous item in the inventory
        // (determined by `forward`). The next or previous item is based on the
        // original item stored in the slot. If there are no other available
        // items, the original item will remain in the slot.
        //
        // If `slot` is empty, then we chose the first item available.
        //
        // Pre-condition: `this.slots` has index `slot`
        public void LoadItemInSlot(int slot, bool forward)
        {
            // Clear `slot` after saving the original item loaded there. By the
            // end of this method, we will have replaced the item in `slot`. If
            // there are no other item kinds available, the original item kind
            // can be loaded back in.
            Option <Item.Kind> oldItemKindOpt = slots[slot];

            ClearSlot(slot);

            List <Item.Kind> itemKinds = Enum.GetValues(typeof(Item.Kind))
                                         .Cast <Item.Kind>().ToList();

            int startIndex = oldItemKindOpt.Match(
                some: oldItemKind => {
                int result = itemKinds.FindIndex(x => x == oldItemKind);
                result    += forward ? 1 : -1;
                result     = Mod(result, itemKinds.Count);
                return(result);
            },
                none: () => { return(0); }
                );

            // Loop through `itemKinds` starting at `startIndex`. If we find an
            // item that we can use, put it into the slot and break from the
            // loop. The boolean `forward` determines whether we loop forwards
            // or backwards.
            int i = startIndex; // Index for `itemKinds`

            for (int j = 0; j < itemKinds.Count; j++)
            {
                // `j` only ensures that we only loop through the list at most
                // once
                Item.Kind itemKind = itemKinds[i];
                if (IsUsableInSlots(itemKind))
                {
                    slots[slot] = itemKind.Some();
                    Console.WriteLine(
                        "Loaded item {0} into slot {1}",
                        itemKind, slot
                        );
                    break;
                }

                i += forward ? 1 : -1;
                i  = Mod(i, itemKinds.Count);
            }
        }
Ejemplo n.º 7
0
        // Remove from slots if present
        private void RemoveFromSlots(Item.Kind itemKind)
        {
            for (int i = 0; i < slots.Count; i++)
            {
                bool remove = slots[i].Match(
                    some: slot => { return(slot == itemKind); },
                    none: () => { return(false); }
                    );

                if (remove)
                {
                    slots[i] = Option.None <Item.Kind>();
                    return;
                }
            }
        }
Ejemplo n.º 8
0
        private void TakeItem(Item.Kind itemKind)
        {
            data[itemKind].Count++;
            Console.WriteLine(
                "Stored item {0} for a total of {1}",
                itemKind,
                data[itemKind].Count
                );

            if (IsUsable(itemKind))
            {
                LoadInEmptySlot(itemKind);
            }
            else
            {
                Console.WriteLine("Not usable in slot");
            }
        }
Ejemplo n.º 9
0
        // Determine whether `itemKind` is an item that can be used (i.e. one
        // that produces an action).
        private static bool IsUsable(Item.Kind itemKind)
        {
            switch (itemKind)
            {
            case Item.Kind.Compass:
            case Item.Kind.Bow:
            case Item.Kind.Sword:
            case Item.Kind.Bomb:
            case Item.Kind.Boomerang:
            case Item.Kind.Clock:
            case Item.Kind.RedKey:
            case Item.Kind.BlueKey:
            case Item.Kind.GreenKey:
            case Item.Kind.PurpleKey:
            case Item.Kind.YellowKey:
            case Item.Kind.TriforceShard:
                return(true);

            default:
                return(false);
            }
        }
Ejemplo n.º 10
0
 public bool Contains(Item.Kind itemKind)
 {
     return(data[itemKind].Count > 0);
 }
Ejemplo n.º 11
0
 public Unit(Item.Kind kind, Point location, Player player) : base(kind, location, player)
 {
     BaseDamage = UNIT_DAMAGE;
     HP         = UNIT_HP;
 }
Ejemplo n.º 12
0
 public Tower(Item.Kind kind, Point location, Player player) : base(kind, location, player)
 {
     HP         = TOWER_HP;
     BaseDamage = TOWER_DAMAGE;
     Notifer.Subscribe(TargetHandler, TOWER_FREQ);
 }
Ejemplo n.º 13
0
 public Building(Item.Kind kind, Point location, Player player) : base(kind, location, player)
 {
 }
Ejemplo n.º 14
0
 public Miner(Item.Kind kind, Point location, Player player) : base(kind, location, player)
 {
     Notifer.Subscribe(OnTimerTick, TIMER_TICK);
     HP = MINER_HP;
 }
Ejemplo n.º 15
0
 public Producer(Item.Kind kind, Point location, Player player) : base(kind, location, player)
 {
     Notifer.Subscribe(OnTimerTick, TIMER_TICK);
     HP           = PRODUCER_HP;
     UnitProgress = 0;
 }