-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cpp
More file actions
152 lines (140 loc) · 4.03 KB
/
controller.cpp
File metadata and controls
152 lines (140 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "controller.h"
#include <iostream>
#include "SDL.h"
void Controller::HandleInput(bool &running, GameLogic &gamelogic, Coz &coz) const
{
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
running = false;
}
else if (e.type==SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE:
//set ESC key to return main menu
coz.setMenu(Coz::MENU::MAIN);
break;
case SDLK_h:
coz.setMenu(Coz::MENU::HELP);
break;
default:
break;
}
}
else if (e.type == SDL_MOUSEBUTTONDOWN)
{
int x, y;
SDL_GetMouseState(&x, &y);
switch (coz.CurrentMenu())
{
case Coz::MENU::MAIN:
{ //check if start button is clicked
//in main screen if a click occurs in button rect
//button bounds x:100,y:300,w:280,h:110,
if (x > 100 && x < 380 && y > 110 && y < 410)
{
//button clicked, move to next menu
coz.setMenu(Coz::MENU::OPTIONS);
}
break;
}
case Coz::MENU::OPTIONS:
{ //three buttons here
//user will choose difficulty level
//options easy, moderate, hard
bool isClicked = false;
if (x > 100 && x < 380 && y > 240 && y < 300)
{
gamelogic.setLevel(GameLogic::GameLevel::EASY);
isClicked = true;
}
else if (x > 100 && x < 380 && y > 320 && y < 380)
{
gamelogic.setLevel(GameLogic::GameLevel::MODERATE);
isClicked = true;
}
else if (x > 100 && x < 380 && y > 400 && y < 460)
{
gamelogic.setLevel(GameLogic::GameLevel::HARD);
isClicked = true;
}
if (isClicked)
{
gamelogic.init();
coz.setMenu(Coz::MENU::GAME);
}
break;
}
case Coz::MENU::GAME:
{ //in game, two player will move sequently,
//in the beginning of the round players will place their pieces one by one
Point mousePos;
mousePos.x = x;
mousePos.y = y;
if (!gamelogic.allPiecesPlaced() && gamelogic.selectTarget(mousePos))
{
printf("Piece placed!\n");
}
else
{
//after placement is done, game starts
//in each move player turn will change
//first, check the piece coordinates if clicked
if (!gamelogic.PieceSelected())
{
if (gamelogic.selectPiece(mousePos))
{
//printf("%d\t\t%d\t\t%d\t\t%d\t\t%d \n", gamelogic.PieceSelected(), gamelogic.source.x, gamelogic.source.y, gamelogic.target.x, gamelogic.target.y);
}
}
}
break;
}
case Coz::MENU::HELP:
{
//nothing to control here, just to avoid compile warning
break;
}
case Coz::MENU::GAMEOVER:
{
//nothing to control here, just to avoid compile warning
break;
}
}
}
else if (e.type == SDL_MOUSEBUTTONUP)
{
if (gamelogic.PieceSelected())
{
if (coz.CurrentMenu() == Coz::MENU::GAME)
{
int x, y;
SDL_GetMouseState(&x, &y);
Point mousePos;
mousePos.x = x;
mousePos.y = y;
if (gamelogic.selectTarget(mousePos))
{
//printf("%d\t\t%d\t\t%d\t\t%d\t\t%d \n", gamelogic.PieceSelected(), gamelogic.source.x, gamelogic.source.y, gamelogic.target.x, gamelogic.target.y);
MovePiece(gamelogic);
if (gamelogic.isGameOver())
{
coz.setMenu(Coz::MENU::GAMEOVER);
}
gamelogic.togglePlayer();
}
}
}
}
}
}
void Controller::MovePiece(GameLogic &gamelogic) const
{
GameLogic::State sourceState = gamelogic.board[gamelogic.source.y][gamelogic.source.x];
gamelogic.board[gamelogic.source.y][gamelogic.source.x] = GameLogic::State::EMPTY;
gamelogic.board[gamelogic.target.y][gamelogic.target.x] = sourceState;
}