forked from therevills/diddy2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite.monkey2
360 lines (300 loc) · 8.59 KB
/
sprite.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
Namespace diddy2.sprite
Class Sprite
Private
Field _z:Float
Field _rotation:Float
Field _image:Image
Field _wrapImageX:Bool = False
Field _wrapImageY:Bool = False
Field _colour:Color = Color.White
Field _alpha:Float = 1
Field _currentAnimation:Image[]
Field _animationBank:AnimationBank
Field _frame:Int
Field _startFrame:Int
Field _maxFrame:Int
Field _frameTimer:Int
Field _frameTimerSpeed:Int
Field _loopAnimation:Bool = False
Field _reverseAnimation:Bool = False
Field _pingPongAnimation:Bool = False
Field _pingPongAnimationCounter:Int
Field _maxPingPongAnimationCounter:Int = 1
Field _speed:Float = 1
Public
Field name:String
Field position:Vec2f
Field gravity:Vec2f = New Vec2f(0, 0)
Field originPosition:Vec2f
Field scale:Vec2f = New Vec2f(1, 1)
Field speed:Vec2f = New Vec2f(1, 1)
Field velocity:Vec2f = New Vec2f(0, 0)
Field deltaValue:Vec2f = New Vec2f(0, 0)
Field track:Bool = False
Field currentAnimationName:String
Field collRect:Rectf
Field displayCollRect:Bool
Field displayCollRectAlpha:Float = .5
Method New(image:Image, position:Vec2f)
Self.position = position
Self.originPosition = position
_image = image
_animationBank = New AnimationBank
UpdateCollisionRect()
End
Method Render(canvas:Canvas, offsetX:Float = 0, offsetY:Float = 0, roundPosition:Bool = False, roundRotation:Bool = False) Virtual
Local localImage:Image
If _currentAnimation
localImage = _currentAnimation[_frame]
Else
localImage = _image
End
Local canvasColor := canvas.Color
Local canvasAlpha := canvas.Alpha
canvas.Color = _colour
canvas.Alpha = _alpha
Local localPosition:Vec2f = position
localPosition.x -= offsetX
localPosition.y -= offsetY
If roundPosition
localPosition.x = Floor(localPosition.x)
localPosition.y = Floor(localPosition.y)
End
Local r := _rotation
If roundRotation
Local d := ToDegrees(_rotation)
d = Floor(d)
r = ToRadians(d)
End
Local s := scale
canvas.DrawImage(localImage, localPosition, -r, s)
Local vrWidth := DiddyApp.GetInstance().Window.VirtualResolution.X
Local vrHeight := DiddyApp.GetInstance().Window.VirtualResolution.Y
If _wrapImageX
If localPosition.x - localImage.Radius < 0 canvas.DrawImage(localImage, localPosition.x + vrWidth, localPosition.y, r, s.x, s.y)
If localPosition.x + localImage.Radius > vrWidth canvas.DrawImage(localImage, localPosition.x - vrWidth, localPosition.y, r, s.x, s.y)
End
If _wrapImageY
If localPosition.y - localImage.Radius < 0 canvas.DrawImage(localImage, localPosition.x, localPosition.y + vrHeight, r, s.x, s.y)
If localPosition.y + localImage.Radius > vrHeight canvas.DrawImage(localImage, localPosition.x, localPosition.y - vrHeight, r, s.x, s.y)
End
canvas.Color = canvasColor
canvas.Alpha = canvasAlpha
If displayCollRect
canvas.Alpha = displayCollRectAlpha
canvas.DrawRect(collRect)
canvas.Alpha = canvasAlpha
End
End
Method RenderDebug(canvas:Canvas)
Local y:Int = 10
Local gap:Int = canvas.Font.Height
canvas.DrawText("Frame: " + Frame, 10, y)
y += gap
canvas.DrawText("Start Frame: " + StartFrame, 10, y)
y += gap
canvas.DrawText("Max Frame: " + MaxFrame, 10, y)
y += gap
canvas.DrawText("_frameTimerSpeed: " + _frameTimerSpeed, 10, y)
y += gap
canvas.DrawText("_frameTimer: " + _frameTimer, 10, y)
y += gap
End
Method MouseCollision:Bool(ignoreScroll:Bool = True, mouseBufferX:Float = 2, mouseBufferY:Float = 2, updateCollisionRect:Bool = True, bufferX:Float = 0, bufferY:Float = 0)
Local mx := Mouse.X
Local my := Mouse.Y
Local collRect := New Rectf(mx, my, mx + mouseBufferX ,my + mouseBufferY)
If updateCollisionRect
UpdateCollisionRect(bufferX, bufferY, ignoreScroll)
End
If Self.collRect.Intersects(collRect)
Return True
End
Return False
End
Method UpdateCollisionRect(bufferX:Float = 0, bufferY:Float = 0, ignoreScroll:Bool = False)
Local image:Image
If CurrentAnimation
image = CurrentAnimation[Frame]
Else
image = Image
End
Local ww := Window
Local sx:Float = ww.ScrollX
Local sy:Float = ww.ScrollY
If ignoreScroll
sx = 0
sy = 0
End
Local x0 := position.x + image.Bounds.Left - sx - (bufferX / 2)
Local y0 := position.y + image.Bounds.Top - sy - (bufferY / 2)
Local x1 := x0 + image.Width + bufferX
Local y1 := y0 + image.Height + bufferY
collRect = New Rectf(x0, y0, x1 ,y1 )
End
Method Move()
position.x += deltaValue.x
position.y += deltaValue.y
'apply gravity
If gravity.x <> 0 Then deltaValue.x += gravity.x
If gravity.y <> 0 Then deltaValue.y += gravity.y
End Method
Method MoveForward()
Local dx:Float = Cos(_rotation) * _speed
Local dy:Float = Sin(_rotation) * _speed
position.x += dx
position.y += dy
End
Property WrapImageX:Bool()
Return _wrapImageX
Setter (wrapImageX:Bool)
_wrapImageX = wrapImageX
End
Property WrapImageY:Bool()
Return _wrapImageY
Setter (wrapImageY:Bool)
_wrapImageY = wrapImageY
End
Property WrapImage:Bool()
Return _wrapImageX And _wrapImageY
Setter (wrapImage:Bool)
_wrapImageX = wrapImage
_wrapImageY = wrapImage
End
Property CurrentAnimation:Image[]()
Return _currentAnimation
Setter (frames:Image[])
_currentAnimation = frames
End
Property Frame:Int()
Return _frame
Setter (frame:Int)
_frame = frame
End
Property StartFrame:Int()
Return _startFrame
Setter (startFrame:Int)
_startFrame = startFrame
End
Property MaxFrame:Int()
Return _maxFrame
Setter (maxFrame:Int)
_maxFrame = maxFrame
End
Property Colour:Color()
Return _colour
Setter (color:Color)
_colour = color
End
Property Speed:Float()
Return _speed
Setter (speed:Float)
_speed = speed
End
Property Rotation:Float()
Return _rotation
Setter (rotation:Float)
_rotation = rotation Mod TwoPi
End
Property Alpha:Float()
Return _alpha
Setter (alpha:Float)
alpha = Clamp(alpha, 0.0, 1.0)
_alpha = alpha
End
Property Image:Image()
Return _image
Setter (image:Image)
_image = image
End
Method CreateAnimation(name:String, noOfFrames:Int)
_animationBank.CreateAnimation(name, noOfFrames)
End
Method AddFrame(nameOfAnimation:String, nameOfImage:String, frameIndex:Int)
_animationBank.AddFrame(nameOfAnimation, nameOfImage, frameIndex)
End
Method GetAnimation:Image[](nameOfAnimation:String, frameTimerSpeed:Int = 125, loopAnimation:Bool = False, pingPongAnimation:Bool = False, startFrame:Int = 0)
Local frames := _animationBank.GetAnimation(nameOfAnimation)
_maxFrame = frames.Length - 1
_frameTimerSpeed = frameTimerSpeed
_frameTimer = Millisecs()
_frame = startFrame
_startFrame = startFrame
_pingPongAnimation = pingPongAnimation
_pingPongAnimationCounter = 0
_loopAnimation = loopAnimation
_reverseAnimation = False
Return frames
End
Method SetCurrentAnimation(nameOfAnimation:String, frameTimerSpeed:Int = 125, loopAnimation:Bool = False, pingPongAnimation:Bool = False, startFrame:Int = 0)
_currentAnimation = GetAnimation(nameOfAnimation, frameTimerSpeed, loopAnimation, pingPongAnimation, startFrame)
currentAnimationName = nameOfAnimation
End
Method UpdateAnimation:Bool()
Local rv:Bool = False
If _frameTimerSpeed > 0
If Millisecs() > _frameTimer + _frameTimerSpeed
If Not _reverseAnimation
_frame += 1
If _frame > _maxFrame
rv = ResetAnimation()
End
Else
_frame -= 1
If _frame < _maxFrame
rv = ResetAnimation()
End
End
_frameTimer = Millisecs()
End
End
Return rv
End
Method ResetAnimation:Bool()
If _loopAnimation
If _pingPongAnimation
FlipAnimationFrames()
Else
_frame = _startFrame
End
Else
If _pingPongAnimation And _pingPongAnimationCounter < _maxPingPongAnimationCounter
FlipAnimationFrames()
_pingPongAnimationCounter += 1
Else
_frame = _maxFrame
Return True
End
End
Return False
End
Method FlipAnimationFrames()
_reverseAnimation = Not _reverseAnimation
_frame = _maxFrame
Local ts:Int = _startFrame
_startFrame = _maxFrame
_maxFrame = ts
End
Property Window:DiddyWindow()
Return DiddyApp.GetInstance().Window
End
Method OnScreen:Bool(bufferX:Float = 0, bufferY:Float = 0)
Local image:Image
If _currentAnimation
image = _currentAnimation[_frame]
Else
image = _image
End
Local ww := Window
Local wr := ww.Rect
Local x0 := position.x + image.Bounds.Left - ww.ScrollX - (bufferX / 2)
Local y0 := position.y + image.Bounds.Top - ww.ScrollY - (bufferY / 2)
Local x1 := x0 + image.Width + bufferX
Local y1 := y0 + image.Height + bufferY
Local collRect := New Rectf(x0, y0, x1 ,y1 )
If collRect.Intersects(wr)
Return True
End
Return False
End
End