Class: Tilemap

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

Overview

** Tilemap


This class draws the map according to the data given.

Version 1.05 Author Ramiro


Changelog: 11/28/11 (1.00): First Version. 11/29/11 (1.03): Waterfall Tiles, A3, A4 Fixed. 11/29/11 (1.05): Added Shadows, default configurations. www.rpgmakervx.net/index.php?showtopic=51646

Constant Summary collapse

DEFAULT_FRAME_INTERVAL =

  • Default Configurations


20
DEFAULT_FLASH_INTERVAL =

Sets the interval of the animated tiles

15
DEFAULT_SHADOW_VISIBLE =

Sets the interval of flash data

true
AUTOTILE_PARTS =

  • Constants


The parts of the autotiles

[[18,17,14,13], [ 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],
[16,17,12,13], [16, 3,12,13], [16,17,12, 7], [12, 3,16, 7], 
[10, 9,14,13], [10, 9,14, 7], [10, 9, 6,13], [10, 9, 6, 7],
[18,19,14,15], [18,19, 6,15], [ 2,19,14,15], [ 2,19, 6,15],
[18,17,22,21], [ 2,17,22,21], [18, 3,22,21], [ 2, 3,21,22],
[16,19,12,15], [10, 9,22,21], [ 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]]
WATERFALL_PIECES =
[[ 2, 1, 5, 6], [ 0, 1, 4, 5], [ 2, 3, 6, 7]]
WALL_PIECES =
[[10, 9, 6, 5], [ 8, 9, 4, 5], [ 2, 1, 6, 5], [ 0, 1, 4, 5],
[10,11, 6, 7], [ 8,11, 4, 7], [ 2, 3, 6, 7], [ 0, 3, 4, 7],
[10, 9,14,13], [ 8, 9,12,13], [ 2, 1,14,13], [ 0, 1,12,13],
[10,11,14,15], [10,11, 6, 7], [ 2, 3,14,15], [ 0, 3,12,15]]
PASSABLE_TILE =

Passabilityes of the passages data

0x00
IMPASSABLE_TILE =
0x01
OVER_TILE =
0x10
@@autotile_list =

  • Class Variables


[{}, {}, {}]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(viewport = nil) ⇒ Tilemap


  • Object Initialization

    viewport : the viewport of the tilemap
    



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
# File 'lib/openrgss/tilemap.rb', line 75

def initialize(viewport=nil)
  @bitmaps = []
  @layers = [Plane.new(viewport), Plane.new(viewport), Plane.new(viewport),
             Plane.new(viewport), Plane.new(viewport), Plane.new(viewport)]
  @viewport = viewport
  @ox = 0
  @oy = 0
  @layers[0].z = 0   # soil

  @layers[1].z = 50  # soil objects

  @layers[2].z = 100 # upper layer

  @layers[3].z = 250 # upper layer 2   

  @layers[4].z = 300 # flash layer

  @layers[5].z = 75  # shadow layer

  @need_refresh = false
  @map_data = nil
  @passages = nil
  @flash_data = nil
  @visible = true
  @disposed = false
  @layer_bitmaps = []
  @frame = 0
  @frame_count = 0
  @frame_interval = DEFAULT_FRAME_INTERVAL
  @flash_count = 0
  @flash_interval = DEFAULT_FLASH_INTERVAL
  @flash_goal = 0
  @shadow_visible = DEFAULT_SHADOW_VISIBLE
  self.visible = true
end

Instance Attribute Details

#bitmapsObject

The bitmap array of the tileset



63
64
65
# File 'lib/openrgss/tilemap.rb', line 63

def bitmaps
  @bitmaps
end

#flagsObject

Returns the value of attribute flags.



70
71
72
# File 'lib/openrgss/tilemap.rb', line 70

def flags
  @flags
end

#flash_intervalObject

The flash interval of flash_data



68
69
70
# File 'lib/openrgss/tilemap.rb', line 68

def flash_interval
  @flash_interval
end

#frame_intervalObject

The frame interval of water animation



67
68
69
# File 'lib/openrgss/tilemap.rb', line 67

def frame_interval
  @frame_interval
end

#map_dataObject

The data of the map



62
63
64
# File 'lib/openrgss/tilemap.rb', line 62

def map_data
  @map_data
end

#oxObject

