class Tilemap

Constants

A1
A1E
A1POS
A3
TILESIZE

Attributes

bitmaps[RW]
flags[RW]
flash_data[RW]
map_data[R]
ox[R]
oy[R]
viewport[RW]
visible[RW]

Public Class Methods

new(viewport = nil) click to toggle source
# 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

Public Instance Methods

dispose() click to toggle source
# File lib/rgss3/tilemap.rb, line 58
def dispose
  for layer in @layers
    layer.bitmap.dispose
    layer.dispose
  end
  for layer in @animated_layer
    layer.dispose
  end
  @disposed = true
end
disposed?() click to toggle source
# File lib/rgss3/tilemap.rb, line 69
def disposed?
  @disposed
end
flags=(data) click to toggle source
# File lib/rgss3/tilemap.rb, line 39
def flags=(data)
  @flags = data
  refresh
end
map_data=(data) click to toggle source
# File lib/rgss3/tilemap.rb, line 33
def map_data=(data)
  return if @map_data == data
  @map_data = data
  refresh
end
ox=(value) click to toggle source
# File lib/rgss3/tilemap.rb, line 44
def ox=(value)
  @ox = value
  for layer in @layers
    layer.ox = @ox
  end
end
oy=(value) click to toggle source
# File lib/rgss3/tilemap.rb, line 51
def oy=(value)
  @oy = value
  for layer in @layers
    layer.oy = @oy
  end
end
update() click to toggle source
# File lib/rgss3/tilemap.rb, line 73
def update
  @anim_count = (@anim_count + 1) % (@animated_layer.size * 30)
  @layers[0].bitmap = @animated_layer[@anim_count/30]
end

Private Instance Methods

bitmap_for_autotile(autotile) click to toggle source
# File lib/rgss3/tilemap.rb, line 114
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
draw_A1tile(x,y,id,animated = false) click to toggle source
# File lib/rgss3/tilemap.rb, line 143
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) click to toggle source
# File lib/rgss3/tilemap.rb, line 210
def draw_A2tile(x,y,id)
  draw_A1tile(x,y,id)
end
draw_A3tile(x,y,id) click to toggle source
# File lib/rgss3/tilemap.rb, line 221
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) click to toggle source
# File lib/rgss3/tilemap.rb, line 262
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) click to toggle source
# File lib/rgss3/tilemap.rb, line 276
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_layer() click to toggle source
# File lib/rgss3/tilemap.rb, line 89
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_layers() click to toggle source
# File lib/rgss3/tilemap.rb, line 295
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) click to toggle source
# File lib/rgss3/tilemap.rb, line 185
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?() click to toggle source
# File lib/rgss3/tilemap.rb, line 284
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
refresh() click to toggle source
# File lib/rgss3/tilemap.rb, line 80
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