Class: PFM::MiningGame::GridHandler

Inherits:
Object
  • Object
show all
Defined in:
scripts/01450 Systems/08001 Mining Game/00020 PFM_Mining_Game/00002 MiningGame_GridHandler.rb

Overview

Class handling the grid for the mining game

Constant Summary collapse

MIN_ITEM_COUNT =

Constant telling the minimum amount of item possible on a grid

2
MAX_ITEM_COUNT =

Constant telling the maximum amount of item possible on a grid

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wanted_items, item_count, grid_width, grid_height) ⇒ GridHandler

Create a new grid handler

Parameters:

  • wanted_items (Array<Symbol>, nil)

    list of wanted items

  • item_count (Integer, nil)

    maximum number of items

  • grid_width (Integer)

    width of the grid

  • grid_height (Integer)

    height of the grid



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'scripts/01450 Systems/08001 Mining Game/00020 PFM_Mining_Game/00002 MiningGame_GridHandler.rb', line 29

def initialize(wanted_items, item_count, grid_width, grid_height)
  @item_count = (item_count || wanted_items&.size || rand(MIN_ITEM_COUNT..MAX_ITEM_COUNT)).clamp(MIN_ITEM_COUNT, MAX_ITEM_COUNT)
  @wanted_items = (map_symbol2hash(wanted_items) || randomize_items(@item_count)).take(@item_count)
  @width = grid_width
  @height = grid_height
  @nb_irons = rand(4..7)
  # @type [Array<Diggable>]
  @arr_irons = []
  @ready = false
  # We create all the grids
  @arr_tiles_state = Array.new(@height) { Array.new(@width, 0) }
  @mgt = Random::MINING_GAME_TILES
  initialize_grid_content
end

Instance Attribute Details

#arr_ironsArray<Diggable> (readonly)

Get the list of irons

Returns:



23
24
25
# File 'scripts/01450 Systems/08001 Mining Game/00020 PFM_Mining_Game/00002 MiningGame_GridHandler.rb', line 23

def arr_irons
  @arr_irons
end

#arr_itemsArray<Diggable> (readonly)

Get the list of diggable items

Returns:



20
21
22
# File 'scripts/01450 Systems/08001 Mining Game/00020 PFM_Mining_Game/00002 MiningGame_GridHandler.rb', line 20

def arr_items
  @arr_items
end

#arr_tiles_stateArray<Array<Integer>> (readonly)

Get the tile states

Returns:



17
18
19
# File 'scripts/01450 Systems/08001 Mining Game/00020 PFM_Mining_Game/00002 MiningGame_GridHandler.rb', line 17

def arr_tiles_state
  @arr_tiles_state
end

#heightInteger (readonly)

Get the grid height

Returns:



14
15
16
# File 'scripts/01450 Systems/08001 Mining Game/00020 PFM_Mining_Game/00002 MiningGame_GridHandler.rb', line 14

def height
  @height
end

#widthInteger (readonly)

Get the grid width

Returns:



11
12
13
# File 'scripts/01450 Systems/08001 Mining Game/00020 PFM_Mining_Game/00002 MiningGame_GridHandler.rb', line 11

def width
  @width
end

Instance Method Details

#check_presence_of_diggable(x, y) ⇒ Diggable, Boolean

Check if a diggable is present at the said coordinate and return it if it's an item

Parameters:

  • x (Integer)

    x coordinate where we went to find the diggable

  • y (Integer)

    y coordinate where we went to find the diggable

Returns:

  • (Diggable, Boolean)

    Diggable instance if item, true if iron, false if nothing



54
55
56
57
58
59
60
61
62
63
64
65
# File 'scripts/01450 Systems/08001 Mining Game/00020 PFM_Mining_Game/00002 MiningGame_GridHandler.rb', line 54

def check_presence_of_diggable(x, y)
  diggable = @arr_items.find { |item| (item.x...item.x + item.width).cover?(x) && (item.y...item.y + item.height).cover?(y) }
  if diggable
    return diggable if diggable.pattern[y - diggable.y][x - diggable.x]
  end
  diggable = @arr_irons.find { |item| (item.x...item.x + item.width).cover?(x) && (item.y...item.y + item.height).cover?(y) }
  if diggable
    return true if diggable.pattern[y - diggable.y][x - diggable.x]
  end

  return false
end

#ready?Boolean

Tell if the mining game grid is ready

Returns:

  • (Boolean)


46
47
48
# File 'scripts/01450 Systems/08001 Mining Game/00020 PFM_Mining_Game/00002 MiningGame_GridHandler.rb', line 46

def ready?
  return @ready
end