formatting

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

166
sketch.js
View File

@ -80,7 +80,7 @@ function GameCharacter() {
this.y,
);
}
}
};
this._drawHead = function () {
fill(palette.ground_colors[this.curGroundIndex]);
ellipse(
@ -89,16 +89,23 @@ function GameCharacter() {
this.x_step * 6,
this.y_step * 4,
);
}
};
this._drawEyes = function (draw_left, draw_right, eyes_height) {
fill(0);
if (this.curLives <= 0) {
textAlign(CENTER);
textSize(this.x_step*2);
text("x", this.x - this.x_step * 1.2, this.y - this.y_step * eyes_height);
text("x", this.x + this.x_step * 1.2, this.y - this.y_step * eyes_height);
}
else {
textSize(this.x_step * 2);
text(
'x',
this.x - this.x_step * 1.2,
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) {
ellipse(
this.x - this.x_step * 1.2,
@ -116,7 +123,7 @@ function GameCharacter() {
);
}
}
}
};
this._updateSprite();
push();
strokeWeight(1);
@ -173,7 +180,7 @@ function GameCharacter() {
// Walking left
else if (this.sprite == 4) {
// Body
this. _drawBody();
this._drawBody();
// Hand. (Hand!)
line(
this.x,
@ -235,10 +242,15 @@ function GameCharacter() {
noLoop();
}
// 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)');
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.y_step /= this.scale;
@ -250,23 +262,17 @@ function GameCharacter() {
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];
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,
);
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.takeLife = function () {
gameChar.curLives--;
gameChar.invincibility_time+=180;
}
gameChar.invincibility_time += 180;
};
this._checkPlayerDie = function () {
if (frameCount - death_timer > 60) {
if (this.curLives > 1) {
@ -281,7 +287,7 @@ function GameCharacter() {
}
};
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.isLeft && !this.isRight) {
this.x -= this.speed;
@ -302,11 +308,14 @@ function GameCharacter() {
for (i = 0; i < platforms[this.curGroundIndex].length; i++) {
// If a character is falling above some platform, memorize it
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 (typeof this.memorizedPlatform !== 'undefined' &&
this.memorizedPlatform.isBelow(this)) {
if (
typeof this.memorizedPlatform !== 'undefined' &&
this.memorizedPlatform.isBelow(this)
) {
this.y = this.memorizedPlatform.y;
this.isFalling = false;
this.curJumpingStrength = 0;
@ -324,9 +333,10 @@ function GameCharacter() {
// Not plummeting, standing or falling means being on a platform.
// Perform checks whether the player is still on the same platform,
// and fall them in case they aren't.
if (!this.memorizedPlatform._isWithinX(this)
||
this.curGroundIndex != this.memorizedPlatform.curGroundIndex) {
if (
!this.memorizedPlatform._isWithinX(this) ||
this.curGroundIndex != this.memorizedPlatform.curGroundIndex
) {
this.isFalling = true;
this.isOnPlatform = false;
this.memorizedPlatform = undefined;
@ -364,7 +374,6 @@ function GameCharacter() {
this.scale += 0.1;
}
};
}
// NB: All the platforms and enemies mechanics were done
// before the task was seen on Week 20.
@ -387,12 +396,7 @@ function Platform(curGroundIndex, x, width, y) {
stroke(0);
strokeWeight(2);
fill(palette.ground_colors[curGroundIndex]);
rect(
this.x - this.width,
this.y,
width * 2,
height / 100,
);
rect(this.x - this.width, this.y, width * 2, height / 100);
pop();
};
this.drawShadow = function () {
@ -438,33 +442,47 @@ function Enemy(curGroundIndex, x, y, size) {
this.curGroundIndex = curGroundIndex;
this.draw = function () {
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);
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);
textSize(this.size/1.5);
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);
textSize(this.size / 1.5);
textAlign(CENTER);
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();
}
};
this.drawShadow = function () {
push();
fill('rgba(0,0,0,0.2)');
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();
}
};
this.collidesWith = function (who) {
if (dist(who.x, who.y, this.x, this.y) < this.size/2 &&
who.curGroundIndex == this.curGroundIndex) return true;
if (
dist(who.x, who.y, this.x, this.y) < this.size / 2 &&
who.curGroundIndex == this.curGroundIndex
)
return true;
return false;
}
};
this.updatePosition = function () {
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.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));
};
}
var text_size;
@ -495,7 +513,7 @@ function setup() {
coin_inner: color('#FDC334'),
heart_color: color('darkred'),
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));
}
@ -562,10 +580,7 @@ function generateObjects() {
skew: random(-20, 20),
};
// To prevent spiky mountains which are quite ugly
mountains[i].height = max(
mountains[i].width,
mountains[i].height,
);
mountains[i].height = max(mountains[i].width, mountains[i].height);
}
// Start mountains.
for (i = 0; i < 6; i++) {
@ -599,9 +614,7 @@ function generateObjects() {
collectables[0] = {
x: 600,
curGroundIndex: 0,
y:
groundPositions[0] -
10 /*taking 10 for better visuals*/,
y: groundPositions[0] - 10 /*taking 10 for better visuals*/,
size: 75,
isFound: false,
};
@ -614,9 +627,7 @@ function generateObjects() {
collectables[i] = {
x: collectables[i - 1].x + 50 + 100 * random(0.5, 1),
curGroundIndex: r,
y:
groundPositions[r] -
10 /*taking 10 for better visuals*/,
y: groundPositions[r] - 10 /*taking 10 for better visuals*/,
size: 75,
isFound: false,
};
@ -624,10 +635,8 @@ function generateObjects() {
// marking it as isFound (making it disabled) in case if.
for (k = 0; k < rivers.length; k++) {
if (
rivers[k].x - rivers[k].width / 2 <
collectables[i].x &&
rivers[k].x + rivers[k].width / 2 >
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;
}
@ -660,9 +669,7 @@ function generateObjects() {
groundPositions[2] - Platform.max_y_deviation,
);
} else {
let groundPosIndex = floor(
random(0, groundPositions.length),
);
let groundPosIndex = floor(random(0, groundPositions.length));
// array.slice(-1)[0] gets the last element of an array without removing it,
// contrary to array.pop().
prev = platforms[groundPosIndex].slice(-1)[0];
@ -677,7 +684,7 @@ function generateObjects() {
max(
random(
prev.y - prev.max_y_deviation,
floorPos_y - prev.max_y_deviation/2,
floorPos_y - prev.max_y_deviation / 2,
),
height / 8,
),
@ -687,16 +694,16 @@ function generateObjects() {
}
// Enemies
enemies = [];
for (i = 0; i < floor(finish_position_x/130); i++) {
index = floor(random(0, groundPositions.length))
for (i = 0; i < floor(finish_position_x / 130); i++) {
index = floor(random(0, groundPositions.length));
enemies.push(
new Enemy(
index,
random(500, finish_position_x),
groundPositions[index] - random(50, floorPos_y),
random(40, 60)
)
)
random(40, 60),
),
);
}
}
function draw() {
@ -749,11 +756,14 @@ function draw() {
// GAMECHAR & PLATFORMS RENDER --
complexDraw();
// -------- ENEMIES -------------
for (i=0; i<enemies.length; i++) {
for (i = 0; i < enemies.length; i++) {
enemies[i].updatePosition();
enemies[i].draw();
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();
}
}
@ -769,7 +779,9 @@ function complexDraw() {
for (i = 0; i <= gameChar.curGroundIndex; i++) {
drawPlatforms(i);
}
if (shadows_enabled) {gameChar.drawShadow();}
if (shadows_enabled) {
gameChar.drawShadow();
}
gameChar.draw();
for (i = gameChar.curGroundIndex + 1; i < groundPositions.length; i++) {
drawPlatforms(i);