forked from iamkun/tower_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
177 lines (166 loc) · 5.25 KB
/
utils.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
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
import * as constant from './constant'
export const checkMoveDown = engine =>
(engine.checkTimeMovement(constant.moveDownMovement))
export const getMoveDownValue = (engine, store) => {
const pixelsPerFrame = store ? store.pixelsPerFrame : engine.pixelsPerFrame.bind(engine)
const successCount = engine.getVariable(constant.successCount)
const calHeight = engine.getVariable(constant.blockHeight) * 2
if (successCount <= 4) {
return pixelsPerFrame(calHeight * 1.25)
}
return pixelsPerFrame(calHeight)
}
export const getAngleBase = (engine) => {
const successCount = engine.getVariable(constant.successCount)
const gameScore = engine.getVariable(constant.gameScore)
const { hookAngle } = engine.getVariable(constant.gameUserOption)
if (hookAngle) {
return hookAngle(successCount, gameScore)
}
if (engine.getVariable(constant.hardMode)) {
return 90
}
switch (true) {
case successCount < 10:
return 30
case successCount < 20:
return 60
default:
return 80
}
}
export const getSwingBlockVelocity = (engine, time) => {
const successCount = engine.getVariable(constant.successCount)
const gameScore = engine.getVariable(constant.gameScore)
const { hookSpeed } = engine.getVariable(constant.gameUserOption)
if (hookSpeed) {
return hookSpeed(successCount, gameScore)
}
let hard
switch (true) {
case successCount < 1:
hard = 0
break
case successCount < 10:
hard = 1
break
case successCount < 20:
hard = 0.8
break
case successCount < 30:
hard = 0.7
break
default:
hard = 0.74
break
}
if (engine.getVariable(constant.hardMode)) {
hard = 1.1
}
return Math.sin(time / (200 / hard))
}
export const getLandBlockVelocity = (engine, time) => {
const successCount = engine.getVariable(constant.successCount)
const gameScore = engine.getVariable(constant.gameScore)
const { landBlockSpeed } = engine.getVariable(constant.gameUserOption)
if (landBlockSpeed) {
return landBlockSpeed(successCount, gameScore)
}
const { width } = engine
let hard
switch (true) {
case successCount < 5:
hard = 0
break
case successCount < 13:
hard = 0.001
break
case successCount < 23:
hard = 0.002
break
default:
hard = 0.003
break
}
return Math.cos(time / 200) * hard * width
}
export const getHookStatus = (engine) => {
if (engine.checkTimeMovement(constant.hookDownMovement)) {
return constant.hookDown
}
if (engine.checkTimeMovement(constant.hookUpMovement)) {
return constant.hookUp
}
return constant.hookNormal
}
export const touchEventHandler = (engine) => {
if (!engine.getVariable(constant.gameStartNow)) return
if (engine.debug && engine.paused) {
return
}
if (getHookStatus(engine) !== constant.hookNormal) {
return
}
engine.removeInstance('tutorial')
engine.removeInstance('tutorial-arrow')
const b = engine.getInstance(`block_${engine.getVariable(constant.blockCount)}`)
if (b && b.status === constant.swing) {
engine.setTimeMovement(constant.hookUpMovement, 500)
b.status = constant.beforeDrop
}
}
export const addSuccessCount = (engine) => {
const { setGameSuccess } = engine.getVariable(constant.gameUserOption)
const lastSuccessCount = engine.getVariable(constant.successCount)
const success = lastSuccessCount + 1
engine.setVariable(constant.successCount, success)
if (engine.getVariable(constant.hardMode)) {
engine.setVariable(constant.ropeHeight, engine.height * engine.utils.random(0.35, 0.55))
}
if (setGameSuccess) setGameSuccess(success)
}
export const addFailedCount = (engine) => {
const { setGameFailed } = engine.getVariable(constant.gameUserOption)
const lastFailedCount = engine.getVariable(constant.failedCount)
const failed = lastFailedCount + 1
engine.setVariable(constant.failedCount, failed)
engine.setVariable(constant.perfectCount, 0)
if (setGameFailed) setGameFailed(failed)
if (failed >= 3) {
engine.pauseAudio('bgm')
engine.playAudio('game-over')
engine.setVariable(constant.gameStartNow, false)
}
}
export const addScore = (engine, isPerfect) => {
const { setGameScore, successScore, perfectScore } = engine.getVariable(constant.gameUserOption)
const lastPerfectCount = engine.getVariable(constant.perfectCount, 0)
const lastGameScore = engine.getVariable(constant.gameScore)
const perfect = isPerfect ? lastPerfectCount + 1 : 0
const score = lastGameScore + (successScore || 25) + ((perfectScore || 25) * perfect)
engine.setVariable(constant.gameScore, score)
engine.setVariable(constant.perfectCount, perfect)
if (setGameScore) setGameScore(score)
}
export const drawYellowString = (engine, option) => {
const {
string, size, x, y, textAlign
} = option
const { ctx } = engine
const fontName = 'wenxue'
const fontSize = size
const lineSize = fontSize * 0.1
ctx.save()
ctx.beginPath()
const gradient = ctx.createLinearGradient(0, 0, 0, y)
gradient.addColorStop(0, '#FAD961')
gradient.addColorStop(1, '#F76B1C')
ctx.fillStyle = gradient
ctx.lineWidth = lineSize
ctx.strokeStyle = '#FFF'
ctx.textAlign = textAlign || 'center'
ctx.font = `${fontSize}px ${fontName}`
ctx.strokeText(string, x, y)
ctx.fillText(string, x, y)
ctx.restore()
}