StarRubyでラジコンビークル

A.pngが左右キーで旋回,上下キーで加速減速.

ゲームライブラリ使ったことなかったけど楽.

プロトタイプなので,いろいろカッコ悪い.

require "starruby"
include StarRuby

require 'matrix'
include Math

class Vector
  def []=(i,x)
    @elements[i]=x
  end
end

#移動体管理クラス	#todo
#class ManagerVehicle
#
#end

class Vehicle

	# テクスチャオブジェクト生成
	@@texVeh = Texture.load("A.png")

	#関数:イニシャライザ
	def initialize#( id )
		#@id = id	#todo:
		@vel = 1	#todo:マジックナンバー
		@dir = Vector[0, -1]	#todo:マジックナンバー
		@lct = Vector[0, 0]
	end

	#関数:毎時呼び出し
	def exec(tex, keys)
		# 位置更新
		if true then
			@lct -= @dir * (@vel / 100)
		end

		# 速度更新
		if true then
			#左旋回
			if keys.include?(:left) then
				turn(:dir => :left)
			end
			#右旋回
			if keys.include?(:right) then
				turn(:dir => :right)
			end
			#加速
			if keys.include?(:up) then
				@vel += 20
			end
			#減速
			if keys.include?(:down) then
				@vel -= 20
			end

			#自然減速
			if !keys.include?(:numpad5) then
				if @vel != 0 then
					if @vel.abs <= 5 then
						@vel = 0
					end
	
					if @vel > 0 then
						@vel -= 5
					elsif @vel < 0 then
						@vel += 5
					end
				end 
			end
		end

		# 画面に出す
		angle = atan2( @dir[1], @dir[0] ) + 90.degrees
		tex.render_texture(@@texVeh, @lct[0], @lct[1], :angle => angle, :blend_type => :add)

		p @vel	#for debugging
	end

	#関数:旋回
	def turn(details = {})
		rad = Math::PI / 180 * 2

		rad = (lambda do
			if details[:dir] == :left then
				return rad * -1
			else
				return rad
			end
		end).call

		tmpDir = @dir.clone
		@dir[0] = tmpDir[0] * cos(rad) - tmpDir[1] * sin(rad)	#x
		@dir[1] = tmpDir[0] * sin(rad) + tmpDir[1] * cos(rad)	#y
	end
end

# ***実行部分 ->

# タスク生成
# -タスクリスト
HashTask = Hash.new

# -移動体
HashTask.store( "veh1", Vehicle.new )

# 中間レンダリングバッファ生成
midTex = Texture.new(640, 480)

# ゲームループ
Game.run(640, 480) do |game|

	# 中間バッファリセット
	midTex.clear

	# 入力受付
	keys = Input.keys(:keyboard)
	#ESCキーで中断
	break if keys.include?(:escape)

	# タスクエクゼク
	HashTask.each_value do |task|

		task.exec(midTex, keys)
	end

	# 画面更新
	game.screen.clear
	game.screen.render_texture(midTex, 8, 8)
end
# <- *実行部分