69 lines
1.3 KiB
TypeScript
69 lines
1.3 KiB
TypeScript
|
import * as planck from 'planck'
|
||
|
import { SpriteInstance } from '../shared/sprites'
|
||
|
|
||
|
export class Entity {
|
||
|
sprite: SpriteInstance
|
||
|
body?: planck.Body
|
||
|
velocity: [number, number] = [0, 0]
|
||
|
acceleration: number = 0.5
|
||
|
maxSpeed: number = 4
|
||
|
direction: number = 0
|
||
|
turnRate: number = 10
|
||
|
|
||
|
constructor(ctor: string) {
|
||
|
this.sprite = new SpriteInstance(ctor)
|
||
|
}
|
||
|
|
||
|
update(delta: number) {
|
||
|
// TODO: Update sprite.
|
||
|
}
|
||
|
|
||
|
get position(): [number, number] {
|
||
|
if (this.body) {
|
||
|
let p = this.body.getPosition()
|
||
|
return [p.x, p.y]
|
||
|
}
|
||
|
return [this.sprite.container.x, this.sprite.container.y]
|
||
|
}
|
||
|
get x(): number {
|
||
|
if (this.body) {
|
||
|
let p = this.body.getPosition()
|
||
|
return p.x
|
||
|
}
|
||
|
return this.sprite.container.x
|
||
|
}
|
||
|
set x(v: number) {
|
||
|
if (this.body) {
|
||
|
let p = this.body.getPosition()
|
||
|
this.body.setPosition(
|
||
|
planck.Vec2(
|
||
|
v,
|
||
|
p.y,
|
||
|
)
|
||
|
)
|
||
|
} else {
|
||
|
this.sprite.container.y = v
|
||
|
}
|
||
|
}
|
||
|
get y(): number {
|
||
|
if (this.body) {
|
||
|
let p = this.body.getPosition()
|
||
|
return p.y
|
||
|
}
|
||
|
return this.sprite.container.y
|
||
|
}
|
||
|
set y(v: number) {
|
||
|
if (this.body) {
|
||
|
let p = this.body.getPosition()
|
||
|
this.body.setPosition(
|
||
|
planck.Vec2(
|
||
|
p.x,
|
||
|
v,
|
||
|
)
|
||
|
)
|
||
|
} else {
|
||
|
this.sprite.container.y = v
|
||
|
}
|
||
|
}
|
||
|
}
|