Commit e06974ab authored by Håkon Berg Borhaug's avatar Håkon Berg Borhaug
Browse files

Added highscore feature

parent 23f01e7c
......@@ -69,6 +69,7 @@ void keyDown(unsigned char key, int x, int y)
case 'q':
case 27:
glutDestroyWindow(window);
exit(0);
#ifndef _WIN32
// Must use this with regular glut, since it never returns control to main().
exit(0);
......
......@@ -39,11 +39,14 @@ class GameManager : public SceneObject
std::vector<std::shared_ptr<Obstacle>> obstacles_;
std::shared_ptr<Camera> cam_;
std::shared_ptr<Snow> snow_;
std::shared_ptr<Text> text_;
std::shared_ptr<Text> scoreText_;
std::shared_ptr<Text> highscoreText_;
std::shared_ptr<Minimap> minimap_;
void checkCollisions();
void handleCollision(std::shared_ptr<Obstacle> obs);
void resetCharacterAfterCollision();
int highscore_ = 0;
};
......@@ -10,7 +10,7 @@
class Text : public SceneObject
{
public:
Text(std::string string);
Text(std::string string, float x, float y, float z, bool constant = false, unsigned int score = 0);
~Text();
unsigned int getScore() { return score_; }
......@@ -24,7 +24,12 @@ class Text : public SceneObject
private:
float pos_x;
float pos_y;
float pos_z;
std::string string_;
unsigned int stringLength_;
bool constant_;
unsigned int score_ = 0;
};
......
5125
\ No newline at end of file
......@@ -26,11 +26,13 @@ void Character::privateInit()
{
setUpVertices();
createNormalLineList();
// setUpNormals();
setUpLighting();
// createNormalLineList();
//setUpTextures();
// setUpLighting();
// setUpTextures();
// setUpHealthbar();
......@@ -54,10 +56,10 @@ void Character::privateInit()
void Character::privateRender()
{
// Enable stuff
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
//glEnable(GL_LIGHT1);
//glEnable(GL_COLOR_MATERIAL);
// glEnable(GL_LIGHTING);
// glEnable(GL_LIGHT0);
// glEnable(GL_LIGHT1);
// glEnable(GL_COLOR_MATERIAL);
// Draw cube
renderCube();
......@@ -69,10 +71,10 @@ void Character::privateRender()
// renderHealthbar();
// Disable stuff
//glDisable(GL_LIGHT1);
//glDisable(GL_LIGHTING);
//glDisable(GL_LIGHT0);
//glDisable(GL_COLOR_MATERIAL);
// glDisable(GL_LIGHT1);
// glDisable(GL_LIGHTING);
// glDisable(GL_LIGHT0);
// glDisable(GL_COLOR_MATERIAL);
// glDisable(GL_FOG);
GLenum err = glGetError();
......@@ -293,6 +295,14 @@ void Character::createNormalLineList()
vertexArray_[i].y + sharedNormalArray_[i].y * 10,
vertexArray_[i].z + sharedNormalArray_[i].z * 10);
}
// for (size_t i = 0; i < vertexArray_.size() && i < normalArray_.size(); i++) {
// glVertex3f(vertexArray_[i].x,
// vertexArray_[i].y,
// vertexArray_[i].z);
// glVertex3f(vertexArray_[i].x + normalArray_[i].x * 10,
// vertexArray_[i].y + normalArray_[i].y * 10,
// vertexArray_[i].z + normalArray_[i].z * 10);
// }
glEnd();
glEndList();
......@@ -387,7 +397,7 @@ void Character::moveLeft()
void Character::moveRight()
{
if (position_.z < 180.0f) {
if (position_.z < 230.0f) {
auto const trans = glm::vec3(0.0f, 0.0f, 5.0f);
translation_ = glm::translate(translation_, trans);
matrix_ = translation_ * rotation_ * scaling_;
......
#include "../include/GameManager.hpp"
#include <math.h>
#include <iostream>
#include <fstream>
GameManager::GameManager()
{
......@@ -72,13 +74,26 @@ void GameManager::privateInit()
snow_.reset(new Snow());
this->addSubObject(snow_);
// Minimap
minimap_.reset(new Minimap());
this->addSubObject(minimap_);
// // Minimap
// minimap_.reset(new Minimap());
// this->addSubObject(minimap_);
// Text
text_.reset(new Text("Score: "));
this->addSubObject(text_);
scoreText_.reset(new Text("Score: ", -0.95f, - 0.9f, 0.0f));
this->addSubObject(scoreText_);
std::ifstream infile("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/data/highscore.dat");
if (infile){
infile >> highscore_;
}
else {
std::cout << "COULD NOT OPEN HIGHSCORE FILE!" << std::endl;
}
infile.close();
highscoreText_.reset(new Text("Highscore: ", -0.95f, - 0.8f, 0.0f, true, highscore_));
this->addSubObject(highscoreText_);
}
void GameManager::privateRender()
......@@ -101,8 +116,6 @@ void GameManager::checkCollisions()
const auto obsSize = obs->getSize();
const auto charPos = character_->getPosition();
const auto charSize = character_->getSize();
// std::cout << "OBSTACLE: " << obsPos.x << " " << obsPos.y << std::endl;
// std::cout << "CHARACTER: " << charPos.x << " " << charPos.y << std::endl;
const bool collisionX = charPos.x - charSize <= obsPos.x + obsSize && charPos.x + charSize >= obsPos.x - obsSize;
const bool collisionY = charPos.y - charSize <= obsPos.y + obsSize && charPos.y + charSize >= obsPos.y - obsSize;
......@@ -142,6 +155,20 @@ void GameManager::resetCharacterAfterCollision()
character_->setColor(1.0f, 0.0f, 0.0f);
}
else {
unsigned int score = scoreText_->getScore();
if (score > highscore_) {
std::ofstream outfile("C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/data/highscore.dat", std::ios::trunc);
if (outfile){
outfile << score;
}
else {
std::cout << "COULD NOT OPEN HIGHSCORE FILE!" << std::endl;
}
outfile.close();
}
exit(0);
}
......
......@@ -25,7 +25,7 @@ void Snow::privateInit() {
particles[loop].yi=float((rand()%50)-25.0f)*10.0f; // Random Speed On Y Axis
particles[loop].zi=float((rand()%50)-25.0f)*10.0f; // Random Speed On Z Axis
particles[loop].xg=0.0f; // Set Horizontal Pull To Zero
particles[loop].yg=-2.8f; // Set Vertical Pull Downward
particles[loop].yg=-3.8f; // Set Vertical Pull Downward
particles[loop].zg=0.0f; // Set Pull On Z Axis To Zero
}
......@@ -100,7 +100,7 @@ void Snow::setUpTexture() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int width, height, nrChannels;
std::string filepath = "C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/textures/snowflake.jpg";
std::string filepath = "C:/dev/uni/DTE-3609_VR_graphics_animation/start_code/resources/textures/snow.png";
unsigned char *data = stbi_load(filepath.c_str(), &width, &height, &nrChannels, 0);
if (data)
{
......
#include "../include/Text.hpp"
Text::Text(std::string string)
:string_(string)
Text::Text(std::string string, float x, float y, float z, bool constant, unsigned int score)
:pos_x(x), pos_y(y), pos_z(z), string_(string), stringLength_(string.length()), constant_(constant), score_(score)
{
}
......@@ -19,7 +19,6 @@ void Text::privateRender() {
glEnable(GL_COLOR_MATERIAL);
glColor3f(1.0f, 1.0f, 1.0f);
// glRasterPos3f(-100.0f, 200.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
......@@ -27,7 +26,7 @@ void Text::privateRender() {
glPushMatrix();
glLoadIdentity();
glRasterPos3f(-0.9f, -0.9f, 0.0f);
glRasterPos3f(pos_x, pos_y, pos_z);
glLineWidth(1.0f);
......@@ -46,7 +45,9 @@ void Text::privateRender() {
}
void Text::privateUpdate() {
score_++;
string_.resize(7);
string_ += std::to_string(score_);
if (!constant_) {
score_++;
string_.resize(stringLength_);
string_ += std::to_string(score_);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment