drawing platforms, constructor function for them

This commit is contained in:
Mottributo 2023-03-26 01:29:52 +03:00
parent d1c0001865
commit aa0943467f

View File

@ -5,6 +5,7 @@ var processStop_timer;
var fps_recent_values = [];
var death_timer;
var showDebugData = false;
var debug_charTrace = [];
var gameChar;
@ -21,6 +22,45 @@ var platforms = [];
var enemies = [];
var flagpole;
function Platform(curGroundIndex, x, width, y) {
this.curGroundIndex = curGroundIndex;
this.x = x;
this.width = width;
this.y = y || Math.pow(gameChar.baseJumpingStrength, 2) / 2 - 2 * gameChar.baseJumpingStrength;
this.draw = function () {
push();
stroke(0);
strokeWeight(2);
fill(palette.ground_color0);
rect(
this.x - this.width,
floorPos_y - this.y,
width*2, height/100,
);
pop();
};
this._isWithinX = function (who) {
if (who.x > this.x - this.width && who.x < this.x + this.width) {
return true;
}
};
this.isBelow = function (who) {
if (this._isWithinX(who) && who.y > this.y) {
return true;
}
};
this.isAbove = function (who) {
if (this._isWithinX(who) && who.y < this.y) {
return true;
}
};
}
var text_size;
// Variables to set colors. Set in setup()
var palette;
@ -401,14 +441,13 @@ function startGame(level_start) {
}
}
}
for (i = 0; i < groundPositions.length; i++) {
platforms[i] = [];
}
for (i = 0; i < 100; i++) {
if (i == 0) {
platforms[0] = {
curGroundIndex: 0,
x: 700,
width: 50,
y: pow(gameChar.baseJumpingStrength, 2) / 2,
}
platforms[0][0] = new Platform(0, 700, 50,
pow(gameChar.baseJumpingStrength, 2) / 2 - 2*gameChar.baseJumpingStrength);
}
}
}
@ -462,6 +501,8 @@ function draw() {
// -------- FLAGPOLE ------------
renderFlagpole();
if (!flagpole.isReached) checkFlagpole();
// -------- PLATFORMS
drawPlatforms();
// -------- GAME CHARACTER ------
{
@ -511,12 +552,28 @@ function draw() {
text("Level complete. Press space to continue...", 0, height / 2);
if (showDebugData) {
text(gameChar.curGroundIndex, 99, 99);
text(gameChar.getCurGroundPosY(), 188, 99);
text(gameChar.getCurGroundY(), 188, 99);
text(gameChar.sprite, 277, 99);
// draw game character's trajectory
drawCharTrace();
drawFps();
}
pop();
}
function drawCharTrace() {
debug_charTrace.push([gameChar.x, gameChar.y]);
if (debug_charTrace.length>100) debug_charTrace.shift();
push()
stroke(255,0,0);
strokeWeight(3);
translate(-cameraPosX, 0);
for (i=0;i<debug_charTrace.length;i++) {
point(debug_charTrace[i][0], debug_charTrace[i][1]);
}
translate(cameraPosX, 0);
pop();
}
function checkRiver(t_river) {
if (
gameChar.x > river.x - river.width / 2 &&
@ -610,7 +667,6 @@ function drawRiver(t_river) {
}
function drawCollectable(t_collectable) {
push();
// centering coins a bit upper
// animating the coin's jiggle
// a - vert. intensity, c - hor. intensity, b - vert. speed, d - hor. speed
a = 0.1;
@ -786,6 +842,15 @@ function drawTrees() {
}
}
}
function drawPlatforms() {
for (i=0; i<groundPositions.length; i++) {
for (k=0; k<platforms[i].length;k++) {
platforms[i][k].draw();
}
}
}
function renderFlagpole() {
// NB - This function is implemented a bit differently than how it was requested in Part 6.
// It has no states to switch between - instead, the flag gradually goes into the opposite direction