Skip to content

Instantly share code, notes, and snippets.

@TorD

TorD/gist:9679a32cac2ef28399d6 Secret

Created May 15, 2015
Embed
What would you like to do?
The Vendor InventoryGrid logic Model
module TDD
module Models
module InventoryGrid
module_function
#--------------------------------------------------------------------------
# Grid Layouts
#--------------------------------------------------------------------------
GRIDS = {
:backpack => [
[0,1,1,1,0],
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[0,1,1,1,0]
],
:mule => [
[1,1,1,1,0,0,0,0,1,1,1,0],
[1,1,1,1,0,0,0,0,1,1,1,0],
[1,1,1,1,0,0,0,0,1,1,1,0],
[1,1,1,1,0,0,0,0,1,1,1,1],
[1,1,1,1,0,0,0,0,1,1,1,1],
],
:test => [
[1,1,1,0,0,1,1,0,0,1,1,1],
[1,1,1,0,1,1,1,1,0,1,1,1],
[0,1,1,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,1,1,1,0],
[1,1,1,0,1,1,1,1,0,1,1,1],
[1,1,1,0,0,1,1,0,0,1,1,1],
],
}
#--------------------------------------------------------------------------
# * Get Current Grid Matrix
#--------------------------------------------------------------------------
def grid_matrix
GRIDS[:mule]
end
#--------------------------------------------------------------------------
# * Get the value of the grid matrix at given point
#--------------------------------------------------------------------------
def grid_matrix_value(col, row)
return nil if row < 0 || col < 0
return nil unless grid_matrix[row]
grid_matrix[row][col]
end
#--------------------------------------------------------------------------
# * Check if Grid Matrix Slot is Taken
#--------------------------------------------------------------------------
def grid_matrix_slot_taken?(col, row, ignore_item=nil)
if ignore_item != true
return true if get_item_at_slot(col, row, ignore_item)
end
grid_matrix_value(col, row) != 1
end
#--------------------------------------------------------------------------
# * Check if grid matrix slot is free
#--------------------------------------------------------------------------
def grid_matrix_slot_free?(col, row, ignore_item=nil)
!grid_matrix_slot_taken?(col, row, ignore_item)
end
#--------------------------------------------------------------------------
# * Get items from an area by using a start position and item's size
#--------------------------------------------------------------------------
def get_items_from_area_with_item_comparison(col, row, item)
item.grid_size.map{|x,y| get_item_at_slot(col + x, row + y, item)}.compact.uniq
end
#--------------------------------------------------------------------------
# * Get any item residing in a given slot. Supports ignoring an item
#--------------------------------------------------------------------------
def get_item_at_slot(col, row, ignore_item=nil)
inventory_matrix(ignore_item).select{|item| item.grid_occupation.include?([col, row])}.first
end
#--------------------------------------------------------------------------
# * Check if can place item at?
#--------------------------------------------------------------------------
def can_place_item_at?(item, col, row, ignore_item=nil)
ignore_item ||= item # Don't include self if no other given
item.grid_size.select{|grid| grid_matrix_slot_free?(grid.first + col, grid.last + row, ignore_item)}.length == item.size
end
#--------------------------------------------------------------------------
# * Perform swap of items at slot with item
# - Returns: True if possible, false if not
#--------------------------------------------------------------------------
def attempt_to_place_item_at(item, col, row)
return false unless can_place_item_at?(item, col, row, true)
item.grid_placement = [col, row]
items_at = get_items_from_area_with_item_comparison(col, row, item)
return false unless items_at.select{|i| get_available_grid_placement_for(i)}
.size == items_at.size
items_at.each{|i| i.grid_placement = get_available_grid_placement_for(i)}
end
#--------------------------------------------------------------------------
# * Get Current Inventory Matrix
#--------------------------------------------------------------------------
def inventory_matrix(ignore_item=nil)
inventory.reject{|i| i == ignore_item}
end
#--------------------------------------------------------------------------
# Get available grid placement for an item
#--------------------------------------------------------------------------
def get_available_grid_placement_for(item)
result = nil
grid_matrix.each_with_index do |row_data, row|
row_data.each_with_index do |col_data, col|
next unless col_data == 1
if can_place_item_at?(item, col, row)
result = [col, row]
break
end
end
break if result
end
return result
end
#--------------------------------------------------------------------------
# Get player inventory. A list of Item Wrappers
#--------------------------------------------------------------------------
def inventory
InventoryManager.inventory_for(:player)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.