-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
350 lines (313 loc) · 11.3 KB
/
main.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Some Strange Game
scene.setBackgroundImage(assets.image`backgroundImage`)
scene.setTileMapLevel(assets.tilemap`title-map`)
game.splash("Some Strange Game")
console.log("Begin;")
let gameMusic = "C:2 D:2 E:2 F:2 G:8 G:2 F:2 E:2 D:2 E:4 D:4 C:4 C:2 D:2 E:2 F:2 G:2 G:2 F:2 E:2 D:2 E:4 D:4 C:4"
let gameMelody = "C5 B A G E G E G "
let musicOption = 1
let level = 1
let levelShake = true
const levelAmount = 11
/* used to switch levels */
function switchLevel(option: number) {
if (option <= levelAmount) {
switch (option) {
case -1:
scene.setTileMapLevel(assets.tilemap`testing-map`)
break
case 0:
scene.setTileMapLevel(assets.tilemap`title-map`)
break
case 1:
scene.setTileMapLevel(assets.tilemap`level-1-map`)
break
case 2:
scene.setTileMapLevel(assets.tilemap`level-2-map`)
break
case 3:
scene.setTileMapLevel(assets.tilemap`level-3-map`)
break
case 4:
scene.setTileMapLevel(assets.tilemap`level-4-map`)
break
case 5:
scene.setTileMapLevel(assets.tilemap`level-5-map`)
break
case 6:
scene.setTileMapLevel(assets.tilemap`level-6-map`)
break
case 7:
scene.setTileMapLevel(assets.tilemap`level-7-map`)
break
case 8:
scene.setTileMapLevel(assets.tilemap`level-8-map`)
break
case 9:
scene.setTileMapLevel(assets.tilemap`level-9-map`)
break
case 10:
scene.setTileMapLevel(assets.tilemap`level-10-map`)
break
case 11:
scene.setTileMapLevel(assets.tilemap`level-11-map`)
break
}
} else {
console.log("Number wrong;")
}
}
function reroll() {
spriteSetRandPos(apple)
spriteSetRandPos(enemy)
spriteSetRandPos(projectileAdd)
spriteSetRandPos(emerald)
}
// Setup
// Music Setup
forever(() => {
if (musicOption == 1) {
music.playSoundUntilDone(gameMusic)
} else if (musicOption == 2) {
music.playMelody(gameMelody, 120)
}
})
console.log("Music started;")
let originalMusicOption = musicOption
function pauseMusic() {
originalMusicOption = musicOption
music.stopAllSounds()
musicOption = 0
}
function resumeMusic() {
musicOption = originalMusicOption
}
const imageKind = SpriteKind.create()
// Sprite Creation
/* used to set a random position for sprites */
function spriteSetRandPos(sprite: Sprite) {
let randomLocationX = Math.floor(Math.randomRange(0, tiles.tilemapRows()) * 16)
let randomLocationY = Math.floor(Math.randomRange(0, tiles.tilemapColumns()) * 16)
sprite.setPosition(randomLocationX, randomLocationY)
// tiles.placeOnRandomTile(sprite, assets.tile`grass`) // seems interesting, but tested and doesn't really follow the `grass` rule
}
// Player Setup
let player = sprites.create(assets.image`player`, SpriteKind.Player)
// controller.moveSprite(player) // setup controller
info.setLife(3) // set lives
scene.cameraFollowSprite(player) // camera follow player
scene.setTileMapLevel(assets.tilemap`level-1-map`)
let projectilesLeft = 10
game.onUpdate(() => {
if (!menuIsVisible) {
player.x += controller.dx() /* 1.1 */
player.y += controller.dy() /* 1.1 */
}
})
// Enemy Setup
let enemy = sprites.create(assets.image`enemy`, SpriteKind.Enemy)
spriteSetRandPos(enemy)
let enemyLives = 5
let enemySpeed = 50
forever(() => {
if (!menuIsVisible) {
enemy.follow(player, enemySpeed, 50)
} else {
enemy.follow(null, 0, 0)
}
})
// Apple Setup
let apple = sprites.create(assets.image`apple`, SpriteKind.Food)
spriteSetRandPos(apple)
// +1 Projectile Setup
let projectileAddKind = SpriteKind.create()
let projectileAdd = sprites.create(assets.image`projectile-add`, projectileAddKind)
spriteSetRandPos(projectileAdd)
// Emerald Setup
let emeraldKind = SpriteKind.create()
let emerald = sprites.create(assets.image`emerald`, emeraldKind)
game.onUpdateInterval(1000, () => {
animation.runImageAnimation(emerald, assets.animation`anim-emerald-shine`, 200, false)
})
spriteSetRandPos(emerald)
// Menu
// controller.B.onEvent(ControllerButtonEvent.Pressed, menu)
let menuIsVisible = false
controller.menu.onEvent(ControllerButtonEvent.Pressed, menu)
function menu() {
if (!(menuIsVisible)) {
menuIsVisible = true
pauseMusic()
controller.moveSprite(null)
const menuSprite = miniMenu.createMenuFromArray([
miniMenu.createMenuItem("Music", assets.image`icon-music`),
miniMenu.createMenuItem("Shake", assets.image`icon-none`),
miniMenu.createMenuItem("Reroll", assets.image`icon-reroll`),
miniMenu.createMenuItem("Debug", assets.image`icon-information`),
miniMenu.createMenuItem("Close", assets.image`icon-close`),
])
menuSprite.setTitle("Game Menu")
menuSprite.setDimensions((scene.screenWidth() - 30), (scene.screenHeight() - 30))
menuSprite.setPosition(scene.cameraProperty(CameraProperty.Left) + 15, scene.cameraProperty(CameraProperty.Top) + 15)
scene.cameraFollowSprite(null)
// menuSprite.setMenuStyleProperty(miniMenu.MenuStyleProperty.Padding, 4)
// menuSprite.setMenuStyleProperty(miniMenu.MenuStyleProperty.Border, 1)
// menuSprite.setMenuStyleProperty(miniMenu.MenuStyleProperty.BorderColor, 2)
// menuSprite.setMenuStyleProperty(miniMenu.MenuStyleProperty.Rows, 2)
// menuSprite.setMenuStyleProperty(miniMenu.MenuStyleProperty.Columns, 2)
// menuSprite.onSelectionChanged(function (_selection, _selectedIndex) {
// // music.ringTone(Note.E)
// music.tonePlayable(Note.E, BeatFraction.Quarter)
// })
menuSprite.onButtonPressed(controller.A, function (selection, _selectedIndex) {
// music.ringTone(Note.G)
// music.tonePlayable(Note.G, BeatFraction.Quarter)
switch (selection) {
default: {
break
}
case "Music": {
let musicOptionT = game.askForNumber("Choose 1 or 2", 1)
if (musicOptionT == 1) {
music.stopAllSounds()
musicOption = 1
} else if (musicOptionT == 2) {
music.stopAllSounds()
musicOption = 2
} else {
console.log("Error: Number wrong;")
game.ask("Error: Number wrong")
}
break
}
case "Shake": {
levelShake = game.ask("Would you like to shake on level switch?")
break
}
case "Reroll": {
if (game.ask("Are you sure you want to reroll this level?"))
reroll()
break
}
case "Debug": {
info.setLife(9999)
break
}
}
menuSprite.close()
menuIsVisible = false
resumeMusic()
scene.cameraFollowSprite(player)
// controller.moveSprite(player)
})
}
}
// Secret Menu
controller.combos.attachCombo("ududlrau+a", () => {
let option = game.askForNumber("Level Selector. Choose a level: ", 2)
if (option <= levelAmount) {
switchLevel(option)
level = option
} else {
console.log("Error: Number wrong;")
game.ask("Error: Number wrong")
}
})
// Background Loop
forever(() => {
if (enemyLives == 0) {
enemy.destroy()
}
info.setScore(projectilesLeft)
})
// Main Loop
// Projectile Shooting
controller.A.onEvent(ControllerButtonEvent.Pressed, () => {
if (!menuIsVisible) {
if (projectilesLeft > 0) {
// record direction moving
let velocityX = controller.dx() * 100
let velocityY = controller.dy() * 100
// if not moving, set the directionX and directionY to shoot right
if (velocityX == 0 && velocityY == 0) {
velocityX = 100
velocityY = 0
}
// create projectile in direction moving (or just right)
let projectile = sprites.createProjectileFromSprite(assets.image`projectile`, player, velocityX, velocityY)
projectilesLeft--
console.log("Projectile shot;")
} else {
music.playSound("C:1")
console.log("No projectiles left;")
}
}
})
// Player Uses Food
sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, (playerItem: Sprite, foodItem: Sprite) => {
console.log("food eaten by player;")
playerItem.sayText("Tasty!", 500, true)
music.playSound("C:1 G:1")
animation.runImageAnimation(playerItem, assets.animation`anim-player-eating`, 200, false) // eating animation
info.changeLifeBy(+1) // add 1 life
spriteSetRandPos(foodItem) // randpos food
})
// Enemy Hurts Player
sprites.onOverlap(SpriteKind.Enemy, SpriteKind.Player, (enemyItem: Sprite, playerItem: Sprite) => {
console.log("player hurt by enemy;")
playerItem.sayText("Ouch!", 500, true)
music.playSound("C:1 C:1")
info.changeLifeBy(-1) // remove 1 life
spriteSetRandPos(enemyItem) // randpos enemy
})
// Player (projectile) Hurts Enemy
sprites.onOverlap(SpriteKind.Projectile, SpriteKind.Enemy, (projectileItem: Sprite, enemyItem: Sprite) => {
console.log("enemy hurt by projectile;")
enemyItem.sayText("Ouch!", 500, true)
music.playSound("C:1 G:1")
spriteSetRandPos(enemyItem)
enemyLives--
})
// Player Uses ProjectileAdd
sprites.onOverlap(SpriteKind.Player, projectileAddKind, (playerItem: Sprite, projectileAddItem: Sprite) => {
console.log("projectileAdd picked up by player;")
playerItem.sayText("Yeah!", 500, true)
music.playSound("C:1 G:1")
animation.runImageAnimation(playerItem, assets.animation`anim-player-projectileadd`, 200, false)
spriteSetRandPos(projectileAddItem)
projectilesLeft++
})
// Emerald Overlap
sprites.onOverlap(SpriteKind.Player, emeraldKind, (playerItem: Sprite, emeraldItem: Sprite) => {
console.log("Next level;")
// a little camera shake (if turned on)
if (levelShake) {
scene.cameraShake(4, 500)
}
// go to next level
if (level < levelAmount) {
// set random locations
reroll()
enemySpeed += 5
// Next Level
let upNext = level + 1
// let nextLevel = assets.tilemap(`level-${upNext}-map`)
// let nextLevel = assets.image`level-${upNext}-map`
// scene.setTileMapLevel(nextLevel)
switchLevel(upNext)
level++
} else {
console.log("Win;")
// destroy all sprites
apple.destroy()
enemy.destroy()
projectileAdd.destroy()
emerald.destroy()
// set ending tilemap
scene.setTileMapLevel(assets.tilemap`title-map`)
// add confetti effect
effects.confetti.startScreenEffect(120000)
// credits
player.sayText("Thanks for playing!", 3000, true)
}
})