You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

77 lines
1.6 KiB

<template>
<div id="gameContainer" :style="containerStyle"></div>
</template>
<script>
import phaserGames from "./phaser";
const games = phaserGames;
const STATE_RUNNING = 0;
const STATE_LOSE = 1;
const STATE_WON = 2;
const STATE_PAUSED = 3;
export default {
props: ["name", "containerStyle", "debug"],
data() {
return {
state: STATE_RUNNING,
game: null,
containerId: "gameContainer",
};
},
watch: {
name(val, oldVal) {
if (oldVal) {
this.dispose();
this.reload(val);
}
},
},
methods: {
pause() {
this.game.pause();
this.state = STATE_PAUSED;
this.$emit("paused");
},
resume() {
if (this.state === STATE_PAUSED) {
this.game.resume();
this.state = STATE_RUNNING;
this.$emit("resumed");
}
},
restart() {
this.game.restart();
this.state = STATE_RUNNING;
},
dispose() {
this.game.dispose();
this.game.destroy(false);
const container = document.getElementById("gameContainer");
const canvas = this.game.canvas;
if (canvas.parentNode === container) container.removeChild(canvas);
},
reload(name) {
this.game = new games[name]({
containerId: this.containerId,
onLose: () => {
if (this.state !== STATE_LOSE) this.$emit("lost");
this.state = STATE_LOSE;
},
onWon: () => {
if (this.state !== STATE_WON) this.$emit("won");
this.state = STATE_WON;
},
});
},
},
async mounted() {
this.reload(this.name);
},
destroyed() {
this.dispose();
},
};
</script>
<style>
</style>