-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflxvelocity.monkey
343 lines (293 loc) · 14.1 KB
/
flxvelocity.monkey
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
'/**
' * FlxColor
' * -- Part of the Flixel Power Tools set
' *
' * v1.5 Added RGBtoWebString
' * v1.4 getHSVColorWheel now supports an alpha value per color
' * v1.3 Added getAlphaFloat
' * v1.2 Updated for the Flixel 2.5 Plugin system
' *
' * @version 1.5 - August 4th 2011
' * @link http://www.photonstorm.com
' * @author Richard Davey / Photon Storm
' * @see Depends upon FlxMath
' Copyright: Monkey port - 2012 Aleksey 'KaaPex' Kazantsev
'*/
Strict
Import flixel
Import flxmath
Class FlxVelocity
'/**
'* Sets the source FlxSprite x/y velocity so it will move directly towards the destination FlxSprite at the speed given (in pixels per second)<br>
'* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.<br>
'* Timings are approximate due to the way Flash timers work, and irrespective of SWF frame rate. Allow for a variance of +- 50ms.<br>
'* The source object doesn't stop moving automatically should it ever reach the destination coordinates.<br>
'* If you need the object to accelerate, see accelerateTowardsObject() instead
'* Note: Doesn't take into account acceleration, maxVelocity or drag (if you set drag or acceleration too high this object may not move at all)
'*
'* @param source The FlxSprite on which the velocity will be set
'* @param dest The FlxSprite where the source object will move to
'* @param speed The speed it will move, in pixels per second (default is 60 pixels/sec)
'* @param maxTime Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the source will arrive at destination in the given number of ms
'*/
Function MoveTowardsObject:Void(source:FlxSprite, dest:FlxSprite, speed:Int = 60, maxTime:Int = 0)
Local a:Float = AngleBetween(source, dest)
If (maxTime > 0) Then
Local d:int = DistanceBetween(source, dest)
'// We know how many pixels we need to move, but how fast?
speed = d / (maxTime / 1000)
Endif
source.velocity.x = Cosr(a) * speed
source.velocity.y = Sinr(a) * speed
End Function
'/**
'* Sets the x/y acceleration on the source FlxSprite so it will move towards the destination FlxSprite at the speed given (in pixels per second)<br>
'* You must give a maximum speed value, beyond which the FlxSprite won't go any faster.<br>
'* If you don't need acceleration look at moveTowardsObject() instead.
'*
'* @param source The FlxSprite on which the acceleration will be set
'* @param dest The FlxSprite where the source object will move towards
'* @param speed The speed it will accelerate in pixels per second
'* @param xSpeedMax The maximum speed in pixels per second in which the sprite can move horizontally
'* @param ySpeedMax The maximum speed in pixels per second in which the sprite can move vertically
'*/
Function AccelerateTowardsObject:Void(source:FlxSprite, dest:FlxSprite, speed:Int, xSpeedMax:Int, ySpeedMax:Int)
Local a:Float = AngleBetween(source, dest)
source.velocity.x = 0
source.velocity.y = 0
source.acceleration.x = Cosr(a) * speed
source.acceleration.y = Sinr(a) * speed
source.maxVelocity.x = xSpeedMax
source.maxVelocity.y = ySpeedMax
End Function
'/**
'* Move the given FlxSprite towards the mouse pointer coordinates at a steady velocity
'* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.<br>
'* Timings are approximate due to the way Flash timers work, and irrespective of SWF frame rate. Allow for a variance of +- 50ms.<br>
'* The source object doesn't stop moving automatically should it ever reach the destination coordinates.<br>
'*
'* @param source The FlxSprite to move
'* @param speed The speed it will move, in pixels per second (default is 60 pixels/sec)
'* @param maxTime Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the source will arrive at destination in the given number of ms
'*/
Function MoveTowardsMouse:Void(source:FlxSprite, speed:Int = 60, maxTime:Int = 0)
Local a:Float = AngleBetweenMouse(source)
If (maxTime > 0) Then
Local d:int = DistanceToMouse(source)
'// We know how many pixels we need to move, but how fast?
speed = d / (maxTime / 1000)
Endif
source.velocity.x = Cosr(a) * speed
source.velocity.y = Sinr(a) * speed
End Function
'/**
'* Sets the x/y acceleration on the source FlxSprite so it will move towards the mouse coordinates at the speed given (in pixels per second)<br>
'* You must give a maximum speed value, beyond which the FlxSprite won't go any faster.<br>
'* If you don't need acceleration look at moveTowardsMouse() instead.
'*
'* @param source The FlxSprite on which the acceleration will be set
'* @param speed The speed it will accelerate in pixels per second
'* @param xSpeedMax The maximum speed in pixels per second in which the sprite can move horizontally
'* @param ySpeedMax The maximum speed in pixels per second in which the sprite can move vertically
'*/
Function AccelerateTowardsMouse:Void(source:FlxSprite, speed:Int, xSpeedMax:int, ySpeedMax:int)
Local a:Float = AngleBetweenMouse(source)
source.velocity.x = 0
source.velocity.y = 0
source.acceleration.x = Cosr(a) * speed
source.acceleration.y = Sinr(a) * speed
source.maxVelocity.x = xSpeedMax
source.maxVelocity.y = ySpeedMax
End Function
'/**
'* Sets the x/y velocity on the source FlxSprite so it will move towards the target coordinates at the speed given (in pixels per second)<br>
'* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.<br>
'* Timings are approximate due to the way Flash timers work, and irrespective of SWF frame rate. Allow for a variance of +- 50ms.<br>
'* The source object doesn't stop moving automatically should it ever reach the destination coordinates.<br>
'*
'* @param source The FlxSprite to move
'* @param target The FlxPoint coordinates to move the source FlxSprite towards
'* @param speed The speed it will move, in pixels per second (default is 60 pixels/sec)
'* @param maxTime Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the source will arrive at destination in the given number of ms
'*/
Function MoveTowardsPoint:Void(source:FlxSprite, target:FlxPoint, speed:Int = 60, maxTime:Int = 0)
Local a:Float = AngleBetweenPoint(source, target)
If (maxTime > 0) Then
Local d:Int = DistanceToPoint(source, target)
'// We know how many pixels we need to move, but how fast?
speed = d / (maxTime / 1000)
Endif
source.velocity.x = Cosr(a) * speed
source.velocity.y = Sinr(a) * speed
End Function
'/**
'* Sets the x/y acceleration on the source FlxSprite so it will move towards the target coordinates at the speed given (in pixels per second)<br>
'* You must give a maximum speed value, beyond which the FlxSprite won't go any faster.<br>
'* If you don't need acceleration look at moveTowardsPoint() instead.
'*
'* @param source The FlxSprite on which the acceleration will be set
'* @param target The FlxPoint coordinates to move the source FlxSprite towards
'* @param speed The speed it will accelerate in pixels per second
'* @param xSpeedMax The maximum speed in pixels per second in which the sprite can move horizontally
'* @param ySpeedMax The maximum speed in pixels per second in which the sprite can move vertically
'*/
Function AccelerateTowardsPoint:Void(source:FlxSprite, target:FlxPoint, speed:Int, xSpeedMax:Int, ySpeedMax:Int)
Local a:Float = AngleBetweenPoint(source, target)
source.velocity.x = 0
source.velocity.y = 0
source.acceleration.x = Cosr(a) * speed
source.acceleration.y = Sinr(a) * speed
source.maxVelocity.x = xSpeedMax
source.maxVelocity.y = ySpeedMax
End Function
'/**
'* Find the distance (in pixels, rounded) between two FlxSprites, taking their origin into account
'*
'* @param a The first FlxSprite
'* @param b The second FlxSprite
'* @return int Distance (in pixels)
'*/
Function DistanceBetween:Int(a:FlxSprite, b:FlxSprite)
Local dx:Float = (a.x + a.origin.x) - (b.x + b.origin.x)
Local dy:Float = (a.y + a.origin.y) - (b.y + b.origin.y)
Return Int(FlxMath.VectorLength(dx, dy))
End Function
'/**
'* Find the distance (in pixels, rounded) from an FlxSprite to the given FlxPoint, taking the source origin into account
'*
'* @param a The first FlxSprite
'* @param target The FlxPoint
'* @return int Distance (in pixels)
'*/
Function DistanceToPoint:Int(a:FlxSprite, target:FlxPoint)
Local dx:Float = (a.x + a.origin.x) - (target.x)
Local dy:Float = (a.y + a.origin.y) - (target.y)
Return Int(FlxMath.VectorLength(dx, dy))
End Function
'/**
'* Find the distance (in pixels, rounded) from the Object x/y And the mouse x/y
'*
'* @param a The FlxSprite to test against
'* @return int The distance between the given sprite and the mouse coordinates
'*/
Function DistanceToMouse:Int(a:FlxSprite)
Local dx:Float = (a.x + a.origin.x) - FlxG.Mouse.screenX
Local dy:Float = (a.y + a.origin.y) - FlxG.Mouse.screenY
Return Int(FlxMath.VectorLength(dx, dy))
End Function
'/**
'* Find the angle (in radians) between an FlxSprite and an FlxPoint. The source sprite takes its x/y and origin into account.
'* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
'*
'* @param a The FlxSprite to test from
'* @param target The FlxPoint to angle the FlxSprite towards
'* @param asDegrees If you need the value in degrees instead of radians, set to true
'*
'* @return Number The angle (in radians unless asDegrees is true)
'*/
Function AngleBetweenPoint:Float(a:FlxSprite, target:FlxPoint, asDegrees:Bool = False)
Local dx:Float = (target.x) - (a.x + a.origin.x)
Local dy:Float = (target.y) - (a.y + a.origin.y)
If (asDegrees) Then
Return FlxMath.AsDegrees(ATan2r(dy, dx))
Else
Return ATan2r(dy, dx)
Endif
End Function
'/**
'* Find the angle (in radians) between an FlxPoint and an FlxPoint. The source sprite takes its x/y and origin into account.
'* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
'*
'* @param a The FlxSprite to test from
'* @param target The FlxPoint to angle the FlxSprite towards
'* @param asDegrees If you need the value in degrees instead of radians, set to true
'*
'* @return Number The angle (in radians unless asDegrees is true)
'*/
Function AngleBetweenPoints:Float(a:FlxPoint, target:FlxPoint, asDegrees:Bool = False)
Local dx:Float = target.x - a.x
Local dy:Float = target.y - a.y
If (asDegrees) Then
Return FlxMath.AsDegrees(ATan2r(dy, dx))
Else
Return ATan2r(dy, dx)
Endif
End Function
'/**
'* Find the angle (in radians) between the two FlxSprite, taking their x/y and origin into account.
'* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
'*
'* @param a The FlxSprite to test from
'* @param b The FlxSprite to test to
'* @param asDegrees If you need the value in degrees instead of radians, set to true
'*
'* @return Number The angle (in radians unless asDegrees is true)
'*/
Function AngleBetween:Float(a:FlxSprite, b:FlxSprite, asDegrees:Bool = False)
Local dx:Float = (b.x + b.origin.x) - (a.x + a.origin.x)
Local dy:Float = (b.y + b.origin.y) - (a.y + a.origin.y)
If (asDegrees) Then
Return FlxMath.AsDegrees(ATan2r(dy, dx))
Else
Return ATan2r(dy, dx)
Endif
End Function
'/**
'* Given the angle and speed calculate the velocity and return it as an FlxPoint
'*
'* @param angle The angle (in degrees) calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
'* @param speed The speed it will move, in pixels per second sq
'*
'* @return An FlxPoint where FlxPoint.x contains the velocity x value and FlxPoint.y contains the velocity y value
'*/
Function VelocityFromAngle:FlxPoint(angle:Float, speed:Int)
Local result:FlxPoint = New FlxPoint()
result.x = Cos(angle) * speed
result.y = Sin(angle) * speed
Return result
End Function
'/**
'* Given the FlxSprite and speed calculate the velocity and return it as an FlxPoint based on the direction the sprite is facing
'*
'* @param parent The FlxSprite to get the facing value from
'* @param speed The speed it will move, in pixels per second sq
'*
'* @return An FlxPoint where FlxPoint.x contains the velocity x value and FlxPoint.y contains the velocity y value
'*/
Function VelocityFromFacing:FlxPoint(parent:FlxSprite, speed:Int)
Local a:Float
If (parent.Facing = FlxObject.LEFT) Then
a = 180
Else If (parent.Facing = FlxObject.RIGHT) Then
a = 0
Else If (parent.Facing = FlxObject.UP) Then
a = -90
Else If (parent.Facing = FlxObject.DOWN) Then
a = 90
Endif
Local result:FlxPoint = New FlxPoint()
result.x = Cos(a) * speed
result.y = Sin(a) * speed
Return result
End Function
'/**
'* Find the angle (in radians) between an FlxSprite and the mouse, taking their x/y and origin into account.
'* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
'*
'* @param a The FlxObject to test from
'* @param asDegrees If you need the value in degrees instead of radians, set to true
'*
'* @return Number The angle (in radians unless asDegrees is true)
'*/
Function AngleBetweenMouse:Float(a:FlxSprite, asDegrees:Bool = False)
'// In order to get the angle between the object and mouse, we need the objects screen coordinates (rather than world coordinates)
Local p:FlxPoint = a.GetScreenXY()
Local dx:Float = FlxG.Mouse.screenX - p.x
Local dy:Float = FlxG.Mouse.screenY - p.y
If (asDegrees) Then
Return FlxMath.AsDegrees(ATan2r(dy, dx))
Else
Return ATan2r(dy, dx)
Endif
End Function
End Class