2020-08-07 12:21:15 -04:00
|
|
|
import InputReceiver from "../Input/InputReceiver";
|
|
|
|
import Vec2 from "../DataTypes/Vec2";
|
2020-08-10 13:19:59 -04:00
|
|
|
import Receiver from "../Events/Receiver";
|
2020-09-19 20:18:39 -04:00
|
|
|
import Emitter from "../Events/Emitter";
|
2020-09-06 18:07:09 -04:00
|
|
|
import Scene from "../Scene/Scene";
|
2020-09-05 00:04:14 -04:00
|
|
|
import Layer from "../Scene/Layer";
|
2020-10-14 14:55:22 -04:00
|
|
|
import { Positioned, Unique, Updateable } from "../DataTypes/Interfaces/Descriptors"
|
2020-08-07 12:21:15 -04:00
|
|
|
|
2020-09-13 20:57:28 -04:00
|
|
|
/**
|
|
|
|
* The representation of an object in the game world
|
|
|
|
*/
|
2020-10-14 14:55:22 -04:00
|
|
|
export default abstract class GameNode implements Positioned, Unique, Updateable {
|
2020-08-07 12:21:15 -04:00
|
|
|
protected input: InputReceiver;
|
2020-09-28 18:57:02 -04:00
|
|
|
private _position: Vec2;
|
2020-09-19 20:18:39 -04:00
|
|
|
protected receiver: Receiver;
|
|
|
|
protected emitter: Emitter;
|
2020-09-06 18:07:09 -04:00
|
|
|
protected scene: Scene;
|
|
|
|
protected layer: Layer;
|
2020-09-28 18:57:02 -04:00
|
|
|
private id: number;
|
2020-08-07 12:21:15 -04:00
|
|
|
|
|
|
|
constructor(){
|
|
|
|
this.input = InputReceiver.getInstance();
|
2020-09-28 18:57:02 -04:00
|
|
|
this._position = new Vec2(0, 0);
|
|
|
|
this._position.setOnChange(this.positionChanged);
|
2020-09-19 20:18:39 -04:00
|
|
|
this.receiver = new Receiver();
|
|
|
|
this.emitter = new Emitter();
|
2020-08-07 12:21:15 -04:00
|
|
|
}
|
2020-08-17 20:09:41 -04:00
|
|
|
|
2020-09-06 18:07:09 -04:00
|
|
|
setScene(scene: Scene): void {
|
2020-08-17 20:09:41 -04:00
|
|
|
this.scene = scene;
|
|
|
|
}
|
|
|
|
|
2020-09-06 18:07:09 -04:00
|
|
|
getScene(): Scene {
|
2020-08-17 20:09:41 -04:00
|
|
|
return this.scene;
|
|
|
|
}
|
2020-09-06 18:07:09 -04:00
|
|
|
|
|
|
|
setLayer(layer: Layer): void {
|
|
|
|
this.layer = layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
getLayer(): Layer {
|
|
|
|
return this.layer;
|
|
|
|
}
|
2020-08-07 12:21:15 -04:00
|
|
|
|
2020-09-28 18:57:02 -04:00
|
|
|
get position(): Vec2 {
|
|
|
|
return this._position;
|
|
|
|
}
|
|
|
|
|
|
|
|
set position(pos: Vec2) {
|
|
|
|
this._position = pos;
|
|
|
|
this._position.setOnChange(this.positionChanged);
|
|
|
|
this.positionChanged();
|
|
|
|
}
|
|
|
|
|
2020-08-07 12:21:15 -04:00
|
|
|
getPosition(): Vec2 {
|
2020-09-28 18:57:02 -04:00
|
|
|
return this._position.clone();
|
2020-08-07 12:21:15 -04:00
|
|
|
}
|
|
|
|
|
2020-08-10 19:13:42 -04:00
|
|
|
setPosition(vecOrX: Vec2 | number, y: number = null): void {
|
|
|
|
if(vecOrX instanceof Vec2){
|
|
|
|
this.position.set(vecOrX.x, vecOrX.y);
|
|
|
|
} else {
|
|
|
|
this.position.set(vecOrX, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 18:57:02 -04:00
|
|
|
setId(id: number): void {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
getId(): number {
|
|
|
|
return this.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called if the position vector is modified or replaced
|
|
|
|
*/
|
2020-10-05 15:01:26 -04:00
|
|
|
protected positionChanged = (): void => {};
|
2020-09-28 18:57:02 -04:00
|
|
|
|
2020-09-06 18:07:09 -04:00
|
|
|
// TODO - This doesn't seem ideal. Is there a better way to do this?
|
2020-10-05 15:01:26 -04:00
|
|
|
getViewportOriginWithParallax(): Vec2 {
|
|
|
|
return this.scene.getViewport().getOrigin().mult(this.layer.getParallax());
|
2020-09-06 18:07:09 -04:00
|
|
|
}
|
|
|
|
|
2020-10-18 18:34:13 -04:00
|
|
|
getViewportScale(): number {
|
|
|
|
return this.scene.getViewport().getZoomLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-07 12:21:15 -04:00
|
|
|
abstract update(deltaT: number): void;
|
|
|
|
}
|