-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practice.cpp
239 lines (184 loc) · 5.42 KB
/
Practice.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// Practice.cpp
// prototype
//
// Created by Chris Morgan on 2015-03-08.
// Copyright (c) 2015 Chris Morgan. All rights reserved.
//
#include "Practice.h"
bool forcePQuit = false;
Practice::Practice(float gt, int gs) : practiceTime(gt), endScore(gs){
// init(); // maybe should be done in start
score = new ScoreBoard(practiceTime, vec2(SCREEN_WIDTH/2, SCREEN_HEIGHT/10));
SDL_Rect r;
r.x = 0;
r.y = (SCREEN_HEIGHT * 9)/10;
r.w = SCREEN_WIDTH;
r.h = SCREEN_HEIGHT/10;
platforms.push_back(new Platform(r));
r.x = SCREEN_WIDTH / 3;
r.y = SCREEN_HEIGHT * 3 / 5;
r.w = SCREEN_WIDTH / 3;
r.h = SCREEN_HEIGHT / 20;
platforms.push_back(new Platform(r));
r.x = -SCREEN_WIDTH / 10;
r.y = 0;
r.w = SCREEN_WIDTH / 10;
r.h = SCREEN_HEIGHT;
platforms.push_back(new Platform(r));
r.x = SCREEN_WIDTH;
r.y = 0;
r.w = SCREEN_WIDTH / 10;
r.h = SCREEN_HEIGHT;
platforms.push_back(new Platform(r));
r.x = 0;
r.y = 0;
r.w = PLAYER_WIDTH;
r.h = PLAYER_HEIGHT;
player1 = new Player(r, true, platforms, HOME);
r.x = 0;
r.y = 0;
r.w = SCREEN_WIDTH * 0.02;
r.h = SCREEN_HEIGHT * 0.3;
net1 = new Net(r, true, SCREEN_HEIGHT * 0.05, SCREEN_HEIGHT * 0.9 - r.h - SCREEN_HEIGHT * 0.05, HOME);
r.x = SCREEN_WIDTH-NET_WIDTH;
r.y = 0;
r.w = NET_WIDTH;
r.h = NET_HEIGHT;
net2 = new Net(r, true, SCREEN_HEIGHT * 0.05, SCREEN_HEIGHT * 0.9 - r.h - SCREEN_HEIGHT * 0.05, AWAY);
r.x = SCREEN_WIDTH/2 - PUCK_WIDTH/2;
r.y = 0;
r.w = PUCK_WIDTH;
r.h = PUCK_HEIGHT;
puck = new Puck(r, score);
}
Practice::~Practice(){
close();
}
void Practice::init(){
}
void Practice::close(){
}
void Practice::reset(){
}
void Practice::start(){
reset();
float time = 0.0; // time for frame loop in seconds since it starts
int timeStart = SDL_GetTicks();
float deltaTime = 0.0; // time taken between frame;
LTexture bg;
bg.loadFromFile("backgroundWithPlatform.png");
while (!isPracticeOver()) {
// update Time information...
int currentMillis = SDL_GetTicks();
float nextTime = ((float)(currentMillis - timeStart))/1000.0f;
deltaTime = nextTime-time;
time = nextTime;//((float)(currentMillis - timeStart))/1000.0f;
//cout << 1 / deltaTime << endl;
//cout << "Time: " << time << " DeltaTime: " << deltaTime << endl;
// get Input and update object positions from input
handleInput();
player1->update(deltaTime);
net1->update(deltaTime);
net2->update(deltaTime);
puck->update(deltaTime);
// check collisions...
//CollisionFace collFace = GameObject::checkCollision(player1, player2);
//Player::OnCollisionEnter(player1, player2, collFace, deltaTime);
CollisionFace collFace;
for (int i = 0; i < platforms.size(); i++){
collFace = GameObject::checkCollision(player1, platforms[i]);
player1->OnCollisionEnter(platforms[i], collFace, deltaTime);
//collFace = GameObject::checkCollision(player2, platforms[i]);
//player2->OnCollisionEnter(platforms[i], collFace, deltaTime);
if (!puck->isPossessed()){
collFace = GameObject::checkCollision(puck, platforms[i]);
puck->OnCollisionEnter(platforms[i], collFace, deltaTime);
}
}
if (!puck->isPossessed()){
collFace = GameObject::checkCollision(puck, net1);
puck->OnCollisionEnter(net1, collFace, deltaTime);
collFace = GameObject::checkCollision(puck, net2);
puck->OnCollisionEnter(net2, collFace, deltaTime);
collFace = GameObject::checkCollision(puck, player1);
player1->OnCollisionEnter(puck, collFace, deltaTime);
//
// collFace = GameObject::checkCollision(puck, player2);
// player2->OnCollisionEnter(puck, collFace, deltaTime);
}
// end collision checking
// Practice logic implementation...
// end Practice logic
// render frame
SDL_SetRenderDrawColor(SDLGI::gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( SDLGI::gRenderer );
bg.render(0,0);
score->draw();
//Draw Platforms
SDL_SetRenderDrawColor( SDLGI::gRenderer, 0xFF, 0xFF, 0x00, 0xFF );
for(int i = 0; i < platforms.size(); i++)
{
platforms[i]->draw();
}
player1->draw();
net1->draw();
net2->draw();
if (!puck->isPossessed()){
puck->draw();
}
//Update screen
SDL_RenderPresent( SDLGI::gRenderer );
// end render
}
forcePQuit = false;
close();
}
void Practice::handleInput(){
SDL_Event e;
SDL_PumpEvents();
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
//User requests quit
if( e.type == SDL_QUIT || e.key.keysym.sym == SDLK_ESCAPE)
{
forcePQuit=true;
}
//If a key was pressed
if( e.type == SDL_KEYDOWN && e.key.repeat == 0 )
{
//Adjust the velocity
switch( e.key.keysym.sym )
{
case SDLK_w: player1->Up(); puck->Up(); break;
case SDLK_s: player1->Down(); break;
case SDLK_a: player1->Left(); break;
case SDLK_d: player1->Right(); break;
case SDLK_LSHIFT: player1->Shoot(); break;
}
}
//If a key was released
else if( e.type == SDL_KEYUP && e.key.repeat == 0 )
{
//Adjust the velocity
switch( e.key.keysym.sym )
{
case SDLK_w: player1->UpUnpressed(); break;
case SDLK_s: player1->DownUnpressed(); break;
case SDLK_a: player1->LeftUnpressed(); break;
case SDLK_d: player1->RightUnpressed(); break;
case SDLK_LSHIFT: player1->ShootUnpressed(); break;
}
}
//Handle input for the player objects...
//coming soon;
}
}
bool Practice:: isPracticeOver(){
return score->getMaxScore()==endScore || score->isTimeDone() || forcePQuit;
}
bool Practice:: isGoal(){
return false;
}