workadventure/front/src/Phaser/Items/Computer/computer.ts

91 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-09-06 14:27:54 +02:00
import * as Phaser from "phaser";
import Sprite = Phaser.GameObjects.Sprite;
2021-09-06 14:27:54 +02:00
import type { ITiledMapObject } from "../../Map/ITiledMap";
import type { ItemFactoryInterface } from "../ItemFactoryInterface";
import type { GameScene } from "../../Game/GameScene";
import { ActionableItem } from "../ActionableItem";
2020-07-27 22:36:07 +02:00
import * as tg from "generic-type-guard";
2021-09-06 14:27:54 +02:00
const isComputerState = new tg.IsInterface()
.withProperties({
2020-07-27 22:36:07 +02:00
status: tg.isString,
2021-09-06 14:27:54 +02:00
})
.get();
2020-07-27 22:36:07 +02:00
type ComputerState = tg.GuardedType<typeof isComputerState>;
let state: ComputerState = {
2021-09-06 14:27:54 +02:00
status: "off",
2020-07-27 22:36:07 +02:00
};
export default {
preload: (loader: Phaser.Loader.LoaderPlugin): void => {
2021-09-06 14:27:54 +02:00
loader.atlas(
"computer",
"/resources/items/computer/computer.png",
"/resources/items/computer/computer_atlas.json"
);
},
create: (scene: GameScene): void => {
2020-07-27 22:36:07 +02:00
scene.anims.create({
2021-09-06 14:27:54 +02:00
key: "computer_off",
2020-07-27 22:36:07 +02:00
frames: [
{
2021-09-06 14:27:54 +02:00
key: "computer",
frame: "computer_off",
},
2020-07-27 22:36:07 +02:00
],
frameRate: 10,
2021-09-06 14:27:54 +02:00
repeat: -1,
2020-07-27 22:36:07 +02:00
});
scene.anims.create({
2021-09-06 14:27:54 +02:00
key: "computer_run",
2020-07-27 22:36:07 +02:00
frames: [
2021-09-06 14:27:54 +02:00
{
key: "computer",
frame: "computer_on1",
},
{
key: "computer",
frame: "computer_on2",
},
],
2020-07-27 22:36:07 +02:00
frameRate: 5,
2021-09-06 14:27:54 +02:00
repeat: -1,
2020-07-27 22:36:07 +02:00
});
},
2020-07-27 22:36:07 +02:00
factory: (scene: GameScene, object: ITiledMapObject, initState: unknown): ActionableItem => {
if (initState !== undefined) {
if (!isComputerState(initState)) {
2021-09-06 14:27:54 +02:00
throw new Error("Invalid state received for computer object");
2020-07-27 22:36:07 +02:00
}
state = initState;
}
// Idée: ESSAYER WebPack? https://paultavares.wordpress.com/2018/07/02/webpack-how-to-generate-an-es-module-bundle/
2021-09-06 14:27:54 +02:00
const computer = new Sprite(scene, object.x, object.y, "computer");
2020-07-27 22:36:07 +02:00
scene.add.existing(computer);
2021-09-06 14:27:54 +02:00
if (state.status === "on") {
computer.anims.play("computer_run");
2020-07-27 22:36:07 +02:00
}
const item = new ActionableItem(object.id, computer, scene, 32, (item: ActionableItem) => {
2021-09-06 14:27:54 +02:00
if (state.status === "off") {
state.status = "on";
item.emit("TURN_ON", state);
2020-07-27 22:36:07 +02:00
} else {
2021-09-06 14:27:54 +02:00
state.status = "off";
item.emit("TURN_OFF", state);
2020-07-27 22:36:07 +02:00
}
});
2021-09-06 14:27:54 +02:00
item.on("TURN_ON", () => {
computer.anims.play("computer_run");
2020-07-27 22:36:07 +02:00
});
2021-09-06 14:27:54 +02:00
item.on("TURN_OFF", () => {
computer.anims.play("computer_off");
2020-07-27 22:36:07 +02:00
});
2020-07-27 22:36:07 +02:00
return item;
//scene.add.sprite(object.x, object.y, 'computer');
2021-09-06 14:27:54 +02:00
},
} as ItemFactoryInterface;