-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathExample - 2d Homing Missiles.monkey2
83 lines (73 loc) · 1.9 KB
/
Example - 2d Homing Missiles.monkey2
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
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
Class missile
Field x:Double,y:Double
Field currentangle:Double
Field deleteme:Bool
Method New(x:Double,y:Double)
Self.x = x
Self.y = y
End Method
Method update(mx:Double,my:Double)
'Home the missile
local targetx:Double = mx - x
local targety:Double = my - y
currentangle = ATan2(targety, targetx) * 180 / Pi
'2 here below is the movement speed
Local vx:Double = 2 * (90 - Abs(currentangle)) / 90
local vy:Double
if (currentangle < 0)
vy = -2 + Abs(vx)
Else
vy = 2 - Abs(vx)
End if
x += vx
y += vy
'if collide then set remove flag
If rectsoverlap(mx-5,my-5,10,10,x-5,y-5,10,10) Then deleteme = true
End Method
Method draw(canvas:Canvas)
canvas.Color = Color.Black
canvas.DrawCircle(x,y,5)
End Method
Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False
If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False
Return True
End Function
End Class
Global mymissile:List<missile> = New List<missile>
Class MyWindow Extends Window
Method New()
For Local i:Int=0 until 5
mymissile.Add(New missile(Rnd(Width),Rnd(Height)))
Next
End Method
Method OnRender( canvas:Canvas ) Override
App.RequestRender() ' Activate this method
'
'Update the missiles
For Local i:=Eachin mymissile
i.update(Mouse.X,Mouse.Y)
Next
For Local i:=Eachin mymissile
If i.deleteme Then
mymissile.Remove(i)
mymissile.Add(New missile(Rnd(Width),Height+50))
End If
Next
'Draw the missile
For Local i:=Eachin mymissile
i.draw(canvas)
Next
' if key escape then quit
If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()
End Method
End Class
Function Main()
New AppInstance
New MyWindow
App.Run()
End Function