Skip to content
Permalink
Browse files

wasd support & add slime sheet

  • Loading branch information...
mipearson committed Oct 7, 2018
1 parent b39e9b4 commit f24fb45a40c15a834aa968318b678e281c5f0d0f
Showing with 83 additions and 34 deletions.
  1. +34 −12 src/assets/Graphics.ts
  2. +36 −20 src/entities/Player.ts
  3. +11 −0 src/scenes/DungeonScene.ts
  4. +2 −2 src/scenes/ReferenceScene.ts
@@ -1,9 +1,12 @@
import RogueEnvironment from "../../assets/fongoose/RogueEnvironment.png";
import RoguePlayer from "../../assets/fongoose/RoguePlayer.png";
import RogueSlime from "../../assets/fongoose/RogueSlime.png";
import RogueItems from "../../assets/fongoose/RogueItems.png";

import Util from "../../assets/Util.png";

export default class Graphics {
static readonly environment = {
export default {
environment: {
name: "environment",
width: 16,
height: 16,
@@ -34,9 +37,9 @@ export default class Graphics {
}
}
}
};
},

static readonly player = {
player: {
name: "player",
width: 32,
height: 32,
@@ -98,19 +101,38 @@ export default class Graphics {
repeat: false
}
}
};
},

static readonly util = {
slime: {
name: "slime",
width: 32,
height: 32,
file: RogueSlime,
animations: {
idle: {
name: "slimeIdle",
start: 0x01,
end: 0x04,
frameRate: 6,
repeat: true
}
}
},

items: {
name: "items",
width: 16,
height: 16,
file: RogueItems
},

util: {
name: "util",
width: 16,
height: 16,
file: Util,
indices: {
black: 0x00
}
};

static randomIndex = (indices: Array<number>): number => {
return indices[Math.floor(Math.random() * indices.length)];
};
}
}
};
@@ -6,33 +6,44 @@ const attackSpeed = 500;
const attackDuration = 165;
const attackCooldown = attackDuration * 2;

interface Keys {
up: Phaser.Input.Keyboard.Key;
down: Phaser.Input.Keyboard.Key;
left: Phaser.Input.Keyboard.Key;
right: Phaser.Input.Keyboard.Key;
space: Phaser.Input.Keyboard.Key;
w: Phaser.Input.Keyboard.Key;
a: Phaser.Input.Keyboard.Key;
s: Phaser.Input.Keyboard.Key;
d: Phaser.Input.Keyboard.Key;
}

export default class Player {
public sprite: Phaser.Physics.Arcade.Sprite;
private keys: Phaser.Input.Keyboard.CursorKeys;
private keys: Keys;

private attackUntil: number;
private attackLockedUntil: number;
private emitter: Phaser.GameObjects.Particles.ParticleEmitter;
private body: Phaser.Physics.Arcade.Body;

constructor(x: number, y: number, scene: Phaser.Scene) {
Object.values(Graphics.player.animations).forEach(anim => {
if (!scene.anims.get(anim.name)) {
scene.anims.create({
key: anim.name,
frames: scene.anims.generateFrameNumbers(Graphics.player.name, anim),
frameRate: anim.frameRate,
repeat: anim.repeat ? -1 : 0
});
}
});

this.sprite = scene.physics.add.sprite(x, y, Graphics.player.name, 0);
this.sprite.setSize(8, 8);
this.sprite.setOffset(12, 20);
this.sprite.anims.play(Graphics.player.animations.idle.name);

this.keys = scene.input.keyboard.createCursorKeys();
this.keys = scene.input.keyboard.addKeys({
up: Phaser.Input.Keyboard.KeyCodes.UP,
down: Phaser.Input.Keyboard.KeyCodes.DOWN,
left: Phaser.Input.Keyboard.KeyCodes.LEFT,
right: Phaser.Input.Keyboard.KeyCodes.RIGHT,
space: Phaser.Input.Keyboard.KeyCodes.SPACE,
w: "w",
a: "a",
s: "s",
d: "d"
}) as Keys;

this.attackUntil = 0;
this.attackLockedUntil = 0;
@@ -63,27 +74,32 @@ export default class Player {
}
this.body.setVelocity(0);

if (!this.body.blocked.left && keys.left!.isDown) {
const left = keys.left.isDown || keys.a.isDown;
const right = keys.right.isDown || keys.d.isDown;
const up = keys.up.isDown || keys.w.isDown;
const down = keys.down.isDown || keys.s.isDown;

if (!this.body.blocked.left && left) {
this.body.setVelocityX(-speed);
this.sprite.setFlipX(true);
} else if (!this.body.blocked.right && keys.right!.isDown) {
} else if (!this.body.blocked.right && right) {
this.body.setVelocityX(speed);
this.sprite.setFlipX(false);
}

if (!this.body.blocked.up && keys.up!.isDown) {
if (!this.body.blocked.up && up) {
this.body.setVelocityY(-speed);
} else if (!this.body.blocked.down && keys.down!.isDown) {
} else if (!this.body.blocked.down && down) {
this.body.setVelocityY(speed);
}

if (keys.left!.isDown || keys.right!.isDown) {
if (left || right) {
moveAnim = Graphics.player.animations.walk.name;
attackAnim = Graphics.player.animations.slash.name;
} else if (keys.down!.isDown) {
} else if (down) {
moveAnim = Graphics.player.animations.walk.name;
attackAnim = Graphics.player.animations.slashDown.name;
} else if (keys.up!.isDown) {
} else if (up) {
moveAnim = Graphics.player.animations.walkBack.name;
attackAnim = Graphics.player.animations.slashUp.name;
} else {
@@ -40,6 +40,17 @@ export default class DungeonScene extends Phaser.Scene {

this.fov = new FOVLayer(map);

Object.values(Graphics.player.animations).forEach(anim => {
if (!this.anims.get(anim.name)) {
this.anims.create({
key: anim.name,
frames: this.anims.generateFrameNumbers(Graphics.player.name, anim),
frameRate: anim.frameRate,
repeat: anim.repeat ? -1 : 0
});
}
});

this.player = new Player(
this.tilemap.tileToWorldX(map.startingX),
this.tilemap.tileToWorldY(map.startingY),
@@ -1,7 +1,7 @@
import Phaser from "phaser";
import Graphics from "../assets/Graphics";

const tilesets = [Graphics.environment, Graphics.player, Graphics.util];
const tilesets = Object.values(Graphics);

export default class ReferenceScene extends Phaser.Scene {
index: number;
@@ -75,7 +75,7 @@ export default class ReferenceScene extends Phaser.Scene {
8,
0x222222
)
.setAltFillStyle(0x333333)
.setAltFillStyle(0x2a2a2a)
.setOutlineStyle();
layer.setDepth(5);
this.group.add(grid);

0 comments on commit f24fb45

Please sign in to comment.
You can’t perform that action at this time.