Skip to content

Commit

Permalink
Player pos on stats (#43)
Browse files Browse the repository at this point in the history
* Add player position to stats line
* Clamp stats.health value between 0 and 5 for stats line. Saved values from earlier versions may have different values.
  • Loading branch information
WimYedema authored Jan 27, 2024
1 parent a5fe6cb commit 730fe40
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/core/levelLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export abstract class LevelLayout extends ex.Scene implements iSceneNode {

protected playerStart: ex.Vector = ex.vec(2, 2);
protected player?: Player;
protected assignment: string = "";

/**
* Populate the level with actors, creating the walls, floors, monsters,
Expand Down Expand Up @@ -53,22 +54,36 @@ export abstract class LevelLayout extends ex.Scene implements iSceneNode {
this.camera.clearAllStrategies();
this.camera.strategy.elasticToActor(player, 0.05, 0.1);
}
statsLine() {
let x = 0;
let y = 0;
let h = stats.health;
if (this.player !== undefined) {
x = Math.floor(this.player.pos.x / tileSize);
y = -Math.floor(this.player.pos.y / tileSize);
}
h = Math.max(0, Math.min(stats.health, 5));
return (
"♥".repeat(h) +
"♡".repeat(5 - h) +
" x:" +
x +
" y:" +
y +
" Opdracht: " +
this.assignment
);
}
onInitialize(engine: ex.Engine) {
this.layoutLevel(engine);

this.player = new Player(this.playerStart.x, this.playerStart.y);

engine.add(this.player);
let assignment = "src/scenes/" + this.thisScene + ".ts";
this.assignment = "src/scenes/" + this.thisScene + ".ts";

const scoreLabel = new ex.Label({
text:
"♥".repeat(stats.health) +
"♡".repeat(5 - stats.health) +
" S" +
stats.score +
" Opdracht: " +
assignment,
text: this.statsLine(),
pos: ex.vec(10, 20),
z: 2,
});
Expand All @@ -79,13 +94,7 @@ export abstract class LevelLayout extends ex.Scene implements iSceneNode {
scoreLabel.transform.coordPlane = ex.CoordPlane.Screen;
scoreLabel.color = ex.Color.Black;
scoreLabel.on("preupdate", (evt) => {
scoreLabel.text =
"♥".repeat(stats.health) +
"♡".repeat(5 - stats.health) +
" S" +
stats.score +
" Opdracht: " +
assignment;
scoreLabel.text = this.statsLine();
});
engine.add(scoreLabel);

Expand Down
1 change: 1 addition & 0 deletions src/core/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class Stats {
public nextScene: boolean = false;
public currentNode: string = "playerSelect";
public score: number = 0;

load(d: any) {
this.charName = d["charName"] ?? this.charName;
this.health = d["health"] ?? this.health;
Expand Down

0 comments on commit 730fe40

Please sign in to comment.