initial commit
This commit is contained in:
15
src/entities/Exit.ts
Normal file
15
src/entities/Exit.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import Phaser from "phaser";
|
||||
|
||||
export class Exit extends Phaser.GameObjects.Rectangle {
|
||||
static readonly WIDTH = 40;
|
||||
static readonly HEIGHT = 80;
|
||||
static readonly COLOR = 0xffd700;
|
||||
|
||||
declare body: Phaser.Physics.Arcade.StaticBody;
|
||||
|
||||
constructor(scene: Phaser.Scene, x: number, groundY: number) {
|
||||
super(scene, x, groundY - Exit.HEIGHT / 2, Exit.WIDTH, Exit.HEIGHT, Exit.COLOR);
|
||||
scene.add.existing(this);
|
||||
scene.physics.add.existing(this, true);
|
||||
}
|
||||
}
|
||||
13
src/entities/Platform.ts
Normal file
13
src/entities/Platform.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import Phaser from "phaser";
|
||||
|
||||
export class Platform extends Phaser.GameObjects.Rectangle {
|
||||
static readonly COLOR = 0x4a7c3a;
|
||||
|
||||
declare body: Phaser.Physics.Arcade.StaticBody;
|
||||
|
||||
constructor(scene: Phaser.Scene, x: number, y: number, width: number, height: number) {
|
||||
super(scene, x, y, width, height, Platform.COLOR);
|
||||
scene.add.existing(this);
|
||||
scene.physics.add.existing(this, true);
|
||||
}
|
||||
}
|
||||
56
src/entities/Player.ts
Normal file
56
src/entities/Player.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import Phaser from "phaser";
|
||||
|
||||
export class Player extends Phaser.GameObjects.Rectangle {
|
||||
static readonly SIZE = 32;
|
||||
static readonly COLOR = 0xff4444;
|
||||
static readonly MOVE_SPEED = 250;
|
||||
static readonly JUMP_VELOCITY = 600;
|
||||
|
||||
declare body: Phaser.Physics.Arcade.Body;
|
||||
|
||||
private cursors: Phaser.Types.Input.Keyboard.CursorKeys;
|
||||
private keyA: Phaser.Input.Keyboard.Key;
|
||||
private keyD: Phaser.Input.Keyboard.Key;
|
||||
private keyW: Phaser.Input.Keyboard.Key;
|
||||
private keySpace: Phaser.Input.Keyboard.Key;
|
||||
|
||||
constructor(scene: Phaser.Scene, x: number, y: number) {
|
||||
super(scene, x, y, Player.SIZE, Player.SIZE, Player.COLOR);
|
||||
scene.add.existing(this);
|
||||
scene.physics.add.existing(this);
|
||||
this.body.setCollideWorldBounds(true);
|
||||
|
||||
const kb = scene.input.keyboard!;
|
||||
this.cursors = kb.createCursorKeys();
|
||||
this.keyA = kb.addKey("A");
|
||||
this.keyD = kb.addKey("D");
|
||||
this.keyW = kb.addKey("W");
|
||||
this.keySpace = kb.addKey("SPACE");
|
||||
}
|
||||
|
||||
override update(): void {
|
||||
const left = this.cursors.left.isDown || this.keyA.isDown;
|
||||
const right = this.cursors.right.isDown || this.keyD.isDown;
|
||||
const jumpPressed =
|
||||
Phaser.Input.Keyboard.JustDown(this.cursors.up) ||
|
||||
Phaser.Input.Keyboard.JustDown(this.keyW) ||
|
||||
Phaser.Input.Keyboard.JustDown(this.keySpace);
|
||||
|
||||
if (left) {
|
||||
this.body.setVelocityX(-Player.MOVE_SPEED);
|
||||
} else if (right) {
|
||||
this.body.setVelocityX(Player.MOVE_SPEED);
|
||||
} else {
|
||||
this.body.setVelocityX(0);
|
||||
}
|
||||
|
||||
if (jumpPressed && this.body.touching.down) {
|
||||
this.body.setVelocityY(-Player.JUMP_VELOCITY);
|
||||
}
|
||||
}
|
||||
|
||||
freeze(): void {
|
||||
this.body.setVelocity(0, 0);
|
||||
this.body.moves = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user