feat(#10): reset camera position after changing level
This commit is contained in:
parent
e2c439d1f3
commit
abf334607e
6 changed files with 47 additions and 2 deletions
1
index.js
1
index.js
|
@ -26,6 +26,7 @@ const foregroundMaps = [
|
||||||
layer: 1,
|
layer: 1,
|
||||||
selected: true,
|
selected: true,
|
||||||
},
|
},
|
||||||
|
{ name: "ocean", imageId: "overworld", elementId: "level2", layer: 2 },
|
||||||
];
|
];
|
||||||
|
|
||||||
const clicableObjects = ["debug", "level1", "level2"];
|
const clicableObjects = ["debug", "level1", "level2"];
|
||||||
|
|
32
modules/event-emitter.js
Normal file
32
modules/event-emitter.js
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
class EventEmitterSingleton {
|
||||||
|
static instance;
|
||||||
|
eventEmitter;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.eventEmitter = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
static getInstance() {
|
||||||
|
if (!EventEmitterSingleton.instance) {
|
||||||
|
EventEmitterSingleton.instance = new EventEmitterSingleton();
|
||||||
|
}
|
||||||
|
return EventEmitterSingleton.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
on(eventName, callback) {
|
||||||
|
if (!this.eventEmitter[eventName]) {
|
||||||
|
this.eventEmitter[eventName] = [];
|
||||||
|
}
|
||||||
|
this.eventEmitter[eventName].push(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
emit(eventName, ...args) {
|
||||||
|
if (this.eventEmitter[eventName]) {
|
||||||
|
this.eventEmitter[eventName].forEach((callback) => {
|
||||||
|
callback(...args);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const eventEmitter = EventEmitterSingleton.getInstance();
|
|
@ -27,6 +27,11 @@ export class Camera extends GameObject {
|
||||||
(acc, item) => ({ ...acc, [item.key]: item }),
|
(acc, item) => ({ ...acc, [item.key]: item }),
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
|
this.eventEmitter.on("changeLevel", () => {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.moveCamera(0, 0, 0);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
update(delta) {
|
update(delta) {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { eventEmitter } from "../event-emitter.js";
|
||||||
|
|
||||||
export class GameObject {
|
export class GameObject {
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
const {
|
const {
|
||||||
|
@ -14,6 +16,7 @@ export class GameObject {
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.gameObjects = gameObjects;
|
this.gameObjects = gameObjects;
|
||||||
this.debug = debug;
|
this.debug = debug;
|
||||||
|
this.eventEmitter = eventEmitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
|
|
|
@ -28,5 +28,6 @@ export class MapManagement extends GameObject {
|
||||||
}
|
}
|
||||||
const map = this.gameObjects.find((item) => item.elementId === elementId);
|
const map = this.gameObjects.find((item) => item.elementId === elementId);
|
||||||
this.selected = map.name;
|
this.selected = map.name;
|
||||||
|
this.eventEmitter.emit("changeLevel");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ export class Map extends GameObject {
|
||||||
});
|
});
|
||||||
this.levelConfig = await response.json();
|
this.levelConfig = await response.json();
|
||||||
const layer = this.levelConfig.layers[this.layer];
|
const layer = this.levelConfig.layers[this.layer];
|
||||||
const { data, height, width } = layer;
|
const { data, height, width } = layer ?? {};
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
|
@ -39,6 +39,9 @@ export class Map extends GameObject {
|
||||||
if (this._level) {
|
if (this._level) {
|
||||||
return this._level;
|
return this._level;
|
||||||
}
|
}
|
||||||
|
if (!this.width || !this.height) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
const { ctx, canvas } = createCanvas(
|
const { ctx, canvas } = createCanvas(
|
||||||
this.width * TILE_SIZE,
|
this.width * TILE_SIZE,
|
||||||
this.height * TILE_SIZE
|
this.height * TILE_SIZE
|
||||||
|
@ -84,7 +87,7 @@ export class Map extends GameObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
render(ctx, sourceX, sourceY, width, height) {
|
render(ctx, sourceX, sourceY, width, height) {
|
||||||
if (!this.levelConfig || !this.selected) {
|
if (!this.levelConfig || !this.selected || !this.level) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue