Game logic done

This commit is contained in:
Your Name 2018-12-17 18:43:06 +01:00
parent b9360cee57
commit 4b5662791f
3 changed files with 90 additions and 1 deletions

View File

@ -48,7 +48,8 @@ set(SRCS
"src/Main.cpp"
"src/Model.cpp"
"src/Shader.cpp"
"src/System.cpp")
"src/System.cpp"
"src/GameLogic.cpp")
# Define C++ compiler flags
set(CMAKE_CXX_FLAGS "-std=c++14 -Wall -Wextra -Wpedantic -Wfatal-errors -Werror -pedantic-errors -fno-elide-constructors")

View File

@ -77,6 +77,39 @@ System::System() : running(true) {
asset_mngr.loadModel(
"../assets/models/sphere.obj",
"sphere");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
board[i][j]=0;
}
}
xturn=true;
tempx=1;
tempy=1;
}
int System::CheckWin(){
for(int i=0;i<3;i++){
if(board[0][i]==board[1][i] && board[0][i]==board[2][i]){
if(board[0][i]!=0){
return board[0][i];
}
}
}
for(int i=0;i<3;i++){
if(board[i][0]==board[i][1] && board[i][0]==board[i][2]){
if(board[i][0]!=0){
return board[i][0];
}
}
}
if(board[1][1]!=0){
if(board[0][0]==board[1][1] && board[1][1]==board[2][2]){
return board[1][1];
}
if(board[0][2]==board[1][1] && board[1][1]==board[2][0]){
return board[1][1];
}
}
return 0;
}
System::~System() {
@ -118,6 +151,15 @@ void System::run() {
asset_mngr.getModel("board")->setColor(1,0,0);
asset_mngr.getModel("board")->draw(shaders.at("default"));
asset_mngr.getModel("cube")->setColor(0,1,0);
asset_mngr.getModel("cube")->draw(shaders.at("default"));
for(unsigned int i = 0; i < 3; ++i)
{
for(unsigned int j = 0; j < 3; ++j)
{
}
}
SDL_GL_SwapWindow(window);
if(SDL_GetTicks() - lastTime < 1000 / FPS)
@ -129,6 +171,41 @@ void System::run() {
}
}
void System::handleKey(SDL_Keysym key){
switch(key.sym)
{
case SDLK_UP:
tempy = (tempy + 1) % 3;
break;
case SDLK_DOWN:
tempy = (tempy - 1) % 3;
break;
case SDLK_RIGHT:
tempx = (tempx + 1) % 3;
break;
case SDLK_LEFT:
tempx = (tempx - 1) % 3;
break;
case SDLK_SPACE:
if(board[tempx][tempy]!=0){
break;
}
board[tempx][tempy]=xturn?1:2;
tempx=1;
tempy=1;
xturn=!xturn;
int winner=CheckWin();
if(winner==1){
logger->write("Player 1 wins!");
running=false;
}
if(winner==2){
logger->write("Player 2 wins");
running=false;
}
break;
}
}
void System::syncInputs() {
SDL_Event e;
while(SDL_PollEvent(&e))
@ -138,6 +215,9 @@ void System::syncInputs() {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
handleKey(e.key.keysym);
break;
}
}
}

View File

@ -27,6 +27,7 @@
#include <GL/glew.h>
#include <GL/gl.h>
#include "Camera.hpp"
#include "GameLogic.hpp"
class System {
public:
System();
@ -37,10 +38,17 @@ public:
private:
void syncInputs();
int board[3][3];
bool xturn;
int tempx;
int tempy;
AssetManager asset_mngr;
SDL_Window *window;
SDL_GLContext glcontext;
std::map<std::string, Shader> shaders;
Camera cam;
bool running;
GameLogic board_ctrl;
int CheckWin();
void handleKey(SDL_Keysym key);
};