diff --git a/src/components/Lose.tsx b/src/components/Lose.tsx new file mode 100644 index 0000000..b710290 --- /dev/null +++ b/src/components/Lose.tsx @@ -0,0 +1,49 @@ +import { createRef, type Ref, Text, useScene } from 'phaser-jsx'; + +interface Props { + onClick: () => void; + ref: (ref: Ref) => void; +} + +export function Lose(props: Props) { + const scene = useScene(); + const ref = createRef(); + props.ref(ref); + + return ( + { + ref.current!.setColor('#ff7f50'); + scene.input.setDefaultCursor('pointer'); + }} + onPointerOut={() => { + ref.current!.setColor('#8c7ae6'); + scene.input.setDefaultCursor('default'); + }} + onPointerDown={() => { + scene.add.tween({ + targets: ref.current!, + ease: Phaser.Math.Easing.Bounce.InOut, + y: -1000, + onComplete: () => { + props.onClick(); + }, + }); + }} + ref={ref} + /> + ); +} diff --git a/src/components/Win.tsx b/src/components/Win.tsx index b82e4d5..deee977 100644 --- a/src/components/Win.tsx +++ b/src/components/Win.tsx @@ -27,17 +27,6 @@ export function Win(props: Props) { originX={0.5} originY={0.5} depth={3} - addedToScene={() => { - // title tween like retro arcade - scene.add.tween({ - targets: ref.current!, - duration: 800, - ease: (value: number) => value > 0.8, - alpha: 0, - repeat: -1, - yoyo: true, - }); - }} onPointerOver={() => { ref.current!.setColor('#ff7f50'); scene.input.setDefaultCursor('pointer'); diff --git a/src/components/index.ts b/src/components/index.ts index 18538dd..57835d8 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -1,2 +1,3 @@ +export * from './Lose'; export * from './Title'; export * from './Win'; diff --git a/src/scenes/Main.tsx b/src/scenes/Main.tsx index 43328ed..004d541 100644 --- a/src/scenes/Main.tsx +++ b/src/scenes/Main.tsx @@ -1,7 +1,7 @@ import Phaser from 'phaser'; import { type Ref, render } from 'phaser-jsx'; -import { Title, Win } from '../components'; +import { Lose, Title, Win } from '../components'; import { Audio, Image, Scene } from '../constants'; import { createCard } from '../utils/createCard'; @@ -177,8 +177,8 @@ export class Main extends Phaser.Scene { } startGame() { - // WinnerText and GameOverText let winnerTextRef: Ref; + render( ; + + render( + (gameOverTextRef = ref)} + />, + this, + ); // Start lifes images const hearts = this.createHearts(); @@ -224,9 +216,10 @@ export class Main extends Phaser.Scene { Phaser.Input.Events.POINTER_MOVE, (pointer: Phaser.Input.Pointer) => { if (this.canMove) { - const card = this.cards.find((card) => - card.gameObject.hasFaceAt(pointer.x, pointer.y), - ); + const card = this.cards.find((card) => { + card.gameObject.hasFaceAt(pointer.x, pointer.y); + }); + if (card) { this.input.setDefaultCursor('pointer'); } else { @@ -303,7 +296,7 @@ export class Main extends Phaser.Scene { // Show Game Over text this.sound.play(Audio.Whoosh, { volume: 1.3 }); this.add.tween({ - targets: gameOverText, + targets: gameOverTextRef.current, ease: Phaser.Math.Easing.Bounce.Out, y: this.sys.game.scale.height / 2, }); @@ -339,26 +332,5 @@ export class Main extends Phaser.Scene { } }, ); - - gameOverText.on(Phaser.Input.Events.POINTER_OVER, () => { - gameOverText.setColor('#FF7F50'); - this.input.setDefaultCursor('pointer'); - }); - - gameOverText.on(Phaser.Input.Events.POINTER_OUT, () => { - gameOverText.setColor('#8c7ae6'); - this.input.setDefaultCursor('default'); - }); - - gameOverText.on(Phaser.Input.Events.POINTER_DOWN, () => { - this.add.tween({ - targets: gameOverText, - ease: Phaser.Math.Easing.Bounce.InOut, - y: -1000, - onComplete: () => { - this.restartGame(); - }, - }); - }); } }