-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.rb
130 lines (101 loc) · 2.09 KB
/
test2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 2次元ベクタ管理クラス
class Vector2d
attr_reader :dx, :dy
# コンストラクタ
def initialize(dx, dy)
@dx = dx
@dy = dy
@scalar = Math.sqrt(@dx * @dx + @dy * @dy)
@angle = Math.atan2(dy, dx)
end
# 加速
def accel(acc, max)
@scalar += acc
if (@scalar > max) then
@scalar = max
end
updateXy
self
end
# 方位変更
def turn(r)
@angle += r
if (@angle > (Math::PI * 2)) then
@angle -= (Math::PI * 2)
end
updateXy
self
end
def updateXy
# dx, dyを更新
@dx = Math::cos(@angle) * @scalar
@dy = Math::sin(@angle) * @scalar
end
def print
printf("dx:%f dy:%f angle:%f scalar:%f\n", @dx, @dy, @angle, @scalar)
self
end
end
class Point2d
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def moveBy(vec)
@x += vec.dx
@y += vec.dy
self
end
end
class Image
def initialize(filename)
@pos = Point2d.new(0, 0)
end
def draw(pos)
printf("%d %d\n",pos.x, pos.y)
self
end
end
class Element
attr_reader :pos
attr_accessor :image, :vec2d
def initialize(x, y)
@pos = Point2d.new(x, y)
end
def moveBy(vec)
@pos.moveBy(vec)
self
end
def move
if @vec2d != nil then
@pos.moveBy(@vec2d)
end
end
def render
@image.draw(@pos)
end
end
imageX = Image.new("xxx.grf")
elem = Element.new(10, 10)
elem.image = imageX
elem.render()
elem.moveBy(Vector2d.new(5, 3))
elem.render
elem.vec2d = Vector2d.new(2, 1)
elem.move
elem.render
elem.move
elem.render
elem.move
elem.render
#sprite.draw(elem.pos)
#sprite.draw(elem.moveBy(Vector2d.new(2, 1)).pos)
#v = Vector2d.new(2, 0)
#v.print
#dturn = Math::PI / 16
#for ang in 0..16 do
# v.turn(dturn).print
#end
#str = sprintf("aaa:%d\n", 10);
#p str