The ox of the tilemap



64
65
66
# File 'lib/openrgss/tilemap.rb', line 64

def ox
  @ox
end

#oyObject

The oy of the tilemap



65
66
67
# File 'lib/openrgss/tilemap.rb', line 65

def oy
  @oy
end

#passagesObject

The passability of the map



66
67
68
# File 'lib/openrgss/tilemap.rb', line 66

def passages
  @passages
end

#shadow_visibleObject

determines if the auto shadows are visible



69
70
71
# File 'lib/openrgss/tilemap.rb', line 69

def shadow_visible
  @shadow_visible
end

#viewportObject


  • Public Instance Variables




60
61
62
# File 'lib/openrgss/tilemap.rb', line 60

def viewport
  @viewport
end

#visibleObject

The visibility of the map



61
62
63
# File 'lib/openrgss/tilemap.rb', line 61

def visible
  @visible
end

Instance Method Details

#correct_positionObject


  • Correct the position of the map




509
510
511
512
513
514
# File 'lib/openrgss/tilemap.rb', line 509

def correct_position
  @layers.each do |i|
    i.ox = @ox
    i.oy = @oy
  end    
end

#create_shadow_bitmapObject


  • Creates the shadow bitmap




646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/openrgss/tilemap.rb', line 646

def create_shadow_bitmap
  @layers[5].bitmap.dispose if @layers[5].bitmap
  bmp = Bitmap.new(@map_data.xsize * 32, @map_data.ysize * 32)
  @layers[5].bitmap = bmp
  for i in 1...@map_data.xsize
    for j in 0...@map_data.ysize
      if !projects_shadow?(@map_data[i,j,0])
        if projects_shadow?(@map_data[i-1,j,0]) && (j == 0 || projects_shadow?(@map_data[i-1,j-1,0]))
          bmp.fill_rect(i * 32, j * 32, 16, 32, Color.new(0,0,0,128))
        end
      end  
    end
  end
end

#disposeObject


  • Dispose the Tilemap




566
567
568
569
570
571
572
573
574
575
# File 'lib/openrgss/tilemap.rb', line 566

def dispose
  @layers.each do |i|
    i.bitmap.dispose if i.bitmap
    i.dispose
  end  
  @layer_bitmaps.each do |i|
    i.dispose if i
  end    
  @disposed = true
end

#disposed?Boolean


  • Checks if the tilemap is disposed


Returns:

  • (Boolean)


459
460
461
# File 'lib/openrgss/tilemap.rb', line 459

def disposed?
  @disposed
end

#flash_dataObject


  • Gets the flash data




482
483
484
485
# File 'lib/openrgss/tilemap.rb', line 482

def flash_data
  @need_flash_refresh = true
  @flash_data
end

#flash_data=(value) ⇒ Object


  • Sets the flash data




489
490
491
492
# File 'lib/openrgss/tilemap.rb', line 489

def flash_data=(value)
  @flash_data = value
  @need_flash_refresh = true
end

#get_bitmap(id, frame = 0) ⇒ Object


  • Gets the tile by the id




169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/openrgss/tilemap.rb', line 169

