diff --git a/sketch.js b/sketch.js index 9112f2a..7ac6d16 100644 --- a/sketch.js +++ b/sketch.js @@ -78,10 +78,181 @@ function startGame(full_start, update_objects) { ], curGroundPosYIndex: 0, - getCurGroundPosY() { + draw: function() { + push(); + strokeWeight(1); + stroke(1); + gameChar.x_step *= gameChar.scale; + gameChar.y_step *= gameChar.scale; + function _drawBody(jumping = false) { + fill(palette.body_color); + if (jumping) { + triangle( + gameChar.x - gameChar.x_step * 2, + gameChar.y - gameChar.y_step * 1.5, + gameChar.x, + gameChar.y - gameChar.y_step * 4, + gameChar.x + gameChar.x_step * 2, + gameChar.y - gameChar.y_step * 1.5 + ); + } else { + triangle( + gameChar.x - gameChar.x_step * 2, + gameChar.y, + gameChar.x, + gameChar.y - gameChar.y_step * 4, + gameChar.x + gameChar.x_step * 2, + gameChar.y + ); + } + } + function _drawHead() { + fill(palette.head_color); + ellipse( + gameChar.x, + gameChar.y - gameChar.y_step * 5, + gameChar.x_step * 6, + gameChar.y_step * 4 + ); + } + function _drawEyes(draw_left, draw_right, eyes_height) { + fill(0); + if (draw_left) { + ellipse( + gameChar.x - gameChar.x_step * 1.2, + gameChar.y - gameChar.y_step * eyes_height, + gameChar.x_step / 1.5, + gameChar.y_step / 2 + ); + } + if (draw_right) { + ellipse( + gameChar.x + gameChar.x_step * 1.2, + gameChar.y - gameChar.y_step * eyes_height, + gameChar.x_step / 1.5, + gameChar.y_step / 2 + ); + } + } + // Standing, facing frontwards + if (gameChar.sprite == 1) { + // Body + _drawBody(); + // Head + _drawHead(); + // Eyes + _drawEyes(true, true, 4.2); + } + // Jumping, facing forwards + else if (gameChar.sprite == 2) { + // Body + _drawBody(true); + // Hands. (Hands!) + line( + gameChar.x - gameChar.x_step * 1, + gameChar.y - gameChar.y_step * 2.5, + gameChar.x - gameChar.x_step * 3.2, + gameChar.y - gameChar.y_step * 3.2 + ); + line( + gameChar.x + gameChar.x_step * 1, + gameChar.y - gameChar.y_step * 2.5, + gameChar.x + gameChar.x_step * 3.2, + gameChar.y - gameChar.y_step * 3.2 + ); + // Head + _drawHead(); + // Eyes + _drawEyes(true, true, 3.8); + } + // Walking right + else if (gameChar.sprite == 3) { + // Body + _drawBody(); + // Hand. (Hand!) + line( + gameChar.x, + gameChar.y - gameChar.y_step * 2.5, + gameChar.x + gameChar.x_step * 0.8, + gameChar.y - gameChar.y_step * 1 + ); + // Head + _drawHead(); + // Eyes + _drawEyes(false, true, 4.2); + } + // Walking left + else if (gameChar.sprite == 4) { + // Body + _drawBody(); + // Hand. (Hand!) + line( + gameChar.x, + gameChar.y - gameChar.y_step * 2.5, + gameChar.x - gameChar.x_step * 0.8, + gameChar.y - gameChar.y_step * 1 + ); + // Head + _drawHead(); + // Eyess + _drawEyes(true, false, 4.2); + } + // Jumping left + else if (gameChar.sprite == 5) { + // Body + _drawBody(true); + // Hands. (Hands!) + line( + gameChar.x - gameChar.x_step * 1, + gameChar.y - gameChar.y_step * 2.5, + gameChar.x - gameChar.x_step * 3.2, + gameChar.y - gameChar.y_step * 3.2 + ); + line( + gameChar.x + gameChar.x_step * 1, + gameChar.y - gameChar.y_step * 2.5, + gameChar.x + gameChar.x_step * 3.2, + gameChar.y - gameChar.y_step * 3.2 + ); + // Head + _drawHead(); + // Eyes + _drawEyes(true, false, 3.8); + } + // Jumping right + else if (gameChar.sprite == 6) { + // Body + _drawBody(true); + // Hands. (Hands!) + line( + gameChar.x - gameChar.x_step * 1, + gameChar.y - gameChar.y_step * 2.5, + gameChar.x - gameChar.x_step * 3.2, + gameChar.y - gameChar.y_step * 3.2 + ); + line( + gameChar.x + gameChar.x_step * 1, + gameChar.y - gameChar.y_step * 2.5, + gameChar.x + gameChar.x_step * 3.2, + gameChar.y - gameChar.y_step * 3.2 + ); + // Head + _drawHead(); + // Eyes + _drawEyes(false, true, 3.8); + } else { + text("Bad sprite number!", 10, 10); + console.error("Bad gameChar sprite number: " + gameChar.sprite); + } + gameChar.x_step /= gameChar.scale; + gameChar.y_step /= gameChar.scale; + pop(); + }, + + getCurGroundPosY: function() { return this.possibleGroundPosY[this.curGroundPosYIndex]; }, - goUp() { + goUp: function() { memorizedIndex = this.curGroundPosYIndex; this.curGroundPosYIndex = max(0, this.curGroundPosYIndex - 1); if (memorizedIndex != this.curGroundPosYIndex) { @@ -89,7 +260,7 @@ function startGame(full_start, update_objects) { gameChar.scale -= 0.1; } }, - goDown() { + goDown: function() { memorizedIndex = this.curGroundPosYIndex; this.curGroundPosYIndex = min( this.possibleGroundPosY.length - 1, @@ -284,7 +455,7 @@ function draw() { gameChar.y += 3; } // Drawing a sprite - drawGameCharacter(); + gameChar.draw(); } pop(); // Scrolling // -------- INTERFACE ----------- @@ -304,175 +475,6 @@ function draw() { } pop(); } - -function drawGameCharacter() { - strokeWeight(1); - stroke(1); - gameChar.x_step *= gameChar.scale; - gameChar.y_step *= gameChar.scale; - function _drawBody(jumping = false) { - fill(palette.body_color); - if (jumping) { - triangle( - gameChar.x - gameChar.x_step * 2, - gameChar.y - gameChar.y_step * 1.5, - gameChar.x, - gameChar.y - gameChar.y_step * 4, - gameChar.x + gameChar.x_step * 2, - gameChar.y - gameChar.y_step * 1.5 - ); - } else { - triangle( - gameChar.x - gameChar.x_step * 2, - gameChar.y, - gameChar.x, - gameChar.y - gameChar.y_step * 4, - gameChar.x + gameChar.x_step * 2, - gameChar.y - ); - } - } - function _drawHead() { - fill(palette.head_color); - ellipse( - gameChar.x, - gameChar.y - gameChar.y_step * 5, - gameChar.x_step * 6, - gameChar.y_step * 4 - ); - } - function _drawEyes(draw_left, draw_right, eyes_height) { - fill(0); - if (draw_left) { - ellipse( - gameChar.x - gameChar.x_step * 1.2, - gameChar.y - gameChar.y_step * eyes_height, - gameChar.x_step / 1.5, - gameChar.y_step / 2 - ); - } - if (draw_right) { - ellipse( - gameChar.x + gameChar.x_step * 1.2, - gameChar.y - gameChar.y_step * eyes_height, - gameChar.x_step / 1.5, - gameChar.y_step / 2 - ); - } - } - // Standing, facing frontwards - if (gameChar.sprite == 1) { - // Body - _drawBody(); - // Head - _drawHead(); - // Eyes - _drawEyes(true, true, 4.2); - } - // Jumping, facing forwards - else if (gameChar.sprite == 2) { - // Body - _drawBody(true); - // Hands. (Hands!) - line( - gameChar.x - gameChar.x_step * 1, - gameChar.y - gameChar.y_step * 2.5, - gameChar.x - gameChar.x_step * 3.2, - gameChar.y - gameChar.y_step * 3.2 - ); - line( - gameChar.x + gameChar.x_step * 1, - gameChar.y - gameChar.y_step * 2.5, - gameChar.x + gameChar.x_step * 3.2, - gameChar.y - gameChar.y_step * 3.2 - ); - // Head - _drawHead(); - // Eyes - _drawEyes(true, true, 3.8); - } - // Walking right - else if (gameChar.sprite == 3) { - // Body - _drawBody(); - // Hand. (Hand!) - line( - gameChar.x, - gameChar.y - gameChar.y_step * 2.5, - gameChar.x + gameChar.x_step * 0.8, - gameChar.y - gameChar.y_step * 1 - ); - // Head - _drawHead(); - // Eyes - _drawEyes(false, true, 4.2); - } - // Walking left - else if (gameChar.sprite == 4) { - // Body - _drawBody(); - // Hand. (Hand!) - line( - gameChar.x, - gameChar.y - gameChar.y_step * 2.5, - gameChar.x - gameChar.x_step * 0.8, - gameChar.y - gameChar.y_step * 1 - ); - // Head - _drawHead(); - // Eyess - _drawEyes(true, false, 4.2); - } - // Jumping left - else if (gameChar.sprite == 5) { - // Body - _drawBody(true); - // Hands. (Hands!) - line( - gameChar.x - gameChar.x_step * 1, - gameChar.y - gameChar.y_step * 2.5, - gameChar.x - gameChar.x_step * 3.2, - gameChar.y - gameChar.y_step * 3.2 - ); - line( - gameChar.x + gameChar.x_step * 1, - gameChar.y - gameChar.y_step * 2.5, - gameChar.x + gameChar.x_step * 3.2, - gameChar.y - gameChar.y_step * 3.2 - ); - // Head - _drawHead(); - // Eyes - _drawEyes(true, false, 3.8); - } - // Jumping right - else if (gameChar.sprite == 6) { - // Body - _drawBody(true); - // Hands. (Hands!) - line( - gameChar.x - gameChar.x_step * 1, - gameChar.y - gameChar.y_step * 2.5, - gameChar.x - gameChar.x_step * 3.2, - gameChar.y - gameChar.y_step * 3.2 - ); - line( - gameChar.x + gameChar.x_step * 1, - gameChar.y - gameChar.y_step * 2.5, - gameChar.x + gameChar.x_step * 3.2, - gameChar.y - gameChar.y_step * 3.2 - ); - // Head - _drawHead(); - // Eyes - _drawEyes(false, true, 3.8); - } else { - text("Bad sprite number!", 10, 10); - console.error("Bad gameChar sprite number: " + gameChar.sprite); - } - gameChar.x_step /= gameChar.scale; - gameChar.y_step /= gameChar.scale; -} function checkCanyon(t_canyon) { if ( gameChar.x > canyon.x - canyon.width / 2 &&