formatting

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

194
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,34 +89,41 @@ 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',
}
else {
if (draw_left) {
ellipse(
this.x - this.x_step * 1.2, this.x - this.x_step * 1.2,
this.y - this.y_step * eyes_height, this.y - this.y_step * eyes_height,
this.x_step / 1.5,
this.y_step / 2,
); );
} text(
if (draw_right) { 'x',
ellipse(
this.x + this.x_step * 1.2, this.x + this.x_step * 1.2,
this.y - this.y_step * eyes_height, this.y - this.y_step * eyes_height,
this.x_step / 1.5,
this.y_step / 2,
); );
} else {
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(); this._updateSprite();
push(); push();
strokeWeight(1); strokeWeight(1);
@ -173,7 +180,7 @@ function GameCharacter() {
// Walking left // Walking left
else if (this.sprite == 4) { else if (this.sprite == 4) {
// Body // Body
this. _drawBody(); this._drawBody();
// Hand. (Hand!) // Hand. (Hand!)
line( line(
this.x, this.x,
@ -235,10 +242,15 @@ function GameCharacter() {
noLoop(); noLoop();
} }
// Drawing an aura of invincibility // Drawing an aura of invincibility
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,23 +262,17 @@ 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();
}; };
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) {
@ -281,7 +287,7 @@ function GameCharacter() {
} }
}; };
this.act = function () { this.act = function () {
this.invincibility_time = max(0, this.invincibility_time-1); this.invincibility_time = max(0, this.invincibility_time - 1);
if (!this.isPlummeting) { if (!this.isPlummeting) {
if (this.isLeft && !this.isRight) { if (this.isLeft && !this.isRight) {
this.x -= this.speed; this.x -= this.speed;
@ -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,13 +333,14 @@ 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.isOnPlatform = false; this.isFalling = true;
this.memorizedPlatform = undefined; this.isOnPlatform = false;
} this.memorizedPlatform = undefined;
}
} }
} }
this._checkPlayerDie(); this._checkPlayerDie();
@ -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,33 +442,47 @@ 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);
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);
textSize(this.size/1.5); textSize(this.size / 1.5);
textAlign(CENTER); textAlign(CENTER);
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];
@ -677,7 +684,7 @@ function generateObjects() {
max( max(
random( random(
prev.y - prev.max_y_deviation, prev.y - prev.max_y_deviation,
floorPos_y - prev.max_y_deviation/2, floorPos_y - prev.max_y_deviation / 2,
), ),
height / 8, height / 8,
), ),
@ -687,16 +694,16 @@ 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() {
@ -749,11 +756,14 @@ function draw() {
// GAMECHAR & PLATFORMS RENDER -- // GAMECHAR & PLATFORMS RENDER --
complexDraw(); complexDraw();
// -------- ENEMIES ------------- // -------- ENEMIES -------------
for (i=0; i<enemies.length; i++) { for (i = 0; i < enemies.length; i++) {
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);