Skip to content

Commit

Permalink
Added watch for game prop to fix issues initializing the phaser game
Browse files Browse the repository at this point in the history
  • Loading branch information
jdnichollsc committed Nov 23, 2019
1 parent 919a266 commit 6a096fe
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 29 deletions.
9 changes: 7 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
node_modules
react
demo-*
.github
.stencil
.editorconfig

react/
demo-*
img/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Looking for [Phaser Framework CE (Community Edition)](https://github.com/photons

### Script tag

- Put a script tag similar to this `<script src='https://unpkg.com/@ion-phaser/core@1.2.0/dist/ionphaser.js'></script>` in the head of your index.html
- Put a script tag similar to this `<script src='https://unpkg.com/@ion-phaser/core@1.2.1/dist/ionphaser.js'></script>` in the head of your index.html
- Then you can use the element anywhere in your template, JSX, html etc

### Node Modules
Expand Down
144 changes: 144 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
{
"name": "@ion-phaser/core",
"version": "1.2.0",
"version": "1.2.1",
"private": false,
"description": "A web component to integrate Phaser Framework with Angular, React, Vue, etc",
"keywords": [
"ionic",
"phaser",
"canvas",
"webgl",
"pwa",
"framework",
"angular",
"react",
"vue",
"app",
"vanillajs",
"stencil",
"stenciljs",
"webcomponent",
"web component",
"web components"
Expand All @@ -31,24 +36,19 @@
"build": "stencil build --docs",
"start": "stencil build --dev --watch --serve",
"test": "stencil test --spec --e2e",
"test.watch": "stencil test --spec --e2e --watchAll"
"test.watch": "stencil test --spec --e2e --watchAll",
"generate": "stencil generate"
},
"peerDependencies": {
"phaser": "^3.19.0"
},
"devDependencies": {
"phaser": "^3.19.0",
"@stencil/core": "~1.5.3",
"@types/jest": "24.0.18",
"@types/puppeteer": "1.19.1",
"jest": "24.9.0",
"jest-cli": "24.9.0",
"puppeteer": "1.19.0"
"@stencil/core": "^1.8.1"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/proyecto26/ion-phaser.git"
},
"private": false
}
}
1 change: 1 addition & 0 deletions src/components.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
/* tslint:disable */
/**
* This is an autogenerated file created by the Stencil compiler.
Expand Down
3 changes: 3 additions & 0 deletions src/components/ion-phaser/ion-phaser.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ion-phaser {
display: block;
}
24 changes: 15 additions & 9 deletions src/components/ion-phaser/ion-phaser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GameInstance } from '../models'

@Component({
tag: 'ion-phaser',
styleUrl: 'ion-phaser.css',
shadow: false
})
export class IonPhaser {
Expand All @@ -12,9 +13,16 @@ export class IonPhaser {
*/
@Prop({
mutable: true,
reflectToAttr: true
reflect: true
}) game: GameInstance

@Watch('game')
onGameChange(game: GameInstance) {
if (this.initialize && !this.getGameInstance()) {
this.initializeGame(game)
}
}

/**
* To initialize the plugin manually
*/
Expand All @@ -32,7 +40,7 @@ export class IonPhaser {
*/
@Method()
async getInstance(): Promise<Game> {
return this.getGameInstance()
return Promise.resolve(this.getGameInstance())
}

/**
Expand All @@ -48,16 +56,14 @@ export class IonPhaser {

@Element() el: HTMLElement

initializeGame = () => {
if(!this.game){
throw new Error("The configuration of the game is required")
}
if(this.game.instance){
initializeGame = (game = this.game) => {
if(!game) return
if(game.instance){
throw new Error("A Phaser game already exist")
}

this.game.parent = this.game.parent || this.el
this.game.instance = new Game(this.game)
game.parent = game.parent || this.el
game.instance = new Game(game)
}

componentWillLoad() {
Expand Down
16 changes: 10 additions & 6 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,15 @@

</head>
<body>
<ion-phaser></ion-phaser>
<ion-phaser initialize="false"></ion-phaser>
<div id="init" class="flex">
<a href="#0" class="bttn">Initialize</a>
</div>
<script>
const ionPhaser = document.querySelector('ion-phaser');
const initButton = document.getElementById('init')
ionPhaser.initialize = false
const initButton = document.getElementsByClassName('bttn')[0];

ionPhaser.game = {
const game = {
width: "100%",
height: "100%",
type: Phaser.AUTO,
Expand All @@ -146,8 +145,13 @@
}
}

initButton.addEventListener("click", function(){
ionPhaser.initialize = true
initButton.addEventListener("click", function (){
ionPhaser.game = game;
ionPhaser.initialize = true;

ionPhaser.getInstance()
.then((i) => console.log(i))
.catch((error) => console.error(error))
});
</script>
</body>
Expand Down

0 comments on commit 6a096fe

Please sign in to comment.