Class: Gridmap

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, map_path, symbol_details, images, tile_size) ⇒ Gridmap

Returns a new instance of Gridmap.



5
6
7
8
9
10
11
12
13
14
# File 'lib/gridmap.rb', line 5

def initialize(window, map_path, symbol_details, images, tile_size)
 		@window 			= window
 		@map_path 			= map_path
 		@symbol_details		= symbol_details
 		@images 			= images
 		@tile_size 			= tile_size

 		make_map
 		define_tiles
end

Instance Attribute Details

#connected_tilesObject (readonly)

Returns the value of attribute connected_tiles.



4
5
6
# File 'lib/gridmap.rb', line 4

def connected_tiles
  @connected_tiles
end

#grid_heightObject (readonly)

Returns the value of attribute grid_height.



4
5
6
# File 'lib/gridmap.rb', line 4

def grid_height
  @grid_height
end

#grid_widthObject (readonly)

Returns the value of attribute grid_width.



4
5
6
# File 'lib/gridmap.rb', line 4

def grid_width
  @grid_width
end

#orientated_tilesObject (readonly)

Returns the value of attribute orientated_tiles.



4
5
6
# File 'lib/gridmap.rb', line 4

def orientated_tiles
  @orientated_tiles
end

#pixel_heightObject (readonly)

Returns the value of attribute pixel_height.



4
5
6
# File 'lib/gridmap.rb', line 4

def pixel_height
  @pixel_height
end

#pixel_widthObject (readonly)

Returns the value of attribute pixel_width.



4
5
6
# File 'lib/gridmap.rb', line 4

def pixel_width
  @pixel_width
end

#tile_sizeObject (readonly)

Returns the value of attribute tile_size.



4
5
6
# File 'lib/gridmap.rb', line 4

def tile_size
  @tile_size
end

Instance Method Details

#angle_orientated_tilesObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gridmap.rb', line 124

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

#change_level(new_map, *p) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gridmap.rb', line 152

def change_level(new_map, *p)
	puts "changing level"
	#Clears the current grid
	self.count.times { self.pop }

	@map_path = new_map
	#Replaces instance variables with param variables if they exist.
	@symbol_details = p[0] if p[0]
	@images 		= p[1] if p[1]
	@tile_size		= p[2] if p[2]

	make_map
 		define_tiles
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.



46
47
48
49
50
51
52
53
54
# File 'lib/gridmap.rb', line 46

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_tilesObject



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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gridmap.rb', line 43

def define_tiles
	#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 = :buffer
			for n in 0..3
				tile.angle = 180 + 90 * n if tile.connections[n] == true
			end
		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

		if @images[img_key] == nil
			raise StandardError, "Oops, an image was not found for the key: #{img_key}."
		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



144
145
146
# File 'lib/gridmap.rb', line 144

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

#find_tile(x, y) ⇒ Object



148
149
150
# File 'lib/gridmap.rb', line 148

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

#make_mapObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gridmap.rb', line 16

def make_map
	@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

    #Checks to make sure all lines are the same length.
    line_count = self.first.count
    self.each {|line| if line.count != line_count
    						raise StandardError, "The lines in your map file are not all the same length." 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