private void OnUnloadWeapon() { var selectionIsLoadedWeapon = selection?.Ammunition != null; var bothHandsAreEmpty = soldier.RightHand == null && soldier.LeftHand == null; var canUnloadWeapon = selectionIsLoadedWeapon && bothHandsAreEmpty; if (!canUnloadWeapon) return; if (!TryConsumeTimeUnits(8)) return; soldier.LeftHand = new BattleItem { Item = selection.Ammunition, Ammunition = null, Rounds = selection.Rounds }; soldier.RightHand = selection; selection.Ammunition = null; selection.Rounds = 0; selection = null; BattlescapeSoundEffect.Reload.Play(); }
private void TryLoadWeapon(BattleItem item) { if (!item.CanLoadWith(selection)) return; if (!TryConsumeTimeUnits(15)) return; item.Ammunition = (AmmunitionType) selection.Item; item.Rounds = selection.Rounds; selection = null; BattlescapeSoundEffect.Reload.Play(); }
private void ClickBattleItemRow(BattleItem[] items, InventoryLocation location, int column) { if (selection == null) { selectionSource = location; selection = SelectItem(items, column); } else { var dropLocation = GetDropLocation(items, column, selection); if (dropLocation == null) return; if (!TryConsumeTimeUnits(GetTransferTimeUnits(location))) return; items[dropLocation.Value] = selection; selection = null; BattlescapeSoundEffect.ItemDrop.Play(); } }
private BattleItem ClickHand(BattleItem item, InventoryLocation location) { if (selection == null && item == null) return null; if (selection != null && item != null) { TryLoadWeapon(item); return item; } if (item != null) { selection = item; selectionSource = location; return null; } if (!TryConsumeTimeUnits(GetTransferTimeUnits(location))) return null; var newItem = selection; selection = null; BattlescapeSoundEffect.ItemDrop.Play(); return newItem; }
private static BattleItem SelectItem(BattleItem[] items, int column) { foreach (var index in Enumerable.Range(0, items.Length)) { var item = items[index]; if (item == null) continue; if (column >= index && column < index + item.Width) { items[index] = null; return item; } } return null; }
private void ClickBattleItemGrid( BattleItem[,] items, InventoryLocation location, int row, int column, Func<int, int, int, int, bool> canDropHere = null) { if (selection == null) { selectionSource = location; selection = SelectItem(items, row, column); if (location == InventoryLocation.Ground && selection != null) ground.Remove(selection); } else { var dropLocation = GetDropLocation(items, row, column, selection); if (dropLocation == null) return; var dropRow = dropLocation.Value.Y; var dropColumn = dropLocation.Value.X; if (canDropHere != null && !canDropHere(dropRow, dropColumn, selection.Width, selection.Height)) return; if (!TryConsumeTimeUnits(GetTransferTimeUnits(location))) return; items[dropRow, dropColumn] = selection; if (location == InventoryLocation.Ground) ground.Add(selection); selection = null; BattlescapeSoundEffect.ItemDrop.Play(); } }
private static int? GetDropLocation(BattleItem[] items, int column, BattleItem item) { if (item.Height > 1) return null; var width = item.Width; if (items[column] != null) return null; if (width == 1) return column; if (column > 0 && items[column - 1] == null) return column - 1; if (column + 1 < items.Length && items[column + 1] == null) return column; return null; }
private static BattleItem SelectItem(BattleItem[,] items, int row, int column) { var target = GetClickTargets(items)[row, column]; if (target == null) return null; var item = items[target.Value.Y, target.Value.X]; items[target.Value.Y, target.Value.X] = null; return item; }
private static Point?[,] GetClickTargets(BattleItem[,] items) { var rows = items.GetLength(0); var columns = items.GetLength(1); var targets = new Point?[rows, columns]; foreach (var row in Enumerable.Range(0, rows)) { foreach (var column in Enumerable.Range(0, columns)) { var item = items[row, column]; if (item == null) continue; foreach (var rowOffset in Enumerable.Range(0, item.Height)) foreach (var columnOffset in Enumerable.Range(0, item.Width)) targets[row + rowOffset, column + columnOffset] = new Point { X = column, Y = row }; } } return targets; }
private static Point? GetDropLocation(BattleItem[,] items, int row, int column, BattleItem item) { var targets = GetClickTargets(items); var itemWidth = item.Width; var itemHeight = item.Height; var width = items.GetLength(1); var height = items.GetLength(0); foreach (var rowOffset in Enumerable.Range(0, itemHeight)) foreach (var columnOffset in Enumerable.Range(0, itemWidth)) { var dropRow = row - rowOffset; var dropColumn = column - columnOffset; if (CanDropHere(targets, dropRow, dropColumn, itemWidth, itemHeight, width, height)) return new Point { X = dropColumn, Y = dropRow }; } return null; }
private static void FillGroundView(BattleItem[,] groundView, List<BattleItem> items, int row, int column, int maxWidth, int maxHeight) { if (maxWidth == 0 || maxHeight == 0 || !items.Any()) return; for (var width = maxWidth; width > 0; --width) { for (var height = maxHeight; height > 0; --height) { var matchingItem = items.FirstOrDefault(item => item.Width == width && item.Height == height); if (matchingItem == null) continue; groundView[row, column] = matchingItem; items.Remove(matchingItem); FillGroundView(groundView, items, row + height, column, width, maxHeight - height); FillGroundView(groundView, items, row, column + width, maxWidth - width, maxHeight); return; } } }
private static void DrawCenteredBattleItem(GraphicsBuffer buffer, BattleItem item, int topRow, int leftColumn) { if (item == null) return; var top = topRow + (3 - item.Height) * 16 / 2; var left = leftColumn + (2 - item.Width) * 16 / 2; buffer.DrawItem(top, left, item.Image); }
private static void DrawBattleItems(GraphicsBuffer buffer, BattleItem[] items, int topRow, int leftColumn) { foreach (var column in Enumerable.Range(0, items.Length)) { var item = items[column]; if (item != null) buffer.DrawItem(topRow, leftColumn + column * 16, item.Image); } }
private static BattleItem[,] CreateGroundView(List<BattleItem> items) { var groundView = new BattleItem[3, 20]; FillGroundView(groundView, items, 0, 0, 20, 3); return groundView; }
public bool CanLoadWith(BattleItem item) { return Ammunition == null && (item.Item as AmmunitionType?)?.Metadata().Weapon == (WeaponType?)Item; }