refactoring

This commit is contained in:
Mottributo 2023-03-26 17:58:24 +03:00
parent 11d12e3d65
commit 1543e60d68

734
sketch.js
View File

@ -23,6 +23,300 @@ var platforms = [];
var enemies = []; var enemies = [];
var flagpole; var flagpole;
function GameCharacter() {
this.x = width / 2;
this.y = floorPos_y + (height - floorPos_y) / 6;
this.x_step = 5;
this.y_step = 8;
this.scale = 1;
this.sprite = 1;
this.speed = 5;
this.baseLives = 3;
this.curLives = 3;
this.baseJumpingStrength = 16;
this.curJumpingStrength = 0;
this.isRight = false;
this.isLeft = false;
this.isFalling = false;
this.isJumping = false;
this.isPlummeting = false;
this.curGroundIndex = 0;
this._updateSprite = function () {
if (this.isPlummeting) {
this.sprite = 2;
} else if (this.isFalling) {
this.sprite = 2;
if (this.isLeft && !this.isRight) this.sprite = 5;
else if (this.isRight && !this.isLeft) this.sprite = 6;
} else {
if (this.isLeft && !this.isRight) this.sprite = 4;
else if (this.isRight && !this.isLeft) this.sprite = 3;
else this.sprite = 1;
}
};
this.draw = function () {
this._drawBody = function (jumping = false) {
fill(palette.body_color);
if (jumping) {
triangle(
this.x - this.x_step * 2,
this.y - this.y_step * 1.5,
this.x,
this.y - this.y_step * 4,
this.x + this.x_step * 2,
this.y - this.y_step * 1.5,
);
} else {
triangle(
this.x - this.x_step * 2,
this.y,
this.x,
this.y - this.y_step * 4,
this.x + this.x_step * 2,
this.y,
);
}
}
this._drawHead = function () {
fill(palette.head_color);
ellipse(
this.x,
this.y - this.y_step * 5,
this.x_step * 6,
this.y_step * 4,
);
}
this._drawEyes = function (draw_left, draw_right, eyes_height) {
fill(0);
if (draw_left) {
ellipse(
this.x - this.x_step * 1.2,
this.y - this.y_step * eyes_height,
this.x_step / 1.5,
this.y_step / 2,
);
}
if (draw_right) {
ellipse(
this.x + this.x_step * 1.2,
this.y - this.y_step * eyes_height,
this.x_step / 1.5,
this.y_step / 2,
);
}
}
this._updateSprite();
push();
strokeWeight(1);
stroke(1);
this.x_step *= this.scale;
this.y_step *= this.scale;
// Standing, facing frontwards
if (this.sprite == 1) {
// Body
this._drawBody();
// Head
this._drawHead();
// Eyes
this._drawEyes(true, true, 4.2);
}
// Jumping, facing forwards
else if (this.sprite == 2) {
// Body
this._drawBody(true);
// Hands. (Hands!)
line(
this.x - this.x_step * 1,
this.y - this.y_step * 2.5,
this.x - this.x_step * 3.2,
this.y - this.y_step * 3.2,
);
line(
this.x + this.x_step * 1,
this.y - this.y_step * 2.5,
this.x + this.x_step * 3.2,
this.y - this.y_step * 3.2,
);
// Head
this._drawHead();
// Eyes
this._drawEyes(true, true, 3.8);
}
// Walking right
else if (this.sprite == 3) {
// Body
this._drawBody();
// Hand. (Hand!)
line(
this.x,
this.y - this.y_step * 2.5,
this.x + this.x_step * 0.8,
this.y - this.y_step * 1,
);
// Head
this._drawHead();
// Eyes
this._drawEyes(false, true, 4.2);
}
// Walking left
else if (this.sprite == 4) {
// Body
this. _drawBody();
// Hand. (Hand!)
line(
this.x,
this.y - this.y_step * 2.5,
this.x - this.x_step * 0.8,
this.y - this.y_step * 1,
);
// Head
this._drawHead();
// Eyess
this._drawEyes(true, false, 4.2);
}
// Jumping left
else if (this.sprite == 5) {
// Body
this._drawBody(true);
// Hands. (Hands!)
line(
this.x - this.x_step * 1,
this.y - this.y_step * 2.5,
this.x - this.x_step * 3.2,
this.y - this.y_step * 3.2,
);
line(
this.x + this.x_step * 1,
this.y - this.y_step * 2.5,
this.x + this.x_step * 3.2,
this.y - this.y_step * 3.2,
);
// Head
this._drawHead();
// Eyes
this._drawEyes(true, false, 3.8);
}
// Jumping right
else if (this.sprite == 6) {
// Body
this._drawBody(true);
// Hands. (Hands!)
line(
this.x - this.x_step * 1,
this.y - this.y_step * 2.5,
this.x - this.x_step * 3.2,
this.y - this.y_step * 3.2,
);
line(
this.x + this.x_step * 1,
this.y - this.y_step * 2.5,
this.x + this.x_step * 3.2,
this.y - this.y_step * 3.2,
);
// Head
this._drawHead();
// Eyes
this._drawEyes(false, true, 3.8);
} else {
text('Bad sprite number!', 10, 10);
console.error('Bad this sprite number: ' + this.sprite);
noLoop();
}
this.x_step /= this.scale;
this.y_step /= this.scale;
pop();
};
this.drawShadow = function () {
push();
noStroke();
this.x_step *= this.scale;
this.y_step *= this.scale;
fill('rgba(0,0,0,0.2)');
if (!this.isPlumetting)
shadow_y = groundPositions[this.curGroundIndex];
else shadow_y = NaN;
ellipse(
this.x,
shadow_y,
this.x_step * 5,
this.y_step * 1.5,
);
this.x_step /= this.scale;
this.y_step /= this.scale;
pop();
};
this._checkPlayerDie = function () {
if (frameCount - death_timer > 60) {
if (this.curLives > 1) {
this.curLives--;
death_timer = undefined;
startGame();
} else if (this.curLives == 1) {
this.curLives--;
} else {
this.curLives = 0;
}
}
};
this.act = function () {
if (!this.isPlummeting) {
if (this.isLeft && !this.isRight) {
this.x -= this.speed;
} else if (this.isRight && !this.isLeft) {
this.x += this.speed;
}
if (this.isJumping) {
this.y -= this.curJumpingStrength;
this.curJumpingStrength -= gravity;
if (this.curJumpingStrength <= 0) {
this.isFalling = true;
this.isJumping = false;
}
} else if (this.isFalling) {
this.y -= this.curJumpingStrength;
this.curJumpingStrength -= gravity;
if (this.y >= this.getCurGroundY()) {
this.y = this.getCurGroundY();
this.isFalling = false;
}
} else {
}
}
this._checkPlayerDie();
// Doing plummeting
if (this.isPlummeting) {
// using OR operator to set to frameCount in case death_timer is undefined,
// but not updating the death_timer again in case it is not undefined.
death_timer = death_timer || frameCount;
this.y += 3;
}
};
this.getCurGroundY = function () {
return groundPositions[this.curGroundIndex];
};
this.goUp = function () {
memorizedIndex = this.curGroundIndex;
this.curGroundIndex = max(0, this.curGroundIndex - 1);
if (memorizedIndex != this.curGroundIndex) {
this.y -= (height - floorPos_y) / 3;
this.scale -= 0.1;
}
};
this.goDown = function () {
memorizedIndex = this.curGroundIndex;
this.curGroundIndex = min(
groundPositions.length - 1,
this.curGroundIndex + 1,
);
if (memorizedIndex != this.curGroundIndex) {
this.y += (height - floorPos_y) / 3;
this.scale += 0.1;
}
};
}
function Platform(curGroundIndex, x, width, y) { function Platform(curGroundIndex, x, width, y) {
this.curGroundIndex = curGroundIndex; this.curGroundIndex = curGroundIndex;
this.x = x; this.x = x;
@ -30,7 +324,9 @@ function Platform(curGroundIndex, x, width, y) {
// To make platforms jumpable to each other, define a max vertical difference // To make platforms jumpable to each other, define a max vertical difference
// thru an arithmetic progression dependent on the game character's jumping strength and gravity. // thru an arithmetic progression dependent on the game character's jumping strength and gravity.
this.max_y_deviation = this.max_y_deviation =
(gameChar.baseJumpingStrength/gravity)*(gameChar.baseJumpingStrength)/2; ((gameChar.baseJumpingStrength / gravity) *
gameChar.baseJumpingStrength) /
2;
this.y = y || groundPositions[curGroundIndex] - this.max_y_deviation; this.y = y || groundPositions[curGroundIndex] - this.max_y_deviation;
this.draw = function () { this.draw = function () {
@ -42,7 +338,7 @@ function Platform(curGroundIndex, x, width, y) {
this.x - this.width, this.x - this.width,
this.y + height / 100, this.y + height / 100,
width * 2, width * 2,
height / 100 height / 100,
); );
pop(); pop();
}; };
@ -50,12 +346,12 @@ function Platform(curGroundIndex, x, width, y) {
push(); push();
// drawing shadow on the ground // drawing shadow on the ground
noStroke(); noStroke();
fill("rgba(0,0,0, 0.15)"); fill('rgba(0,0,0, 0.15)');
rect( rect(
this.x - this.width, this.x - this.width,
groundPositions[curGroundIndex], groundPositions[curGroundIndex],
width * 2, width * 2,
height / 100 height / 100,
); );
pop(); pop();
@ -87,32 +383,28 @@ var palette;
function setup() { function setup() {
createCanvas(1024, 576); createCanvas(1024, 576);
palette = { palette = {
body_color: color("white"), body_color: color('white'),
head_color: color("darkgreen"), head_color: color('darkgreen'),
sky_color: color("#8E9887"), sky_color: color('#8E9887'),
ground_colors: [ ground_colors: [color('#874321'), color('#636721'), color('#634345')],
color("#874321"), river_river_color: color('#56C525'),
color("#636721"), river_river_wave_color: color('#4BAD21'),
color("#634345") river_color: color('#7B672A'),
], pine_leaves_color: color('#3F4834'),
river_river_color: color("#56C525"), maple_leaves_color: color('#4E3D1F'),
river_river_wave_color: color("#4BAD21"), pine_stem: color('#644D0D'),
river_color: color("#7B672A"), maple_stem: color('#936907'),
pine_leaves_color: color("#3F4834"), mountain: color('#5a5a5a'),
maple_leaves_color: color("#4E3D1F"), mountain_shadow: color('#3c3c3c'),
pine_stem: color("#644D0D"),
maple_stem: color("#936907"),
mountain: color("#5a5a5a"),
mountain_shadow: color("#3c3c3c"),
cloud0: color(200), cloud0: color(200),
cloud1: color(220), cloud1: color(220),
cloud2: color(255), cloud2: color(255),
coin_outer: color("#FDC334"), coin_outer: color('#FDC334'),
coin_middle: color("#ababab"), coin_middle: color('#ababab'),
coin_inner: color("#FDC334"), coin_inner: color('#FDC334'),
heart_color: color("darkred"), heart_color: color('darkred'),
enemy_head_color: color("red"), enemy_head_color: color('red'),
enemy_body_color: color("red"), enemy_body_color: color('red'),
}; };
startGame((level_start = true)); startGame((level_start = true));
} }
@ -139,300 +431,7 @@ function startGame(level_start) {
cell_size_h: 20, cell_size_h: 20,
}; };
if (level_start) { if (level_start) {
gameChar = { gameChar = new GameCharacter();
x: width / 2,
y: floorPos_y + (height - floorPos_y) / 6,
x_step: 5,
y_step: 8,
scale: 1,
sprite: 1,
speed: 5,
baseLives: 3,
curLives: 3,
baseJumpingStrength: 16,
curJumpingStrength: 0,
isRight: false,
isLeft: false,
isFalling: false,
isJumping: false,
isPlummeting: false,
curGroundIndex: 0,
_updateSprite: function () {
if (gameChar.isPlummeting) {
gameChar.sprite = 2;
} else if (gameChar.isFalling) {
gameChar.sprite = 2;
if (gameChar.isLeft && !gameChar.isRight)
gameChar.sprite = 5;
else if (gameChar.isRight && !gameChar.isLeft)
gameChar.sprite = 6;
} else {
if (gameChar.isLeft && !gameChar.isRight)
gameChar.sprite = 4;
else if (gameChar.isRight && !gameChar.isLeft)
gameChar.sprite = 3;
else gameChar.sprite = 1;
}
},
draw: function () {
this._updateSprite();
push();
strokeWeight(1);
stroke(1);
this.x_step *= this.scale;
this.y_step *= this.scale;
function _drawBody(jumping = false) {
fill(palette.body_color);
if (jumping) {
triangle(
gameChar.x - gameChar.x_step * 2,
gameChar.y - gameChar.y_step * 1.5,
gameChar.x,
gameChar.y - gameChar.y_step * 4,
gameChar.x + gameChar.x_step * 2,
gameChar.y - gameChar.y_step * 1.5
);
} else {
triangle(
gameChar.x - gameChar.x_step * 2,
gameChar.y,
gameChar.x,
gameChar.y - gameChar.y_step * 4,
gameChar.x + gameChar.x_step * 2,
gameChar.y
);
}
}
function _drawHead() {
fill(palette.head_color);
ellipse(
gameChar.x,
gameChar.y - gameChar.y_step * 5,
gameChar.x_step * 6,
gameChar.y_step * 4
);
}
function _drawEyes(draw_left, draw_right, eyes_height) {
fill(0);
if (draw_left) {
ellipse(
gameChar.x - gameChar.x_step * 1.2,
gameChar.y - gameChar.y_step * eyes_height,
gameChar.x_step / 1.5,
gameChar.y_step / 2
);
}
if (draw_right) {
ellipse(
gameChar.x + gameChar.x_step * 1.2,
gameChar.y - gameChar.y_step * eyes_height,
gameChar.x_step / 1.5,
gameChar.y_step / 2
);
}
}
// Standing, facing frontwards
if (this.sprite == 1) {
// Body
_drawBody();
// Head
_drawHead();
// Eyes
_drawEyes(true, true, 4.2);
}
// Jumping, facing forwards
else if (gameChar.sprite == 2) {
// Body
_drawBody(true);
// Hands. (Hands!)
line(
this.x - this.x_step * 1,
this.y - this.y_step * 2.5,
this.x - this.x_step * 3.2,
this.y - this.y_step * 3.2
);
line(
this.x + this.x_step * 1,
this.y - this.y_step * 2.5,
this.x + this.x_step * 3.2,
this.y - this.y_step * 3.2
);
// Head
_drawHead();
// Eyes
_drawEyes(true, true, 3.8);
}
// Walking right
else if (this.sprite == 3) {
// Body
_drawBody();
// Hand. (Hand!)
line(
this.x,
this.y - this.y_step * 2.5,
this.x + this.x_step * 0.8,
this.y - this.y_step * 1
);
// Head
_drawHead();
// Eyes
_drawEyes(false, true, 4.2);
}
// Walking left
else if (this.sprite == 4) {
// Body
_drawBody();
// Hand. (Hand!)
line(
this.x,
this.y - this.y_step * 2.5,
this.x - this.x_step * 0.8,
this.y - this.y_step * 1
);
// Head
_drawHead();
// Eyess
_drawEyes(true, false, 4.2);
}
// Jumping left
else if (this.sprite == 5) {
// Body
_drawBody(true);
// Hands. (Hands!)
line(
this.x - this.x_step * 1,
this.y - this.y_step * 2.5,
this.x - this.x_step * 3.2,
this.y - this.y_step * 3.2
);
line(
this.x + this.x_step * 1,
this.y - this.y_step * 2.5,
this.x + this.x_step * 3.2,
this.y - this.y_step * 3.2
);
// Head
_drawHead();
// Eyes
_drawEyes(true, false, 3.8);
}
// Jumping right
else if (this.sprite == 6) {
// Body
_drawBody(true);
// Hands. (Hands!)
line(
this.x - this.x_step * 1,
this.y - this.y_step * 2.5,
this.x - this.x_step * 3.2,
this.y - this.y_step * 3.2
);
line(
this.x + this.x_step * 1,
this.y - this.y_step * 2.5,
this.x + this.x_step * 3.2,
this.y - this.y_step * 3.2
);
// Head
_drawHead();
// Eyes
_drawEyes(false, true, 3.8);
} else {
text("Bad sprite number!", 10, 10);
console.error("Bad gameChar sprite number: " + this.sprite);
}
this.x_step /= this.scale;
this.y_step /= this.scale;
pop();
},
drawShadow: function () {
push();
noStroke();
this.x_step *= this.scale;
this.y_step *= this.scale;
fill('rgba(0,0,0,0.2)');
if (!gameChar.isPlumetting) shadow_y = groundPositions[gameChar.curGroundIndex];
else shadow_y = NaN;
ellipse(gameChar.x,
shadow_y,
gameChar.x_step*5,
gameChar.y_step*1.5);
this.x_step /= this.scale;
this.y_step /= this.scale;
pop();
},
_checkPlayerDie: function () {
if (frameCount - death_timer > 60) {
if (gameChar.curLives > 1) {
gameChar.curLives--;
death_timer = undefined;
startGame();
} else if (gameChar.curLives == 1) {
gameChar.curLives--;
} else {
gameChar.curLives = 0;
}
}
},
act: function () {
if (!gameChar.isPlummeting) {
if (gameChar.isLeft && !gameChar.isRight) {
gameChar.x -= gameChar.speed;
} else if (gameChar.isRight && !gameChar.isLeft) {
gameChar.x += gameChar.speed;
}
if (gameChar.isJumping) {
gameChar.y -= gameChar.curJumpingStrength;
gameChar.curJumpingStrength -= gravity;
if (gameChar.curJumpingStrength <= 0) {
gameChar.isFalling = true;
gameChar.isJumping = false;
}
} else if (gameChar.isFalling) {
gameChar.y -= gameChar.curJumpingStrength;
gameChar.curJumpingStrength -= gravity;
if (gameChar.y >= gameChar.getCurGroundY()) {
gameChar.y = gameChar.getCurGroundY();
gameChar.isFalling = false;
}
} else {
}
}
this._checkPlayerDie();
// Doing plummeting
if (gameChar.isPlummeting) {
// using OR operator to set to frameCount in case death_timer is undefined,
// but not updating the death_timer again in case it is not undefined.
death_timer = death_timer || frameCount;
gameChar.y += 3;
}
},
getCurGroundY: function () {
return groundPositions[this.curGroundIndex];
},
goUp: function () {
memorizedIndex = this.curGroundIndex;
this.curGroundIndex = max(0, this.curGroundIndex - 1);
if (memorizedIndex != this.curGroundIndex) {
gameChar.y -= (height - floorPos_y) / 3;
gameChar.scale -= 0.1;
}
},
goDown: function () {
memorizedIndex = this.curGroundIndex;
this.curGroundIndex = min(
groundPositions.length - 1,
this.curGroundIndex + 1
);
if (memorizedIndex != this.curGroundIndex) {
gameChar.y += (height - floorPos_y) / 3;
gameChar.scale += 0.1;
}
},
};
cameraPosX = gameChar.x - width / 2; cameraPosX = gameChar.x - width / 2;
// Creating trees, clouds, mountains, rivers, collectables. // Creating trees, clouds, mountains, rivers, collectables.
{ {
@ -458,7 +457,7 @@ function startGame(level_start) {
// To prevent spiky mountains which are quite ugly // To prevent spiky mountains which are quite ugly
mountains[i].height = max( mountains[i].height = max(
mountains[i].width, mountains[i].width,
mountains[i].height mountains[i].height,
); );
} }
// Start mountains. // Start mountains.
@ -535,23 +534,23 @@ function startGame(level_start) {
0, 0,
700, 700,
50, 50,
groundPositions[0] - Platform.max_y_deviation groundPositions[0] - Platform.max_y_deviation,
); );
platforms[1][0] = new Platform( platforms[1][0] = new Platform(
1, 1,
800, 800,
50, 50,
groundPositions[1] - Platform.max_y_deviation groundPositions[1] - Platform.max_y_deviation,
); );
platforms[2][0] = new Platform( platforms[2][0] = new Platform(
2, 2,
900, 900,
50, 50,
groundPositions[2] - Platform.max_y_deviation groundPositions[2] - Platform.max_y_deviation,
); );
} else { } else {
let groundPosIndex = floor( let groundPosIndex = floor(
random(0, groundPositions.length) random(0, groundPositions.length),
); );
// array.slice(-1)[0] gets the last element of an array without removing it, // array.slice(-1)[0] gets the last element of an array without removing it,
// contrary to array.pop(). // contrary to array.pop().
@ -561,18 +560,16 @@ function startGame(level_start) {
platforms[groundPosIndex].push( platforms[groundPosIndex].push(
new Platform( new Platform(
groundPosIndex, groundPosIndex,
prev.x + prev.x + random(prev.width + 70, prev.width + 155),
random(
prev.width + 70,
prev.width + 155
),
random(15, 60), random(15, 60),
max( max(
random(prev.y - random(
prev.max_y_deviation, prev.y - prev.max_y_deviation,
floorPos_y - prev.max_y_deviation), floorPos_y - prev.max_y_deviation,
height / 8) ),
) height / 8,
),
),
); );
} }
} }
@ -607,7 +604,7 @@ function draw() {
cameraPosX = lerp( cameraPosX = lerp(
finish_position_x - width / 2, finish_position_x - width / 2,
cameraPosX, cameraPosX,
camera_speed camera_speed,
); );
// -------- CLOUDS -------------- // -------- CLOUDS --------------
@ -624,7 +621,7 @@ function draw() {
drawTrees(); drawTrees();
// -------- PLATFORMS SHADOWS --- // -------- PLATFORMS SHADOWS ---
if (shadows_enabled) if (shadows_enabled)
for (i=0; i<groundPositions.length; i++) { for (i = 0; i < groundPositions.length; i++) {
drawPlatformsShadows(i); drawPlatformsShadows(i);
} }
// -------- COLLECTABLES -------- // -------- COLLECTABLES --------
@ -647,12 +644,12 @@ function draw() {
function complexDraw() { function complexDraw() {
// Since platforms require being drawn in three different ordrs relative to enemies and the game character, // Since platforms require being drawn in three different ordrs relative to enemies and the game character,
// a function is made to handle this and to decrease clutter. // a function is made to handle this and to decrease clutter.
for (i=0; i<=gameChar.curGroundIndex; i++) { for (i = 0; i <= gameChar.curGroundIndex; i++) {
drawPlatforms(i); drawPlatforms(i);
} }
if (shadows_enabled) gameChar.drawShadow(); if (shadows_enabled) gameChar.drawShadow();
gameChar.draw(); gameChar.draw();
for (i=gameChar.curGroundIndex+1; i<groundPositions.length; i++) { for (i = gameChar.curGroundIndex + 1; i < groundPositions.length; i++) {
drawPlatforms(i); drawPlatforms(i);
} }
} }
@ -660,12 +657,12 @@ function drawInterface() {
push(); push();
fill(0); fill(0);
stroke(0); stroke(0);
text("Score: " + game_score, 12, text_size); text('Score: ' + game_score, 12, text_size);
drawLives(); drawLives();
if (gameChar.curLives < 1) if (gameChar.curLives < 1)
text("Game over. Press space to continue...", 0, height / 2); text('Game over. Press space to continue...', 0, height / 2);
else if (flagpole.isReached) else if (flagpole.isReached)
text("Level complete. Press space to continue...", 0, height / 2); text('Level complete. Press space to continue...', 0, height / 2);
if (showDebugData) { if (showDebugData) {
text(gameChar.curGroundIndex, 99, 99); text(gameChar.curGroundIndex, 99, 99);
text(gameChar.getCurGroundY(), 188, 99); text(gameChar.getCurGroundY(), 188, 99);
@ -706,7 +703,7 @@ function checkCollectable(t_collectable) {
t_collectable.x, t_collectable.x,
t_collectable.y, t_collectable.y,
gameChar.x, gameChar.x,
gameChar.y - gameChar.x_step * 4 gameChar.y - gameChar.x_step * 4,
) < ) <
t_collectable.size / 2 t_collectable.size / 2
) { ) {
@ -724,14 +721,14 @@ function drawGround() {
0, 0,
floorPos_y + (2 * (height - floorPos_y)) / 6, floorPos_y + (2 * (height - floorPos_y)) / 6,
width, width,
floorPos_y + (2 * (height - floorPos_y)) / 6 floorPos_y + (2 * (height - floorPos_y)) / 6,
); );
fill(palette.ground_colors[2]); fill(palette.ground_colors[2]);
rect( rect(
0, 0,
floorPos_y + (4 * (height - floorPos_y)) / 6, floorPos_y + (4 * (height - floorPos_y)) / 6,
width, width,
floorPos_y + (4 * (height - floorPos_y)) / 6 floorPos_y + (4 * (height - floorPos_y)) / 6,
); );
stroke(1); stroke(1);
translate(-cameraPosX, 0); translate(-cameraPosX, 0);
@ -740,13 +737,13 @@ function drawGround() {
i, i,
floorPos_y + (2 * (height - floorPos_y)) / 6, floorPos_y + (2 * (height - floorPos_y)) / 6,
i + 1, i + 1,
floorPos_y + (2 * (height - floorPos_y)) / 6 floorPos_y + (2 * (height - floorPos_y)) / 6,
); );
line( line(
i, i,
floorPos_y + (4 * (height - floorPos_y)) / 6, floorPos_y + (4 * (height - floorPos_y)) / 6,
i + 1, i + 1,
floorPos_y + (4 * (height - floorPos_y)) / 6 floorPos_y + (4 * (height - floorPos_y)) / 6,
); );
} }
pop(); pop();
@ -760,7 +757,7 @@ function drawRiver(t_river) {
if (frameCount % 2 == 0) { if (frameCount % 2 == 0) {
PointX = random( PointX = random(
t_river.x - t_river.width / 2 + 5, t_river.x - t_river.width / 2 + 5,
t_river.x + t_river.width / 2 - 5 t_river.x + t_river.width / 2 - 5,
); );
PointY = random(floorPos_y, height); PointY = random(floorPos_y, height);
if (t_river.points.length > 2) { if (t_river.points.length > 2) {
@ -775,7 +772,7 @@ function drawRiver(t_river) {
t_river.points[k][0], t_river.points[k][0],
t_river.points[k][1], t_river.points[k][1],
t_river.points[k][0], t_river.points[k][0],
t_river.points[k][1] + t_river.points[k][2] t_river.points[k][1] + t_river.points[k][2],
); );
} }
@ -817,27 +814,27 @@ function drawClouds() {
clouds[i].x - 20, clouds[i].x - 20,
clouds[i].y - 10, clouds[i].y - 10,
20 * clouds[i].size, 20 * clouds[i].size,
30 * clouds[i].size 30 * clouds[i].size,
); );
fill(palette.cloud1); fill(palette.cloud1);
ellipse( ellipse(
clouds[i].x + 20, clouds[i].x + 20,
clouds[i].y - 20, clouds[i].y - 20,
70 * clouds[i].size, 70 * clouds[i].size,
50 * clouds[i].size 50 * clouds[i].size,
); );
fill(palette.cloud2); fill(palette.cloud2);
ellipse( ellipse(
clouds[i].x, clouds[i].x,
clouds[i].y, clouds[i].y,
90 * clouds[i].size, 90 * clouds[i].size,
40 * clouds[i].size 40 * clouds[i].size,
); );
ellipse( ellipse(
clouds[i].x + 45, clouds[i].x + 45,
clouds[i].y - 10, clouds[i].y - 10,
50 * clouds[i].size, 50 * clouds[i].size,
35 * clouds[i].size 35 * clouds[i].size,
); );
pop(); pop();
} }
@ -855,7 +852,7 @@ function drawMountains() {
mountains[i].x + mountains[i].skew, mountains[i].x + mountains[i].skew,
mountains[i].height, mountains[i].height,
mountains[i].x + mountains[i].width, mountains[i].x + mountains[i].width,
floorPos_y floorPos_y,
); );
fill(palette.mountain); fill(palette.mountain);
triangle( triangle(
@ -864,7 +861,7 @@ function drawMountains() {
mountains[i].x + mountains[i].skew, mountains[i].x + mountains[i].skew,
mountains[i].height, mountains[i].height,
mountains[i].x - mountains[i].width / 1.5, mountains[i].x - mountains[i].width / 1.5,
floorPos_y floorPos_y,
); );
pop(); pop();
} }
@ -894,7 +891,7 @@ function drawLives() {
true, true,
width - i * heart_size * 4.5, width - i * heart_size * 4.5,
heart_size * 1.5, heart_size * 1.5,
heart_size heart_size,
); );
} }
for (i = gameChar.curLives; i > 0; i--) { for (i = gameChar.curLives; i > 0; i--) {
@ -902,7 +899,7 @@ function drawLives() {
false, false,
width - i * heart_size * 4.5, width - i * heart_size * 4.5,
heart_size * 1.5, heart_size * 1.5,
heart_size heart_size,
); );
} }
pop(); pop();
@ -919,7 +916,7 @@ function drawTrees() {
trees_x[i], trees_x[i],
floorPos_y - 150, floorPos_y - 150,
trees_x[i] + 15, trees_x[i] + 15,
floorPos_y floorPos_y,
); );
fill(palette.pine_leaves_color); fill(palette.pine_leaves_color);
triangle( triangle(
@ -928,7 +925,7 @@ function drawTrees() {
trees_x[i], trees_x[i],
floorPos_y - 120, floorPos_y - 120,
trees_x[i] + 45, trees_x[i] + 45,
floorPos_y - 45 floorPos_y - 45,
); );
triangle( triangle(
trees_x[i] - 45, trees_x[i] - 45,
@ -936,7 +933,7 @@ function drawTrees() {
trees_x[i], trees_x[i],
floorPos_y - 180, floorPos_y - 180,
trees_x[i] + 45, trees_x[i] + 45,
floorPos_y - 85 floorPos_y - 85,
); );
} }
// Draw maple // Draw maple
@ -948,7 +945,7 @@ function drawTrees() {
trees_x[i], trees_x[i],
floorPos_y - 120, floorPos_y - 120,
trees_x[i] + 10, trees_x[i] + 10,
floorPos_y floorPos_y,
); );
fill(palette.maple_leaves_color); fill(palette.maple_leaves_color);
ellipse(trees_x[i], floorPos_y - 50, 80, 30); ellipse(trees_x[i], floorPos_y - 50, 80, 30);
@ -998,7 +995,7 @@ function renderFlagpole() {
flagpole.x + flagpole.cell_size_h * i, flagpole.x + flagpole.cell_size_h * i,
floorPos_y / 2 + flagpole.cell_size_v * j, floorPos_y / 2 + flagpole.cell_size_v * j,
flagpole.cell_size_h, flagpole.cell_size_h,
flagpole.cell_size_v flagpole.cell_size_v,
); );
} }
} }
@ -1019,9 +1016,9 @@ function drawFps() {
if (fps_recent_values.length < 200) fps_recent_values.push(fps); if (fps_recent_values.length < 200) fps_recent_values.push(fps);
else fps_recent_values.shift(); else fps_recent_values.shift();
fps_recent_values.push(fps); fps_recent_values.push(fps);
fill("red"); fill('red');
text(fps, 400, 99); text(fps, 400, 99);
stroke("black"); stroke('black');
beginShape(LINES); beginShape(LINES);
for (i = 1; i < fps_recent_values.length; i++) { for (i = 1; i < fps_recent_values.length; i++) {
vertex(i, fps_recent_values[i]); vertex(i, fps_recent_values[i]);
@ -1030,7 +1027,7 @@ function drawFps() {
pop(); pop();
} }
function keyPressed() { function keyPressed() {
console.log(frameCount + " pressed " + key + " " + keyCode); console.log(frameCount + ' pressed ' + key + ' ' + keyCode);
if (gameChar.curLives < 1 || flagpole.isReached) { if (gameChar.curLives < 1 || flagpole.isReached) {
if (keyCode == 32 /*Space*/) { if (keyCode == 32 /*Space*/) {
/*Hard resets the game*/ /*Hard resets the game*/
@ -1043,7 +1040,8 @@ function keyPressed() {
gameChar.isRight = true; gameChar.isRight = true;
if (keyCode == 83 /*S*/ || keyCode == DOWN_ARROW) gameChar.goDown(); if (keyCode == 83 /*S*/ || keyCode == DOWN_ARROW) gameChar.goDown();
if (keyCode == 87 /*W*/ || keyCode == UP_ARROW) gameChar.goUp(); if (keyCode == 87 /*W*/ || keyCode == UP_ARROW) gameChar.goUp();
// Rewrote jumping routine to make it more natural and be able to support platforms and different player dimensions // Rewrote jumping routine to make it more natural and be able to
// support platforms and different player dimensions
if ( if (
(keyCode == 32 /*Space*/ || keyCode == 88) /*X*/ && (keyCode == 32 /*Space*/ || keyCode == 88) /*X*/ &&
!gameChar.isFalling && !gameChar.isFalling &&
@ -1056,7 +1054,7 @@ function keyPressed() {
if (keyCode == 77 /*M*/) showDebugData = !showDebugData; if (keyCode == 77 /*M*/) showDebugData = !showDebugData;
} }
function keyReleased() { function keyReleased() {
console.log(frameCount + " released " + key + " " + keyCode); console.log(frameCount + ' released ' + key + ' ' + keyCode);
if (keyCode == 65 /*A*/ || keyCode == LEFT_ARROW) gameChar.isLeft = false; if (keyCode == 65 /*A*/ || keyCode == LEFT_ARROW) gameChar.isLeft = false;
if (keyCode == 68 /*D*/ || keyCode == RIGHT_ARROW) gameChar.isRight = false; if (keyCode == 68 /*D*/ || keyCode == RIGHT_ARROW) gameChar.isRight = false;
} }