refactoring and polishing
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
export const GAME_COLORS = {
|
export const GAME_COLORS = {
|
||||||
background: '#e8e2d6',
|
background: '#e8e2d6',
|
||||||
basicPlat: '#6c8ae4',
|
basicPlat: '#6c8ae4',
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { updatePlayerPosition, createPlayer } from './gameUtils.js';
|
import { updatePlayerPosition, createPlayer } from './player.js';
|
||||||
import { initPlatforms, moveWorld } from './platforms.js';
|
import { initPlatforms, moveWorld } from './platforms.js';
|
||||||
import { GAME_COLORS, PLATFORMS_GAP } from './constants.js';
|
import { GAME_COLORS, PLATFORMS_GAP } from './constants.js';
|
||||||
|
|
||||||
@@ -18,10 +18,8 @@ export function isGameOver() {
|
|||||||
|
|
||||||
export function resetGame() {
|
export function resetGame() {
|
||||||
allSprites.remove();
|
allSprites.remove();
|
||||||
|
|
||||||
pendingWorldMove = 0;
|
pendingWorldMove = 0;
|
||||||
gameOver = false;
|
gameOver = false;
|
||||||
|
|
||||||
platforms = initPlatforms();
|
platforms = initPlatforms();
|
||||||
player = createPlayer();
|
player = createPlayer();
|
||||||
}
|
}
|
||||||
@@ -40,6 +38,7 @@ export function updateGame() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pendingWorldMove += updatePlayerPosition(player, platforms);
|
pendingWorldMove += updatePlayerPosition(player, platforms);
|
||||||
|
player.rotation = player.vel.x * 1.5;
|
||||||
|
|
||||||
if (pendingWorldMove > PLATFORMS_GAP) {
|
if (pendingWorldMove > PLATFORMS_GAP) {
|
||||||
const move = Math.min(pendingWorldMove, 5);
|
const move = Math.min(pendingWorldMove, 5);
|
||||||
@@ -49,8 +48,6 @@ export function updateGame() {
|
|||||||
player.bounciness = 0;
|
player.bounciness = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
player.rotation = player.vel.x * 1.5;
|
|
||||||
|
|
||||||
if (player.y > height) {
|
if (player.y > height) {
|
||||||
gameOver = true;
|
gameOver = true;
|
||||||
}
|
}
|
||||||
|
|||||||
97
src/game/platformTypes.js
Normal file
97
src/game/platformTypes.js
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,101 +1,5 @@
|
|||||||
|
|
||||||
import { PLATFORM_WIDHT, PLATFORM_HEIGHT, PLAT_TYPE, PLATFORMS_GAP, GAME_COLORS } from './constants.js';
|
import { PLATFORM_WIDHT, PLATFORM_HEIGHT, PLAT_TYPE, PLATFORMS_GAP, GAME_COLORS } from './constants.js';
|
||||||
|
import { addTypeSpecifics, selectPlatformType } from './platformTypes.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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createPlatform(x, y, elevation, type, startingPlatform = false) {
|
function createPlatform(x, y, elevation, type, startingPlatform = false) {
|
||||||
const platform = new Sprite();
|
const platform = new Sprite();
|
||||||
|
|||||||
@@ -2,20 +2,17 @@ import { PLAT_TYPE } from './constants.js';
|
|||||||
|
|
||||||
export function createPlayer() {
|
export function createPlayer() {
|
||||||
const player = new Sprite();
|
const player = new Sprite();
|
||||||
player.x = width / 2;
|
|
||||||
player.y = 700;
|
|
||||||
|
|
||||||
player.scale = 0.20;
|
player.scale = 0.20;
|
||||||
player.img = "assets/nubzuki.png";
|
player.img = "assets/nubzuki.png";
|
||||||
|
player.bounciness = 0;
|
||||||
player.rotationLock = true;
|
player.rotationLock = true;
|
||||||
|
|
||||||
player.w = 20;
|
player.w = 20;
|
||||||
player.h = 20;
|
player.h = 20;
|
||||||
player.offset.y = 25;
|
player.offset.y = 25;
|
||||||
|
|
||||||
player.bounciness = 0;
|
|
||||||
player.elevation = 0;
|
player.elevation = 0;
|
||||||
|
player.x = width / 2;
|
||||||
|
player.y = height - 80;
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user