Skip to content

Commit be7bed3

Browse files
committed
style: Small code updates
- Comment code better - Cleaned up the code - Blocked worse scores for local-storage
1 parent 982890b commit be7bed3

File tree

6 files changed

+35
-26
lines changed

6 files changed

+35
-26
lines changed

src/game/custom_classes/Background.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export enum Layer {
1313
const layers: Layer[] = [Layer.FRONT, Layer.MIDDLE, /*Layer.BACK*/];
1414

1515
// Config
16+
const backgroundImage: string = "gameBackground3";
1617
const backgroundSpeed: number = 1;
1718
const houseKeys: string[] = ["house1", "house2", "house3", "house4", "church"];
1819
const layerPropertiesMap: Record<Layer, LayerProperties> = {
@@ -43,7 +44,7 @@ const layerPropertiesMap: Record<Layer, LayerProperties> = {
4344
scale: () => 4 + Math.random() * 0.2,
4445
depth: -3,
4546
y: () => globalConsts.gameHeight - 55,
46-
speed: () => 1.2 * globalConsts.houseSpeed
47+
speed: () => 1.2 * globalConsts.houseSpeed
4748
speed: () => 0.6,
4849
opacity: 0.6,
4950
// Data
@@ -92,12 +93,12 @@ export function spawnHouses(scene: Scene): void {
9293
// Create Background
9394
function createBackground(): void {
9495
// Background A
95-
backgroundA = currentScene.add.image(globalConsts.gameWidth / 2, globalConsts.gameHeight / 2.2, "gameBackground3");
96+
backgroundA = currentScene.add.image(globalConsts.gameWidth / 2, globalConsts.gameHeight / 2.2, backgroundImage);
9697
backgroundA.setScale(4.5);
9798
backgroundA.setDepth(-4);
9899

99100
// Background B
100-
backgroundB = currentScene.add.image(globalConsts.gameWidth / 2 + backgroundA.displayWidth, globalConsts.gameHeight / 2.2, "gameBackground3");
101+
backgroundB = currentScene.add.image(globalConsts.gameWidth / 2 + backgroundA.displayWidth, globalConsts.gameHeight / 2.2, backgroundImage);
101102
backgroundB.setScale(4.5);
102103
backgroundB.setDepth(-4);
103104
}
@@ -129,26 +130,27 @@ function spawnHouse(layer: Layer): void {
129130
// Moves every house on every layer
130131
export function updateMovement(): void {
131132
for (let layer of layers) moveHouses(getLayerDetails(layer).houses, getLayerDetails(layer).speed()); // Moves houses
132-
moveBackground(); // ️ Moves Background
133+
moveBackground(); // ️ Moves Backgrounds
133134
}
134135

135136
// Moves background
136137
function moveBackground(): void {
137138
backgroundA.x -= backgroundSpeed * globalConsts.backgroundSpeed;
138139
backgroundB.x -= backgroundSpeed * globalConsts.backgroundSpeed;
139140

140-
141141
if (backgroundA.x <= -backgroundA.displayWidth / 2) backgroundA.x = backgroundB.x + backgroundB.displayWidth;
142142
if (backgroundB.x <= -backgroundB.displayWidth / 2) backgroundB.x = backgroundA.x + backgroundA.displayWidth;
143143
}
144144

145145

146146
// Moves every house on specified layer
147147
function moveHouses(houses: Phaser.GameObjects.Image[], speed: number): void {
148+
// Moves houses
148149
houses.forEach(house => {
149150
house.x -= speed;
150151
});
151152

153+
// Destroys houses when out of bounds
152154
for (let i = houses.length - 3; i >= 0; i--) {
153155
if (houses[i].x < -houses[i].width * 4 - 50) {
154156
houses[i].destroy();

src/game/custom_classes/Button.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {GameObjects, Scene} from "phaser";
22

3-
//import via (import {Button} from "../custom_classes/Button")
43
export class Button {
54
readonly x: number;
65
readonly y: number;
@@ -13,7 +12,7 @@ export class Button {
1312
//initialise variables
1413
this.x = x; //x position
1514
this.y = y; //y position
16-
this.image = image; //image as string
15+
this.image = image; // imageID
1716
this.scene = curScene; //the scene the buttons is in (this)
1817

1918
// add buttons to the current scene (just "this" in the scene you implement the buttons)
@@ -25,18 +24,20 @@ export class Button {
2524
// scales the button
2625
this.button.setScale(scale, scale);
2726

28-
// Buttons actions
27+
// On (Un-)Hover
2928
this.button.on('pointerover', () => {
3029
this.button.setAlpha(0.7);
3130
});
3231

3332
this.button.on('pointerout', () => {
3433
this.button.setAlpha(1);
3534
});
35+
3636
// Calls function that is provided
3737
this.button.on('pointerdown', onButtonPressed);
3838
}
3939

40+
// Helper methode to change image
4041
setImage(newImage: string): void {
4142
this.image = newImage;
4243
this.button.setTexture(newImage)

src/game/loader/Boot.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ export class Boot extends Scene {
5353
}
5454

5555
// When all assets are done
56-
// When all the assets have loaded, it's often worth creating global objects here that the rest of the game can use.
57-
// For example, you can define global animations here, so we can use them in other scenes.
5856
export function start(scene: Phaser.Scenes.ScenePlugin): void {
5957

6058
// Switch to main menu

src/game/main.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {Preloader} from './loader/Preloader.ts';
66
import {Options} from './scenes/Options.ts';
77
import {Credits} from './scenes/Credits.ts';
88
import {Controls} from './scenes/Controls.ts';
9-
import Image = Phaser.GameObjects.Image;
109
import {ThatGame} from './thatFolder/ThatGame.ts';
1110
import {Leaderboard} from './scenes/Leaderboard.ts';
11+
import Image = Phaser.GameObjects.Image;
1212

1313
// Config
1414
const gameW: number = 1024;
@@ -19,11 +19,6 @@ const background: number = 0xd3d1fa;
1919
const debugMode: boolean = false;
2020
const pixelFontName: string = "pixelFont";
2121
const api: string | undefined = undefined // "http://localhost:3000"; // Dont forget the http(s) | undefined -> localstorage
22-
const getRandomInt: Function = (min: number, max: number): number => {//return a random number between min(inclusive) and max(inclusive)
23-
const minCeiled: number = Math.ceil(min);
24-
const maxFloored: number = Math.floor(max);
25-
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled)
26-
}
2722
let speed: number = 1;
2823

2924
// Global variables
@@ -72,11 +67,11 @@ const config: Phaser.Types.Core.GameConfig = {
7267
};
7368

7469
// Creates new Game instance
75-
export default function startGame() {
76-
return new Game({...config});
70+
export default function startGame(): void {
71+
new Game({...config});
7772
}
7873

79-
// Displays player
74+
// Displays player in the corner
8075
export function displayPlayer(that: any): void {
8176
const player: Image = that.add.image(globalConsts.santaX, globalConsts.santaY, 'player2');
8277
player.setScale(4);
@@ -88,3 +83,10 @@ export function displayDebug(scene: Scene): void {
8883
scene.add.graphics().setAlpha(0.75);
8984
scene.physics.world.createDebugGraphic();
9085
}
86+
87+
// Get random Int between to points
88+
function getRandomInt(min: number, max: number): number {
89+
const minCeiled: number = Math.ceil(min);
90+
const maxFloored: number = Math.floor(max);
91+
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled)
92+
}

src/game/scenes/GameOver.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import {Scene} from 'phaser';
22
import {displayPlayer, globalConsts} from '../main.ts';
33
import {formatTime} from '../thatFolder/ThatPlayer.ts';
44
import {Button} from '../custom_classes/Button.ts';
5+
import {fetchLeaderboard, removeEntry, sortedLeaderboard, sortLeaderboard} from './Leaderboard.ts';
56
import Text = Phaser.GameObjects.Text;
67
import Rectangle = Phaser.GameObjects.Rectangle;
7-
import {fetchLeaderboard, removeEntry, sortedLeaderboard, sortLeaderboard} from './Leaderboard.ts';
88

99
// config
1010
const range: number = 2;
@@ -64,7 +64,6 @@ export class GameOver extends Scene {
6464

6565
// Save score button
6666
saveButton = new Button(700, 700, 7, "button_save", scene, () => prompt());
67-
// TODO | add "to full leaderboard" button
6867

6968
// Clicker
7069
const blocker: Rectangle = this.add.rectangle(0, 0, globalConsts.gameWidth, globalConsts.gameHeight, 0x000000, 0.001)
@@ -91,7 +90,7 @@ function prompt(): void {
9190
// Cancel
9291
if (prompt == null) return;
9392

94-
// YOU cannot be used
93+
// "YOU" cannot be used
9594
if (prompt.toUpperCase() == "YOU") {
9695
alert("This cannot be used as name");
9796
return;
@@ -133,7 +132,7 @@ function prompt(): void {
133132

134133
// Removes and rerenders leaderboard
135134
clearsLeaderboardLine();
136-
renderLeaderboard();
135+
renderLeaderboard().then();
137136

138137
// Resets button
139138
saveButton.setImage("button_saved");
@@ -229,10 +228,12 @@ export async function generateCode(): Promise<string | undefined> {
229228
async function saveLeaderboard(name: string, key: string | null, value: number): Promise<Response | undefined> {
230229
// Local-storage
231230
if (globalConsts.apiURL == undefined) {
231+
const oldEntry: leaderboardEntry | undefined = getEntryByName(name);
232+
if (oldEntry && oldEntry.score > value) return new Response(JSON.stringify({success: false}), {status: 208}); // Better score exists
232233
sortedLeaderboard?.push({name: name, score: value}); // Adds entry
233234
sortedLeaderboard?.filter(entry => entry.name !== "YOU"); // Removes YOU
234235
localStorage.setItem("leaderboard", JSON.stringify(sortedLeaderboard, null, 0))
235-
return new Response(JSON.stringify({ success: true }), { status: 200 });
236+
return new Response(JSON.stringify({success: true}), {status: 200});
236237
}
237238

238239
// Request info
@@ -254,3 +255,8 @@ async function saveLeaderboard(name: string, key: string | null, value: number):
254255
return undefined;
255256
}
256257
}
258+
259+
// Gets leaderboard entry by name
260+
function getEntryByName(name: string): leaderboardEntry | undefined {
261+
return sortedLeaderboard?.find(entry => entry.name === name);
262+
}

src/game/scenes/Leaderboard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ let currentCategory: leaderboardCategory = "default";
3131
export let sortedLeaderboard: leaderboardEntry[] | undefined;
3232
let clickedRefresh: number = 0;
3333

34-
// Scene class
34+
// Full Leaderboard
3535
export class Leaderboard extends Scene {
3636

3737
// Constructor
@@ -60,7 +60,7 @@ export class Leaderboard extends Scene {
6060

6161
// Subtitle
6262
subtitle = scene.add.text(110, 163, "Loading this text", {
63-
font: "30px pixelFont",
63+
font: "27px pixelFont",
6464
color: "#ffffff",
6565
align: 'center'
6666
});

0 commit comments

Comments
 (0)