-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
153 lines (132 loc) · 3.55 KB
/
Copy pathapp.js
File metadata and controls
153 lines (132 loc) · 3.55 KB
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
RFall.init = function () {
RFall.gameSpeed0 = 10 // increment in milliseconds
RFall.gameSpeed = RFall.gameSpeed0 // increment in milliseconds
RFall.elapsedTime = 0 // milliseconds
RFall.winTime = 30000 // milliseconds
RFall.canvasWidth = 500 // px
RFall.canvasHeight = 500 // px
RFall.difficulty = 1
RFall.startingHearts = 2
RFall.hearts = RFall.startingHearts
RFall.paused = false
RFall.playing = false
window.addEventListener('keydown',RFall.onKeyDown,true)
RFall.resources.loadImages( RFall.drawSplash )
}
RFall.newGame = function () {
RFall.mineshaft = new RFall.MineShaft()
RFall.cage = new RFall.Cage()
RFall.player = new RFall.Player()
RFall.rocks = new RFall.Rocks()
RFall.effectsManager = new RFall.EffectsManager()
RFall.elapsedTime = 0
RFall.playing = true
RFall.appStep()
}
RFall.appStep = function () {
if (RFall.paused) {
RFall.drawPaused()
setTimeout("RFall.appStep()", 100)
}
else if (RFall.playing) {
RFall.rocks.collisionDetect()
if (RFall.player.isDead()) {
RFall.loseGame()
}
else {
RFall.rocks.moveRocksDown()
RFall.mineshaft.moveUp()
RFall.draw()
RFall.elapsedTime += RFall.gameSpeed
if (RFall.elapsedTime > RFall.winTime)
RFall.winGame()
else
setTimeout("RFall.appStep()", RFall.gameSpeed )
}
}
}
RFall.winGame = function () {
alert( "You beat level " + RFall.difficulty + "! Hit ok to turn the up heat." )
++RFall.difficulty
RFall.newGame()
}
RFall.loseGame = function () {
RFall.playing = false
RFall.difficulty = 0
RFall.hearts = RFall.startingHearts
RFall.drawSplash()
}
RFall.draw = function () {
var canvas = document.getElementById('gameCanvas')
if (canvas.getContext){
var ctx = canvas.getContext('2d')
ctx.clearRect(0,0,RFall.canvasWidth,RFall.canvasHeight)
ctx.save()
RFall.mineshaft.draw( ctx )
RFall.drawStats( ctx )
RFall.rocks.draw( ctx )
RFall.player.draw( ctx )
RFall.cage.draw( ctx )
ctx.restore()
}
}
RFall.drawPaused = function () {
var canvas = document.getElementById('gameCanvas')
if (canvas.getContext){
var ctx = canvas.getContext('2d')
ctx.save()
ctx.drawImage(RFall.resources.images.paused,0,200)
ctx.restore()
}
}
RFall.drawSplash = function () {
var canvas = document.getElementById('gameCanvas')
if (canvas.getContext){
var ctx = canvas.getContext('2d')
ctx.save()
ctx.drawImage(RFall.resources.images.splash,0,0)
ctx.restore()
}
}
RFall.drawStats = function (ctx) {
timeText = "Time left: " + (RFall.winTime - RFall.elapsedTime).toString()
diffText = "Difficulty: " + RFall.difficulty.toString()
ctx.fillStyle = "rgb(255,255,255)"
ctx.fillRect(0,0,100,32)
ctx.strokeRect(0,0,100,32)
ctx.fillStyle = "rgb(0,0,0)"
ctx.fillText(timeText, 10, 12)
ctx.fillText(diffText, 10, 28)
}
RFall.onKeyDown = function (evt) {
if (RFall.playing) {
if (evt.keyCode == 37) {
RFall.player.move("left") // <-
}
if (evt.keyCode == 39) { // ->
RFall.player.move("right")
}
if (evt.keyCode == 80) { // p
RFall.pause()
}
}
if (!RFall.playing) {
if (evt.keyCode == 78) { // n
RFall.newGame()
}
}
if (RFall.playing) {
if (evt.keyCode == 83) { // s
RFall.effectsManager.start("slowTime")
}
}
}
RFall.pause = function () {
RFall.paused = !RFall.paused
}
RFall.randomMinMax = function ( min, max ) {
return Math.random() * (max - min) + min;
}
RFall.setGameSpeed = function ( newGameSpeed ) {
RFall.gameSpeed = newGameSpeed
}