Skip to content

Commit da8f41a

Browse files
committed
[Update] WebFontFile utils
1 parent 549fb6a commit da8f41a

File tree

7 files changed

+80
-23
lines changed

7 files changed

+80
-23
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"typescript": "^3.8.3"
2626
},
2727
"dependencies": {
28-
"phaser": "^3.50.1"
28+
"phaser": "^3.50.1",
29+
"webfontloader": "^1.6.28"
2930
},
3031
"parcelCleanPaths": [
3132
"dist"

src/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>LOL2D</title>
8+
9+
<link rel="stylesheet" href="style.css">
810
</head>
911

1012
<body>

src/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ const config: Phaser.Types.Core.GameConfig = {
66
type: Phaser.AUTO,
77
width: 800,
88
height: 600,
9+
render: {
10+
pixelArt: true,
11+
},
12+
scale: {
13+
mode: Phaser.Scale.FIT,
14+
autoCenter: Phaser.Scale.CENTER_BOTH,
15+
},
916
physics: {
1017
default: 'arcade',
1118
arcade: {
1219
gravity: { y: 200 },
20+
debug: true,
1321
},
1422
},
1523
scene: [HelloWorldScene],

src/scenes/HelloWorldScene.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
import Phaser from 'phaser'
22

3+
import WebFontFile from '~/utils/WebFontFile'
4+
35
export default class HelloWorldScene extends Phaser.Scene {
46
constructor() {
57
super('hello-world')
68
}
79

810
preload() {
9-
this.load.setBaseURL('http://labs.phaser.io')
10-
11-
this.load.image('sky', 'assets/skies/space3.png')
12-
this.load.image('logo', 'assets/sprites/phaser3-logo.png')
13-
this.load.image('red', 'assets/particles/red.png')
11+
const fonts = new WebFontFile(this.load, 'Roboto Mono')
12+
this.load.addFile(fonts)
1413
}
1514

1615
create() {
17-
this.add.image(400, 300, 'sky')
18-
19-
const particles = this.add.particles('red')
20-
21-
const emitter = particles.createEmitter({
22-
speed: 100,
23-
scale: { start: 1, end: 0 },
24-
blendMode: 'ADD',
25-
})
26-
27-
const logo = this.physics.add.image(400, 100, 'logo')
28-
29-
logo.setVelocity(100, 200)
30-
logo.setBounce(1, 1)
31-
logo.setCollideWorldBounds(true)
32-
33-
emitter.startFollow(logo)
16+
const x = this.scale.width * 0.5
17+
const y = this.scale.height * 0.5
18+
19+
this.add
20+
.text(x, y, 'Bầu trời trong xanh thăm thẳm, không một gợn mây!', {
21+
fontFamily: 'Roboto Mono',
22+
fontSize: '13px',
23+
color: '#fff',
24+
})
25+
.setOrigin(0.5, 0.5)
3426
}
3527
}

src/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
4+
overflow: hidden;
5+
background-color: black;
6+
}

src/utils/WebFontFile.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Phaser from 'phaser'
2+
3+
import WebFontLoader from 'webfontloader'
4+
5+
export default class WebFontFile extends Phaser.Loader.File {
6+
private fontNames: string[] = []
7+
private service: string = ''
8+
9+
constructor(
10+
loader: Phaser.Loader.LoaderPlugin,
11+
fontNames: string | string[],
12+
service = 'google'
13+
) {
14+
super(loader, {
15+
type: 'webfont',
16+
key: fontNames.toString(),
17+
})
18+
19+
this.fontNames = Array.isArray(fontNames) ? fontNames : [fontNames]
20+
this.service = service
21+
}
22+
23+
load() {
24+
const config = {
25+
active: () => {
26+
this.loader.nextFile(this, true)
27+
},
28+
}
29+
30+
switch (this.service) {
31+
case 'google':
32+
config['google'] = {
33+
families: this.fontNames,
34+
}
35+
break
36+
37+
default:
38+
throw new Error('Unsupported font service')
39+
}
40+
41+
WebFontLoader.load(config)
42+
}
43+
}

0 commit comments

Comments
 (0)