/
mainScene.ts
135 lines (121 loc) · 4.28 KB
/
mainScene.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { Map } from '../components/map'
import TilesGroup from '../components/tiles/tilesGroup'
import Player from '../components/player/player'
import CoinGroup from '../components/coins/coinGroup'
import BeeSprite from '../components/enemies/bee'
import EnemiesGroup from '../components/enemies/enemiesGroup'
import GoalSprite from '../components/goalSprite'
import Controls from '../components/controls/controls'
import LevelText from '../components/levelText'
import Background from '../components/background'
import MiniMap from '../components/miniMap'
import PhaserVersionText from '../components/phaserVersionText'
export default class MainScene extends Phaser.Scene {
player: Player
tilesGroup: TilesGroup
cursors: Phaser.Input.Keyboard.CursorKeys
background: Background
enemiesGroup: EnemiesGroup
controls: Controls
goal: GoalSprite
level: number
miniMap: MiniMap
constructor() {
super({
key: 'MainScene'
})
}
init(props: { level?: number }) {
const { level = 0 } = props
this.level = Map.calcCurrentLevel(level)
}
create() {
const map = new Map(this.level)
this.cameras.main.setBackgroundColor('#ade6ff')
this.cameras.main.fadeIn()
this.cameras.main.setBounds(map.size.x, map.size.y, map.size.width, map.size.height)
this.physics.world.setBounds(map.size.x, map.size.y, map.size.width, map.size.height)
this.input.addPointer(1)
this.cursors = this.input.keyboard.createCursorKeys()
this.background = new Background(this)
this.tilesGroup = new TilesGroup(this, map.info.filter((el: TilesConfig) => el.type === 'tile'))
this.goal = new GoalSprite(this, map.info.filter((el: TilesConfig) => el.type === 'goal')[0])
this.player = new Player(this, map.info.filter((el: TilesConfig) => el.type === 'player')[0], map.size, this.level)
this.enemiesGroup = new EnemiesGroup(this, map.info)
const coinGroup = new CoinGroup(this, map.info.filter((el: TilesConfig) => el.type === 'coin'))
this.controls = new Controls(this)
const levelText = new LevelText(this, this.level)
const phaserVersion = new PhaserVersionText(this, 0, 0, `Phaser v${Phaser.VERSION}`)
this.cameras.main.startFollow(this.player)
this.physics.add.collider(this.tilesGroup, this.player)
this.physics.add.collider(this.tilesGroup, this.enemiesGroup)
// @ts-ignore
this.physics.add.overlap(this.player, this.enemiesGroup, (player: Player, enemy: BeeSprite) => {
if (enemy.dead) return
if (enemy.body.touching.up && player.body.touching.down) {
player.killEnemy()
enemy.kill()
} else {
player.kill()
}
})
//@ts-ignore
this.physics.add.overlap(this.player, coinGroup, (player, coin) => coin.collect())
//@ts-ignore
this.physics.add.overlap(this.player, this.goal, (player: Player, goal: GoalSprite) => {
player.halt()
goal.nextLevel(this, this.level)
})
this.miniMap = new MiniMap(
this,
10,
10,
Math.min(map.size.width / 8, (map.size.height / 8) * 2.5),
map.size.height / 8,
map
)
this.miniMap.setIgnore([
this.background,
levelText,
this.controls.buttons.up,
this.controls.buttons.left,
this.controls.buttons.right,
phaserVersion
])
this.miniMap.update(this.player)
// remove the loading screen
let loadingScreen = document.getElementById('loading-screen')
if (loadingScreen) {
loadingScreen.classList.add('transparent')
this.time.addEvent({
delay: 1000,
callback: () => {
// @ts-ignore
loadingScreen.remove()
}
})
}
// the resize function
const resize = () => {
this.controls.adjustPositions()
phaserVersion.x = this.cameras.main.width - 15
phaserVersion.y = 15
this.background.adjustPosition()
levelText.adjustPosition()
}
this.scale.on('resize', (gameSize: any) => {
this.cameras.main.width = gameSize.width
this.cameras.main.height = gameSize.height
//this.cameras.resize(gameSize.width, gameSize.height)
resize()
})
resize()
}
update() {
this.background.parallax()
this.controls.update()
this.enemiesGroup.update()
this.player.update(this.cursors, this.controls)
this.miniMap.update(this.player)
}
}