import Box from "../math/box";
import vec3 from "../math/vec3";
import quat from "../math/quat";
import { ObjectType } from "./object-type";
export class PhysicsObject {
    constructor() {
        this.type = ObjectType.object;
        this.boundingBox = new Box(new vec3([0, 0, 0]), new vec3([1, 1, 1]));
        this.rotation = new quat();
        this.velocity = new vec3([0, 0, 0]);
        this.affectedByGravity = true;
        this.collides = true;
        this.isStatic = false;
        this.isDirty = true;
        this.collisionContacts = new Set();
    }
    move(position) {
        if (this.isStatic)
            return;
        this.boundingBox.position = position;
        this.isDirty = true;
    }
    lookAt(position) {
        this.rotation = quat.fromVectors(this.boundingBox.position.copy(), position.copy());
    }
    collidesWith(obj) {
        if (obj.type !== ObjectType.terrain) {
            return this.boundingBox.overlaps(obj.boundingBox);
        }
        else {
            return obj.collidesWith(this);
        }
        return false;
    }
    onCollision(obj, world) {
        if (!this.collisionContacts.has(obj)) {
            this.collisionContacts.add(obj);
            world.emit('collision.enter', [this, obj]);
        }
        if (this.isStatic)
            return;
        this.velocity.negate();
        this.boundingBox.position.add(this.velocity.multiply(new vec3([world.deltaTime, world.deltaTime, world.deltaTime])));
        this.velocity = new vec3([0, 0, 0]);
        this.checkCollisionContacts();
        return true;
    }
    checkCollisionContacts() {
        this.collisionContacts.forEach((contact) => {
            if (!contact.collidesWith(this)) {
                this.collisionContacts.delete(contact);
            }
        });
    }
}
