赞 | 0 |
VIP | 40 |
好人卡 | 2 |
积分 | 1 |
经验 | 10932 |
最后登录 | 2016-5-17 |
在线时间 | 462 小时 |
Lv1.梦旅人
- 梦石
- 0
- 星屑
- 50
- 在线时间
- 462 小时
- 注册时间
- 2007-7-30
- 帖子
- 643
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
這是我參考kiss姐的VX板Tilemap
做出來的
如果有BUG請回復一下
- class Tilemap
- FBLX = 640#x方向解析度
- FBLY = 480#y方向解析度
- SIZEX = 32
- SIZEY = 32
- ANI = 15#動畫等待楨數
- #以下畫元件用
- AUTO_XY = [
- [8, 8, 8, 8], [3, 8, 8, 8], [8, 3, 8, 8], [3, 3, 8, 8],
- [8, 8, 8, 3], [3, 8, 8, 3], [8, 3, 8, 3], [3, 3, 8, 3],
- [8, 8, 3, 8], [3, 8, 3, 8], [8, 3, 3, 8], [3, 3, 3, 8],
- [8, 8, 3, 3], [3, 8, 3, 3], [8, 8, 3, 3], [3, 3, 3, 3],
- [7, 7, 7, 7], [7, 3, 7, 7], [7, 7, 7, 3], [7, 3, 7, 3],
- [5, 5, 5, 5], [5, 5, 5, 3], [5, 5, 3, 5], [5, 5, 3, 3],
- [9, 9, 9, 9], [9, 9, 3, 9], [3, 9, 9, 9], [3, 9, 3, 9],
- [11, 11, 11, 11], [3, 11, 11, 11], [11, 3, 11, 11], [3, 3, 11, 11],
- [7, 9, 7, 9], [5, 5, 11, 11], [4, 4, 4, 4], [4, 4, 4, 3],
- [6, 6, 6, 6], [6, 6, 3, 6], [12, 12, 12, 12], [3, 12, 12, 12],
- [10, 10, 10, 10], [10, 3, 10, 10], [4, 6, 4, 6], [4, 4, 10, 10],
- [10, 12, 10, 12], [6, 6, 12, 12], [4, 6, 10, 12], [1, 1, 1, 1]
- ]
- #--------------------------------------------------------------------------
- # ● 定義外部變數
- #--------------------------------------------------------------------------
- attr_accessor :autotiles
- attr_accessor :tileset
- attr_reader :ox
- attr_reader :oy
- #--------------------------------------------------------------------------
- # ● 初始化物件
- #--------------------------------------------------------------------------
- def initialize(viewport = nil)
- @shadow = Bitmap.new(SIZEX, SIZEY)
- @shadow.fill_rect(0, 0, 17, SIZEY, Color.new(0, 0, 0, 120))#陰影
- @viewport = viewport
- @bitmaps = []
- @tileset = nil
- @autotiles= []
- @backs = {}#元件精靈
- @backbitmap = {}#元件圖塊
- @switches = {}#動畫開關
- @count = 0#當前顯示動畫
- @count1 = 0#等待楨數
- @visible = true
- @ox = 0
- @oy = 0
- @lastox = 0
- @lastoy = 0
- end
- #--------------------------------------------------------------------------
- # ● 釋放
- #--------------------------------------------------------------------------
- def dispose
- for i in @backbitmap.values
- i.dispose
- end
- for i in @backs.values
- i.dispose
- end
- @shadow.dispose
- end
- #--------------------------------------------------------------------------
- # ● 是否已釋放
- #--------------------------------------------------------------------------
- def disposed?
- return @shadow.disposed?
- end
- #---------------------------------------------------------------------------
- def map_data=(value)
- @map_data = value
- #如果地圖太大,按ESC打開功能表後返回地圖太慢,下面這段迴圈可以不要
- for i in 0...value.xsize
- for j in 0...value.ysize
- for k in 0..2
- cachebitmap(value[i, j, k])
- end
- end
- end
- #如果地圖太大,按ESC打開功能表後返回地圖太慢,上面這段迴圈可以不要
- #需要重新描繪的圖快
- if @map_data.xsize * SIZEX <= FBLX
- rangei = (@ox / SIZEX - 1)..[((@ox + FBLX) / SIZEX), FBLX / SIZEX - 1].min
- else
- rangei = (@ox / SIZEX - 1)..((@ox + FBLX) / SIZEX)
- end
- if @map_data.xsize * SIZEX <= FBLX
- rangej = (@oy / SIZEY - 1)..[((@oy + FBLY) / SIZEY), FBLY / SIZEY - 1].min
- else
- rangej = (@oy / SIZEY - 1)..((@oy + FBLY) / SIZEY)
- end
- for i in rangei
- for j in rangej
- for k in 0..2
- draw(i, j, k)
- end
- end
- end
- end
- #--------------------------------------------------------------------------
- def refreshox
- #需要釋放掉的圖塊
- rangej = (@lastoy / SIZEY - 1)..((@lastoy + FBLY) / SIZEY)
- if @ox > @lastox
- rangei = ((@lastox / SIZEX - 1)...(@ox / SIZEX - 1))
- elsif @ox < @lastox
- rangei = (((@ox + FBLX) / SIZEX + 1)..((@lastox + FBLX) / SIZEX))
- end
- if (rangei != nil and rangej != nil)
- for i in rangei
- for j in rangej
- for k in 0..2
- next if @backs[[i, j, k]].nil?
- @backs[[i, j, k]].dispose
- @backs.delete([i, j, k])
- @switches.delete([i, j, k])
- end
- end
- end
- end
- #恢復走路時候的偏移
- if @ox >= (@map_data.xsize * SIZEX - FBLX)
- for i in @backs.keys
- @backs[i].ox = 0
- @backs[i].x = i[0] * SIZEX - @ox
- if i[0] < (@ox / 32 - 1)
- @backs[i].x += @map_data.xsize * SIZEX
- elsif i[0] == -1
- @backs[i].x = 0
- end
- end
- else
- for i in @backs.keys
- @backs[i].ox = 0
- @backs[i].x = i[0] * SIZEX - @ox
- end
- end
- #需要重新描繪的圖快
- rangej = (@oy / SIZEY - 1)..((@oy + FBLY) / SIZEY)
- if @ox > @lastox
- rangei = ([((@lastox + FBLX) / SIZEX + 1), (@ox / SIZEX - 1)].max..((@ox + FBLX) / SIZEX))
- elsif @ox < @lastox
- rangei = ((@ox / SIZEX - 1)...[(@lastox / SIZEX - 1), (@ox + FBLX) / SIZEX + 1].min)
- end
- if (rangei != nil and rangej != nil)
- for i in rangei
- for j in rangej
- for k in 0..2
- draw(i, j, k)
- end
- end
- end
- end
- end
- #--------------------------------------------------------------------------
- def refreshoy
- #需要釋放掉的圖塊
- rangei = (@lastox / SIZEX - 1)..((@lastox + FBLX) / SIZEX)
- if @oy > @lastoy
- rangej = ((@lastoy / SIZEY - 1)...(@oy / SIZEY - 1))
- elsif @oy < @lastoy
- rangej = (((@oy + FBLY) / SIZEY + 1)..((@lastoy + FBLY) / SIZEY))
- end
- for i in rangei
- for j in rangej
- for k in 0..2
- next if @backs[[i, j, k]].nil?
- @backs[[i, j, k]].dispose
- @backs.delete([i, j, k])
- @switches.delete([i, j, k])
- end
- end
- end
- #恢復走路時候的偏移
- if @oy >= (@map_data.ysize * SIZEY - FBLY)
- for i in @backs.keys
- @backs[i].oy = 0
- @backs[i].y = i[1] * SIZEY - @oy
- if i[1] < (@oy / 32 - 1)
- @backs[i].y += @map_data.ysize * SIZEY
- elsif i[1] == -1
- @backs[i].y = 0
- end
- end
- else
- for i in @backs.keys
- @backs[i].oy = 0
- @backs[i].y = i[1] * SIZEY - @oy
- end
- end
- #需要重新描繪的圖快
- rangei = (@ox / SIZEX - 1)..((@ox + FBLX) / SIZEX)
- if @oy > @lastoy
- rangej = ([((@lastoy + FBLY) / SIZEY + 1), @oy / SIZEY - 1].max..((@oy + FBLY) / SIZEY))
- elsif @oy < @lastoy
- rangej = ((@oy / SIZEY - 1)...[(@lastoy / SIZEY - 1), ((@oy + FBLY) / SIZEY + 1)].min)
- end
- for i in rangei
- for j in rangej
- for k in 0..2
- draw(i, j, k)
- end
- end
- end
- end
- #--------------------------------------------------------------------------
- #i為x座標,j為y座標,k為圖層
- def draw(i, j, k)
- i0 = i
- j0 = j
- i -= @map_data.xsize if i >= @map_data.xsize
- i += @map_data.xsize if i < 0
- j -= @map_data.ysize if j >= @map_data.ysize
- j += @map_data.ysize if j < 0
- id = @map_data[i, j, k]
- return if id == 0
- unless @backs[[i, j, k]].nil?
- unless @backs[[i, j, 3]].nil?
- @backs[[i, j, 3]].x = i0 * SIZEX - @ox
- @backs[[i, j, 3]].y = j0 * SIZEY - @oy
- end
- @backs[[i, j, k]].x = i0 * SIZEX - @ox
- @backs[[i, j, k]].y = j0 * SIZEY - @oy
- return
- end
- @backs[[i, j, k]] = Sprite.new(@viewport)
- @backs[[i, j, k]].x = i0 * SIZEX - @ox
- @backs[[i, j, k]].y = j0 * SIZEY - @oy
- @backs[[i, j, k]].z = k * 2
- @backs[[i, j, k]].bitmap = cachebitmap(id)
- if id < 384
- if @backbitmap[id + 10000] != nil
- #動畫
- @switches[[i, j, k]] = true
- @backs[[i, j, k]].bitmap = @backbitmap[id + @count * 10000]
- end
- end
- return if @passages.nil?
- return unless @passages[id] == 22
- @backs[[i, j, k]].z = 200
- end
- #---------------------------------------------------------------------------
- def cachebitmap(id)
- if @backbitmap[id].nil?
- if id < 384#自動元件的情況
- bitmapid = (id - (id % 48)) / 48 - 1
- @backbitmap[id] = drawbitmap1(id, bitmapid)
- if @autotiles[bitmapid].width > 96
- @backbitmap[id + 10000] = drawbitmap1(id, bitmapid, 1)
- @backbitmap[id + 20000] = drawbitmap1(id, bitmapid, 2)
- @backbitmap[id + 30000] = drawbitmap1(id, bitmapid, 3)
- end
- else
- rect = Rect.new((id - 384) % 8 * SIZEX, (id - 384) / 8 * SIZEY, SIZEX, SIZEY)
- @backbitmap[id] = Bitmap.new(SIZEX, SIZEY)
- @backbitmap[id].blt(0, 0, @tileset, rect)
- end
- end
- return @backbitmap[id]
- end
- #---------------------------------------------------------------------------
- #此處的x,y指圖片上的xy位置,id範圍為48-383,bitmapid為圖片編號
- def drawbitmap1(id, bitmapid, ani = 0)
- bitmap = Bitmap.new(SIZEX, SIZEY)
- pic_id = id % 48
- for i in 0..3
- rect_x = (AUTO_XY[pic_id][i] - 1) % 3
- rect_y = (AUTO_XY[pic_id][i] - 1 - rect_x) / 3
- x = (i % 2) * 16
- y = (i - (x / 16)) / 2 * 16
- bitmap.blt(x, y, @autotiles[bitmapid], Rect.new(rect_x * 32 + x + ani * 96, rect_y * 32 + y, 16, 16))
- end
- return bitmap
- end
- #---------------------------------------------------------------------------
- def priorities=(value)
- @passages = value
- for i in ([@ox / SIZEX - 1, 0].max)..((@ox + FBLX) / SIZEX)
- for j in ([@oy / SIZEY - 1, 0].max)..((@oy + FBLY) / SIZEY)
- id = @map_data[i, j, 2]
- next if id.nil?
- next unless value[id] == 22
- @backs[[i, j, 2]].z = 200
- end
- end
- end
- #--------------------------------------------------------------------------
- def ox=(value)
- if @ox != value
- @ox = value
- if value >= (@map_data.xsize * SIZEX - FBLX)
- @lastox += @map_data.xsize * SIZEX if @lastox < (@ox - FBLX)
- end
- #人物走路時候
- if @ox % (SIZEX/2) != 0
- for i in @backs.values
- i.ox = @ox - @lastox
- end
- else
- refreshox
- @lastox = @ox
- end
- end
- end
- #--------------------------------------------------------------------------
- def oy=(value)
- if @oy != value
- @oy = value
- if value >= (@map_data.ysize * SIZEY - FBLY)
- @lastoy += @map_data.ysize * SIZEY if @lastoy < (@oy - FBLY)
- end
- #人物走路時候
- if @oy % SIZEY != 0
- for i in @backs.values
- i.oy = @oy - @lastoy
- end
- else
- refreshoy
- @lastoy = @oy
- end
- end
- end
- #---------------------------------------------------------------------------
- def count=(value)
- value = 0 if value >= 4
- @count = value
- for i in @switches.keys
- id = @map_data[i[0], i[1], i[2]]
- @backs[i].bitmap = @backbitmap[id + value * 10000]
- end
- end
- #---------------------------------------------------------------------------
- def update
- if @count1 == ANI
- @count1 = 0
- self.count = @count+1
- else
- @count1 += 1
- end
- end
- end
复制代码 |
|