Skip to content

Commit

Permalink
add started event and remove optimistic mode
Browse files Browse the repository at this point in the history
  • Loading branch information
okwme committed Apr 11, 2024
1 parent cb4f405 commit 0a248b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
54 changes: 24 additions & 30 deletions src/anybody.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class Anybody extends EventEmitter {
freeze: false,
stopEvery: 0,
util: false,
optimistic: false,
paused: true,
globalStyle: 'default', // 'default', 'psycho'
timer: GAME_LENGTH * FPS,
Expand Down Expand Up @@ -92,6 +91,7 @@ export class Anybody extends EventEmitter {
this.firstFrame = true
this.loaded = false
this.showPlayAgain = false
this.hasStarted = false
}

// run once at initilization
Expand Down Expand Up @@ -336,38 +336,32 @@ export class Anybody extends EventEmitter {
// this.finished = true
// this.setPause(true)
this.calculateBodyFinal()
if (!this.optimistic) {
const missileInits = []
if (this.mode == 'game') {
let missileIndex = 0
for (
let i = this.alreadyRun;
i < this.alreadyRun + this.stopEvery;
i++
) {
if (this.missileInits[missileIndex]?.step == i) {
const missile = this.missileInits[missileIndex]
missileInits.push([
missile.x,
missile.y,
missile.vx,
missile.vy,
missile.radius
])
missileIndex++
} else {
missileInits.push([0, 0, 0, 0, 0])
}
const missileInits = []
if (this.mode == 'game') {
let missileIndex = 0
for (let i = this.alreadyRun; i < this.alreadyRun + this.stopEvery; i++) {
if (this.missileInits[missileIndex]?.step == i) {
const missile = this.missileInits[missileIndex]
missileInits.push([
missile.x,
missile.y,
missile.vx,
missile.vy,
missile.radius
])
missileIndex++
} else {
missileInits.push([0, 0, 0, 0, 0])
}
missileInits.push([0, 0, 0, 0, 0])
}
results = {
missiles: JSON.parse(JSON.stringify(missileInits)),
bodyInits: JSON.parse(JSON.stringify(this.bodyInits)),
bodyFinal: JSON.parse(JSON.stringify(this.bodyFinal))
}
this.emit('finished', results)
missileInits.push([0, 0, 0, 0, 0])
}
results = {
missiles: JSON.parse(JSON.stringify(missileInits)),
bodyInits: JSON.parse(JSON.stringify(this.bodyInits)),
bodyFinal: JSON.parse(JSON.stringify(this.bodyFinal))
}
this.emit('finished', results)
this.bodyInits = JSON.parse(JSON.stringify(this.bodyFinal))
this.alreadyRun = this.frames
this.missileInits = this.processMissileInits(this.missiles).map((m) => {
Expand Down
7 changes: 4 additions & 3 deletions src/visuals.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ export const Visuals = {
this.setPause(true)
return
}
if (!this.firstFrame && !this.hasStarted) {
this.hasStarted = true
this.started()
}

this.frames++
const results = this.step(this.bodies, this.missiles)
Expand Down Expand Up @@ -269,9 +273,6 @@ export const Visuals = {
if (didNotJustPause) {
this.finish()
}
// if (this.optimistic) {
// this.started()
// }
} else {
this.justPaused = false
}
Expand Down

0 comments on commit 0a248b2

Please sign in to comment.