inflate ranges by entity radii
This commit is contained in:
+35
-54
@@ -5,65 +5,42 @@ import { Vector2 } from 'three'
|
||||
export default class Projectile {
|
||||
id = crypto.randomUUID()
|
||||
after = null
|
||||
speed = 1000
|
||||
radius = 5
|
||||
owner = null
|
||||
onCollide = null
|
||||
height = 50
|
||||
onCollide = null
|
||||
owner = null
|
||||
position = new Vector2()
|
||||
radius = 5
|
||||
speed = 1000
|
||||
|
||||
#position = new Vector2()
|
||||
#dest = null
|
||||
#homingTarget = null
|
||||
#game = null
|
||||
|
||||
get collider() {
|
||||
return new SAT.Circle(new SAT.Vector(this.x, this.y), this.radius)
|
||||
}
|
||||
|
||||
get homing() {
|
||||
return !!this.#homingTarget
|
||||
}
|
||||
get game() { return this.#game }
|
||||
set game(value) { this.#game = value }
|
||||
set destination(value) { this.#dest = value }
|
||||
set homingTarget(value) { this.#homingTarget = value }
|
||||
|
||||
get destination() {
|
||||
return this.#dest ?? this.#homingTarget?.position
|
||||
}
|
||||
|
||||
set homingTarget(value) {
|
||||
this.#homingTarget = value
|
||||
}
|
||||
|
||||
constructor(options = {}) {
|
||||
Object.entries(options).forEach(([key, value]) => this[key] = value)
|
||||
}
|
||||
|
||||
get game() { return this.#game }
|
||||
get position() { return this.#position }
|
||||
get x() { return this.position.x }
|
||||
get y() { return this.position.y }
|
||||
set game(value) { this.#game = value }
|
||||
set x(value) { this.position.x = value }
|
||||
set y(value) { this.position.y = value }
|
||||
set destination(value) { this.#dest = value }
|
||||
set position(value) { this.#position = value }
|
||||
|
||||
checkCollisions() {
|
||||
(this.game?.entities ?? []).filter((e) => e.id != this.id).forEach((e) => {
|
||||
if (e.id == this.owner?.id) { return }
|
||||
|
||||
if (SATX.collideObject(this.collider, e.collider)) {
|
||||
if (SATX.collideObject(this.collider(), e.collider)) {
|
||||
this.onCollide(this, e)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
checkIfArrived() {
|
||||
if (!this.#position.equals(this.destination)) { return }
|
||||
|
||||
if (this.after != null) {
|
||||
this.after(this, this.#homingTarget)
|
||||
}
|
||||
|
||||
this.despawn()
|
||||
|
||||
collider() {
|
||||
return new SAT.Circle(new SAT.Vector(this.x, this.y), this.radius)
|
||||
}
|
||||
|
||||
despawn() {
|
||||
@@ -73,28 +50,32 @@ export default class Projectile {
|
||||
state() {
|
||||
return {
|
||||
...this,
|
||||
position: {
|
||||
x: this.x,
|
||||
y: this.y,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
takeStep() {
|
||||
const speed = (this.speed / (this.game?.tickBudget ?? 1000))
|
||||
const destination = this.destination
|
||||
const difference = destination.clone().sub(this.position)
|
||||
const distance = difference.length()
|
||||
const direction = difference.clone().normalize()
|
||||
const stepTaken = this.position.clone().add(direction.multiplyScalar(speed))
|
||||
const position = distance <= speed ? destination : stepTaken
|
||||
|
||||
this.position.copy(position)
|
||||
update() {
|
||||
this.#move()
|
||||
if (this.onCollide != null) { this.checkCollisions() }
|
||||
this.#checkIfArrived()
|
||||
}
|
||||
|
||||
update() {
|
||||
this.takeStep()
|
||||
if (this.onCollide != null) { this.checkCollisions() }
|
||||
this.checkIfArrived()
|
||||
#checkIfArrived() {
|
||||
if (!this.position.equals(this.destination)) { return }
|
||||
|
||||
if (this.after != null) {
|
||||
this.after(this, this.#homingTarget)
|
||||
}
|
||||
|
||||
this.despawn()
|
||||
}
|
||||
|
||||
#move() {
|
||||
const speed = (this.speed / (this.game?.tickBudget ?? 1000))
|
||||
if (this.position.distanceTo(this.destination) < speed) {
|
||||
this.position.copy(this.destination)
|
||||
}
|
||||
|
||||
const step = this.destination.clone().sub(this.position).normalize().multiplyScalar(speed)
|
||||
this.position.add(step)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user