Class: Tilemap

Inherits:
Object
  • Object
show all
Defined in:
lib/rgss3/tilemap.rb

Constant Summary collapse

TILESIZE =
32
A1 =
[
  [13,14,17,18], [2,14,17,18],  [13,3,17,18],  [2,3,17,18],
  [13,14,17,7],  [2,14,17,7],   [13,3,17,7],   [2,3,17,7],
  [13,14,6,18],  [2,14,6,18],   [13,3,6,18],   [2,3,6,18],
  [13,14,6,7],   [2,14,6,7],    [13,3,6,7],    [2,3,6,7],
  [12,14,16,18], [12,3,16,18],  [12,14,16,7],  [12,3,16,7],
  [9,10,17,18],  [9,10,17,7],   [9,10,6,18],   [9,10,6,7],
  [13,15,17,19], [13,15,6,19],  [2,15,17,19],  [2,15,6,19],
  [13,14,21,22], [2,14,21,22],  [13,3,21,22],  [2,3,21,22],
  [12,15,16,19], [9,10,21,22],  [8,9,12,13],   [8,9,12,7],
  [10,11,14,15], [10,11,6,15],  [18,19,22,23], [2,19,22,23],
  [16,17,20,21], [16,3,20,21],  [8,11,12,15],  [8,9,20,21],
  [16,19,20,23], [10,11,22,23], [8,11,20,23],  [0,1,4,5]
]
A1POS =
[
[0,0],[0,TILESIZE*3],[TILESIZE*6,0],[TILESIZE*6,TILESIZE*3],
[TILESIZE*8,0],[TILESIZE*14,0],[TILESIZE*8,TILESIZE*3],[TILESIZE*14,TILESIZE*3],
[0,TILESIZE*6],[TILESIZE*6,TILESIZE*6],[0,TILESIZE*9],[TILESIZE*6,TILESIZE*9],
[TILESIZE*8,TILESIZE*6],[TILESIZE*14,TILESIZE*6],[TILESIZE*8,TILESIZE*9],[TILESIZE*14,TILESIZE*9]
]
A1E =
[
[0,1,6,7],[0,1,4,5],[2,3,6,7],[1,2,5,6]
]
A3 =
[
  [5,6,9,10],    [4,5,8,9],    [1,2,5,6],   [0,1,4,5],
  [6,7,10,11],   [4,7,8,11],   [2,3,6,7],   [0,3,4,7],
  [9,10,13,14],  [8,9,12,13],  [1,2,13,14], [0,1,12,13],
  [10,11,14,15], [8,11,12,13], [2,3,14,15], [0,3,12,15]
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(viewport = nil) ⇒ Tilemap

Returns a new instance of Tilemap.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rgss3/tilemap.rb', line 15

def initialize(viewport = nil)
  @bitmaps = []
  @viewport = viewport
  @visible = true
  @ox = 0
  @oy = 0
  @animated_layer = []
  @layers = [Plane.new, Plane.new, Plane.new]
  @anim_count = 0
  @disposed = false
  @layers[0].z = 0
  @layers[0].viewport = @viewport
  @layers[1].z = 100
  @layers[1].viewport = @viewport
  @layers[2].z = 200
  @layers[2].viewport = @viewport
end

Instance Attribute Details

#bitmapsObject

Returns the value of attribute bitmaps.



6
7
8
# File 'lib/rgss3/tilemap.rb', line 6

def bitmaps
  @bitmaps
end

#flagsObject

Returns the value of attribute flags.



9
10
11
# File 'lib/rgss3/tilemap.rb', line 9

def flags
  @flags
end

#flash_dataObject

Returns the value of attribute flash_data.



8
9
10
# File 'lib/rgss3/tilemap.rb', line 8

def flash_data
  @flash_data
end

#map_dataObject

Returns the value of attribute map_data.



7
8
9
# File 'lib/rgss3/tilemap.rb', line 7

def map_data
  @map_data
end

#oxObject

Returns the value of attribute ox.



12
13
14
# File 'lib/rgss3/tilemap.rb', line 12

def ox
  @ox
end

#oyObject

Returns the value of attribute oy.



13
14
15
# File 'lib/rgss3/tilemap.rb', line 13

def oy
  @oy
end

#viewportObject

Returns the value of attribute viewport.



10
11
12
# File 'lib/rgss3/tilemap.rb', line 10

def viewport
  @viewport
end

#visibleObject

Returns the value of attribute visible.



11
12
13
# File 'lib/rgss3/tilemap.rb', line 11

def visible
  @visible
end

Instance Method Details

#bitmap_for_autotile(autotile) ⇒ Object



87
88
89
90
91
92
# File 'lib/rgss3/tilemap.rb', line 87

def bitmap_for_autotile(autotile)
  return 0 if autotile.between?(0,15)
  return 1 if autotile.between?(16,47)
  return 2 if autotile.between?(48,79)
  return 3 if autotile.between?(80,127)
end

#disposeObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/rgss3/tilemap.rb', line 33

def dispose
  for layer in @layers
    layer.bitmap.dispose
    layer.dispose
  end
  for layer in @animated_layer
    layer.dispose
  end
  @disposed = true
end

#disposed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rgss3/tilemap.rb', line 44

def disposed?
  @disposed
end

#draw_A1tile(x, y, id, animated = false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rgss3/tilemap.rb', line 116

def draw_A1tile(x,y,id,animated = false)
  autotile = (id - 2048) / 48
  return draw_waterfalltile(x,y,id) if [5,7,9,11,13,15].include?(autotile)
  index = (id - 2048) % 48
  case bitmap_for_autotile(autotile)
  when 0
    x2 = A1POS[autotile][0]
    y2 = A1POS[autotile][1]
  when 1
    x2 = (TILESIZE * 2) * ((autotile - 16) % 8)
    y2 = (TILESIZE * 3) * ((autotile - 16) / 8)
  when 2
    x2 = (TILESIZE * 2) * ((autotile - 48) % 8)
    y2 = (TILESIZE * 2) * ((autotile - 48) / 8)
  when 3
    x2 = (TILESIZE * 2) * ((autotile - 80) % 8)
    y2 = (TILESIZE * 3) * ((((autotile - 80) / 8)+1)/2) + (TILESIZE * 2) * (((autotile - 80) / 8)/2)
  end
  rect = Rect.new(0,0,TILESIZE/2,TILESIZE/2)
  for layer in @animated_layer
    for i in 0..3
      rect.x = x2 + (TILESIZE/2) * (A1[index][i] % 4)
      rect.y = y2 + (TILESIZE/2) * (A1[index][i] / 4)
      case i
      when 0
        layer.blt(x * TILESIZE, y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
      when 1
        layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
      when 2
        layer.blt(x * TILESIZE, y * TILESIZE + (TILESIZE/2),@bitmaps[bitmap_for_autotile(autotile)],rect)
      when 3
        layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE + (TILESIZE/2),@bitmaps[bitmap_for_autotile(autotile)],rect)
      end
    end
    x2 += TILESIZE * 2 if animated && ![2,3].include?(autotile)
  end
end

#draw_A2tile(x, y, id) ⇒ Object



183
184
185
# File 'lib/rgss3/tilemap.rb', line 183

def draw_A2tile(x,y,id)
  draw_A1tile(x,y,id)
end

#draw_A3tile(x, y, id) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rgss3/tilemap.rb', line 194

def draw_A3tile(x,y,id)
  autotile = (id - 2048) / 48
  index = (id - 2048) % 48
  case bitmap_for_autotile(autotile)
  when 0
    x2 = (TILESIZE * 2) * ((autotile) % 8)
    y2 = (TILESIZE * 3) * ((autotile) / 8)
  when 1
    x2 = (TILESIZE * 2) * ((autotile - 16) % 8)
    y2 = (TILESIZE * 3) * ((autotile - 16) / 8)
  when 2
    x2 = (TILESIZE * 2) * ((autotile - 48) % 8)
    y2 = (TILESIZE * 2) * ((autotile - 48) / 8)
  when 3
    x2 = (TILESIZE * 2) * ((autotile - 80) % 8)
    y2 = (TILESIZE * 3) * ((((autotile - 80) / 8)+1)/2) + (TILESIZE * 2) * (((autotile - 80) / 8)/2)
  end
  rect = Rect.new(0,0,TILESIZE/2,TILESIZE/2)
  for layer in @animated_layer
    for i in 0..3
      if A3[index].nil?
        rect.x = x2 + (TILESIZE/2) * (A1[index][i] % 4)
        rect.y = y2 + (TILESIZE/2) * (A1[index][i] / 4)
      else
        rect.x = x2 + (TILESIZE/2) * (A3[index][i] % 4)
        rect.y = y2 + (TILESIZE/2) * (A3[index][i] / 4)
      end
      case i
      when 0
        layer.blt(x * TILESIZE, y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
      when 1
        layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
      when 2
        layer.blt(x * TILESIZE, y * TILESIZE + (TILESIZE/2),@bitmaps[bitmap_for_autotile(autotile)],rect)
      when 3
        layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE + (TILESIZE/2),@bitmaps[bitmap_for_autotile(autotile)],rect)
      end
    end
  end
end

#draw_A4tile(x, y, id) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rgss3/tilemap.rb', line 235

def draw_A4tile(x,y,id)
  autotile = (id - 2048) / 48
  case autotile
  when 80..87
    draw_A1tile(x,y,id)
  when 96..103
    draw_A1tile(x,y,id)
  when 112..119
    draw_A1tile(x,y,id)
  else
    draw_A3tile(x,y,id)
  end
end

#draw_A5tile(x, y, id) ⇒ Object



249
250
251
252
253
254
255
# File 'lib/rgss3/tilemap.rb', line 249

def draw_A5tile(x,y,id)
  id -= 1536
  rect = Rect.new(TILESIZE * (id % 8),TILESIZE * ((id % 128) / 8),TILESIZE,TILESIZE)
  for layer in @animated_layer
    layer.blt(x * TILESIZE, y * TILESIZE,@bitmaps[4],rect)
  end
end

#draw_animated_layerObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rgss3/tilemap.rb', line 62

def draw_animated_layer
  bitmap = Bitmap.new(@map_data.xsize * TILESIZE, @map_data.ysize * TILESIZE)
  if need_animated_layer?
    @animated_layer = [bitmap, bitmap.clone, bitmap.clone]
  else
    @animated_layer = [bitmap]
  end
  @layers[0].bitmap = @animated_layer[0]
  for x in 0..@map_data.xsize - 1
    for y in 0..@map_data.ysize - 1
      draw_A1tile(x,y,@map_data[x,y,0],true) if @map_data[x,y,0].between?(2048,2815)
      draw_A2tile(x,y,@map_data[x,y,0]) if @map_data[x,y,0].between?(2816,4351)
      draw_A3tile(x,y,@map_data[x,y,0]) if @map_data[x,y,0].between?(4352,5887)
      draw_A4tile(x,y,@map_data[x,y,0]) if @map_data[x,y,0].between?(5888,8191)
      draw_A5tile(x,y,@map_data[x,y,0]) if @map_data[x,y,0].between?(1536,1663)
    end
  end
  for x in 0..@map_data.xsize - 1
    for y in 0..@map_data.ysize - 1
      draw_A1tile(x,y,@map_data[x,y,1],true) if @map_data[x,y,1].between?(2048,2815)
      draw_A2tile(x,y,@map_data[x,y,1]) if @map_data[x,y,1].between?(2816,4351)
    end
  end
end

#draw_upper_layersObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/rgss3/tilemap.rb', line 268

def draw_upper_layers
  bitmap = Bitmap.new(@map_data.xsize * TILESIZE, @map_data.ysize * TILESIZE)
  @layers[1].bitmap = bitmap
  @layers[2].bitmap = bitmap.clone
  rect = Rect.new(0,0,TILESIZE,TILESIZE)
  for x in 0..@map_data.xsize - 1
    for y in 0..@map_data.ysize - 1
      n = @map_data[x,y,2] % 256
      rect.x = TILESIZE * ((n % 8) + (8 * (n / 128)))
      rect.y = TILESIZE * ((n % 128) / 8)
      if @flags[@map_data[x,y,2]] & 0x10 == 0
        @layers[1].bitmap.blt(x * TILESIZE, y * TILESIZE,@bitmaps[5+@map_data[x,y,2]/256],rect)
      else
        @layers[2].bitmap.blt(x * TILESIZE, y * TILESIZE,@bitmaps[5+@map_data[x,y,2]/256],rect)
      end
    end
  end
end

#draw_waterfalltile(x, y, id) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rgss3/tilemap.rb', line 158

def draw_waterfalltile(x,y,id)
  autotile = (id - 2048) / 48
  index = (id - 2048) % 48
  x2 = A1POS[autotile][0]
  y2 = A1POS[autotile][1]
  rect = Rect.new(0,0,TILESIZE/2,TILESIZE/2)
  for layer in @animated_layer
    for i in 0..3
      rect.x = x2 + (TILESIZE/2) * (A1E[index][i] % 4)
      rect.y = y2 + (TILESIZE/2) * (A1E[index][i] / 4)
      case i
      when 0
        layer.blt(x * TILESIZE, y * TILESIZE,@bitmaps[bitmap_for_autotile(autotile)],rect)
      when 1
        layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE,@bitmaps[0],rect)
      when 2
        layer.blt(x * TILESIZE, y * TILESIZE + (TILESIZE/2),@bitmaps[0],rect)
      when 3
        layer.blt(x * TILESIZE + (TILESIZE/2), y * TILESIZE + (TILESIZE/2),@bitmaps[0],rect)
      end
    end
    y2 += TILESIZE
  end
end

#need_animated_layer?Boolean

Returns:

  • (Boolean)


257
258
259
260
261
262
263
264
265
266
# File 'lib/rgss3/tilemap.rb', line 257

def need_animated_layer?
  for x in 0..@map_data.xsize - 1
    for y in 0..@map_data.ysize - 1
      if @map_data[x,y,0].between?(2048, 2815)
        return true
      end
    end
  end
  return false
end

#refreshObject



53
54
55
56
57
58
59
60
# File 'lib/rgss3/tilemap.rb', line 53

def refresh
  return if @map_data.nil? || @flags.nil?
  for layer in @layers
    layer.bitmap.dispose if layer.bitmap
  end
  draw_animated_layer
  draw_upper_layers
end

#updateObject



48
49
50
51
# File 'lib/rgss3/tilemap.rb', line 48

def update
  @anim_count = (@anim_count + 1) % (@animated_layer.size * 30)
  @layers[0].bitmap = @animated_layer[@anim_count/30]
end