mnml/client/src/components/phaser.combat.js
2018-11-05 16:31:48 +10:00

75 lines
2.8 KiB
JavaScript
Executable File

const Phaser = require('phaser');
const fs = require('fs');
class PhaserCombat extends Phaser.Scene {
preload() {
/* Static Images */
this.textures.addBase64('alk', `data:image/png;base64,${new Buffer.from(fs.readFileSync('./assets/alakazam-f.png')).toString('base64')}`);
this.textures.addBase64('magmar', `data:image/png;base64,${new Buffer.from(fs.readFileSync('./assets/magmar.png')).toString('base64')}`);
this.load.image('sky', 'http://labs.phaser.io/assets/skies/nebula.jpg');
this.load.image('proj', 'http://labs.phaser.io/assets/sprites/bullet.png');
this.load.image('blue', 'http://labs.phaser.io/assets/particles/blue.png');
this.load.image('green', 'http://labs.phaser.io/assets/particles/green.png');
this.load.image('red', 'http://labs.phaser.io/assets/particles/red.png');
this.load.image('white', 'http://labs.phaser.io/assets/particles/white.png');
this.load.image('yellow', 'http://labs.phaser.io/assets/particles/yellow.png');
/* Spritesheets */
this.load.spritesheet({
key: 'explosion',
url: 'http://labs.phaser.io/assets/sprites/explosion.png',
frameConfig: { frameWidth: 64, frameHeight: 64, endFrame: 23 },
});
}
create() {
this.add.image(400, 300, 'sky');// Background image
this.createPlayers();
this.createAnim();
this.cursors = this.input.keyboard.createCursorKeys();
this.combat = false;
}
createPlayers() {
this.players = this.physics.add.staticGroup();
const img = this.players.create(100, 300, 'alk');
const imgTwo = this.players.create(500, 300, 'magmar');
img.setScale(0.5);
imgTwo.setScale(0.5);
this.playerOneHp = this.add.text(20, 200, 'HP', { fontFamily: 'Arial', fontSize: 24, color: '#00ff00' });
this.playerTwoHp = this.add.text(450, 200, 'HP', { fontFamily: 'Arial', fontSize: 24, color: '#00ff00' });
this.loaded = true;
}
createAnim() {
const config = {
key: 'explodeAnimation',
frames: this.anims.generateFrameNumbers('explosion', { start: 0, end: 23, first: 23 }),
frameRate: 20,
repeat: 0,
};
this.anims.create(config);
}
setPlayerOneHp(health) {
this.playerOneHp.text = health;
}
setPlayerTwoHp(health) {
this.playerTwoHp.text = health;
}
explode(proj) {
this.proj.disableBody(true, true);
this.emitter.stop();
const sprite = this.add.sprite(proj.x, proj.y, 'explosion').play('explodeAnimation');
sprite.setScale(3);
}
}
PhaserCombat.prototype.skills = require('./phaser.skills.js');
module.exports = PhaserCombat;