-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e95695c
commit 385558e
Showing
6 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const DegToRad = Phaser.Math.DegToRad; | ||
var DrawPieceMask = function (graphics, config) { | ||
var width = config.width; | ||
var height = config.height; | ||
var indent = config.indent; | ||
|
||
var edgeModes = config.edgeModes; | ||
var rightEdgeMode, bottomEdgeMode, leftEdgeMode, topEdgeMode; | ||
if (typeof (edgeModes) === 'string') { | ||
[rightEdgeMode, bottomEdgeMode, leftEdgeMode, topEdgeMode] = | ||
edgeModes.split('').map(function (x) { return parseInt(x) }); | ||
} else { | ||
rightEdgeMode = edgeModes.right; | ||
bottomEdgeMode = edgeModes.bottom; | ||
leftEdgeMode = edgeModes.left; | ||
topEdgeMode = edgeModes.top; | ||
} | ||
|
||
var centerX = width / 2, centerY = height / 2; | ||
|
||
graphics.clear(); | ||
graphics.beginPath(); | ||
|
||
graphics.moveTo(indent, indent); | ||
|
||
switch (topEdgeMode) { | ||
case 1: | ||
graphics.lineTo(centerX - indent, indent); | ||
graphics.arc(centerX, indent, indent, DegToRad(180), DegToRad(360), false); | ||
break; | ||
case 2: | ||
graphics.lineTo(centerX - indent, indent); | ||
graphics.arc(centerX, indent, indent, DegToRad(180), DegToRad(360), true); | ||
break; | ||
} | ||
graphics.lineTo(width - indent, indent); | ||
|
||
switch (rightEdgeMode) { | ||
case 1: | ||
graphics.arc(width - indent, centerY, indent, DegToRad(270), DegToRad(90), false); | ||
break; | ||
case 2: | ||
graphics.arc(width - indent, centerY, indent, DegToRad(270), DegToRad(90), true); | ||
break; | ||
} | ||
graphics.lineTo(width - indent, height - indent); | ||
|
||
switch (bottomEdgeMode) { | ||
case 1: | ||
graphics.arc(centerX, height - indent, indent, DegToRad(0), DegToRad(180), false); | ||
break; | ||
case 2: | ||
graphics.arc(centerX, height - indent, indent, DegToRad(0), DegToRad(180), true); | ||
break; | ||
} | ||
graphics.lineTo(indent, height - indent); | ||
|
||
switch (leftEdgeMode) { | ||
case 1: | ||
graphics.arc(indent, centerY, indent, DegToRad(90), DegToRad(270), false); | ||
break; | ||
case 2: | ||
graphics.arc(indent, centerY, indent, DegToRad(90), DegToRad(270), true); | ||
break; | ||
} | ||
graphics.lineTo(indent, indent); | ||
|
||
graphics.closePath(); | ||
graphics.fillPath(); | ||
} | ||
|
||
export default DrawPieceMask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import DrawPieceMask from './DrawPieceMask.js'; | ||
|
||
class JigsawPiece extends Phaser.GameObjects.RenderTexture { | ||
constructor(scene, { | ||
width, | ||
height, | ||
indent, | ||
}) { | ||
super(scene, 0, 0, width, height); | ||
|
||
if (indent === undefined) { | ||
indent = Math.min(width, height) / 7; | ||
} | ||
this.indent = indent; | ||
|
||
var maskGraphics = scene.make.graphics({ add: false }); | ||
this.setMask(maskGraphics.createGeometryMask()); | ||
this.maskGraphics = maskGraphics; | ||
} | ||
|
||
destroy(fromScene) { | ||
// This Game Object has already been destroyed | ||
if (!this.scene || this.ignoreDestroy) { | ||
return; | ||
} | ||
|
||
this.maskGraphics.destroy(); | ||
this.maskGraphics = undefined; | ||
|
||
super.destroy(); | ||
} | ||
|
||
drawPiece({ | ||
key, | ||
scrollX, | ||
scrollY, | ||
edgeModes | ||
}) { | ||
this.clear().fill(0x333333) | ||
|
||
this.camera.setScroll(scrollX, scrollY); | ||
|
||
this.stamp(key, undefined, 0, 0, { | ||
originX: 0, originY: 0, | ||
}); | ||
|
||
this.camera.setScroll(0, 0); | ||
|
||
DrawPieceMask(this.maskGraphics, { | ||
width: this.width, | ||
height: this.height, | ||
indent: this.indent, | ||
edgeModes: edgeModes | ||
}) | ||
|
||
return this; | ||
} | ||
} | ||
|
||
export default JigsawPiece; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@echo off | ||
set main=./examples/projects/jigsaw/test-JigsawPiece.js | ||
cd .. | ||
cd .. | ||
npm run watch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import phaser from 'phaser/src/phaser.js'; | ||
import JigsawPiece from './JigsawPiece.js'; | ||
|
||
class Demo extends Phaser.Scene { | ||
constructor() { | ||
super({ | ||
key: 'examples' | ||
}) | ||
} | ||
|
||
preload() { | ||
this.load.image('classroom', 'assets/images/backgrounds/classroom.png'); | ||
} | ||
|
||
create() { | ||
var piece = new JigsawPiece(this, { | ||
width: 115, height: 115, | ||
}) | ||
this.add.existing(piece); | ||
|
||
piece.setOrigin(0); | ||
|
||
piece.drawPiece({ | ||
key: 'classroom', | ||
scrollX: -15, | ||
scrollY: -15, | ||
edgeModes: '2200' | ||
}) | ||
|
||
this.add.graphics() | ||
.lineStyle(3, 0xff0000) | ||
.strokeRectShape(piece.getBounds()) | ||
} | ||
|
||
update() { } | ||
} | ||
|
||
var config = { | ||
type: Phaser.AUTO, | ||
parent: 'phaser-example', | ||
width: 800, | ||
height: 600, | ||
scale: { | ||
mode: Phaser.Scale.FIT, | ||
autoCenter: Phaser.Scale.CENTER_BOTH, | ||
}, | ||
scene: Demo, | ||
}; | ||
|
||
var game = new Phaser.Game(config); |