Miniscriptで2次元ベクトルのクラス作成してみた。
本当は複素数平面のクラスを作りたかったけど、
演算子のオーバーライドは無理なのでベースとなる部分から2次元ベクトルの処理だけ構築した感じ。
結構適当に書くのでも使い勝手がいいので、個人的にはすげえ役立ってる。
自作ゲーム開発でもMiniscriptの使用頻度がかなり多いのですよ。
Complex = {}
Complex.x =0
Complex.y =0
Complex.init = function(a,b)
if a isa number then
self.x =a
end if
if b isa number then
self.y = b
end if
end function
Complex.plus = function(a,b)
if a isa Complex then
self.x = a.x+ b.x
end if
if b isa Complex then
self.y = a.y+ b.y
end if
end function
Complex.minus = function(a,b)
if a isa Complex then
self.x = a.x- b.x
end if
if b isa Complex then
self.y = a.y- b.y
end if
end function
Complex.inv = function()
self.x =-self.x
self.y =-self.y
end function
Complex.multi = function(num_x,a)
if a isa Complex and num_x isa number then
self.x = num_x * a.x
self.y = num_y * a.y
end if
end function
Complex.equal = function( a,b)
if a isa Complex and b isa Complex then
if a.x == b.x and a.y == b.y then
return 1
else
return 0
end if
end if
return -1
end function
使用例
//初期定義 cp_a = new Complex cp_a.init(2,3) cp_b = new Complex cp_b.init(1,-1) //計算 cp_result= new Complex cp_result.plus(cp_a,cp_b) //内部クラス構造が出力される print cp_result //メンバに格納されている値を確認 print cp_result.x print cp_result.y
