formatting

This commit is contained in:
Mottributo 2023-03-27 00:11:03 +03:00
parent c448e28223
commit 8c2fb35569

140
sketch.js
View File

@ -80,7 +80,7 @@ function GameCharacter() {
this.y, this.y,
); );
} }
} };
this._drawHead = function () { this._drawHead = function () {
fill(palette.ground_colors[this.curGroundIndex]); fill(palette.ground_colors[this.curGroundIndex]);
ellipse( ellipse(
@ -89,16 +89,23 @@ function GameCharacter() {
this.x_step * 6, this.x_step * 6,
this.y_step * 4, this.y_step * 4,
); );
} };
this._drawEyes = function (draw_left, draw_right, eyes_height) { this._drawEyes = function (draw_left, draw_right, eyes_height) {
fill(0); fill(0);
if (this.curLives <= 0) { if (this.curLives <= 0) {
textAlign(CENTER); textAlign(CENTER);
textSize(this.x_step * 2); textSize(this.x_step * 2);
text("x", this.x - this.x_step * 1.2, this.y - this.y_step * eyes_height); text(
text("x", this.x + this.x_step * 1.2, this.y - this.y_step * eyes_height); 'x',
} this.x - this.x_step * 1.2,
else { this.y - this.y_step * eyes_height,
);
text(
'x',
this.x + this.x_step * 1.2,
this.y - this.y_step * eyes_height,
);
} else {
if (draw_left) { if (draw_left) {
ellipse( ellipse(
this.x - this.x_step * 1.2, this.x - this.x_step * 1.2,
@ -116,7 +123,7 @@ function GameCharacter() {
); );
} }
} }
} };
this._updateSprite(); this._updateSprite();
push(); push();
strokeWeight(1); strokeWeight(1);
@ -238,7 +245,12 @@ function GameCharacter() {
if (this.invincibility_time > 0 && floor(frameCount / 10) % 2 == 0) { if (this.invincibility_time > 0 && floor(frameCount / 10) % 2 == 0) {
fill('rgba(255,255,255,0.2)'); fill('rgba(255,255,255,0.2)');
noStroke(); noStroke();
ellipse(this.x, this.y - this.y_step*3, this.x_step*9, this.y_step*9); ellipse(
this.x,
this.y - this.y_step * 3,
this.x_step * 9,
this.y_step * 9,
);
} }
this.x_step /= this.scale; this.x_step /= this.scale;
this.y_step /= this.scale; this.y_step /= this.scale;
@ -250,15 +262,9 @@ function GameCharacter() {
this.x_step *= this.scale; this.x_step *= this.scale;
this.y_step *= this.scale; this.y_step *= this.scale;
fill('rgba(0,0,0,0.2)'); fill('rgba(0,0,0,0.2)');
if (!this.isPlumetting) if (!this.isPlumetting) shadow_y = groundPositions[this.curGroundIndex];
shadow_y = groundPositions[this.curGroundIndex];
else shadow_y = NaN; else shadow_y = NaN;
ellipse( ellipse(this.x, shadow_y, this.x_step * 5, this.y_step * 1.5);
this.x,
shadow_y,
this.x_step * 5,
this.y_step * 1.5,
);
this.x_step /= this.scale; this.x_step /= this.scale;
this.y_step /= this.scale; this.y_step /= this.scale;
pop(); pop();
@ -266,7 +272,7 @@ function GameCharacter() {
this.takeLife = function () { this.takeLife = function () {
gameChar.curLives--; gameChar.curLives--;
gameChar.invincibility_time += 180; gameChar.invincibility_time += 180;
} };
this._checkPlayerDie = function () { this._checkPlayerDie = function () {
if (frameCount - death_timer > 60) { if (frameCount - death_timer > 60) {
if (this.curLives > 1) { if (this.curLives > 1) {
@ -302,11 +308,14 @@ function GameCharacter() {
for (i = 0; i < platforms[this.curGroundIndex].length; i++) { for (i = 0; i < platforms[this.curGroundIndex].length; i++) {
// If a character is falling above some platform, memorize it // If a character is falling above some platform, memorize it
if (platforms[this.curGroundIndex][i].isAbove(this)) { if (platforms[this.curGroundIndex][i].isAbove(this)) {
this.memorizedPlatform = platforms[this.curGroundIndex][i]; this.memorizedPlatform =
platforms[this.curGroundIndex][i];
} }
// If a character is below a memorized platform, make them stand on that platform // If a character is below a memorized platform, make them stand on that platform
if (typeof this.memorizedPlatform !== 'undefined' && if (
this.memorizedPlatform.isBelow(this)) { typeof this.memorizedPlatform !== 'undefined' &&
this.memorizedPlatform.isBelow(this)
) {
this.y = this.memorizedPlatform.y; this.y = this.memorizedPlatform.y;
this.isFalling = false; this.isFalling = false;
this.curJumpingStrength = 0; this.curJumpingStrength = 0;
@ -324,9 +333,10 @@ function GameCharacter() {
// Not plummeting, standing or falling means being on a platform. // Not plummeting, standing or falling means being on a platform.
// Perform checks whether the player is still on the same platform, // Perform checks whether the player is still on the same platform,
// and fall them in case they aren't. // and fall them in case they aren't.
if (!this.memorizedPlatform._isWithinX(this) if (
|| !this.memorizedPlatform._isWithinX(this) ||
this.curGroundIndex != this.memorizedPlatform.curGroundIndex) { this.curGroundIndex != this.memorizedPlatform.curGroundIndex
) {
this.isFalling = true; this.isFalling = true;
this.isOnPlatform = false; this.isOnPlatform = false;
this.memorizedPlatform = undefined; this.memorizedPlatform = undefined;
@ -364,7 +374,6 @@ function GameCharacter() {
this.scale += 0.1; this.scale += 0.1;
} }
}; };
} }
// NB: All the platforms and enemies mechanics were done // NB: All the platforms and enemies mechanics were done
// before the task was seen on Week 20. // before the task was seen on Week 20.
@ -387,12 +396,7 @@ function Platform(curGroundIndex, x, width, y) {
stroke(0); stroke(0);
strokeWeight(2); strokeWeight(2);
fill(palette.ground_colors[curGroundIndex]); fill(palette.ground_colors[curGroundIndex]);
rect( rect(this.x - this.width, this.y, width * 2, height / 100);
this.x - this.width,
this.y,
width * 2,
height / 100,
);
pop(); pop();
}; };
this.drawShadow = function () { this.drawShadow = function () {
@ -438,7 +442,13 @@ function Enemy(curGroundIndex, x, y, size) {
this.curGroundIndex = curGroundIndex; this.curGroundIndex = curGroundIndex;
this.draw = function () { this.draw = function () {
push(); push();
fill(lerpColor(palette.enemy_head_color, palette.ground_colors[this.curGroundIndex], 0.7)); fill(
lerpColor(
palette.enemy_head_color,
palette.ground_colors[this.curGroundIndex],
0.7,
),
);
ellipse(this.x, this.y, this.size); ellipse(this.x, this.y, this.size);
fill(255, 0, 0); fill(255, 0, 0);
ellipse(this.x - this.size / 2, this.y, this.size / 8, this.size / 4); ellipse(this.x - this.size / 2, this.y, this.size / 8, this.size / 4);
@ -448,23 +458,31 @@ function Enemy(curGroundIndex, x, y, size) {
fill(palette.enemy_body_color); fill(palette.enemy_body_color);
text(this.curGroundIndex, this.x, this.y + this.size / 4); text(this.curGroundIndex, this.x, this.y + this.size / 4);
pop(); pop();
} };
this.drawShadow = function () { this.drawShadow = function () {
push(); push();
fill('rgba(0,0,0,0.2)'); fill('rgba(0,0,0,0.2)');
noStroke(); noStroke();
ellipse(this.x, groundPositions[this.curGroundIndex], this.size/2, this.size/4); ellipse(
this.x,
groundPositions[this.curGroundIndex],
this.size / 2,
this.size / 4,
);
pop(); pop();
} };
this.collidesWith = function (who) { this.collidesWith = function (who) {
if (dist(who.x, who.y, this.x, this.y) < this.size/2 && if (
who.curGroundIndex == this.curGroundIndex) return true; dist(who.x, who.y, this.x, this.y) < this.size / 2 &&
who.curGroundIndex == this.curGroundIndex
)
return true;
return false; return false;
} };
this.updatePosition = function () { this.updatePosition = function () {
this.x = this.x + sin(frameCount/random(20,50)*random(0.5, 10)); this.x = this.x + sin((frameCount / random(20, 50)) * random(0.5, 10));
this.y = this.y + cos(frameCount/random(10,20)*random(0.5, 10)); this.y = this.y + cos((frameCount / random(10, 20)) * random(0.5, 10));
} };
} }
var text_size; var text_size;
@ -495,7 +513,7 @@ function setup() {
coin_inner: color('#FDC334'), coin_inner: color('#FDC334'),
heart_color: color('darkred'), heart_color: color('darkred'),
enemy_head_color: color('rgba(0,0,0,0.5)'), enemy_head_color: color('rgba(0,0,0,0.5)'),
enemy_body_color: color('rgba(255,255,255,0.3)') enemy_body_color: color('rgba(255,255,255,0.3)'),
}; };
startGame((level_start = true)); startGame((level_start = true));
} }
@ -562,10 +580,7 @@ function generateObjects() {
skew: random(-20, 20), skew: random(-20, 20),
}; };
// 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].height);
mountains[i].width,
mountains[i].height,
);
} }
// Start mountains. // Start mountains.
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
@ -599,9 +614,7 @@ function generateObjects() {
collectables[0] = { collectables[0] = {
x: 600, x: 600,
curGroundIndex: 0, curGroundIndex: 0,
y: y: groundPositions[0] - 10 /*taking 10 for better visuals*/,
groundPositions[0] -
10 /*taking 10 for better visuals*/,
size: 75, size: 75,
isFound: false, isFound: false,
}; };
@ -614,9 +627,7 @@ function generateObjects() {
collectables[i] = { collectables[i] = {
x: collectables[i - 1].x + 50 + 100 * random(0.5, 1), x: collectables[i - 1].x + 50 + 100 * random(0.5, 1),
curGroundIndex: r, curGroundIndex: r,
y: y: groundPositions[r] - 10 /*taking 10 for better visuals*/,
groundPositions[r] -
10 /*taking 10 for better visuals*/,
size: 75, size: 75,
isFound: false, isFound: false,
}; };
@ -624,10 +635,8 @@ function generateObjects() {
// marking it as isFound (making it disabled) in case if. // marking it as isFound (making it disabled) in case if.
for (k = 0; k < rivers.length; k++) { for (k = 0; k < rivers.length; k++) {
if ( if (
rivers[k].x - rivers[k].width / 2 < rivers[k].x - rivers[k].width / 2 < collectables[i].x &&
collectables[i].x && rivers[k].x + rivers[k].width / 2 > collectables[i].x
rivers[k].x + rivers[k].width / 2 >
collectables[i].x
) { ) {
collectables[i].isFound = true; collectables[i].isFound = true;
} }
@ -660,9 +669,7 @@ function generateObjects() {
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().
prev = platforms[groundPosIndex].slice(-1)[0]; prev = platforms[groundPosIndex].slice(-1)[0];
@ -688,15 +695,15 @@ function generateObjects() {
// Enemies // Enemies
enemies = []; enemies = [];
for (i = 0; i < floor(finish_position_x / 130); i++) { for (i = 0; i < floor(finish_position_x / 130); i++) {
index = floor(random(0, groundPositions.length)) index = floor(random(0, groundPositions.length));
enemies.push( enemies.push(
new Enemy( new Enemy(
index, index,
random(500, finish_position_x), random(500, finish_position_x),
groundPositions[index] - random(50, floorPos_y), groundPositions[index] - random(50, floorPos_y),
random(40, 60) random(40, 60),
) ),
) );
} }
} }
function draw() { function draw() {
@ -753,7 +760,10 @@ function draw() {
enemies[i].updatePosition(); enemies[i].updatePosition();
enemies[i].draw(); enemies[i].draw();
if (shadows_enabled) enemies[i].drawShadow(); if (shadows_enabled) enemies[i].drawShadow();
if (enemies[i].collidesWith(gameChar) && gameChar.invincibility_time <= 0) { if (
enemies[i].collidesWith(gameChar) &&
gameChar.invincibility_time <= 0
) {
gameChar.takeLife(); gameChar.takeLife();
} }
} }
@ -769,7 +779,9 @@ function complexDraw() {
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);