#!ruby -Ks # -*- mode: ruby; encoding: sjis -*- # Last updated: <2014/12/02 03:42:49 +0900> # # Spriteの衝突範囲について実験する require 'dxruby' # アタリを取るSprite class MySprite < Sprite def initialize(x, y, img) super(x, y, img) end def update super self.offset_sync = !self.offset_sync if Input.keyPush?(K_O) self.angle = (self.angle - 1) % 360 if Input.keyDown?(K_Q) self.angle = (self.angle + 1) % 360 if Input.keyDown?(K_E) self.center_x += 1 if Input.keyDown?(K_D) self.center_x -= 1 if Input.keyDown?(K_A) self.center_y += 1 if Input.keyDown?(K_S) self.center_y -= 1 if Input.keyDown?(K_W) if Input.keyPush?(K_R) self.center_x = self.image.width / 2 self.center_y = self.image.height / 2 self.angle = 0 self.offset_sync = false end end # アタリ範囲を設定 def set_collison(ary, syncfg) self.collision = ary self.collision_enable = true self.collision_sync = syncfg end end # マウスカーソル位置のSprite class MyMouseSprite < Sprite def initialize(x, y) img = Image.new(4, 4, C_RED) super(x, y, img) self.collision = [0, 0, 3, 3] self.collision_enable = true end def update super self.x = Input.mousePosX self.y = Input.mousePosY end end # 基準線を描画 def draw_baseline(bx, by) c = [128, 0, 0] Window.drawLine(0, by, Window.width, by, c) Window.drawLine(bx, 0, bx, Window.height, c) end # 四角を描画。アタリ範囲の描画に使う def draw_square(bx, by, x1, y1, x2, y2, col) x1 += bx y1 += by x2 += bx y2 += by Window.drawBoxFill(x1, y1, x2, y2, col) end # アタリ範囲のリスト collision_list = [ [32, 32, 96, 96], [-32, -32, 32, 32], [64, 64, 32], [0, 0, 32], ] Window.resize(480, 360) # 画像読み込み base64_text = "" DATA.each {|l| base64_text += l.chomp} img = Image.loadFromFileInMemory(base64_text.unpack('m')[0]) # Sprite生成 spr = MySprite.new(Window.width / 2, Window.height / 2, img) mspr = MyMouseSprite.new(0, 0) # 円のアタリ範囲を表示するために作成 cimg = Image.new(128, 128, [128, 0, 0, 255]).circleFill(64, 64, 32, [128, 255, 0, 0]) fnt = Font.new(14) cnum = 0 count = 0 atari_disp = 0 spr.set_collison(collision_list[cnum], true) Window.loop do break if Input.keyPush?(K_ESCAPE) if Input.keyPush?(K_C) # アタリ範囲を変更 cnum = (cnum + 1) % collision_list.length spr.set_collison(collision_list[cnum], true) end if Input.keyPush?(K_B) # アタリ範囲表示位置を変更 atari_disp = (atari_disp + 1) % 3 end # Sprite更新 mspr.update spr.update draw_baseline(Window.width / 2, Window.height / 2) # 基準線を描画 spr.draw # アタリを取るSpriteの描画 mspr.draw # マウスカーソル位置のSpriteを描画 # アタリ範囲を描画 if atari_disp <= 1 and count & 0x20 == 0 case atari_disp when 0 # offset_sync と cneter_x,y を反映する if spr.offset_sync bx = spr.x - spr.center_x by = spr.y - spr.center_y else bx = spr.x by = spr.y end when 1 # x,y座標しか見ない bx = spr.x by = spr.y end if spr.collision.length == 4 # 四角を描画 x1, y1, x2, y2 = spr.collision draw_square(bx, by, x1, y1, x2, y2, [128, 255, 0, 0]) elsif spr.collision.length == 3 # 丸を描画 x = bx + spr.collision[0] - cimg.width / 2 y = by + spr.collision[1] - cimg.height / 2 Window.draw(x, y, cimg) end end # 当たっていたら背景色を変更 Window.bgcolor = (spr === mspr)? [128, 128, 128] : [0, 0, 0] # テキスト情報を描画 osync = (spr.offset_sync)? "true" : "false" astr = ["type A", "type B", "none"][atari_disp] cols = "[" spr.collision.each do |d| cols += d.to_s + ", " end cols += "]" y = 4 [ "o) offset_sync = #{osync}", "wasd) center_x,y = #{spr.center_x}, #{spr.center_y}", "q e) angle = #{spr.angle}", "c) collison = #{cols}", "b) collison disp = #{astr}", "r) reset", ].each do |s| Window.drawFont(4, y, s, fnt) y += 20 end count += 1 end # 以下、画像ファイルを base64 にしたもの __END__ iVBORw0KGgoAAAANSUhEUgAAAIAAAACABAMAAAAxEHz4AAAAMFBMVEUAAP9w cHD+/v4A/wCOsm4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADz VDixAAAAAXRSTlMAQObYZgAAATtJREFUeJztmVEOgyAMhhkn2DL33ngFriAn AO5/lVF0Q91EbZfgtv4+GB78+ETRJlUXZtRZsXISgAAyAFpQWrcMAKgIqGuA AIpCNmhZBjGkFRgDzJCrmWZpLAABCOB3AbvrgiWDpptmPi7eQnDBNTbYQAUY Z1zT2XjUM3BxDexYYL9BYBnE3GYX7AZ4LoD1HghAABmAmyFu6WFPUAzwg+D9 8F0gGuDVDIM4MxrYamtQ/TEK4IsBx6gPBCAAAQhAAH8C6Oud4IOnAlLF5WPp VcsgKzAMrOcYYDzjKSSLrgj4WH3wmGFtLAABCOCwAGIoAJ2aKYCNHSoA+ykA ig5gG2BDBztLDAMNDIMUGM4FAM7T6n6+lxbWsytVAqSbBdDFJtqaQcK8MdgG UL2BLnUSqWuwBbAtRwDs/a3PcwcRa/rD4rHg2AAAAABJRU5ErkJggg==