Skip to content

Commit

Permalink
[Update] WebFontFile utils
Browse files Browse the repository at this point in the history
  • Loading branch information
HoangTran0410 committed Jan 20, 2021
1 parent 549fb6a commit da8f41a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 23 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"typescript": "^3.8.3"
},
"dependencies": {
"phaser": "^3.50.1"
"phaser": "^3.50.1",
"webfontloader": "^1.6.28"
},
"parcelCleanPaths": [
"dist"
Expand Down
2 changes: 2 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LOL2D</title>

<link rel="stylesheet" href="style.css">
</head>

<body>
Expand Down
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ const config: Phaser.Types.Core.GameConfig = {
type: Phaser.AUTO,
width: 800,
height: 600,
render: {
pixelArt: true,
},
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
physics: {
default: 'arcade',
arcade: {
gravity: { y: 200 },
debug: true,
},
},
scene: [HelloWorldScene],
Expand Down
36 changes: 14 additions & 22 deletions src/scenes/HelloWorldScene.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
import Phaser from 'phaser'

import WebFontFile from '~/utils/WebFontFile'

export default class HelloWorldScene extends Phaser.Scene {
constructor() {
super('hello-world')
}

preload() {
this.load.setBaseURL('http://labs.phaser.io')

this.load.image('sky', 'assets/skies/space3.png')
this.load.image('logo', 'assets/sprites/phaser3-logo.png')
this.load.image('red', 'assets/particles/red.png')
const fonts = new WebFontFile(this.load, 'Roboto Mono')
this.load.addFile(fonts)
}

create() {
this.add.image(400, 300, 'sky')

const particles = this.add.particles('red')

const emitter = particles.createEmitter({
speed: 100,
scale: { start: 1, end: 0 },
blendMode: 'ADD',
})

const logo = this.physics.add.image(400, 100, 'logo')

logo.setVelocity(100, 200)
logo.setBounce(1, 1)
logo.setCollideWorldBounds(true)

emitter.startFollow(logo)
const x = this.scale.width * 0.5
const y = this.scale.height * 0.5

this.add
.text(x, y, 'Bầu trời trong xanh thăm thẳm, không một gợn mây!', {
fontFamily: 'Roboto Mono',
fontSize: '13px',
color: '#fff',
})
.setOrigin(0.5, 0.5)
}
}
6 changes: 6 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
}
43 changes: 43 additions & 0 deletions src/utils/WebFontFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Phaser from 'phaser'

import WebFontLoader from 'webfontloader'

export default class WebFontFile extends Phaser.Loader.File {
private fontNames: string[] = []
private service: string = ''

constructor(
loader: Phaser.Loader.LoaderPlugin,
fontNames: string | string[],
service = 'google'
) {
super(loader, {
type: 'webfont',
key: fontNames.toString(),
})

this.fontNames = Array.isArray(fontNames) ? fontNames : [fontNames]
this.service = service
}

load() {
const config = {
active: () => {
this.loader.nextFile(this, true)
},
}

switch (this.service) {
case 'google':
config['google'] = {
families: this.fontNames,
}
break

default:
throw new Error('Unsupported font service')
}

WebFontLoader.load(config)
}
}

0 comments on commit da8f41a

Please sign in to comment.