-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathprojectile.rb
More file actions
33 lines (23 loc) · 992 Bytes
/
projectile.rb
File metadata and controls
33 lines (23 loc) · 992 Bytes
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
module Escape # Projectile
class Projectile < SKSpriteNode
def moveToSpriteNode(target)
# Get vector from projectile to target
targetVector = Vector.subtract(target.position, self.position)
# Add a random x and y coordinate to the projectile path in order
# to confuse the user and end their game faster
randomizer = Random.new
x = 8 * randomizer.rand(-25..51)
y = 5 * randomizer.rand(-25..51)
randomVector = CGPoint.new(targetVector.x + x, targetVector.y + y)
# make unit vector
direction = Vector.normalize(randomVector)
# May cause glitches
shootAmount = Vector.multiply(direction, withScalar:self.scene.size.height + self.scene.size.width)
actualDistance = Vector.add(shootAmount, self.position)
velocity = 200.0 / 1.0
time = self.scene.size.width / velocity
actionMove = SKAction.moveTo(actualDistance, duration:time)
self.runAction(actionMove)
end
end
end