From 71d15df2dd3e96f6d5730a136ad2a0fc5273e44d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Mon, 10 Sep 2018 19:28:36 +0200 Subject: [PATCH] Add head bob --- demo2.cpp | 2 ++ demo3.cpp | 14 +++++--------- general.hpp | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/demo2.cpp b/demo2.cpp index 37abbb6..4b8fd56 100644 --- a/demo2.cpp +++ b/demo2.cpp @@ -809,7 +809,9 @@ void draw() c.maxSteps = 20; c.computeTextureCoords = 1; +player.mCamera.height += player.mHeadBob; render(player.mCamera,floorHeightAt,0,textureAt,pixelFunc,c); +player.mCamera.height -= player.mHeadBob; if (shotFired) { diff --git a/demo3.cpp b/demo3.cpp index fb4ecca..2c6c775 100644 --- a/demo3.cpp +++ b/demo3.cpp @@ -9,6 +9,9 @@ // redefine player's height #define CAMERA_COLL_HEIGHT_BELOW ((3 * UNITS_PER_SQUARE) / 2) +#define FPS 40 +#define HEAD_BOB_HEIGHT 150 +#define HEAD_BOB_STEP 20 #include "general.hpp" @@ -237,16 +240,9 @@ void draw() c.maxSteps = 20; c.computeTextureCoords = 0; + player.mCamera.height += player.mHeadBob; render(player.mCamera,floorHeightAt,0,colorAt,pixelFunc,c); -/* -pokitto.display.setColor(rgbToIndex(7,7,3)); -pokitto.display.setCursor(1,1); -pokitto.display.print(player.mCamera.position.x); -pokitto.display.print(" "); -pokitto.display.print(player.mCamera.position.y); -pokitto.display.print(" "); -pokitto.display.print(player.mCamera.direction); -*/ + player.mCamera.height -= player.mHeadBob; } int main() diff --git a/general.hpp b/general.hpp index 83fa89f..6893a85 100644 --- a/general.hpp +++ b/general.hpp @@ -33,6 +33,14 @@ Pokitto::Core pokitto; #define PLAYER_JUMP_SPEED 500 #endif +#ifndef HEAD_BOB_HEIGHT +#define HEAD_BOB_HEIGHT 100 +#endif + +#ifndef HEAD_BOB_STEP +#define HEAD_BOB_STEP 10 +#endif + #ifndef GRAVITY_ACCELERATION #define GRAVITY_ACCELERATION ((3 * UNITS_PER_SQUARE) / 2) #endif @@ -205,6 +213,8 @@ public: Camera mCamera; Unit mVericalSpeed; bool mRunning; + Unit mHeadBob; + bool mHeadBobUp; Player() { @@ -217,6 +227,8 @@ public: mCamera.shear = 0; mVericalSpeed = 0; mRunning = false; + mHeadBob = 0; + mHeadBobUp = true; } void setPosition(Unit x, Unit y) @@ -250,7 +262,16 @@ public: moveOffset.x = (moveOffset.x * horizontalStep) / UNITS_PER_SQUARE; moveOffset.y = (moveOffset.y * horizontalStep) / UNITS_PER_SQUARE; + + mHeadBob += mHeadBobUp ? HEAD_BOB_STEP : -HEAD_BOB_STEP; + + if (mHeadBob > HEAD_BOB_HEIGHT) + mHeadBobUp = false; + else if (mHeadBob < -HEAD_BOB_HEIGHT) + mHeadBobUp = true; } + else + mHeadBob /= 2; if (turnDirection != 0) {