forked from iamkun/tower_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (112 loc) · 3.61 KB
/
index.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
import { Engine, Instance } from 'cooljs'
import { touchEventHandler } from './utils'
import { background } from './background'
import { lineAction, linePainter } from './line'
import { cloudAction, cloudPainter } from './cloud'
import { hookAction, hookPainter } from './hook'
import { tutorialAction, tutorialPainter } from './tutorial'
import * as constant from './constant'
import { startAnimate, endAnimate } from './animateFuncs'
window.TowerGame = (option = {}) => {
const {
width,
height,
canvasId,
soundOn
} = option
const game = new Engine({
canvasId,
highResolution: true,
width,
height,
soundOn
})
const pathGenerator = (path) => `./assets/${path}`
game.addImg('background', pathGenerator('background.png'))
game.addImg('hook', pathGenerator('hook.png'))
game.addImg('blockRope', pathGenerator('block-rope.png'))
game.addImg('block', pathGenerator('block.png'))
game.addImg('block-perfect', pathGenerator('block-perfect.png'))
for (let i = 1; i <= 8; i += 1) {
game.addImg(`c${i}`, pathGenerator(`c${i}.png`))
}
game.addLayer(constant.flightLayer)
for (let i = 1; i <= 7; i += 1) {
game.addImg(`f${i}`, pathGenerator(`f${i}.png`))
}
game.swapLayer(0, 1)
game.addImg('tutorial', pathGenerator('tutorial.png'))
game.addImg('tutorial-arrow', pathGenerator('tutorial-arrow.png'))
game.addImg('heart', pathGenerator('heart.png'))
game.addImg('score', pathGenerator('score.png'))
game.addAudio('drop-perfect', pathGenerator('drop-perfect.mp3'))
game.addAudio('drop', pathGenerator('drop.mp3'))
game.addAudio('game-over', pathGenerator('game-over.mp3'))
game.addAudio('rotate', pathGenerator('rotate.mp3'))
game.addAudio('bgm', pathGenerator('bgm.mp3'))
game.setVariable(constant.blockWidth, game.width * 0.25)
game.setVariable(constant.blockHeight, game.getVariable(constant.blockWidth) * 0.71)
game.setVariable(constant.cloudSize, game.width * 0.3)
game.setVariable(constant.ropeHeight, game.height * 0.4)
game.setVariable(constant.blockCount, 0)
game.setVariable(constant.successCount, 0)
game.setVariable(constant.failedCount, 0)
game.setVariable(constant.gameScore, 0)
game.setVariable(constant.hardMode, false)
game.setVariable(constant.gameUserOption, option)
for (let i = 1; i <= 4; i += 1) {
const cloud = new Instance({
name: `cloud_${i}`,
action: cloudAction,
painter: cloudPainter
})
cloud.index = i
cloud.count = 5 - i
game.addInstance(cloud)
}
const line = new Instance({
name: 'line',
action: lineAction,
painter: linePainter
})
game.addInstance(line)
const hook = new Instance({
name: 'hook',
action: hookAction,
painter: hookPainter
})
game.addInstance(hook)
game.startAnimate = startAnimate
game.endAnimate = endAnimate
game.paintUnderInstance = background
game.addKeyDownListener('enter', () => {
if (game.debug) game.togglePaused()
})
game.touchStartListener = () => {
touchEventHandler(game)
}
game.playBgm = () => {
game.playAudio('bgm', true)
}
game.pauseBgm = () => {
game.pauseAudio('bgm')
}
game.start = () => {
const tutorial = new Instance({
name: 'tutorial',
action: tutorialAction,
painter: tutorialPainter
})
game.addInstance(tutorial)
const tutorialArrow = new Instance({
name: 'tutorial-arrow',
action: tutorialAction,
painter: tutorialPainter
})
game.addInstance(tutorialArrow)
game.setTimeMovement(constant.bgInitMovement, 500)
game.setTimeMovement(constant.tutorialMovement, 500)
game.setVariable(constant.gameStartNow, true)
}
return game
}