Skip to content

Commit

Permalink
Logger and Movement Update
Browse files Browse the repository at this point in the history
- Logger can now log vectors
- Logger now supports error and warning coloring
- changed the Logger output of all classes to use this
- movement of the spaceships is keeping the velocity and is now slowy decelerating
- progressbar and label on the left bottom corner now shows the velocity
- added the possibilty to change the maximum value of a progressbar
- spaceship can now move up and down
  • Loading branch information
Christian Koehlke committed Apr 13, 2021
1 parent 38b14ba commit 82805ec
Show file tree
Hide file tree
Showing 30 changed files with 298 additions and 133 deletions.
6 changes: 3 additions & 3 deletions Game/AudioManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void AudioManager::init()

if (!SoundEngine)
{
Logger::log("Error while starting irrKlang SoundEngine");
Logger::error("Error while starting irrKlang SoundEngine");
return; // error starting up the engine
}

Expand Down Expand Up @@ -46,7 +46,7 @@ irrklang::ISound* AudioManager::play2D(std::string musicFile, AudioType audiotyp
irrklang::ISound* sound = SoundEngine->play2D(musicFile.c_str(), playLooped, false, true);
if (!sound)
{
Logger::log("Error playing sound: " + musicFile);
Logger::error("Error playing sound: " + musicFile);
return nullptr;
}

Expand Down Expand Up @@ -77,7 +77,7 @@ irrklang::ISound* AudioManager::play3D(std::string musicFile, glm::vec3 position
irrklang::ISound* sound = SoundEngine->play3D(musicFile.c_str(), glmVec3toIrrklang(position), playLooped, false, true);
if (!sound)
{
Logger::log("Error playing sound: " + musicFile);
Logger::error("Error playing sound: " + musicFile);
return nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion Game/Bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void Bullet::checkHit()

this->registerHit();
collidedObject->object->registerHit();
Logger::log(collidedObject->object->printObject() + " was hit");
Logger::info(collidedObject->object->printObject() + " was hit");

AudioManager::play3D("audio/hit.wav", this->getPosition());
}
Expand Down
2 changes: 1 addition & 1 deletion Game/Camera_FPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void FPSCamera::moveSideways(float amount)
update();
}

void FPSCamera::moveUp(float amount)
void FPSCamera::moveVertical(float amount)
{
translate(up * amount);
update();
Expand Down
2 changes: 1 addition & 1 deletion Game/Camera_FPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FPSCamera : public Camera {

void moveSideways(float amount);

void moveUp(float amount);
void moveVertical(float amount);

protected:
float yaw;
Expand Down
2 changes: 1 addition & 1 deletion Game/Camera_ThirdPerson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void ThirdPersonCamera::moveSideways(float amount)
update();
}

void ThirdPersonCamera::moveUp(float amount)
void ThirdPersonCamera::moveVertical(float amount)
{
translate(up * amount);
update();
Expand Down
2 changes: 1 addition & 1 deletion Game/Camera_ThirdPerson.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ThirdPersonCamera : public Camera {

void moveSideways(float amount);

void moveUp(float amount);
void moveVertical(float amount);

protected:
glm::vec3 lookAt_NotNormalized;
Expand Down
96 changes: 80 additions & 16 deletions Game/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,101 @@ void Character::interactWithObject()

void Character::resetVerticalMovement()
{
movement = movement * glm::vec3(0, 0, 0);
////movement = movement * glm::vec3(0, 0, 0);
float brakevalue = 0.02f;
//
Logger::logVector(movement,"Move",2,"\033[34m");

//if (movement.x > brakevalue) movement.x -= brakevalue;
//if (movement.x < -brakevalue) movement.x += brakevalue;
//if (movement.x > -brakevalue && movement.x < brakevalue) movement.x = 0;

//if (movement.y > brakevalue) movement.y -= brakevalue;
//if (movement.y < -brakevalue) movement.y += brakevalue;
//if (movement.y > -brakevalue && movement.y < brakevalue) movement.y = 0;

//if (movement.z > brakevalue) movement.z -= brakevalue;
//if (movement.z < -brakevalue) movement.z += brakevalue;
//if (movement.z > -brakevalue && movement.z < brakevalue) movement.z = 0;

/*if (!Game::isKeyPressed(PlayerAction::moveForward)) {
glm::vec3 moveFront = movement * vecFront;
if (glm::length(moveFront) > brakevalue) {
if (glm::dot(movement, vecFront) > 0) {
Logger::warn("moving forward");
movement += glm::normalize(moveFront) * brakevalue;
}
else {
Logger::warn("moving backward");
movement -= glm::normalize(moveFront) * brakevalue;
}
}
}*/

glm::vec3 moveVec = glm::normalize(movement);
actualSpeed = glm::length(movement);

if (actualSpeed > 0) {
movement -= moveVec * brakevalue;
}
}

void Character::moveForward() {
glm::vec3 v = /*glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f) * */ getVecFront()/*)*/ * forwardSpeed;// *Game::getDelta() / 1000.0f;
glm::vec3 a = vecFront * forwardAccel;

movement += v;
if (glm::length(movement) < maxSpeed)
{
movement += a;
}
}

void Character::moveBackward() {
glm::vec3 v = /*glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f) * */ getVecFront()/*)*/ * -backwardSidewaySpeed;// *Game::getDelta() / 1000.0f;
glm::vec3 a = vecFront * -backwardSidewayAccel;

movement += v;
if (glm::length(movement) < maxSpeed)
{
movement += a;
}
}

void Character::moveRight() {
glm::mat4 roll_mat = glm::rotate(glm::mat4(1.0f), glm::radians(cha_roll), getVecFront());
glm::vec3 up2 = glm::mat3(roll_mat) * getVecUp();

glm::vec3 v = /*glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f) * */ glm::cross(getVecFront(), up2)/*)*/ * backwardSidewaySpeed;// *Game::getDelta() / 1000.0f;
glm::vec3 a = vecRight * backwardSidewayAccel;

movement += v;
if (glm::length(movement) < maxSpeed)
{
movement += a;
}
}

void Character::moveLeft() {
glm::mat4 roll_mat = glm::rotate(glm::mat4(1.0f), glm::radians(cha_roll), getVecFront());
glm::vec3 up2 = glm::mat3(roll_mat) * getVecUp();

glm::vec3 v = /*glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f) * */ glm::cross(getVecFront(), up2)/*)*/ * -backwardSidewaySpeed; //* Game::getDelta() / 1000.0f;
glm::vec3 a = vecRight * -backwardSidewayAccel;

if (glm::length(movement) < maxSpeed)
{
movement += a;
}
}

void Character::moveUp()
{
glm::vec3 a = vecUp * upwardAccel;

if (glm::length(movement) < maxSpeed)
{
movement += a;
}
}

void Character::moveDown()
{
glm::vec3 a = vecUp * -upwardAccel;

movement += v;
if (glm::length(movement) < maxSpeed)
{
movement += a;
}
}

void Character::rollLeft()
Expand Down Expand Up @@ -134,7 +198,7 @@ void Character::activateJumping()
if (!canJump)
{
canJump = true;
Logger::log("Jumping activated for: " + printObject());
Logger::info("Jumping activated for: " + printObject());
}
}

Expand All @@ -154,12 +218,12 @@ void Character::run(bool run)
{
if (run && !isRunning)
{
forwardSpeed = forwardSpeed * 2;
forwardAccel = forwardAccel * 2;
isRunning = true;
}
else if (!run && isRunning)
{
forwardSpeed = forwardSpeed / 2;
forwardAccel = forwardAccel / 2;
isRunning = false;
}
}
Expand Down
14 changes: 10 additions & 4 deletions Game/Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class Character : public Object

void moveLeft();

void moveUp();

void moveDown();

void rollLeft();

void rollRight();
Expand Down Expand Up @@ -83,12 +87,14 @@ class Character : public Object
glm::vec3 vecRight;



float32 forwardSpeed = 10; //per second
float32 backwardSidewaySpeed = 5; //per second
float32 upwardSpeed = 0; //per second
float32 maxSpeed = 60;
float32 forwardAccel = 0.1f; //per second
float32 backwardSidewayAccel = 0.04f; //per second
float32 upwardAccel = 0.04f; //per second
float32 rollSpeed = 0.1f;

float32 actualSpeed = 0;

const float32 heigth = 4;

std::chrono::system_clock::time_point lastTimeShot = std::chrono::system_clock::now() - std::chrono::hours(1);
Expand Down
16 changes: 8 additions & 8 deletions Game/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ void ConfigManager::init(std::string nConfigFileName)

std::string ConfigManager::readConfig(std::string key)
{
Logger::log("reading Configuration key: " + key);
Logger::info("reading Configuration key: " + key);
std::ifstream configFile;
configFile.open(configFileName);

if (!configFile) {
Logger::log("Cant open Configfile: " + configFileName);
Logger::error("Cant open Configfile: " + configFileName);
return "";
}

Expand Down Expand Up @@ -93,13 +93,13 @@ std::string ConfigManager::readConfig(std::string key)

}

Logger::log("While reading config did not find this key: " + key);
Logger::warn("While reading config did not find this key: " + key);
return "";
}

void ConfigManager::readAllConfigs()
{
Logger::log("Reading Configuration");
Logger::info("Reading Configuration");
std::string config = "";

//[General]
Expand Down Expand Up @@ -194,12 +194,12 @@ void ConfigManager::readAllConfigs()

void ConfigManager::writeConfig(std::string key, std::string value)
{
Logger::log("Writing Configuration key: " + key + " value: " + value);
Logger::info("Writing Configuration key: " + key + " value: " + value);
std::ifstream filein(configFileName); //File to read from
std::ofstream fileout("_" + configFileName); //Temporary file
if (!filein || !fileout)
{
Logger::log("Error opening configfiles for saving!");
Logger::error("Error opening configfiles for saving!");
return;
}

Expand Down Expand Up @@ -256,7 +256,7 @@ void ConfigManager::writeConfig(std::string key, std::string value)
{
lineout = key + " = " + value;
fileout << '\n' << lineout;
Logger::log("While writing did not find this key: " + key + ". Added it at the end");
Logger::info("While writing did not find this key: " + key + ". Added it at the end");
}

filein.close();
Expand All @@ -268,7 +268,7 @@ void ConfigManager::writeConfig(std::string key, std::string value)

void ConfigManager::writeAllConfigs()
{
Logger::log("Writing Configuration");
Logger::info("Writing Configuration");
//[General]
ConfigManager::writeConfig("player_name", ConfigManager::player_name);
ConfigManager::writeConfig("level", ConfigManager::level);
Expand Down
32 changes: 30 additions & 2 deletions Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,34 @@ void Game::processInput()
menu_current->rightOnSelectedMenuElement();
}
}
if (isKeyPressed(PlayerAction::moveUp))
{
if (gameState == GameState::GAME_ACTIVE || gameState == GameState::GAME_GAME_OVER)
{
if (players.size() > 0) {
players[0]->moveUp();
AudioManager::updateAudioListener();
}
}
else if (gameState == GameState::GAME_MENU)
{
menu_current->rightOnSelectedMenuElement();
}
}
if (isKeyPressed(PlayerAction::moveDown))
{
if (gameState == GameState::GAME_ACTIVE || gameState == GameState::GAME_GAME_OVER)
{
if (players.size() > 0) {
players[0]->moveDown();
AudioManager::updateAudioListener();
}
}
else if (gameState == GameState::GAME_MENU)
{
menu_current->rightOnSelectedMenuElement();
}
}
if (isKeyPressed(PlayerAction::rollLeft))
{
if (gameState == GameState::GAME_ACTIVE || gameState == GameState::GAME_GAME_OVER)
Expand Down Expand Up @@ -552,7 +580,7 @@ void Game::keyPressed(SDL_Keycode key)
if (it != keybindings.end()) {
action = it->second;
}
else { Logger::log("Keybinding not found!"); /*return;*/ }
else { Logger::warn("Keybinding not found!"); /*return;*/ }

//Single Action Keys, just one time per pressing
if (!Game::isKeyPressed(key))
Expand Down Expand Up @@ -668,7 +696,7 @@ void Game::keyReleased(SDL_Keycode key)
if (it != keybindings.end()) {
action = it->second;
}
if (action == PlayerAction::None) { Logger::log("Keybinding not found!"); /*return;*/ }
if (action == PlayerAction::None) { Logger::warn("Keybinding not found!"); /*return;*/ }

Game::setKeyPressed(key, false);
}
Expand Down
9 changes: 7 additions & 2 deletions Game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ enum class PlayerAction {
moveBackward,
moveLeft,
moveRight,
moveUp,
moveDown,
rollLeft,
rollRight,
jump,
Expand All @@ -130,11 +132,13 @@ std::unordered_map<SDL_Keycode, PlayerAction> const keybindings =
{SDLK_s, PlayerAction::moveBackward},
{SDLK_a, PlayerAction::moveLeft},
{SDLK_d, PlayerAction::moveRight},
{SDLK_SPACE, PlayerAction::moveUp},
{SDLK_LCTRL, PlayerAction::moveDown},
{SDLK_q, PlayerAction::rollLeft},
{SDLK_e, PlayerAction::rollRight},
{SDLK_SPACE, PlayerAction::jump},
{SDLK_x, PlayerAction::jump},
{SDLK_LSHIFT, PlayerAction::sprint},
{SDLK_LCTRL, PlayerAction::crouch},
{SDLK_c, PlayerAction::crouch},
{SDLK_f, PlayerAction::interact},
{SDLK_l, PlayerAction::toggleFlashlight},
{SDLK_F3, PlayerAction::toggleInfo},
Expand Down Expand Up @@ -313,6 +317,7 @@ static class Game
static UI_Element_Label* lbl_stopwatch1, * lbl_stopwatch2, * lbl_stopwatch3, * lbl_stopwatch4, * lbl_stopwatch5, * lbl_stopwatch6;
static UI_Element_Label* lbl_ObjectCount;


static StopWatch gameStopWatch;
static StopWatch frametimeStopWatch;
};
1 change: 1 addition & 0 deletions Game/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>
#include <algorithm>
#include <ios>
#include <sstream>

#include "libs/glm/glm.hpp"

Expand Down
Loading

0 comments on commit 82805ec

Please sign in to comment.