def get_bitmap(id, frame=0)
 return @@autotile_list[frame][id] if @@autotile_list[frame][id]
 bmp = Bitmap.new(32, 32)
 if    id < 1024 # B-E

   sub_id = id % 256
   case id / 256
   when 0 # B

     tilemap = @bitmaps[5]
   when 1 # C

     tilemap = @bitmaps[6]
   when 2 # D

     tilemap = @bitmaps[7]
   else   # E

     tilemap = @bitmaps[8]
   end  
   if sub_id < 128
      bmp.blt(0,0, tilemap, Rect.new((sub_id % 8) * 32, (sub_id / 8) * 32, 32, 32))
   else
      sub_id -= 128
      bmp.blt(0,0, tilemap, Rect.new((sub_id % 8) * 32 + 256, (sub_id / 8) * 32, 32, 32))
   end 
 elsif id < 1664 # A5  

   tilemap = @bitmaps[4]
   sub_id = id - 1536
   bmp.blt(0,0, tilemap, Rect.new((sub_id % 8) * 32, (sub_id / 8) * 32, 32, 32))
 elsif id < 2816 # A1

   sub_id = id - 2048
   autotile = sub_id / 48
   auto_id = sub_id % 48
   tilemap = @bitmaps[0]
   case autotile
   when  0 # sea water

     case frame
     when 0
      sx = 0
      sy = 0       
     when 1
      sx = 64
      sy = 0         
     else
      sx = 128
      sy = 0        
     end  
   when  1 # sea deep water

     case frame
     when 0
      sx = 0
      sy = 96   
     when 1
      sx = 64
      sy = 96       
     else
      sx = 128
      sy = 96      
    end        
         
   when  2 # sea rocks

      sx = 192
      sy = 0    
   when  3 # ice rocks

      sx = 192
      sy = 96   
   when  4 # shallow water

     case frame
     when 0
      sx = 256
      sy = 0       
     when 1
      sx = 320
      sy = 0         
     else
      sx = 384
      sy = 0        
     end          
   when  5 # shallow waterfall

     case frame
     when 0
      sx = 448
      sy = 0    
     when 1
      sx = 448
      sy = 32      
     else
      sx = 448
      sy = 64     
     end                     
   when  6 # town water

     case frame
     when 0
      sx = 256
      sy = 96       
     when 1
      sx = 320
      sy = 96         
     else
      sx = 384
      sy = 96       
     end         
   when  7 # town waterfall

     case frame
     when 0
      sx = 448
      sy = 96    
     when 1
      sx = 448
      sy = 128      
     else
      sx = 448
      sy = 160     
     end        
   when  8 # cave water

     case frame
     when 0
      sx = 0
      sy = 192      
     when 1
      sx = 64
      sy = 192        
     else
      sx = 128
      sy = 192        
     end         
   when  9 # cave waterfall

     case frame
     when 0
      sx = 192
      sy = 192      
     when 1
      sx = 192
      sy = 224       
     else
      sx = 192
      sy = 256      
     end         
   when 10 # temple water

     case frame
     when 0
      sx = 0
      sy = 288     
     when 1
      sx = 64
      sy = 288      
     else
      sx = 128
      sy = 288     
     end         
     
   when 11 # temple waterfall

     case frame
     when 0
      sx = 192
      sy = 288   
     when 1
      sx = 192
      sy = 320     
     else
      sx = 192
      sy = 352  
     end        
   when 12 # deep water

     case frame
     when 0
      sx = 256
      sy = 192      
     when 1 
      sx = 320
      sy = 192         
     else
      sx = 384
      sy = 192       
     end         
   when 13 # deep waterfall

     case frame
     when 0
      sx = 448
      sy = 192      
     when 1
      sx = 448
      sy = 224       
     else
      sx = 448
      sy = 256      
     end          
   when 14 # magma water

    case frame
     when 0
      sx = 256
      sy = 288   
     when 1
      sx = 320
      sy = 288       
     else
      sx = 384
      sy = 288      
    end                 
      
   else # magma waterfall

     case frame
     when 0
      sx = 448
      sy = 288     
     when 1
      sx = 448
      sy = 320    
     else
      sx = 448
      sy = 352  
     end          
   end  
   
   if is_wall?(id)
     rects = WATERFALL_PIECES[auto_id]
   else  
    rects = AUTOTILE_PARTS[auto_id]  
   end
   rect = Rect.new(0,0,16,16)
   for i in 0...4
     rect.x = (rects[i] % 4) * 16 + sx
     rect.y = (rects[i] / 4) * 16 + sy
     bmp.blt((i % 2) * 16,(i / 2) * 16,tilemap,rect)
   end      
 elsif id < 4352 # A2

   sub_id = id - 2816
   autotile = sub_id / 48
   auto_id = sub_id % 48
   tilemap = @bitmaps[1]
   sx = (autotile % 8) * 64
   sy = (autotile / 8) * 96
   rects = AUTOTILE_PARTS[auto_id]
   rect = Rect.new(0,0,16,16)
   for i in 0...4
     rect.x = (rects[i] % 4) * 16 + sx
     rect.y = (rects[i] / 4) * 16 + sy
     bmp.blt((i % 2) * 16,(i / 2) * 16,tilemap,rect)
   end
   
 elsif id < 5888 # A3

   sub_id = id - 4352
   autotile = sub_id / 48
   auto_id = sub_id % 48
   tilemap = @bitmaps[2]
   sx = (autotile % 8) * 64
   sy = (autotile / 8) * 64
   rects = WALL_PIECES[auto_id]
   rect = Rect.new(0,0,16,16)
   for i in 0...4
     rect.x = (rects[i] % 4) * 16 + sx
     rect.y = (rects[i] / 4) * 16 + sy
     bmp.blt((i % 2) * 16,(i / 2) * 16,tilemap,rect)
   end     
 else # A4

   sub_id = id - 5888
   autotile = sub_id / 48
   auto_id = sub_id % 48
   tilemap = @bitmaps[3]
   sx = (autotile % 8) * 64
   case autotile / 8
   when 0
     sy = 0
   when 1
     sy = 96
   when 2
     sy = 160
   when 3
     sy = 256
   when 4
     sy = 320
   else  
     sy = 416
   end  
   if is_wall?(id)
     rects = WALL_PIECES[auto_id]
   else  
    rects = AUTOTILE_PARTS[auto_id]
   end 
 
   rect = Rect.new(0,0,16,16)
   for i in 0...rects.size
     rect.x = (rects[i] % 4) * 16 + sx
     rect.y = (rects[i] / 4) * 16 + sy
     bmp.blt((i % 2) * 16,(i / 2) * 16,tilemap,rect)
   end      
   
 end  
 @@autotile_list[frame][id] = bmp
 @@autotile_list[frame][id]   
end

#is_roof?(data) ⇒ Boolean


  • Checks if a tile is roof


Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
# File 'lib/openrgss/tilemap.rb', line 124

def is_roof?(data)
  return true if data.between?(4352, 4735)
  return true if data.between?(5120, 5503)
  return true if data.between?(5888, 6271)
  return true if data.between?(6656, 7039)
  return true if data.between?(7424, 7807)
  false
end

#is_soil?(data) ⇒ Boolean


  • Checks if a tile is soil


Returns:

  • (Boolean)


135
136
137
138
139
# File 'lib/openrgss/tilemap.rb', line 135

def is_soil?(data)
  return true if data.between?(2816, 4351) && !is_table?(data)
  return true if data > 1663 && !is_stair?(data)
  false
end

#is_stair?(data) ⇒ Boolean


  • Checks if a tile is a stair


Returns:

  • (Boolean)


143
144
145
146
147
148
# File 'lib/openrgss/tilemap.rb', line 143

def is_stair?(data)
  return true if data.between?(1541, 1542)
  return true if data.between?(1549, 1550)
  return true if data.between?(1600, 1615)
  false
end

#is_table?(data) ⇒ Boolean


  • Checks if a tile is a table


Returns:

  • (Boolean)


152
153
154
155
156
157
158
# File 'lib/openrgss/tilemap.rb', line 152

def is_table?(data)
  return true if data.between?(3152, 3199)
  return true if data.between?(3536, 3583)
  return true if data.between?(3920, 3967)
  return true if data.between?(4304, 4351)
  false
end

#is_wall?(data) ⇒ Boolean


  • Checks if a tile is a wall


Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/openrgss/tilemap.rb', line 107

def is_wall?(data)
  return true if data.between?(2288, 2335)
  return true if data.between?(2384, 2431)
  return true if data.between?(2480, 2527)
  return true if data.between?(2576, 2623)
  return true if data.between?(2672, 2719)
  return true if data.between?(2768, 2815)
  return true if data.between?(4736, 5119)
  return true if data.between?(5504, 5887)
  return true if data.between?(6272, 6655)
  return true if data.between?(7040, 7423)
  return true if data > 7807
  false
end

#projects_shadow?(data) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
# File 'lib/openrgss/tilemap.rb', line 160

def projects_shadow?(data) 
  return true if is_wall?(data)
  return true if is_roof?(data)
  false
end

#refreshObject


  • Refresh the Tilemap




579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/openrgss/tilemap.rb', line 579

