Refactoring, implemented enemies

This commit is contained in:
Mottributo 2023-03-26 23:49:48 +03:00
parent 2f832bd2e6
commit 2daca56a91

127
sketch.js
View File

@ -6,7 +6,7 @@ var fps_recent_values = [];
var death_timer; var death_timer;
var showDebugData = false; var showDebugData = false;
var debug_charTrace = []; var debug_charTrace = [];
var shadows_enabled = false; var shadows_enabled = true;
var gameChar; var gameChar;
@ -43,6 +43,7 @@ function GameCharacter() {
this.isOnPlatform = false; this.isOnPlatform = false;
this.curGroundIndex = 0; this.curGroundIndex = 0;
this.memorizedPlatform; this.memorizedPlatform;
this.invincibility_time = 0;
this._updateSprite = function () { this._updateSprite = function () {
if (this.isPlummeting) { if (this.isPlummeting) {
@ -114,7 +115,6 @@ function GameCharacter() {
stroke(1); stroke(1);
this.x_step *= this.scale; this.x_step *= this.scale;
this.y_step *= this.scale; this.y_step *= this.scale;
// Standing, facing frontwards // Standing, facing frontwards
if (this.sprite == 1) { if (this.sprite == 1) {
// Body // Body
@ -226,6 +226,12 @@ function GameCharacter() {
console.error('Bad this sprite number: ' + this.sprite); console.error('Bad this sprite number: ' + this.sprite);
noLoop(); noLoop();
} }
// Drawing an aura of invincibility
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);
}
this.x_step /= this.scale; this.x_step /= this.scale;
this.y_step /= this.scale; this.y_step /= this.scale;
pop(); pop();
@ -249,6 +255,10 @@ function GameCharacter() {
this.y_step /= this.scale; this.y_step /= this.scale;
pop(); pop();
}; };
this.takeLife = function () {
gameChar.curLives--;
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) {
@ -263,6 +273,7 @@ function GameCharacter() {
} }
}; };
this.act = function () { this.act = function () {
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;
@ -323,7 +334,6 @@ function GameCharacter() {
this.y += 3; this.y += 3;
} }
}; };
this.getCurGroundY = function () { this.getCurGroundY = function () {
return groundPositions[this.curGroundIndex]; return groundPositions[this.curGroundIndex];
}; };
@ -346,8 +356,9 @@ function GameCharacter() {
this.scale += 0.1; this.scale += 0.1;
} }
}; };
} }
// NB: All the platforms 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.
// Therefore, the implementation may // Therefore, the implementation may
// differ from one in the lecture. // differ from one in the lecture.
@ -380,7 +391,7 @@ 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.08)');
rect( rect(
this.x - this.width, this.x - this.width,
groundPositions[curGroundIndex], groundPositions[curGroundIndex],
@ -412,6 +423,41 @@ function Platform(curGroundIndex, x, width, y) {
return false; return false;
}; };
} }
function Enemy(curGroundIndex, x, y, size) {
this.x = x;
this.y = y;
this.size = size;
this.curGroundIndex = curGroundIndex;
this.draw = function () {
push();
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);
textAlign(CENTER);
fill(palette.enemy_body_color);
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);
pop();
}
this.collidesWith = function (who) {
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));
}
}
var text_size; var text_size;
// Variables to set colors. Set in setup() // Variables to set colors. Set in setup()
@ -440,8 +486,8 @@ function setup() {
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('rgba(0,0,0,0.5)'),
enemy_body_color: color('red'), enemy_body_color: color('rgba(255,255,255,0.3)')
}; };
startGame((level_start = true)); startGame((level_start = true));
} }
@ -457,7 +503,7 @@ function startGame(level_start) {
camera_speed = 0.9; camera_speed = 0.9;
gravity = 1; gravity = 1;
game_score = 0; game_score = 0;
finish_position_x = 2000; finish_position_x = 6000;
textSize(width / 20); textSize(width / 20);
flagpole = { flagpole = {
x: finish_position_x, x: finish_position_x,
@ -471,12 +517,27 @@ function startGame(level_start) {
if (level_start) { if (level_start) {
gameChar = new GameCharacter(); gameChar = new GameCharacter();
cameraPosX = gameChar.x - width / 2; cameraPosX = gameChar.x - width / 2;
// Creating trees, clouds, mountains, rivers, collectables. generateObjects();
{ } else {
gameChar.x = width / 2;
gameChar.y = floorPos_y + (height - floorPos_y) / 6;
gameChar.scale = 1;
gameChar.curGroundIndex = 0;
gameChar.isPlummeting = false;
gameChar.isFalling = false;
gameChar.isJumping = false;
gameChar.isLeft = false;
gameChar.isRight = false;
gameChar.isOnPlatform = false;
}
}
function generateObjects() {
// Creating trees, clouds, mountains, rivers, collectables, platforms, enemies. // Creating trees, clouds, mountains, rivers, collectables, platforms, enemies.
// Trees coords
for (i = 0; i < 150; i++) { for (i = 0; i < 150; i++) {
trees_x[i] = random(-100, 10000); trees_x[i] = random(-100, 10000);
} }
// Clouds
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
clouds[i] = { clouds[i] = {
x: random(-100, 10000), x: random(-100, 10000),
@ -507,6 +568,7 @@ function startGame(level_start) {
skew: (random(-10, 20) * i) / 2, skew: (random(-10, 20) * i) / 2,
}; };
} }
// Rivers
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
if (i == 0) { if (i == 0) {
rivers[0] = { rivers[0] = {
@ -517,12 +579,13 @@ function startGame(level_start) {
} else { } else {
if (rivers[i - 1].x + 600 > finish_position_x) break; if (rivers[i - 1].x + 600 > finish_position_x) break;
rivers[i] = { rivers[i] = {
x: rivers[i - 1].x + 300 + 200 * random(0.5, 1), x: rivers[i - 1].x + 100 + 200 * random(0.5, 1),
width: 50 + 30 * random(), width: 50 + 30 * random(),
points: [], points: [],
}; };
} }
} }
// Collectables
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
if (i == 0) { if (i == 0) {
collectables[0] = { collectables[0] = {
@ -563,9 +626,11 @@ function startGame(level_start) {
} }
} }
} }
// Platform rows
for (i = 0; i < groundPositions.length; i++) { for (i = 0; i < groundPositions.length; i++) {
platforms[i] = []; platforms[i] = [];
} }
// Platforms
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
if (i == 0) { if (i == 0) {
platforms[0][0] = new Platform( platforms[0][0] = new Platform(
@ -595,6 +660,7 @@ function startGame(level_start) {
prev = platforms[groundPosIndex].slice(-1)[0]; prev = platforms[groundPosIndex].slice(-1)[0];
// stopping generation after the flagpole // stopping generation after the flagpole
if (prev.x + prev.width + 155 > finish_position_x) break; if (prev.x + prev.width + 155 > finish_position_x) break;
// adding platforms
platforms[groundPosIndex].push( platforms[groundPosIndex].push(
new Platform( new Platform(
groundPosIndex, groundPosIndex,
@ -611,18 +677,18 @@ function startGame(level_start) {
); );
} }
} }
} // Enemies
} else { enemies = [];
gameChar.x = width / 2; for (i = 0; i < floor(finish_position_x/130); i++) {
gameChar.y = floorPos_y + (height - floorPos_y) / 6; index = floor(random(0, groundPositions.length))
gameChar.scale = 1; enemies.push(
gameChar.curGroundIndex = 0; new Enemy(
gameChar.isPlummeting = false; index,
gameChar.isFalling = false; random(500, finish_position_x),
gameChar.isJumping = false; groundPositions[index] - random(50, floorPos_y),
gameChar.isLeft = false; random(40, 60)
gameChar.isRight = false; )
gameChar.isOnPlatform = false; )
} }
} }
function draw() { function draw() {
@ -672,8 +738,17 @@ function draw() {
// -------- FLAGPOLE ------------ // -------- FLAGPOLE ------------
renderFlagpole(); renderFlagpole();
if (!flagpole.isReached) checkFlagpole(); if (!flagpole.isReached) checkFlagpole();
// GAMECHAR, PLATFORMS & ENEMIES RENDER // GAMECHAR & PLATFORMS RENDER --
complexDraw(); complexDraw();
// -------- ENEMIES -------------
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) {
gameChar.takeLife();
}
}
// - ------ GAME CHARACTER ------ // - ------ GAME CHARACTER ------
gameChar.act(); gameChar.act();
pop(); // Scrolling end pop(); // Scrolling end
@ -681,12 +756,12 @@ function draw() {
drawInterface(); drawInterface();
} }
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 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);