From bb24d5df444139a99590f8640c56b9f3e12b234f Mon Sep 17 00:00:00 2001 From: Tomas Horsky Date: Wed, 29 Apr 2026 14:11:50 +0900 Subject: [PATCH] refactoring and polishing --- src/game/constants.js | 1 - src/game/game.js | 7 +- src/game/platformTypes.js | 97 +++++++++++++++++++++++++++ src/game/platforms.js | 98 +--------------------------- src/game/{gameUtils.js => player.js} | 9 +-- 5 files changed, 103 insertions(+), 109 deletions(-) create mode 100644 src/game/platformTypes.js rename src/game/{gameUtils.js => player.js} (98%) diff --git a/src/game/constants.js b/src/game/constants.js index 2077eb0..01d6a88 100644 --- a/src/game/constants.js +++ b/src/game/constants.js @@ -1,4 +1,3 @@ - export const GAME_COLORS = { background: '#e8e2d6', basicPlat: '#6c8ae4', diff --git a/src/game/game.js b/src/game/game.js index 103191c..f3be5ea 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -1,4 +1,4 @@ -import { updatePlayerPosition, createPlayer } from './gameUtils.js'; +import { updatePlayerPosition, createPlayer } from './player.js'; import { initPlatforms, moveWorld } from './platforms.js'; import { GAME_COLORS, PLATFORMS_GAP } from './constants.js'; @@ -18,10 +18,8 @@ export function isGameOver() { export function resetGame() { allSprites.remove(); - pendingWorldMove = 0; gameOver = false; - platforms = initPlatforms(); player = createPlayer(); } @@ -40,6 +38,7 @@ export function updateGame() { } pendingWorldMove += updatePlayerPosition(player, platforms); + player.rotation = player.vel.x * 1.5; if (pendingWorldMove > PLATFORMS_GAP) { const move = Math.min(pendingWorldMove, 5); @@ -49,8 +48,6 @@ export function updateGame() { player.bounciness = 0; } - player.rotation = player.vel.x * 1.5; - if (player.y > height) { gameOver = true; } diff --git a/src/game/platformTypes.js b/src/game/platformTypes.js new file mode 100644 index 0000000..9a40425 --- /dev/null +++ b/src/game/platformTypes.js @@ -0,0 +1,97 @@ +import { PLAT_TYPE, GAME_COLORS, PLATFORM_WIDHT } from './constants.js'; + +function drawBasePlatform(platform) { + fill(platform.color); + stroke(0); + strokeWeight(1); + rect(0, 0, platform.w, platform.h, 15); +} + +function drawSpringPlatform() { + drawBasePlatform(this); + + fill(0); + noStroke(); + textAlign(CENTER, CENTER); + textSize(15); + //textStyle(BOLD); + text('⮝ ⮝ ⮝', 0, 2); + textStyle(NORMAL); +} + +function drawOneTimePlatform() { + drawBasePlatform(this); + + stroke(0); + strokeWeight(1.1); + noFill(); + + const drawCrack = (offsetX) => { + beginShape(); + vertex(offsetX, -this.h * 0.45); + vertex(offsetX - this.w * 0.08, -this.h * 0.1); + vertex(offsetX + this.w * 0.08, this.h * 0.2); + vertex(offsetX, this.h * 0.45); + endShape(); + }; + + drawCrack(-this.w * 0.25); + drawCrack(0); + drawCrack(this.w * 0.25); +} + +function addMovingBehavior(platform) { + platform.moveDir = random() < 0.5 ? -1 : 1; + platform.moveSpeed = random(1, 2); + platform.update = function () { + this.x += this.moveDir * this.moveSpeed; + if (this.x < PLATFORM_WIDHT / 2 || this.x > width - PLATFORM_WIDHT / 2) { + this.moveDir *= -1; + } + }; +} + + + +export function addTypeSpecifics(platform, type) { + switch (platform.type) { + case PLAT_TYPE.MOVING: + platform.color = GAME_COLORS.movingPlat; + platform.draw = function () { + drawBasePlatform(this); + }; + addMovingBehavior(platform); + break; + + case PLAT_TYPE.SPRING: + platform.color = GAME_COLORS.springPlat; + platform.draw = drawSpringPlatform; + break; + + case PLAT_TYPE.ONE_TIME: + platform.color = GAME_COLORS.oneTimePlat; + platform.draw = drawOneTimePlatform; + break; + + default: + platform.color = GAME_COLORS.basicPlat; + platform.draw = function () { + drawBasePlatform(this); + }; + break; + } +} + +export function selectPlatformType(elevation) { + const r = random(); + switch (true) { + case elevation >= 800 && r < 0.15: + return PLAT_TYPE.MOVING; + case elevation >= 2000 && r < 0.30: + return PLAT_TYPE.SPRING; + case elevation >= 4000 && r < 0.45: + return PLAT_TYPE.ONE_TIME; + default: + return PLAT_TYPE.BASIC; + } +} \ No newline at end of file diff --git a/src/game/platforms.js b/src/game/platforms.js index 649572d..a6a7270 100644 --- a/src/game/platforms.js +++ b/src/game/platforms.js @@ -1,101 +1,5 @@ - import { PLATFORM_WIDHT, PLATFORM_HEIGHT, PLAT_TYPE, PLATFORMS_GAP, GAME_COLORS } from './constants.js'; - -function drawBasePlatform(platform) { - fill(platform.color); - stroke(0); - strokeWeight(1); - rect(0, 0, platform.w, platform.h, 15); -} - -function drawSpringPlatform() { - drawBasePlatform(this); - - fill(0); - noStroke(); - textAlign(CENTER, CENTER); - textSize(15); - //textStyle(BOLD); - text('⮝ ⮝ ⮝', 0, 2); - textStyle(NORMAL); -} - -function drawOneTimePlatform() { - drawBasePlatform(this); - - stroke(0); - strokeWeight(1.1); - noFill(); - - const drawCrack = (offsetX) => { - beginShape(); - vertex(offsetX, -this.h * 0.45); - vertex(offsetX - this.w * 0.08, -this.h * 0.1); - vertex(offsetX + this.w * 0.08, this.h * 0.2); - vertex(offsetX, this.h * 0.45); - endShape(); - }; - - drawCrack(-this.w * 0.25); - drawCrack(0); - drawCrack(this.w * 0.25); -} - -function addMovingBehavior(platform) { - platform.moveDir = random() < 0.5 ? -1 : 1; - platform.moveSpeed = random(1, 2); - platform.update = function () { - this.x += this.moveDir * this.moveSpeed; - if (this.x < PLATFORM_WIDHT / 2 || this.x > width - PLATFORM_WIDHT / 2) { - this.moveDir *= -1; - } - }; -} - - - -function addTypeSpecifics(platform, type) { - switch (platform.type) { - case PLAT_TYPE.MOVING: - platform.color = GAME_COLORS.movingPlat; - platform.draw = function () { - drawBasePlatform(this); - }; - addMovingBehavior(platform); - break; - - case PLAT_TYPE.SPRING: - platform.color = GAME_COLORS.springPlat; - platform.draw = drawSpringPlatform; - break; - - case PLAT_TYPE.ONE_TIME: - platform.color = GAME_COLORS.oneTimePlat; - platform.draw = drawOneTimePlatform; - break; - - default: - platform.color = GAME_COLORS.basicPlat; - platform.draw = function () { - drawBasePlatform(this); - }; - break; - } -} - -function selectPlatformType(elevation) { - const r = random(); - switch (true) { - case elevation >= 200 && r < 0.15: - return PLAT_TYPE.ONE_TIME; - case elevation >= 200 && r < 0.30: - return PLAT_TYPE.MOVING; - case elevation >= 200 && r < 0.45: - return PLAT_TYPE.SPRING; - default: - return PLAT_TYPE.BASIC; - } -} +import { addTypeSpecifics, selectPlatformType } from './platformTypes.js'; function createPlatform(x, y, elevation, type, startingPlatform = false) { const platform = new Sprite(); diff --git a/src/game/gameUtils.js b/src/game/player.js similarity index 98% rename from src/game/gameUtils.js rename to src/game/player.js index eb6715d..7659a7d 100644 --- a/src/game/gameUtils.js +++ b/src/game/player.js @@ -2,20 +2,17 @@ import { PLAT_TYPE } from './constants.js'; export function createPlayer() { const player = new Sprite(); - player.x = width / 2; - player.y = 700; - player.scale = 0.20; player.img = "assets/nubzuki.png"; - + player.bounciness = 0; player.rotationLock = true; player.w = 20; player.h = 20; player.offset.y = 25; - - player.bounciness = 0; player.elevation = 0; + player.x = width / 2; + player.y = height - 80; return player; }