Class: Gridmap

Inherits:
Array
  • Object
show all
Defined in:
lib/gridmap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map_path, symbol_details, tile_size) ⇒ Gridmap

Returns a new instance of Gridmap.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gridmap.rb', line 3

def initialize(map_path, symbol_details, tile_size)
    @tile_size = tile_size
    @connected_tiles = []
    @orientated_tiles = []

    IO.readlines(map_path).each_with_index do |line, y|
        gridline = []
        line.chomp.split("").each_with_index do |symbol, x|
          t = Tile.new(x, y, tile_size, symbol_details[symbol])
          gridline      << t
          @connected_tiles  << t if t.details[:connected]
          @orientated_tiles   << t if t.details[:orientated]
      end
        self << gridline
    end

    #Since all lines should contain the same number of characters, the width is set by counting the number of characters in the first line.

  @grid_width  = self.first.count - 1
  @grid_height = self.count - 1

  @pixel_width = (@grid_width + 1) * tile_size
  @pixel_height = (@grid_height + 1) * tile_size
end

Instance Attribute Details

#connected_tilesObject (readonly)

Returns the value of attribute connected_tiles.



2
3
4
# File 'lib/gridmap.rb', line 2

def connected_tiles
  @connected_tiles
end

#grid_heightObject (readonly)

Returns the value of attribute grid_height.



2
3
4
# File 'lib/gridmap.rb', line 2

def grid_height
  @grid_height
end

#grid_widthObject (readonly)

Returns the value of attribute grid_width.



2
3
4
# File 'lib/gridmap.rb', line 2

def grid_width
  @grid_width
end

#orientated_tilesObject (readonly)

Returns the value of attribute orientated_tiles.



2
3
4
# File 'lib/gridmap.rb', line 2

def orientated_tiles
  @orientated_tiles
end

#pixel_heightObject (readonly)

Returns the value of attribute pixel_height.



2
3
4
# File 'lib/gridmap.rb', line 2

def pixel_height
  @pixel_height
end

#pixel_widthObject (readonly)

Returns the value of attribute pixel_width.



2
3
4
# File 'lib/gridmap.rb', line 2

def pixel_width
  @pixel_width
end

#tile_sizeObject (readonly)

Returns the value of attribute tile_size.



2
3
4
# File 'lib/gridmap.rb', line 2

def tile_size
  @tile_size
end

Instance Method Details

#angle_orientated_tilesObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gridmap.rb', line 103

def angle_orientated_tiles
  #Sets angle for orientated tiles based on nearest connected tile in a clockwise direction.

  #CHANGE THIS METHOD AS YOU WISH.

  @orientated_tiles.each do |t1|
      @connected_tiles.each do |t2|
       if t2.y == t1.y - 1
         #dont change from default angle

       elsif t2.x == t1.x + 1
         t1.angle = 90
       elsif t2.y == t1.y + 1
         t1.angle = 180
       elsif t2.x == t1.x - 1
         t1.angle = 270
       else
         #dont change from default angle

       end
      end
  end
end

#check(x, y) ⇒ Object

The check method checks a tile with the given grid co-ordinates to see if it is connected or not. The logic here prevents a tile from being checked if it lies outside the grid.



30
31
32
33
34
35
36
37
38
# File 'lib/gridmap.rb', line 30

def check(x, y)
  if y < 0 || x < 0 || y > @grid_height || x > @grid_width
    false
  elsif self[y][x].details[:connected] || self[y][x].details[:orientated]
    true
  else
    false
  end
end

#define_tiles(window, images) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gridmap.rb', line 27

def define_tiles(window, images)
  #The check method checks a tile with the given grid co-ordinates to see if it is connected or not.

  #The logic here prevents a tile from being checked if it lies outside the grid.

  def check(x, y)
    if y < 0 || x < 0 || y > @grid_height || x > @grid_width
      false
    elsif self[y][x].details[:connected] || self[y][x].details[:orientated]
      true
    else
      false
    end
  end 

  #Works out connections for connected or orientated tiles.

  (@connected_tiles + @orientated_tiles).each do |tile|
        connections    = [false, false, false, false] # up right down left

        connections[0] = check(tile.x, tile.y - 1) #up

    connections[1] = check(tile.x + 1, tile.y) #right

    connections[2] = check(tile.x, tile.y + 1) #down

    connections[3] = check(tile.x - 1, tile.y) #left

    tile.connections = connections
  end

  #Sets image and angle for connected tiles based on their connections.

  @connected_tiles.each do |tile|
    case tile.connections.count(true)
    when 0
      img_key = :block
    when 1
      img_key = :straight
      tile.angle = 90 if tile.connections[1] || tile.connections[3]
    when 2
      if tile.connections == [true, false, true, false]
        img_key = :straight
      elsif tile.connections == [false, true, false, true]
        img_key = :straight
        tile.angle = 90
      else
        img_key = :corner
        case tile.connections
        when [false, true, true, false]
          #angle does not need to change

        when [false, false, true, true]
          tile.angle = 90
        when [true, false, false, true]
          tile.angle = 180
        when [true, true, false, false]
          tile.angle = 270
        end
      end
    when 3
      img_key = :tri
      for n in 0..3
        tile.angle = 90 * n if tile.connections[n] == false
      end
    when 4
      img_key = :quad
    end

    tile.image = Gosu::Image.new(window, images[img_key], true)
  end

  angle_orientated_tiles

  #Sets image for all non-connected tiles.

  self.each do |line|
    line.each do |tile|
      unless tile.details[:connected]
        #img_key is inferred from the tile's type!

        img_key = tile.details[:type].to_sym
        tile.image = Gosu::Image.new(window, images[img_key], true)
      end
    end
  end
end

#draw_tilesObject



123
124
125
# File 'lib/gridmap.rb', line 123

def draw_tiles
  self.each {|line| line.each {|tile| tile.draw}}
end

#find_tile(x, y) ⇒ Object



127
128
129
# File 'lib/gridmap.rb', line 127

def find_tile(x, y)
  self[(y / @tile_size).floor][(x / @tile_size).floor]
end