Aethelgard/js/scenes/GameScene.js

75 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Base from '../objects/Base.js';
import Bullet from '../objects/Bullet.js';
import Enemy from '../objects/Enemy.js';
import LevelManager from '../managers/LevelManager.js';
import XPManager from '../managers/XPManager.js';
import SpawnManager from '../managers/SpawnManager.js';
import UIManager from '../managers/UIManager.js';
import { ASSET_PATH } from '../config.js';
export default class GameScene extends Phaser.Scene {
constructor() { super('Game'); }
preload() {
// Spritesheets
this.load.spritesheet('base', `${ASSET_PATH}sprites/base_spritesheet.png`, { frameWidth:128, frameHeight:128 });
this.load.spritesheet('enemy', `${ASSET_PATH}sprites/enemy_spritesheet.png`, { frameWidth:96, frameHeight:96 });
this.load.image('bullet', `${ASSET_PATH}sprites/bullet.png`);
// Audio
this.load.audio('shoot', `${ASSET_PATH}audio/shoot.mp3`);
this.load.audio('explosion', `${ASSET_PATH}audio/explosion.wav`);
// JSON data
this.load.json('levels', `${ASSET_PATH}data/levels.json`);
this.load.json('units', `${ASSET_PATH}data/units.json`);
}
create() {
// Managers
this.xpManager = new XPManager(this);
this.levelManager = new LevelManager(this, this.cache.json.get('levels'));
this.spawnManager = new SpawnManager(this, this.cache.json.get('units'));
this.uiManager = new UIManager(this, this.xpManager);
// Groups
this.enemies = this.physics.add.group();
this.bullets = this.physics.add.group();
// Base (centerbottom)
const baseX = this.scale.width / 2;
const baseY = this.scale.height - 80;
this.base = new Base(this, baseX, baseY, 'base');
this.add.existing(this.base);
// Collisions
this.physics.add.overlap(this.bullets, this.enemies, this.handleBulletHit, null, this);
this.physics.add.overlap(this.enemies, this.base, this.handleEnemyReachBase, null, this);
// Start first level
this.levelManager.startLevel(1);
this.spawnManager.startSpawning(this.levelManager.currentConfig.spawnInterval);
}
update(time, delta) {
this.base.update(time, delta);
}
handleBulletHit(bullet, enemy) {
bullet.destroy();
enemy.takeDamage(bullet.damage);
if (enemy.isDead()) {
this.xpManager.addXP(enemy.xpValue);
enemy.destroy();
}
}
handleEnemyReachBase(enemy, base) {
base.takeDamage(enemy.collisionDamage || 20);
enemy.destroy();
if (base.isDestroyed()) {
this.scene.start('GameOver');
}
}
}