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;
|
||||
}
|
||||
}
|
||||
22
src/main.ts
Normal file
22
src/main.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import Phaser from "phaser";
|
||||
import { GameScene } from "./scenes/GameScene";
|
||||
|
||||
const GAME_WIDTH = 800;
|
||||
const GAME_HEIGHT = 600;
|
||||
const GRAVITY = 1500;
|
||||
|
||||
new Phaser.Game({
|
||||
type: Phaser.AUTO,
|
||||
width: GAME_WIDTH,
|
||||
height: GAME_HEIGHT,
|
||||
backgroundColor: "#1a1a2e",
|
||||
parent: "screen",
|
||||
physics: {
|
||||
default: "arcade",
|
||||
arcade: {
|
||||
gravity: { x: 0, y: GRAVITY },
|
||||
debug: false,
|
||||
},
|
||||
},
|
||||
scene: [GameScene],
|
||||
});
|
||||
55
src/scenes/GameScene.ts
Normal file
55
src/scenes/GameScene.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import Phaser from "phaser";
|
||||
import { Player } from "../entities/Player";
|
||||
import { Exit } from "../entities/Exit";
|
||||
import { Platform } from "../entities/Platform";
|
||||
|
||||
export class GameScene extends Phaser.Scene {
|
||||
private static readonly FLOOR_TOP = 500;
|
||||
private static readonly FLOOR_HEIGHT = 100;
|
||||
|
||||
private player!: Player;
|
||||
private exit!: Exit;
|
||||
private platform!: Platform;
|
||||
private levelCompleteText!: Phaser.GameObjects.Text;
|
||||
private levelCompleted = false;
|
||||
|
||||
constructor() {
|
||||
super({ key: "GameScene" });
|
||||
}
|
||||
|
||||
create(): void {
|
||||
const width = this.scale.width;
|
||||
const height = this.scale.height;
|
||||
const floorTop = GameScene.FLOOR_TOP;
|
||||
const floorH = GameScene.FLOOR_HEIGHT;
|
||||
|
||||
this.platform = new Platform(this, width / 2, floorTop + floorH / 2, width, floorH);
|
||||
this.exit = new Exit(this, 60, floorTop);
|
||||
this.player = new Player(this, width - Player.SIZE - 40, floorTop - Player.SIZE / 2);
|
||||
|
||||
this.physics.add.collider(this.player, this.platform);
|
||||
this.physics.add.overlap(this.player, this.exit, () => this.completeLevel());
|
||||
|
||||
this.levelCompleteText = this.add
|
||||
.text(width / 2, height / 2, "LEVEL COMPLETED!", {
|
||||
fontFamily: "Arial",
|
||||
fontSize: "48px",
|
||||
color: "#ffffff",
|
||||
fontStyle: "bold",
|
||||
})
|
||||
.setOrigin(0.5)
|
||||
.setVisible(false);
|
||||
}
|
||||
|
||||
override update(): void {
|
||||
if (this.levelCompleted) return;
|
||||
this.player.update();
|
||||
}
|
||||
|
||||
private completeLevel(): void {
|
||||
if (this.levelCompleted) return;
|
||||
this.levelCompleted = true;
|
||||
this.player.freeze();
|
||||
this.levelCompleteText.setVisible(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user