forked from iamkun/tower_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.js
54 lines (51 loc) · 1.48 KB
/
hook.js
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
import { getSwingBlockVelocity } from './utils'
import * as constant from './constant'
export const hookAction = (instance, engine, time) => {
const ropeHeight = engine.getVariable(constant.ropeHeight)
if (!instance.ready) {
instance.x = engine.width / 2
instance.y = ropeHeight * -1.5
instance.ready = true
}
engine.getTimeMovement(
constant.hookUpMovement,
[[instance.y, instance.y - ropeHeight]],
(value) => {
instance.y = value
},
{
after: () => {
instance.y = ropeHeight * -1.5
}
}
)
engine.getTimeMovement(
constant.hookDownMovement,
[[instance.y, instance.y + ropeHeight]],
(value) => {
instance.y = value
},
{
name: 'hook'
}
)
const initialAngle = engine.getVariable(constant.initialAngle)
instance.angle = initialAngle *
getSwingBlockVelocity(engine, time)
instance.weightX = instance.x +
(Math.sin(instance.angle) * ropeHeight)
instance.weightY = instance.y +
(Math.cos(instance.angle) * ropeHeight)
}
export const hookPainter = (instance, engine) => {
const { ctx } = engine
const ropeHeight = engine.getVariable(constant.ropeHeight)
const ropeWidth = ropeHeight * 0.1
const hook = engine.getImg('hook')
ctx.save()
ctx.translate(instance.x, instance.y)
ctx.rotate((Math.PI * 2) - instance.angle)
ctx.translate(-instance.x, -instance.y)
engine.ctx.drawImage(hook, instance.x - (ropeWidth / 2), instance.y, ropeWidth, ropeHeight + 5)
ctx.restore()
}