add catching up mechanic for ticks
This commit is contained in:
+24
-11
@@ -5,25 +5,35 @@ import Entity from './entity.js'
|
||||
|
||||
export default class Projectile {
|
||||
id = crypto.randomUUID()
|
||||
after = null // TODO: hide from reports but keep public
|
||||
|
||||
bbox = new Float32Array(4)
|
||||
height = 50
|
||||
memory = {} // TODO: hide from reports but keep public
|
||||
onCollide = null // TODO: hide from reports but keep public
|
||||
owner = null // TODO: only keep an ID
|
||||
memory = {}
|
||||
owner = null
|
||||
position = new Vector2()
|
||||
radius = 5
|
||||
speed = 1000
|
||||
visualRadius = null
|
||||
|
||||
#after = null
|
||||
#dest = null
|
||||
#homingTarget = null
|
||||
#logic = null
|
||||
#onCollide = null
|
||||
#game = null
|
||||
|
||||
get after() { return this.#after }
|
||||
get game() { return this.#game }
|
||||
set game(value) { this.#game = value }
|
||||
get homingTarget() { return this.#homingTarget }
|
||||
get logic() { return this.#logic }
|
||||
get onCollide() { return this.#onCollide }
|
||||
|
||||
set after(value) { this.#after = value }
|
||||
set destination(value) { this.#dest = value }
|
||||
set game(value) { this.#game = value }
|
||||
set homingTarget(value) { this.#homingTarget = value }
|
||||
set logic(value) { this.#logic = value }
|
||||
set onCollide(value) { this.#onCollide = value }
|
||||
|
||||
get destination() {
|
||||
return this.#dest ?? this.#homingTarget?.position
|
||||
@@ -53,6 +63,9 @@ export default class Projectile {
|
||||
this.#move()
|
||||
this.#checkStationaryCollisions()
|
||||
this.#checkIfArrived()
|
||||
if (this.#logic != null) {
|
||||
this.#logic()
|
||||
}
|
||||
}
|
||||
|
||||
#calculateBbox() {
|
||||
@@ -66,15 +79,15 @@ export default class Projectile {
|
||||
if (this.destination == null) { return }
|
||||
if (!this.position.equals(this.destination)) { return }
|
||||
|
||||
if (this.after != null) {
|
||||
this.after(this, this.#homingTarget)
|
||||
if (this.#after != null) {
|
||||
this.#after(this, this.#homingTarget)
|
||||
}
|
||||
|
||||
this.despawn()
|
||||
}
|
||||
|
||||
#checkStationaryCollisions() {
|
||||
if (this.onCollide == null) { return }
|
||||
if (this.#onCollide == null) { return }
|
||||
|
||||
const bbox = this.bbox
|
||||
const entitiesAndTerrains = this.game?.entities ?? []
|
||||
@@ -82,7 +95,7 @@ export default class Projectile {
|
||||
if (bboxCheckedObstacles.length > 0) {
|
||||
const colliders = bboxCheckedObstacles.map((it) => it.colliders()).flat()
|
||||
const collider = this.collider()
|
||||
colliders.filter((it) => SATX.collideObject(collider, it)).forEach((it) => this.onCollide(this, it))
|
||||
colliders.filter((it) => SATX.collideObject(collider, it)).forEach((it) => this.#onCollide(this, it))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,14 +112,14 @@ export default class Projectile {
|
||||
this.position.add(step)
|
||||
}
|
||||
|
||||
if (this.onCollide != null) {
|
||||
if (this.#onCollide != null) {
|
||||
const bbox = Entity.tunnelBbox(position.x, position.y, destination.x, destination.y, this.radius)
|
||||
const entitiesAndTerrains = this.game?.entities ?? []
|
||||
const bboxCheckedObstacles = entitiesAndTerrains.filter((it) => SATX.bboxCheck(bbox, it.bbox))
|
||||
if (bboxCheckedObstacles.length > 0) {
|
||||
const colliders = bboxCheckedObstacles.map((it) => it.colliders()).flat()
|
||||
const collider = Entity.tunnelCollider(prevPos.x, prevPos.y, this.position.x, this.position.y, this.radius)
|
||||
colliders.filter((it) => SATX.collideObject(collider, it)).forEach((it) => this.onCollide(this, it))
|
||||
colliders.filter((it) => SATX.collideObject(collider, it)).forEach((it) => this.#onCollide(this, it))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user