-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
159 lines (159 loc) · 5.5 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
namespace SpriteKind {
export const Bar = SpriteKind.create()
export const Overlapper = SpriteKind.create()
export const Tile = SpriteKind.create()
}
function setup_bars () {
for (let index = 0; index <= 3; index++) {
sprite_bar = sprites.create(assets.image`bar`, SpriteKind.Bar)
sprite_bar.setFlag(SpriteFlag.Ghost, true)
sprite_bar.top = 0
sprite_bar.z = 1
sprite_bar.left = scene.screenWidth() / 2 - 30 + 15 * index
}
sprites_overlappers = []
overlapper_images = [
assets.image`left_overlap`,
assets.image`up_overlap`,
assets.image`right_overlap`,
assets.image`down_overlap`
]
overlapper_images_pressed = [
assets.image`left_overlap_pressed`,
assets.image`up_overlap_pressed`,
assets.image`right_overlap_pressed`,
assets.image`down_overlap_pressed`
]
for (let image3 of overlapper_images) {
create_overlap(image3)
}
}
controller.up.onEvent(ControllerButtonEvent.Pressed, function () {
if (MusicalImages.is_playing()) {
press_overlap(1, true)
}
})
function press_overlap (index: number, pressed: boolean) {
if (pressed) {
sprites_overlappers[index].setImage(overlapper_images_pressed[index])
} else {
sprites_overlappers[index].setImage(overlapper_images[index])
return
}
overlapping = get_overlapping_tile(sprites_overlappers[index])
if (!(overlapping)) {
scene.cameraShake(2, 100)
info.changeLifeBy(-1)
return
}
sprites.setDataBoolean(overlapping, "played", true)
overlapping.destroy()
info.changeScoreBy(10 - Math.constrain(Math.abs(sprites_overlappers[index].y - overlapping.bottom), 0, 9))
timer.background(function () {
music.playTone(sprites.readDataNumber(overlapping, "frequency"), sprites.readDataNumber(overlapping, "duration"))
})
}
function handle_note (channel: number, note: number, frequency: number, duration: number) {
if (channel == 0) {
image2 = image.create(16, Math.max(Math.round(duration / 20 * 0.75), 1))
image2.fill(1)
sprite_tile = sprites.create(image2, SpriteKind.Tile)
sprite_tile.bottom = 1
sprite_tile.x = sprites_overlappers[Math.floor((note + 9) % 12 / 2) % 4].x
sprite_tile.vy = 50
sprite_tile.setFlag(SpriteFlag.AutoDestroy, true)
sprites.setDataNumber(sprite_tile, "frequency", frequency)
sprites.setDataNumber(sprite_tile, "duration", duration)
sprites.setDataBoolean(sprite_tile, "played", false)
} else {
timer.after((scene.screenHeight() - 16) / 50 * 1000, function () {
timer.background(function () {
music.playTone(frequency, duration)
})
})
}
}
sprites.onDestroyed(SpriteKind.Tile, function (sprite) {
if (MusicalImages.is_playing()) {
if (!(sprites.readDataBoolean(sprite, "played"))) {
scene.cameraShake(2, 100)
info.changeLifeBy(-1)
}
}
})
function prepare_background () {
scene.setBackgroundColor(13)
}
controller.down.onEvent(ControllerButtonEvent.Released, function () {
if (MusicalImages.is_playing()) {
press_overlap(3, false)
}
})
controller.left.onEvent(ControllerButtonEvent.Pressed, function () {
if (MusicalImages.is_playing()) {
press_overlap(0, true)
}
})
controller.right.onEvent(ControllerButtonEvent.Released, function () {
if (MusicalImages.is_playing()) {
press_overlap(2, false)
}
})
controller.left.onEvent(ControllerButtonEvent.Released, function () {
if (MusicalImages.is_playing()) {
press_overlap(0, false)
}
})
controller.right.onEvent(ControllerButtonEvent.Pressed, function () {
if (MusicalImages.is_playing()) {
press_overlap(2, true)
}
})
function create_overlap (image2: Image) {
sprites_overlappers.push(sprites.create(image2, SpriteKind.Overlapper))
sprites_overlappers[sprites_overlappers.length - 1].left = scene.screenWidth() / 2 - 30 + 15 * (sprites_overlappers.length - 1)
sprites_overlappers[sprites_overlappers.length - 1].bottom = scene.screenHeight() - 8
sprites_overlappers[sprites_overlappers.length - 1].z = 1
}
controller.up.onEvent(ControllerButtonEvent.Released, function () {
if (MusicalImages.is_playing()) {
press_overlap(1, false)
}
})
MusicalImages.set_handler(function (channels, notes, frequencys, durations) {
for (let index = 0; index <= channels.length - 1; index++) {
handle_note(channels[index], notes[index], frequencys[index], durations[index])
}
})
controller.down.onEvent(ControllerButtonEvent.Pressed, function () {
if (MusicalImages.is_playing()) {
press_overlap(3, true)
}
})
function play_song (song: any[]) {
prepare_background()
setup_bars()
music.setVolume(20)
MusicalImages.init_musical_image()
MusicalImages.set_queue(song)
info.setScore(0)
info.setLife(100)
MusicalImages.play()
}
function get_overlapping_tile (overlapper: Sprite) {
for (let sprite_tile of sprites.allOfKind(SpriteKind.Tile)) {
if (overlapper.overlapsWith(sprite_tile)) {
return sprite_tile
}
}
return [][0]
}
let sprite_tile: Sprite = null
let image2: Image = null
let overlapping: Sprite = null
let overlapper_images_pressed: Image[] = []
let overlapper_images: Image[] = []
let sprites_overlappers: Sprite[] = []
let sprite_bar: Sprite = null
stats.turnStats(true)
play_song([assets.animation`never_gonna_give_you_up_main`, assets.animation`never_gonna_give_you_up_accompaniment`])