require "dxruby"
require "ostruct"

class O
  attr_accessor :x
  attr_accessor :y
  attr_accessor :img
  attr_accessor :func
  attr_accessor :sym
  attr_accessor :f
  attr_accessor :st
  def initialize oo = Hash.new
    @y = oo[:y]
    @x = oo[:x]
    @img  = oo[:img]
    @func = oo[:func]
    @sym  = oo[:sym]
    @st   = OpenStruct.new
  end
end

def hit_check o , m
  o.x + o.img.width  > m.x && o.x < m.x + m.img.width &&
    o.y + o.img.height > m.y && o.y < m.y + m.img.height
end

task = []
#自機オブジェクト生成
task.push O.new x:300 , y:400 , img:Image.new(20,20,[100,150,150]) , sym: :user ,
func:->o{
  o.x += 2 if Input.keyDown? K_RIGHT
  o.x -= 2 if Input.keyDown? K_LEFT
  o.y -= 2 if Input.keyDown? K_UP
  o.y += 2 if Input.keyDown? K_DOWN
  Window.drawEx o.x , o.y , o.img
  
  #ショット
  if Input.keyPush? K_Z
    task.push O.new x:o.x , y:o.y , img:Image.new(7,15,[100,100,170]) , sym: :user_shot ,
    func:->o{
      o.y -= 0.5
      Window.drawEx o.x , o.y , o.img
    }
  end
  #ホーミングショット
  if Input.keyPush? K_X
    task.push O.new x:o.x , y:o.y , img:Image.new(7,15,[100,100,170]) , sym: :user_shot2 ,
    func:->o{
      #何か処理する
      o.y -= 0.3
      Window.drawEx o.x , o.y , o.img
    }
  end
  #時限式で加速か何か
  if Input.keyPush? K_C
    task.push O.new x:o.x , y:o.y , img:Image.new(7,15,[100,100,170]) , sym: :user_shot3 ,
    func:->o{
      o.st.n ||= 0
      o.st.n += 1
      
      o.y -= 0.5 + (o.st.n/100.0)
      Window.drawEx o.x , o.y , o.img
    }
  end
  #全オブジェクトにアクセス可能
  if Input.keyPush? K_A
    p task
  end
}

#敵オブジェクト生成
task.push O.new x:rand(Window.width-50) , y:rand(50) ,
 img:Image.new(rand(40)+30,15,[100+rand(100),150+rand(100),150+rand(100)]) , sym: :enemy ,
func:->o{
  o.y += 0.5
  Window.drawEx o.x , o.y , o.img
}
Window.loop do
  exit if Input.keyDown? K_ESCAPE
  task.each do | o |
    o.func.call o
  end
end
