// 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())); }
// 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"); }
public void Remove(Item.Kind itemKind) { data[itemKind].Count--; if (data[itemKind].Count == 0) { RemoveFromSlots(itemKind); } }
public bool TryTakeItem(Item.Kind itemKind) { if (data[itemKind].IsFull) { return(false); } else { TakeItem(itemKind); return(true); } }
// --- // 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); } }
// 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); } }
// 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; } } }
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"); } }
// 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); } }
public bool Contains(Item.Kind itemKind) { return(data[itemKind].Count > 0); }
public Unit(Item.Kind kind, Point location, Player player) : base(kind, location, player) { BaseDamage = UNIT_DAMAGE; HP = UNIT_HP; }
public Tower(Item.Kind kind, Point location, Player player) : base(kind, location, player) { HP = TOWER_HP; BaseDamage = TOWER_DAMAGE; Notifer.Subscribe(TargetHandler, TOWER_FREQ); }
public Building(Item.Kind kind, Point location, Player player) : base(kind, location, player) { }
public Miner(Item.Kind kind, Point location, Player player) : base(kind, location, player) { Notifer.Subscribe(OnTimerTick, TIMER_TICK); HP = MINER_HP; }
public Producer(Item.Kind kind, Point location, Player player) : base(kind, location, player) { Notifer.Subscribe(OnTimerTick, TIMER_TICK); HP = PRODUCER_HP; UnitProgress = 0; }