Skip to content

Commit 703fbff

Browse files
authored
Merge branch 'main' into feature/buttons
2 parents 3a878d4 + be7bed3 commit 703fbff

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {GameObjects, Scene, Input} from "phaser";
22
import { ButtonManager } from "./ButtonManager";
33
import Gamepad = Phaser.Input.Gamepad.Gamepad;
44

5-
//import via (import {Button} from "../custom_classes/Button")
65
export class Button {
76
readonly x: number;
87
readonly y: number;
@@ -36,7 +35,7 @@ export class Button {
3635
// scales the button
3736
this.button.setScale(scale, scale);
3837

39-
// Buttons actions
38+
// On (Un-)Hover
4039
this.button.on('pointerover', () => {
4140
this.setFocus(true);
4241
});
@@ -73,6 +72,7 @@ export class Button {
7372
this.button.setAlpha(focused ? 0.7 : 1);
7473
}
7574

75+
// Helper methode to change image
7676
setImage(newImage: string): void {
7777
this.image = newImage;
7878
this.button.setTexture(newImage);

src/game/loader/Boot.ts

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

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

6260
// Switch to main menu

src/game/main.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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';
1211
import Gamepad = Phaser.Input.Gamepad.Gamepad;
@@ -21,11 +20,6 @@ const background: number = 0xd3d1fa;
2120
const debugMode: boolean = false;
2221
const pixelFontName: string = "pixelFont";
2322
const api: string | undefined = undefined // "http://localhost:3000"; // Dont forget the http(s) | undefined -> localstorage
24-
const getRandomInt: Function = (min: number, max: number): number => {//return a random number between min(inclusive) and max(inclusive)
25-
const minCeiled: number = Math.ceil(min);
26-
const maxFloored: number = Math.floor(max);
27-
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled)
28-
}
2923
let speed: number = 1;
3024

3125
// Global variables
@@ -82,7 +76,7 @@ export default function startGame(): void {
8276
new Game({...config});
8377
}
8478

85-
// Displays player
79+
// Displays player in the corner
8680
export function displayPlayer(that: any): void {
8781
const player: Image = that.add.image(globalConsts.santaX, globalConsts.santaY, 'player2');
8882
player.setScale(4);
@@ -109,5 +103,10 @@ export function escapeOption(that: Scene, gamepad?: Gamepad): void {
109103
}
110104
}
111105

112-
// TODO | add controller 2 -> mainMenu
106+
107+
// Get random Int between to points
108+
function getRandomInt(min: number, max: number): number {
109+
const minCeiled: number = Math.ceil(min);
110+
const maxFloored: number = Math.floor(max);
111+
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled)
113112
}

src/game/scenes/GameOver.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function prompt(): void {
134134
// Cancel
135135
if (prompt == null) return;
136136

137-
// YOU cannot be used
137+
// "YOU" cannot be used
138138
if (prompt.toUpperCase() == "YOU") {
139139
alert("This cannot be used as name");
140140
return;
@@ -176,7 +176,7 @@ function prompt(): void {
176176

177177
// Removes and rerenders leaderboard
178178
clearsLeaderboardLine();
179-
renderLeaderboard();
179+
renderLeaderboard().then();
180180

181181
// Disables button
182182
saveButton.setImage("button_saved");
@@ -272,6 +272,8 @@ export async function generateCode(): Promise<string | undefined> {
272272
async function saveLeaderboard(name: string, key: string | null, value: number): Promise<Response | undefined> {
273273
// Local-storage
274274
if (globalConsts.apiURL == undefined) {
275+
const oldEntry: leaderboardEntry | undefined = getEntryByName(name);
276+
if (oldEntry && oldEntry.score > value) return new Response(JSON.stringify({success: false}), {status: 208}); // Better score exists
275277
sortedLeaderboard?.push({name: name, score: value}); // Adds entry
276278
sortedLeaderboard?.filter(entry => entry.name !== "YOU"); // Removes YOU
277279
localStorage.setItem("leaderboard", JSON.stringify(sortedLeaderboard, null, 0))
@@ -297,3 +299,8 @@ async function saveLeaderboard(name: string, key: string | null, value: number):
297299
return undefined;
298300
}
299301
}
302+
303+
// Gets leaderboard entry by name
304+
function getEntryByName(name: string): leaderboardEntry | undefined {
305+
return sortedLeaderboard?.find(entry => entry.name === name);
306+
}

src/game/scenes/Leaderboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export let sortedLeaderboard: leaderboardEntry[] | undefined;
3333
let clickedRefresh: number = 0;
3434
let buttonManager: ButtonManager;
3535

36-
// Scene class
36+
// Full Leaderboard
3737
export class Leaderboard extends Scene {
3838

3939
// Constructor

0 commit comments

Comments
 (0)