fix laser impact

This commit is contained in:
Adrien MALINGREY 2024-08-27 02:52:04 +02:00
parent 5c62de15a4
commit a5285a10e2

28
app.js
View File

@ -1,6 +1,6 @@
const MAX_LEVEL = 3 const MAX_LEVEL = 3
const DRAW_PERIOD = 0.04 // s const DRAW_PERIOD = 0.04 // s
const UPDATE_PERIOD = 0.04 // s const UPDATE_PERIOD = 0.01 // s
const FLOOR = 400 // px const FLOOR = 400 // px
const FIRST_NOTE = 48 // C2 const FIRST_NOTE = 48 // C2
const LAST_NOTE = 73 // C4 const LAST_NOTE = 73 // C4
@ -148,7 +148,7 @@ class Cannon extends Sprite {
class Note extends Sprite { class Note extends Sprite {
constructor(canvasCtx, note, duration, velocity, sx, sy, width, height, frames, shotAnimationPeriod) { constructor(canvasCtx, note, duration, velocity, sx, sy, width, height, frames, shotAnimationPeriod) {
super(canvasCtx, "note.png", 34 * (note - FIRST_NOTE) + 66, -40, width, height, frames, 1) super(canvasCtx, "note.png", 34 * (note - FIRST_NOTE) + 66, 0, width, height, frames, 1)
this.note = note this.note = note
this.duration = duration this.duration = duration
this.velocity = velocity this.velocity = velocity
@ -462,16 +462,20 @@ function update(time) {
cannonSprites.filter(cannonSprite => cannonSprite.shooting).forEach(cannonSprite => { cannonSprites.filter(cannonSprite => cannonSprite.shooting).forEach(cannonSprite => {
let noteSprite = noteSprites.find(noteSprite => noteSprite.note == cannonSprite.note) let noteSprite = noteSprites.find(noteSprite => noteSprite.note == cannonSprite.note)
if (noteSprite && !noteSprite.shot) { if (noteSprite) {
playNote(noteSprite.note, noteSprite.velocity, time + noteSprite.duration) if (!noteSprite.shot) {
cannonSprite.impactY = noteSprite.y playNote(noteSprite.note, noteSprite.velocity, time + noteSprite.duration)
noteSprite.shot = true cannonSprite.impactY = noteSprite.y
window.setTimeout(() => { noteSprite.shot = true
noteSprites.remove(noteSprite) window.setTimeout(() => {
let explosionSprite = noteSprite.explose() noteSprites.remove(noteSprite)
explosionSprites.push(explosionSprite) let explosionSprite = noteSprite.explose()
explosionSprite.play().then(() => explosionSprites.remove(explosionSprite)) explosionSprites.push(explosionSprite)
}, noteSprite.duration * 1000) explosionSprite.play().then(() => explosionSprites.remove(explosionSprite))
}, noteSprite.duration * 1000)
}
} else {
cannonSprite.impactY = 0
} }
}) })
} }