Skip to content

Commit a0f1b09

Browse files
refactor(components): replace Lose
1 parent b2f15f9 commit a0f1b09

File tree

4 files changed

+66
-55
lines changed

4 files changed

+66
-55
lines changed

src/components/Lose.tsx

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { createRef, type Ref, Text, useScene } from 'phaser-jsx';
2+
3+
interface Props {
4+
onClick: () => void;
5+
ref: (ref: Ref<Phaser.GameObjects.Text>) => void;
6+
}
7+
8+
export function Lose(props: Props) {
9+
const scene = useScene();
10+
const ref = createRef<Phaser.GameObjects.Text>();
11+
props.ref(ref);
12+
13+
return (
14+
<Text
15+
x={scene.sys.game.scale.width / 2}
16+
y={-1000}
17+
text={['GAME OVER', 'Click to restart'].join('\n')}
18+
style={{
19+
align: 'center',
20+
strokeThickness: 4,
21+
fontSize: 40,
22+
fontStyle: 'bold',
23+
color: '#ff0000',
24+
}}
25+
originX={0.5}
26+
originY={0.5}
27+
depth={3}
28+
onPointerOver={() => {
29+
ref.current!.setColor('#ff7f50');
30+
scene.input.setDefaultCursor('pointer');
31+
}}
32+
onPointerOut={() => {
33+
ref.current!.setColor('#8c7ae6');
34+
scene.input.setDefaultCursor('default');
35+
}}
36+
onPointerDown={() => {
37+
scene.add.tween({
38+
targets: ref.current!,
39+
ease: Phaser.Math.Easing.Bounce.InOut,
40+
y: -1000,
41+
onComplete: () => {
42+
props.onClick();
43+
},
44+
});
45+
}}
46+
ref={ref}
47+
/>
48+
);
49+
}

src/components/Win.tsx

-11
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ export function Win(props: Props) {
2727
originX={0.5}
2828
originY={0.5}
2929
depth={3}
30-
addedToScene={() => {
31-
// title tween like retro arcade
32-
scene.add.tween({
33-
targets: ref.current!,
34-
duration: 800,
35-
ease: (value: number) => value > 0.8,
36-
alpha: 0,
37-
repeat: -1,
38-
yoyo: true,
39-
});
40-
}}
4130
onPointerOver={() => {
4231
ref.current!.setColor('#ff7f50');
4332
scene.input.setDefaultCursor('pointer');

src/components/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export * from './Lose';
12
export * from './Title';
23
export * from './Win';

src/scenes/Main.tsx

+16-44
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Phaser from 'phaser';
22
import { type Ref, render } from 'phaser-jsx';
33

4-
import { Title, Win } from '../components';
4+
import { Lose, Title, Win } from '../components';
55
import { Audio, Image, Scene } from '../constants';
66
import { createCard } from '../utils/createCard';
77

@@ -177,8 +177,8 @@ export class Main extends Phaser.Scene {
177177
}
178178

179179
startGame() {
180-
// WinnerText and GameOverText
181180
let winnerTextRef: Ref<Phaser.GameObjects.Text>;
181+
182182
render(
183183
<Win
184184
onClick={this.restartGame.bind(this)}
@@ -187,23 +187,15 @@ export class Main extends Phaser.Scene {
187187
this,
188188
);
189189

190-
const gameOverText = this.add
191-
.text(
192-
this.sys.game.scale.width / 2,
193-
-1000,
194-
'GAME OVER\nClick to restart',
195-
{
196-
align: 'center',
197-
strokeThickness: 4,
198-
fontSize: 40,
199-
fontStyle: 'bold',
200-
color: '#ff0000',
201-
},
202-
)
203-
.setName('gameOverText')
204-
.setDepth(3)
205-
.setOrigin(0.5)
206-
.setInteractive();
190+
let gameOverTextRef: Ref<Phaser.GameObjects.Text>;
191+
192+
render(
193+
<Lose
194+
onClick={this.restartGame.bind(this)}
195+
ref={(ref) => (gameOverTextRef = ref)}
196+
/>,
197+
this,
198+
);
207199

208200
// Start lifes images
209201
const hearts = this.createHearts();
@@ -224,9 +216,10 @@ export class Main extends Phaser.Scene {
224216
Phaser.Input.Events.POINTER_MOVE,
225217
(pointer: Phaser.Input.Pointer) => {
226218
if (this.canMove) {
227-
const card = this.cards.find((card) =>
228-
card.gameObject.hasFaceAt(pointer.x, pointer.y),
229-
);
219+
const card = this.cards.find((card) => {
220+
card.gameObject.hasFaceAt(pointer.x, pointer.y);
221+
});
222+
230223
if (card) {
231224
this.input.setDefaultCursor('pointer');
232225
} else {
@@ -303,7 +296,7 @@ export class Main extends Phaser.Scene {
303296
// Show Game Over text
304297
this.sound.play(Audio.Whoosh, { volume: 1.3 });
305298
this.add.tween({
306-
targets: gameOverText,
299+
targets: gameOverTextRef.current,
307300
ease: Phaser.Math.Easing.Bounce.Out,
308301
y: this.sys.game.scale.height / 2,
309302
});
@@ -339,26 +332,5 @@ export class Main extends Phaser.Scene {
339332
}
340333
},
341334
);
342-
343-
gameOverText.on(Phaser.Input.Events.POINTER_OVER, () => {
344-
gameOverText.setColor('#FF7F50');
345-
this.input.setDefaultCursor('pointer');
346-
});
347-
348-
gameOverText.on(Phaser.Input.Events.POINTER_OUT, () => {
349-
gameOverText.setColor('#8c7ae6');
350-
this.input.setDefaultCursor('default');
351-
});
352-
353-
gameOverText.on(Phaser.Input.Events.POINTER_DOWN, () => {
354-
this.add.tween({
355-
targets: gameOverText,
356-
ease: Phaser.Math.Easing.Bounce.InOut,
357-
y: -1000,
358-
onComplete: () => {
359-
this.restartGame();
360-
},
361-
});
362-
});
363335
}
364336
}

0 commit comments

Comments
 (0)