diff --git a/Game/Bullet.cpp b/Game/Bullet.cpp index 050f8b3..7649a0f 100644 --- a/Game/Bullet.cpp +++ b/Game/Bullet.cpp @@ -1,7 +1,7 @@ #include "Bullet.h" #include "Game.h" -Bullet::Bullet(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 direction) : Object(shader, "arrow.bmf") +Bullet::Bullet(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 direction) : Object(shader, "laser.bmf") { this->setType(ObjectType::Object_Bullet); this->position = position; @@ -11,7 +11,17 @@ Bullet::Bullet(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 this->setCollisionBoxType(CollisionBoxType::cube); } -Bullet::Bullet(Shader* shader) : Object(shader, "arrow.bmf") +Bullet::Bullet(Shader* shader, glm::vec3 position, glm::quat rotation, glm::vec3 direction) : Object(shader, "laser.bmf") +{ + this->setType(ObjectType::Object_Bullet); + this->position = position; + this->setRotationQuat(rotation); + this->movement = glm::normalize(direction) * speed;// *Game::getDelta(); //todo : apply Game::getDelta() per frame + this->name = "Bullet"; + this->setCollisionBoxType(CollisionBoxType::cube); +} + +Bullet::Bullet(Shader* shader) : Object(shader, "laser.bmf") { this->setType(ObjectType::Object_Bullet); this->name = "Bullet"; @@ -23,7 +33,7 @@ void Bullet::fall() this->Object::fall(); if (movement == glm::vec3(0, 0, 0)) return; - if (position.y < 0.5) { movement=glm::vec3(0, 0, 0); return; } + //if (position.y < 0.5) { movement=glm::vec3(0, 0, 0); return; } float gegk = movement.y; diff --git a/Game/Bullet.h b/Game/Bullet.h index 35338c6..312ccb8 100644 --- a/Game/Bullet.h +++ b/Game/Bullet.h @@ -8,6 +8,8 @@ class Bullet : public Object public: Bullet(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 direction); + Bullet(Shader* shader, glm::vec3 position, glm::quat rotation, glm::vec3 direction); + Bullet(Shader* shader); //void move(); @@ -21,7 +23,7 @@ class Bullet : public Object private: - float speed = 70; + float speed = 200; }; diff --git a/Game/Character.cpp b/Game/Character.cpp index 7586eba..087e787 100644 --- a/Game/Character.cpp +++ b/Game/Character.cpp @@ -5,7 +5,7 @@ #include -Character::Character(Shader* shader) : Object(shader, "character.bmf") +Character::Character(Shader* shader) : Object(shader, "spaceship.bmf") { this->shader = shader; vecFront = glm::vec3(1.0f, 0.0f, 0.0f); @@ -57,43 +57,28 @@ void Character::interactWithObject() void Character::resetVerticalMovement() { - ////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; - } - } - }*/ + float brakevalue = 0.001f; + brakevalue *= glm::length(movement); + + //Logger::logVector(movement,"Move",2,"\033[34m"); + glm::vec3 moveVec = glm::normalize(movement); - actualSpeed = glm::length(movement); + actualSpeedAbs = glm::length(movement); + + if (glm::dot(movement, vecFront) > 0) { + actualSpeed = actualSpeedAbs; + } + else { + actualSpeed = -actualSpeedAbs; + } - if (actualSpeed > 0) { + if (actualSpeedAbs > brakevalue) { movement -= moveVec * brakevalue; } + else if (actualSpeedAbs > 0) { + movement = glm::vec3(0); + } } void Character::moveForward() { @@ -176,6 +161,30 @@ void Character::rollRight() } +void Character::brake() +{ + float brakevalue = backwardSidewayAccel; + + //Logger::logVector(movement,"Move",2,"\033[34m"); + + glm::vec3 moveVec = glm::normalize(movement); + actualSpeedAbs = glm::length(movement); + + if (glm::dot(movement, vecFront) > 0) { + actualSpeed = actualSpeedAbs; + } + else { + actualSpeed = -actualSpeedAbs; + } + + if (actualSpeedAbs > brakevalue) { + movement -= moveVec * brakevalue; + } + else if (actualSpeedAbs > 0) { + movement = glm::vec3(0); + } +} + void Character::jump() { if (canJump) @@ -251,6 +260,8 @@ std::shared_ptr Character::shoot() { float maxScatteringAngle = 1; + //Logger::info2("shoot"); + float scatteringAngleX = std::rand() * maxScatteringAngle / RAND_MAX; scatteringAngleX -= maxScatteringAngle / 2; @@ -259,12 +270,15 @@ std::shared_ptr Character::shoot() std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::chrono::duration notshotDuration = now - lastTimeShot; - if (notshotDuration.count() > 1000) + if (notshotDuration.count() > shootInterval) { - glm::vec3 bulletCreationPosition = position + glm::vec3(0, 2, 0) +glm::vec3(1, 1, 1) * getVecFront(); - glm::vec3 bulletCreationRotation = glm::vec3(0, rotation.y - 90 + scatteringAngleY, -rotation.x - 90 + scatteringAngleX); - glm::vec3 bulletCreationDirection = getVecFront(); + glm::vec3 bulletCreationPosition = position + glm::vec3(7) * vecFront; + //glm::vec3 bulletCreationRotation = glm::vec3(0, rotation.y - 90 + scatteringAngleY, -rotation.x - 90 + scatteringAngleX); + glm::vec3 bulletCreationDirection = vecFront; + glm::quat bulletRotationquat = this->getRotationQuat(); + + //rotate direction around Y bulletCreationDirection = glm::rotate(bulletCreationDirection, glm::radians(scatteringAngleY), glm::vec3(0, 1, 0)); @@ -272,7 +286,7 @@ std::shared_ptr Character::shoot() bulletCreationDirection = glm::rotate(bulletCreationDirection, glm::radians(scatteringAngleX), glm::vec3(0, 0, 1)); - std::shared_ptr newBullet = std::make_shared(shader, bulletCreationPosition, bulletCreationRotation, bulletCreationDirection); + std::shared_ptr newBullet = std::make_shared(shader, bulletCreationPosition, bulletRotationquat, bulletCreationDirection); newBullet->setNumber(Game::objects.size()); Game::bullets.push_back(newBullet); diff --git a/Game/Character.h b/Game/Character.h index 09db5eb..33b0d44 100644 --- a/Game/Character.h +++ b/Game/Character.h @@ -42,6 +42,8 @@ class Character : public Object void rollRight(); + void brake(); + void jump(); void activateJumping(); @@ -88,17 +90,18 @@ class Character : public Object float32 maxSpeed = 60; - float32 forwardAccel = 0.1f; //per second - float32 backwardSidewayAccel = 0.04f; //per second + float32 forwardAccel = 0.05f; //per second + float32 backwardSidewayAccel = 0.03f; //per second float32 upwardAccel = 0.04f; //per second float32 rollSpeed = 0.1f; float32 actualSpeed = 0; + float32 actualSpeedAbs = 0; const float32 heigth = 4; std::chrono::system_clock::time_point lastTimeShot = std::chrono::system_clock::now() - std::chrono::hours(1); - + float32 shootInterval = 100; int team = 0; diff --git a/Game/Game.cpp b/Game/Game.cpp index 16ac3ba..a5cf83f 100644 --- a/Game/Game.cpp +++ b/Game/Game.cpp @@ -489,7 +489,6 @@ void Game::processInput() AudioManager::updateAudioListener(); } } - } if (isKeyPressed(PlayerAction::rollRight)) { @@ -500,7 +499,15 @@ void Game::processInput() AudioManager::updateAudioListener(); } } - + } + if (isKeyPressed(PlayerAction::brake)) + { + if (gameState == GameState::GAME_ACTIVE || gameState == GameState::GAME_GAME_OVER) + { + if (players.size() > 0) { + players[0]->brake(); + } + } } if (isKeyPressed(PlayerAction::jump)) { diff --git a/Game/Game.h b/Game/Game.h index b777840..95c03b8 100644 --- a/Game/Game.h +++ b/Game/Game.h @@ -108,6 +108,7 @@ enum class PlayerAction { moveDown, rollLeft, rollRight, + brake, jump, sprint, crouch, @@ -136,7 +137,8 @@ std::unordered_map const keybindings = {SDLK_LCTRL, PlayerAction::moveDown}, {SDLK_q, PlayerAction::rollLeft}, {SDLK_e, PlayerAction::rollRight}, - {SDLK_x, PlayerAction::jump}, + {SDLK_x, PlayerAction::brake}, + {SDLK_y, PlayerAction::jump}, {SDLK_LSHIFT, PlayerAction::sprint}, {SDLK_c, PlayerAction::crouch}, {SDLK_f, PlayerAction::interact}, diff --git a/Game/Logger.cpp b/Game/Logger.cpp index f26fe4a..f84dc58 100644 --- a/Game/Logger.cpp +++ b/Game/Logger.cpp @@ -6,6 +6,11 @@ void Logger::info(std::string message) std::cout << getTime() << message << std::endl; } +void Logger::info2(std::string message) +{ + std::cout << "\033[34m" << getTime() << message << "\033[0m" << std::endl; +} + void Logger::warn(std::string message) { std::cout << "\033[33m" << getTime() << message << "\033[0m" << std::endl; diff --git a/Game/Logger.h b/Game/Logger.h index 68fce79..dc59569 100644 --- a/Game/Logger.h +++ b/Game/Logger.h @@ -15,6 +15,8 @@ static class Logger static void info(std::string message); + static void info2(std::string message); + static void warn(std::string message); static void error(std::string message); diff --git a/Game/NPC.cpp b/Game/NPC.cpp index a2caef9..843b8c6 100644 --- a/Game/NPC.cpp +++ b/Game/NPC.cpp @@ -65,10 +65,18 @@ void NPC::followNavPoints() vecFront = targetPosition - myPosition; vecFront = glm::normalize(vecFront); + vecUp = glm::vec3(1, 0, 0); + + + float pitch = glm::degrees(asin(-vecFront.y)); float yaw = glm::degrees(atan2(vecFront.x, vecFront.z)); setRotation(glm::vec3(0, yaw, 0)); + glm::quat newRotQuat = glm::quat(glm::vec3(pitch,yaw,0)); + + setRotationQuat(newRotQuat); + float distance = glm::length(targetPosition - myPosition); if (distance < 0.5) diff --git a/Game/Object.h b/Game/Object.h index 95c107d..7be6ec9 100644 --- a/Game/Object.h +++ b/Game/Object.h @@ -145,9 +145,9 @@ class Object virtual void reactToCollision(CollisionResult collisionResult); - void calculationBeforeFrame(); + virtual void calculationBeforeFrame(); - void calculationAfterFrame(); + virtual void calculationAfterFrame(); glm::vec3 calculateDimensions(); diff --git a/Game/Player.cpp b/Game/Player.cpp index 474b5e4..4410a9b 100644 --- a/Game/Player.cpp +++ b/Game/Player.cpp @@ -24,17 +24,30 @@ Player::Player(Shader* shader, float fov, float width, float height) : Character } +void Player::calculationBeforeFrame() +{ + this->Character::calculationBeforeFrame(); +} + +void Player::calculationAfterFrame() +{ + this->Character::calculationAfterFrame(); + + MouseMoved = false; +} + void Player::onMouseMove(float xRel, float yRel) { + MouseMoved = true; //Logger::log("vecRight: ("+std::to_string(vecRight.x)+"|" + std::to_string(vecRight.y) + "|" + std::to_string(vecRight.z) + ")"); //Logger::log("quat: (" + std::to_string(rotationQuat.x) + "|" + std::to_string(rotationQuat.y) + "|" + std::to_string(rotationQuat.z) + "|" + std::to_string(rotationQuat.w) + ")"); - float maxPitch=0.003f; - float maxYaw = 0.003f; + maxPitch=0.003f; + maxYaw = 0.003f; - float anglePitch = glm::radians(yRel * mouseSensitivity); - float angleYaw = glm::radians(-xRel * mouseSensitivity); + float32 anglePitch = glm::radians(yRel * mouseSensitivity); + float32 angleYaw = glm::radians(-xRel * mouseSensitivity); anglePitch = std::min(anglePitch, maxPitch); angleYaw = std::min(angleYaw, maxYaw); anglePitch = std::max(anglePitch, -maxPitch); @@ -56,36 +69,44 @@ void Player::onMouseMove(float xRel, float yRel) setRotationQuat(rotationQuat); - + updateAngleAroundCharacter(-angleYaw * 2000, 10 + anglePitch * 2000); + //onMouseMoved(); //calculateFrontandUpVector(); - lookAt = vecFront; - updateCameraPosition(); + //lookAt = vecFront; + //updateCameraPosition(); + + } void Player::updateCameraPosition() { - float cameraspeed = 0.1; + if (!MouseMoved) { + updateAngleAroundCharacter(0, 10); + } - glm::vec3 cameratargetposition = position - getVecFront() * camera_distancetoPlayer; - cameratargetposition += getVecUp() * glm::vec3(2); + float horizDistance = cos(glm::radians(pitchAroundCharacter)) * camera_distancetoPlayer; + float verticDistance = sin(glm::radians(pitchAroundCharacter)) * camera_distancetoPlayer; - glm::vec3 cameramovedirection = cameratargetposition - cameraposition; - float cameratargetdistance = glm::length(cameramovedirection); - cameramovedirection = glm::normalize(cameramovedirection); + float offsetX = cos(glm::radians(yawAroundCharacter)) * horizDistance; + float offsetY = verticDistance; + float offsetZ = sin(glm::radians(yawAroundCharacter)) * horizDistance; + + cameraposition = position; + cameraposition += (offsetX+actualSpeed*0.01f) * -vecFront; + cameraposition += offsetY * vecUp; + cameraposition += offsetZ * vecRight; + + lookAt = glm::normalize((position + (vecFront * 400.0f))- cameraposition); - if (cameratargetdistance > cameraspeed) { - cameraposition += cameramovedirection * glm::vec3(cameraspeed); - } - else { - cameraposition = cameratargetposition; - } - lookAt = vecFront; update2(vecUp); + + //anglePitch = 0; + //angleYaw = 0; } void Player::resetCameraPosition() @@ -104,6 +125,29 @@ glm::vec3 Player::getCameraPosition() return cameraposition; } +void Player::updateAngleAroundCharacter(float targetYawAroundCharacter, float targetPitchAroundCharacter) +{ + if (yawAroundCharacter < targetYawAroundCharacter - cameraAroundCharacterSpeed) { + yawAroundCharacter += cameraAroundCharacterSpeed; + } + else if (yawAroundCharacter > targetYawAroundCharacter + cameraAroundCharacterSpeed) { + yawAroundCharacter -= cameraAroundCharacterSpeed; + } + else { + yawAroundCharacter = targetYawAroundCharacter; + } + + if (pitchAroundCharacter < targetPitchAroundCharacter - cameraAroundCharacterSpeed) { + pitchAroundCharacter += cameraAroundCharacterSpeed; + } + else if (pitchAroundCharacter > targetPitchAroundCharacter + cameraAroundCharacterSpeed) { + pitchAroundCharacter -= cameraAroundCharacterSpeed; + } + else { + pitchAroundCharacter = targetPitchAroundCharacter; + } +} + void Player::setCameraPosition(glm::vec3 newPosition) { cameraposition = newPosition; @@ -159,13 +203,13 @@ void Player::registerHit() void Player::rollLeft() { Character::rollLeft(); - updateCameraPosition(); + //updateCameraPosition(); } void Player::rollRight() { Character::rollRight(); - updateCameraPosition(); + //updateCameraPosition(); } void Player::reactToCollision(CollisionResult collisionResult) @@ -182,14 +226,14 @@ void Player::createUIElements() UI::addElement(prb_health); - prb_PlayerSpeed = new UI_Element_ProgressBar(120, 10, 100, 20, 0, 0); + prb_PlayerSpeed = new UI_Element_ProgressBar(125, 10, 100, 20, 0, 0); prb_PlayerSpeed->setForeColor(glm::vec4(0, 0.5, 1, 0.5)); prb_PlayerSpeed->setBackColor(glm::vec4(0.2, 0.2, 0.2, 0.4)); - prb_PlayerSpeed->setValue(actualSpeed); + prb_PlayerSpeed->setValue(actualSpeedAbs); prb_PlayerSpeed->setMaxValue(maxSpeed); UI::addElement(prb_PlayerSpeed); - lbl_PlayerSpeed = new UI_Element_Label(120, 40, 100, 50, "0.00", 0, 1, glm::vec4(0, 0.5, 1, 0.5), glm::vec4(0.2, 0.2, 0.2, 0.4)); + lbl_PlayerSpeed = new UI_Element_Label(125, 40, 100, 50, "0.00", 0, 1, glm::vec4(0, 0.5, 1, 0.5), glm::vec4(0.2, 0.2, 0.2, 0.4)); UI::addElement(lbl_PlayerSpeed); } @@ -232,7 +276,7 @@ void Player::move() { this->Object::move(); - prb_PlayerSpeed->setValue(actualSpeed); + prb_PlayerSpeed->setValue(actualSpeedAbs); lbl_PlayerSpeed->setText(Helper::to_string_with_precision(actualSpeed,2)); if (movement != glm::vec3(0, 0, 0)) diff --git a/Game/Player.h b/Game/Player.h index d177104..dce656c 100644 --- a/Game/Player.h +++ b/Game/Player.h @@ -22,6 +22,10 @@ class Player : public Character, public ThirdPersonCamera //virtual glm::vec3 getLookOrigin(); + void calculationBeforeFrame(); + + void calculationAfterFrame(); + void onMouseMove(float xRel, float yRel); void updateCameraPosition(); @@ -30,6 +34,8 @@ class Player : public Character, public ThirdPersonCamera glm::vec3 getCameraPosition(); + void updateAngleAroundCharacter(float targetYawAroundCharacter, float targetPitchAroundCharacter); + void setCameraPosition(glm::vec3 newPosition); void activateFlashlight(bool enabled); @@ -60,6 +66,15 @@ class Player : public Character, public ThirdPersonCamera UI_Element_Label* lbl_PlayerSpeed; glm::vec3 cameraOffset = glm::vec3(0, 0, 0); + + float32 maxPitch = 0.003f; + float32 maxYaw = 0.003f; + + float32 yawAroundCharacter = 0; + float32 pitchAroundCharacter = 10; + float32 cameraAroundCharacterSpeed = 0.02f; + + bool MouseMoved = false; diff --git a/Game/levels/level_test.xml b/Game/levels/level_test.xml index 6c0a393..a323e37 100644 --- a/Game/levels/level_test.xml +++ b/Game/levels/level_test.xml @@ -24,10 +24,12 @@ - spaceship.bmf - planet_sun.bmf - planet_earth.bmf - planet_moon.bmf + replacemeball.bmf + spaceship.bmf + planet_sun.bmf + planet_earth.bmf + planet_moon.bmf + laser.bmf @@ -72,5 +74,20 @@ + + Bot1 + Bot + 0 + spaceship.bmf + 10;0;10 + 0;0;0 + 1;1;1 + + 200;0;10 + 150;100;200 + 150;100;0 + 100;-100;-50 + + \ No newline at end of file diff --git a/Game/models/laser.bmf b/Game/models/laser.bmf new file mode 100644 index 0000000..4aacc09 Binary files /dev/null and b/Game/models/laser.bmf differ diff --git a/Game/models/replacemeball.bmf b/Game/models/replacemeball.bmf new file mode 100644 index 0000000..d2b44bc Binary files /dev/null and b/Game/models/replacemeball.bmf differ diff --git a/Game/textures/laser.png b/Game/textures/laser.png new file mode 100644 index 0000000..e4a585f Binary files /dev/null and b/Game/textures/laser.png differ diff --git a/Game/textures/laser_n.png b/Game/textures/laser_n.png new file mode 100644 index 0000000..ef4987d Binary files /dev/null and b/Game/textures/laser_n.png differ diff --git a/Game/textures/replaceme01.jpg b/Game/textures/replaceme01.jpg new file mode 100644 index 0000000..83d0a42 Binary files /dev/null and b/Game/textures/replaceme01.jpg differ diff --git a/Game/textures/replaceme01_n.jpg b/Game/textures/replaceme01_n.jpg new file mode 100644 index 0000000..c7fd4b2 Binary files /dev/null and b/Game/textures/replaceme01_n.jpg differ diff --git a/LICENSE b/LICENSE index 0160203..5872ff6 100644 --- a/LICENSE +++ b/LICENSE @@ -28,6 +28,8 @@ Copyright (c) 1994 Copyright (c) 2002-2020 Lee Salzman +Copyright (c) 2019 Solar System Scope + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal