import * as planck from 'planck'
import { Entity } from "./Entity"
import { WorldContext } from "./World"

export class GibletEntity extends Entity {
  lifetime: number = 10000
  elapsed: number = 0
  constructor(ctor: string, dir: number, force: number, life?: number) {
    super(ctor)
    let r = dir * (Math.PI/180)
    this.velocity[0] = Math.cos(r) * force
    this.velocity[1] = Math.sin(r) * force
    if (life) {
      this.lifetime = life + Math.random()*life
    } else {
      this.lifetime = 10000 + Math.random()*10000
    }
  }
  update(delta: number, ctx?: WorldContext) {
    super.update(delta)
    this.elapsed += delta

    this.velocity[0] *= 0.80
    this.velocity[1] *= 0.80

    // Eh... let's manually handle velocity
    this.x += this.velocity[0]
    this.y += this.velocity[1]

    if (this.elapsed >= this.lifetime) {
      ctx?.removeEntity(this)
    }
  }

}