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

Functioning game :)

parent 88069bd0
......@@ -54,8 +54,8 @@ void display()
if(keyPressed[KEY_ID_C]==true) gm->getCam()->moveDown();
if(keyPressed[KEY_ID_H]==true) gm->getCam()->rotateLeft();
if(keyPressed[KEY_ID_J]==true) gm->getCam()->rotateRight();
if(keyPressed[KEY_ID_O]==true) gm->getCharacter()->increaseSpeed();
if(keyPressed[KEY_ID_P]==true) gm->getCharacter()->decreaseSpeed();
if(keyPressed[KEY_ID_O]==true) gm->getCharacter()->moveLeft();
if(keyPressed[KEY_ID_P]==true) gm->getCharacter()->moveRight();
glutSwapBuffers();
glutPostRedisplay();
......
......@@ -15,15 +15,17 @@ class Character : public SceneObject
public:
Character(glm::vec3 trans);
~Character();
void increaseSpeed();
void decreaseSpeed();
void moveLeft();
void moveRight();
glm::vec3 getPosition() { return position_; }
float getSize() { return size_; }
float getState() { return state_; }
int getState() { return state_; }
float getCurrentLife() { return life_; }
void setColor(float r, float g, float b) { color_[0] = r, color_[1] = g, color_[2] = b; }
void setState(int newState) { state_ = newState; }
void reduceLife() { life_ -= 0.3f; }
protected:
void privateInit();
......@@ -54,19 +56,17 @@ class Character : public SceneObject
std::vector< glm::vec2 > healthbarTextureArray_;
unsigned int texture;
float color_[3] = {1.0f, 0.0f, 0.0f};
glm::vec3 position_;
float size_ = 50.0f;
float size_ = 30.0f;
int state_ = characterStates::normal;
int list_id_;
float speed_ = 0;
float life_;
float life_ = 1.0f;
float armor_;
float color_[3] = {0.0f, life_, 0.0f};
};
......@@ -2,6 +2,7 @@
#include <windows.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <iostream>
......
......@@ -27,7 +27,6 @@ public:
glm::vec3 getPosition() { return position_; }
float getSize() { return size_; }
protected:
virtual void privateInit();
virtual void privateRender();
......@@ -102,5 +101,6 @@ private:
float scaling;
glm::vec3 position_;
float speed_ = -3.0f;
};
......@@ -10,9 +10,13 @@
class Text : public SceneObject
{
public:
Text();
Text(std::string string);
~Text();
unsigned int getScore() { return score_; }
void setScore(unsigned int newScore) { score_ = newScore; }
protected:
void privateInit();
void privateRender();
......@@ -21,6 +25,7 @@ class Text : public SceneObject
private:
std::string string_;
unsigned int score_ = 0;
};
......@@ -38,5 +38,5 @@ void main() {
vec3 result = ambient + diffuse + specular;
color = mix(texColor, vec4(result, 1.0f), 0.9);
color = mix(texColor, vec4(result, 1.0f), 0.1);
};
// FRAGMENT SHADER
#shader fragment
#version 330 core
layout(location = 0) out vec4 color;
in vec2 v_TexCoord;
//uniform vec4 u_Color;
uniform sampler2D u_Texture;
void main() {
vec4 texColor = texture(u_Texture, v_TexCoord);
color = texColor;
};
// VERTEX SHADER
#shader vertex
#version 330 core
layout(location = 0) in vec2 position;
layout(location = 1) in vec2 textureCoord;
out vec2 v_TexCoord;
void main() {
gl_Position = vec4(position.x, position.y, 0.0f, 1.0f);
v_TexCoord = textureCoord;
};
......@@ -8,8 +8,8 @@
Camera::Camera()
{
auto translation = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -300.0f));
auto rotation = glm::rotate(glm::mat4(1.0f), glm::radians(0.0f), glm::vec3(0.0f,0.0f,1.0f));
auto translation = glm::translate(glm::mat4(1.0f), glm::vec3(-80.0f, -200.0f, -700.0f));
auto rotation = glm::rotate(glm::mat4(1.0f), glm::radians(45.0f), glm::vec3(0.0f,1.0f,0.0f));
matrix_ = translation * rotation;
}
......
......@@ -32,7 +32,7 @@ void Character::privateInit()
//setUpTextures();
setUpHealthbar();
// setUpHealthbar();
GLenum err = glGetError();
if (err != GL_NO_ERROR)
......@@ -63,10 +63,10 @@ void Character::privateRender()
renderCube();
// Draw normal lines of cube
renderNormals();
// renderNormals();
// Draw healthbar
renderHealthbar();
// renderHealthbar();
// Disable stuff
//glDisable(GL_LIGHT1);
......@@ -375,9 +375,9 @@ void Character::privateUpdate()
}
void Character::increaseSpeed()
void Character::moveLeft()
{
if (position_.z > -150.0f) {
if (position_.z > -180.0f) {
auto const trans = glm::vec3(0.0f, 0.0f, -5.0f);
translation_ = glm::translate(translation_, trans);
matrix_ = translation_ * rotation_ * scaling_;
......@@ -385,9 +385,9 @@ void Character::increaseSpeed()
}
}
void Character::decreaseSpeed()
void Character::moveRight()
{
if (position_.z < 150.0f) {
if (position_.z < 180.0f) {
auto const trans = glm::vec3(0.0f, 0.0f, 5.0f);
translation_ = glm::translate(translation_, trans);
matrix_ = translation_ * rotation_ * scaling_;
......
......@@ -28,7 +28,7 @@ void GameManager::privateInit()
cam_.reset(new Camera());
// Landscape
const auto landscapeSegments = 5;
const auto landscapeSegments = 12;
const auto landscapeSize = 200.0f;
const auto landscapeScaling = 3.0f;
const auto landscapeRealSize = landscapeSize * landscapeScaling;
......@@ -50,19 +50,21 @@ void GameManager::privateInit()
this->addSubObject(sb_);
// Character
character_.reset(new Character(glm::vec3(0.0f, 30.0f, 0.0f)));
character_.reset(new Character(glm::vec3(0.0f, 10.0f, 0.0f)));
this->addSubObject(character_);
// Obstacles
const auto obstacleNumber = 1;
const auto obstacleNumber = 6;
const auto obstacleStartPosX = 1000.0f;
const auto obstacleSpacing = 200.0f;
srand(time(0));
for (unsigned int i = 0; i < obstacleNumber; i++) {
std::shared_ptr<Obstacle> obstacle;
obstacle.reset(new Obstacle(glm::vec3(400 + landscapeX * i, 260 + landscapeY * i, 0.0f), landscapeRotationAxis,
landscapeRotationDegree));
obstacle.reset(new Obstacle(glm::vec3(obstacleStartPosX + obstacleSpacing * i + landscapeX * i, tan(glm::radians(landscapeRotationDegree)) * (obstacleStartPosX + obstacleSpacing * i) + 10.0f + landscapeY * i, - 150.0f + (i % 3) * 150.0f),
landscapeRotationAxis, landscapeRotationDegree));
this->addSubObject(obstacle);
obstacles_.push_back(obstacle);
}
......@@ -70,14 +72,13 @@ void GameManager::privateInit()
snow_.reset(new Snow());
this->addSubObject(snow_);
// Text
text_.reset(new Text());
this->addSubObject(text_);
// Minimap
minimap_.reset(new Minimap());
this->addSubObject(minimap_);
// Text
text_.reset(new Text("Score: "));
this->addSubObject(text_);
}
void GameManager::privateRender()
......@@ -124,12 +125,27 @@ void GameManager::handleCollision(std::shared_ptr<Obstacle> obs)
{
character_->setColor(1.0f, 1.0f, 1.0f);
character_->setState(characterStates::colliding);
character_->reduceLife();
}
void GameManager::resetCharacterAfterCollision()
{
character_->setColor(1.0f, 0.0f, 0.0f);
character_->setState(characterStates::normal);
auto life = character_->getCurrentLife();
if (life > 0.5f) {
character_->setColor(0.0f, 1.0f, 0.0f);
}
else if (life > 0.25f) {
character_->setColor(1.0f, 1.0f, 0.0f);
}
else if (life > 0.0f){
character_->setColor(1.0f, 0.0f, 0.0f);
}
else {
exit(0);
}
character_->setState(characterStates::normal);
}
......
......@@ -29,18 +29,16 @@ void Minimap::privateInit() {
}
void Minimap::privateRender() {
// glMatrixMode(GL_MODELVIEW);
// render to texture
// render to texture
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_fbo);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glLoadIdentity();
// glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// place camera/eye
// glTranslatef(0.0f, 1000.0f, 100.0f);
// glRotatef(0.0f,1.0,0.0,0.0);
// glRotatef(-90.0f,0.0,1.0,0.0);
// place camera/eye
glTranslatef(0.0f, 1000.0f, 100.0f);
glRotatef(0.0f,1.0,0.0,0.0);
glRotatef(-180.0f,0.0,1.0,0.0);
// Save State
float fPrevColor[4];
......@@ -65,16 +63,29 @@ void Minimap::privateRender() {
// glLoadIdentity();
// glTranslatef(0.0f, 0.0f, -2.0f);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-100.0f, -50.0f, 100.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-50.0f, -50.0f, 100.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-50.0f, 0.0f, 100.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-100.0f, 0.0f, 100.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -0.8f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f, -0.8f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f, -0.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -0.5f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
// glutSwapBuffers();
}
......
......@@ -73,21 +73,23 @@ void Obstacle::privateRender()
void Obstacle::privateUpdate()
{
glm::vec3 trans;
float speed = -3.0f;
float resetDistance = 2000.0f;
float resetDistance = 4000.0f;
if (position_.x < - 1.5 * 200 * 3) {
trans = glm::vec3(resetDistance, tan(glm::radians(30.0f)) * resetDistance, 0.0f);
trans = glm::vec3(resetDistance, tan(glm::radians(30.0f)) * resetDistance, -150.0f + float(rand() % 300));
translation_ = glm::translate(glm::mat4(1.0f), trans);
position_ = trans;
}
else {
trans = glm::vec3(speed, tan(glm::radians(30.0f)) * speed, 0.0f);
trans = glm::vec3(speed_, tan(glm::radians(30.0f)) * speed_, 0.0f);
translation_ = glm::translate(translation_, trans);
position_ += trans;
}
rotation_ = glm::rotate(rotation_, 0.05f, glm::vec3(0.0f, 0.0f, 1.0f));
translation_ = glm::translate(translation_, trans);
rotation_ = glm::rotate(rotation_, 0.05f - speed_ / 1000, glm::vec3(0.0f, 0.0f, 1.0f));
matrix_ = translation_ * rotation_ * scaling_;
position_ += trans;
speed_ -= 0.001f;
}
......@@ -141,7 +141,7 @@ void Plane::privateRender()
void Plane::privateUpdate()
{
if (matrix_[3].x < - 1.5 * 200 * scaling) {
matrix_ = glm::translate(matrix_, glm::vec3(1.5 * 200 * scaling - 2, 0.0f, 0.0f));
matrix_ = glm::translate(matrix_, glm::vec3(3.75f * 200 * scaling - 2, 0.0f, 0.0f));
}
else {
matrix_ = glm::translate(matrix_, glm::vec3(-1.0f, 0.0f, 0.0f));
......
#include "../include/Text.hpp"
Text::Text() {
Text::Text(std::string string)
:string_(string)
{
}
......@@ -8,26 +10,43 @@ Text::~Text() {
}
void Text::privateInit() {
string_ = "This is a test";
void Text::privateInit()
{
string_ += std::to_string(score_);
}
void Text::privateRender() {
glEnable(GL_COLOR_MATERIAL);
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos3f(-100.0f, 200.0f, 0.0f);
glLineWidth(100.0f);
// glRasterPos3f(-100.0f, 200.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRasterPos3f(-0.9f, -0.9f, 0.0f);
glLineWidth(1.0f);
for (char& c : string_) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glLineWidth(1.0f);
glDisable(GL_COLOR_MATERIAL);
}
void Text::privateUpdate() {
score_++;
string_.resize(7);
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