Files
MI2Mario/src/scenes/Game.js
T
2020-05-31 11:08:56 +02:00

244 lines
7.9 KiB
JavaScript

/*
* Copyright (c) 2020. Pascal Syma <pascal@syma.dev>
* All rights reserved.
*/
import Player from "../entities/Player";
import Bat from "../entities/Enemys/Bat";
import Item from "../entities/Item";
export default class Game extends Phaser.Scene {
constructor() {
super({
key: "Game",
pixelArt: true,
physics: {
default: 'arcade',
arcade: {
gravity: {y: 300},
// debug: true
}
}
});
}
static setTileCollision(mapLayer, idxOrArray, dirs) {
let mFunc; // tile index matching function
if (idxOrArray.length) {
// if idxOrArray is an array, use a function with a loop
mFunc = function (inp) {
for (let i = 0; i < idxOrArray.length; i++) {
if (idxOrArray[i] === inp - 1) {
return true;
}
}
return false;
};
} else {
// if idxOrArray is a single number, use a simple function
mFunc = function (inp) {
return inp - 1 === idxOrArray;
};
}
// get the 2-dimensional tiles array for this layer
// const d = mapLayer.map.layers[mapLayer.index].data;
const d = mapLayer.layer.data;
for (let i = 0; i < d.length; i++) {
for (let j = 0; j < d[i].length; j++) {
const t = d[i][j];
if (mFunc(t.index)) {
t.collideUp = dirs.top;
t.collideDown = dirs.bottom;
t.collideLeft = dirs.left;
t.collideRight = dirs.right;
t.faceTop = dirs.top;
t.faceBottom = dirs.bottom;
t.faceLeft = dirs.left;
t.faceRight = dirs.right;
}
}
}
}
create() {
this.anims.create({
key: 'idle',
frames: this.anims.generateFrameNumbers('char', {start: 126, end: 131}),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'run',
frames: this.anims.generateFrameNumbers('char', {start: 154, end: 161}),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'jump_start',
frames: this.anims.generateFrameNumbers('char', {start: 140, end: 143}),
frameRate: 10
});
this.anims.create({
key: 'jump',
frames: this.anims.generateFrameNumbers('char', {start: 144, end: 145}),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'attack_a',
frames: this.anims.generateFrameNumbers('char', {start: 0, end: 13}),
frameRate: 15
});
this.anims.create({
key: 'attack_b',
frames: this.anims.generateFrameNumbers('char', {start: 14, end: 26}),
frameRate: 15
});
this.anims.create({
key: 'attack_c',
frames: this.anims.generateFrameNumbers('char', {start: 28, end: 41}),
frameRate: 15
});
this.anims.create({
key: 'die',
frames: this.anims.generateFrameNumbers('char', {start: 42, end: 51}),
frameRate: 10
});
this.anims.create({
key: 'batIdle',
frames: this.anims.generateFrameNumbers('batFly', {start: 0, end: 5}),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'batDie',
frames: this.anims.generateFrameNumbers('batDie', {start: 0, end: 4}),
frameRate: 15
});
const map = this.make.tilemap({key: "map"});
const tileset = map.addTilesetImage("tileset", "tiles", 16, 16, 1, 2);
this.backLayer = map.createStaticLayer("Back", tileset, 0, 0);
this.backLayer2 = map.createStaticLayer("Back2", tileset, 0, 0);
this.worldLayer = map.createDynamicLayer("Main", tileset, 0, 0);
const spawnPoint = map.findObject("Objects", obj => obj.name === "player");
this.enemys = this.add.group();
this.items = this.add.group();
this.player = new Player(this, spawnPoint, 'char');
map.objects[0].objects.filter(obj => obj.name === "bat").forEach(s => {
let bat = new Bat(this, s);
bat.fly(s.type === "" ? 90 : parseInt(s.type));
this.enemys.add(bat)
})
map.objects[0].objects.filter(obj => obj.name === "item").forEach(s => {
let item = new Item(this, s);
this.items.add(item)
})
this.worldLayer.setCollisionByProperty({collides: true});
Game.setTileCollision(this.worldLayer, [29, 30, 31, 39], {
top: true,
bottom: false,
right: false,
left: false
})
this.physics.world.bounds.width = this.backLayer.width;
this.physics.world.bounds.height = this.backLayer.height;
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
this.cameras.main.setBackgroundColor('#ccccff');
this.cameras.main.startFollow(this.player);
this.physics.add.collider(this.player, this.worldLayer);
this.physics.add.overlap(this.player.attackHitbox, this.enemys, (h, b) => {
if (h.getData('active') && h.overlap !== undefined) {
h.overlap(b);
}
})
this.keyW = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
this.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
this.keyA = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);
this.keyD = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
this.keySpace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
this.keyUp = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP);
this.keyLeft = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT);
this.keyRight = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT);
this.keyDown = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN);
// this.keyDie = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.C);
this.keyPickup = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.C);
this.keyAction1 = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q);
this.keyAction2 = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.E);
this.physics.add.overlap(this.player, this.items, (p, i) => {
if (this.keyPickup.isDown && i.canPickup()) {
p.pickup(i);
this.keyPickup.reset();
}
})
}
update(time, delta) {
this.enemys.getChildren().forEach(c => {
c.update()
});
this.player.update(time, delta);
}
dieActive() {
return false;
// return this.keyDie.isDown;
}
rightActive() {
return this.keyRight.isDown || this.keyD.isDown/* || (this.game.input.activePointer.isDown &&
this.game.input.activePointer.x > 2*this.game.config.width/3)*/;
}
leftActive() {
return this.keyLeft.isDown || this.keyA.isDown/* || (this.game.input.activePointer.isDown &&
this.game.input.activePointer.x < this.game.config.width/3)*/;
}
downActive() {
return this.keyDown.isDown || this.keyS.isDown/* || (this.game.input.activePointer.isDown &&
this.game.input.activePointer.y > 2*this.game.config.height/3)*/;
}
upActive() {
return this.keyUp.isDown || this.keyW.isDown/* || (this.game.input.activePointer.isDown &&
this.game.input.activePointer.y < this.game.config.height/3)*/;
}
}