def refresh
  @frame_count = 0
  return if !@map_data
  # return if !@passages

  @need_refresh = false
  @layer_bitmaps.each do |i|
    i.dispose
  end
  # !!!DEBUG

  print "Refresh the Tilemap\n"
  @layer_bitmaps.clear
  rect = Rect.new(0,0,32,32)
  for l in 0...3
    bitmap = Bitmap.new(@map_data.xsize * 32, @map_data.ysize * 32)
    for i in 0...@map_data.xsize
      for j in 0...@map_data.ysize
        x = i * 32
        y = j * 32
        bitmap.blt(x, y, get_bitmap(@map_data[i, j, 0], l), rect)
      end
    end
    @layer_bitmaps.push(bitmap)
  end  
  
  bitmap = Bitmap.new(@map_data.xsize * 32, @map_data.ysize * 32)
  for i in 0...@map_data.xsize
    for j in 0...@map_data.ysize
      x = i * 32
      y = j * 32
      bitmap.blt(x, y, get_bitmap(@map_data[i, j, 1]), rect)
    end
  end
  @layer_bitmaps.push(bitmap)
  
  bitmap = Bitmap.new(@map_data.xsize * 32, @map_data.ysize * 32)
  for i in 0...@map_data.xsize
    for j in 0...@map_data.ysize
      x = i * 32
      y = j * 32
      #if OVER_TILE #& @passages[@map_data[i, j, 2]] != OVER_TILE

        bitmap.blt(x, y, get_bitmap(@map_data[i, j, 2]), rect)
      #end

    end
  end    
  @layer_bitmaps.push(bitmap)
  bitmap = Bitmap.new(@map_data.xsize * 32, @map_data.ysize * 32)
  for i in 0...@map_data.xsize
    for j in 0...@map_data.ysize
      x = i * 32
      y = j * 32
      #if OVER_TILE #& @passages[@map_data[i, j, 2]] == OVER_TILE

        bitmap.blt(x, y, get_bitmap(@map_data[i, j, 2]), rect)
      #end

    end
  end      

  @layer_bitmaps.push(bitmap)
  @layers[0].bitmap = @layer_bitmaps[@frame]
  @layers[1].bitmap = @layer_bitmaps[3]
  @layers[2].bitmap = @layer_bitmaps[4]
  @layers[3].bitmap = @layer_bitmaps[5]
  create_shadow_bitmap
  
end

#refresh_flashObject


  • Refresh the Flash




664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/openrgss/tilemap.rb', line 664

def refresh_flash
  @flash_count = 0
  @layers[4].bitmap.dispose if @layers[4].bitmap
  return if !@flash_data
  bmp = Bitmap.new(@flash_data.xsize * 32, @flash_data.ysize * 32)
  @layers[4].bitmap = bmp
  for i in 0...@flash_data.xsize
    for j in 0...@flash_data.ysize
      next if @flash_data[i, j] == 0
      x = i * 32
      y = j * 32
      r = ((@flash_data[i, j] / 256) % 16) * 255 / 15
      g = ((@flash_data[i, j] / 16) % 16)  * 255 / 15
      b = (@flash_data[i, j] % 16)         * 255 / 15
      c = Color.new(r, g, b)
      c2 = Color.new(r - 32, g - 32, b - 32)
      bmp.fill_rect(x,y,32,32,c)
      
    end
  end  
end

#updateObject


  • Frame update




496
497
498
499
500
501
502
503
504
505
# File 'lib/openrgss/tilemap.rb', line 496

def update
  if @need_refresh
    refresh 
    @need_refresh=false
  end
  update_animation
  correct_position
  @layers[5].visible = @shadow_visible
  update_flash
end

#update_animationObject


  • Updates the animation of animated tiles




518
519
520
521
522
523
524
525
526
527
528
# File 'lib/openrgss/tilemap.rb', line 518

def update_animation
  @frame_count = (@frame_count + 1) % @frame_interval
  if @frame_count == 0
    @frame = (@frame + 1) % 3
    if @frame > 2
      @layers[0].bitmap = @layer_bitmaps[1]#.clone

    else
      @layers[0].bitmap = @layer_bitmaps[@frame]#.clone

    end  
  end       
end

#update_flashObject


  • Updates the flash of the map




532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/openrgss/tilemap.rb', line 532

def update_flash
  if @need_flash_refresh
    @need_flash_refresh = false
    refresh_flash
  end  
  if @flash_count > 0
    @layers[4].opacity = (@layers[4].opacity * (@flash_count - 1) + @flash_goal ) / @flash_count
    @flash_count -= 1
  else
    @flash_count = @flash_interval
    @flash_goal = @flash_goal == 128 ? 0 : 128
  end    
end