2020-08-07 12:21:15 -04:00
|
|
|
import EventQueue from "../Events/EventQueue";
|
|
|
|
import InputReceiver from "../Input/InputReceiver";
|
|
|
|
import Vec2 from "../DataTypes/Vec2";
|
2020-08-10 13:19:59 -04:00
|
|
|
import Map from "../DataTypes/Map";
|
|
|
|
import Receiver from "../Events/Receiver";
|
|
|
|
import GameEvent from "../Events/GameEvent";
|
2020-08-17 20:09:41 -04:00
|
|
|
import Scene from "../GameState/Scene";
|
2020-08-07 12:21:15 -04:00
|
|
|
|
|
|
|
export default abstract class GameNode{
|
2020-08-10 13:19:59 -04:00
|
|
|
private eventQueue: EventQueue;
|
2020-08-07 12:21:15 -04:00
|
|
|
protected input: InputReceiver;
|
|
|
|
protected position: Vec2;
|
2020-08-10 13:19:59 -04:00
|
|
|
private receiver: Receiver;
|
2020-08-17 20:09:41 -04:00
|
|
|
protected scene: Scene;
|
2020-08-07 12:21:15 -04:00
|
|
|
|
|
|
|
constructor(){
|
|
|
|
this.eventQueue = EventQueue.getInstance();
|
|
|
|
this.input = InputReceiver.getInstance();
|
|
|
|
this.position = new Vec2(0, 0);
|
|
|
|
}
|
2020-08-17 20:09:41 -04:00
|
|
|
|
|
|
|
init(scene: Scene){
|
|
|
|
this.scene = scene;
|
|
|
|
}
|
|
|
|
|
|
|
|
getScene(): Scene {
|
|
|
|
return this.scene;
|
|
|
|
}
|
2020-08-07 12:21:15 -04:00
|
|
|
|
|
|
|
getPosition(): Vec2 {
|
|
|
|
return this.position;
|
|
|
|
}
|
|
|
|
|
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-08-24 18:54:52 -04:00
|
|
|
subscribe(eventType: string): void {
|
2020-08-10 13:19:59 -04:00
|
|
|
this.eventQueue.subscribe(this.receiver, eventType);
|
2020-08-07 12:21:15 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 18:54:52 -04:00
|
|
|
emit(eventType: string, data: Map<any> | Record<string, any> = null): void {
|
2020-08-10 13:19:59 -04:00
|
|
|
let event = new GameEvent(eventType, data);
|
|
|
|
this.eventQueue.addEvent(event);
|
2020-08-07 12:21:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract update(deltaT: number): void;
|
|
|
|
}
|