-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_replay.cpp
168 lines (157 loc) · 4.18 KB
/
system_replay.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
#define NOMINMAX
#include "system_replay.h"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include "component.h"
#include "component_player.h"
#include "component_sprite.h"
#include "engine.h"
#include "entity.h"
#include "entity_stream.h"
#include "tags.h"
#include "constants.h"
using namespace std;
void SystemReplay::Update()
{
// Initialize (required)
if (!initialized)
{
initialized = Initialize();
}
Context* cont = engine->GetContext();
// TODO: According to toggled keys in context, either:
// [ARROW_LEFT] Decrease playout speed by factor 2
if(cont->GetKeyPressed(ALLEGRO_KEY_LEFT,true)){
if(speed >= 2){
speed = speed/2;
}
}
// [ARROW_RIGHT] Increase playout speed by factor 2
if(cont->GetKeyPressed(ALLEGRO_KEY_RIGHT,true)){
speed = speed*2;
}
// [ARROW_UP] Go to next point
if(cont->GetKeyPressed(ALLEGRO_KEY_UP,true)){
GoToNextPoint();
}
// [ARROW_DOWN] Go to next level
if(cont->GetKeyPressed(ALLEGRO_KEY_DOWN,true)){
GoToNextLevel();
}
// [P] Pause playout
if(cont->GetKeyPressed(ALLEGRO_KEY_P,true)){
cont->SwitchPaused();
}
// [ESC] Return to menu
if(cont->GetKeyPressed(ALLEGRO_KEY_ESCAPE, true)){
cont->SetState(2);
}
// Is the game running?
if (!engine->GetContext()->IsPaused())
{
// TODO: Go to the next frame(s), if necessary
if(status < 2){
GoToNextFrame();
}else{
if(cont->GetPoints(1) == 7){
cont->ResetPoints();
cont->SetState(1);
}else{
cont->ResetPoints();
cont->SetState(2);
}
}
}
}
void SystemReplay::GoToNextFrame()
{
// TODO: Go to next frame by setting the new coordinates. Set state to:
// 0 if a normal frame has been found
// 1 if a frame has been found in which the ball has dropped
// 2 if there are no coordinates left
// and update the context whenever necessary.
Context* cont = engine->GetContext();
list<coordinates>::iterator itr;
coordinates coord;
itr = cs.begin();
bool pointScored = false;
advance(itr, counter);
for(int i = 0; i<=speed; i++){
itr++;
counter++;
coord = (*itr);
if(itr == cs.end()){
if(coord.x_ball <= NET_POS_X){
cont->IncreasePoints(1);
cont->UpdateScore(200);
}else{
cont->IncreasePoints(2);
cont->UpdateScore(-100);
}
status = 2;
return;
}
if(cspr_ball->y - 20 <= cspr_ball->y_min && coord.x_player_1 == SLIME_1_INIT_X && coord.y_ball == BALL_INIT_Y && coord.x_player_2 == SLIME_2_INIT_X){
pointScored = true;
if(coord.x_ball <= NET_POS_X){
cont->IncreasePoints(1);
cont->UpdateScore(200);
}else{
cont->IncreasePoints(2);
cont->UpdateScore(-100);
}
}
if(pointScored){
status = 1;
}else{
status = 0;
}
cspr_ball->x = coord.x_ball;
cspr_ball->y = coord.y_ball;
cspr_player_1->x = coord.x_player_1;
cspr_player_1->y = coord.y_player_1;
cspr_player_2->x = coord.x_player_2;
cspr_player_2->y = coord.y_player_2;
}
}
void SystemReplay::GoToNextPoint()
{
while (status < 1)
{
GoToNextFrame();
}
}
void SystemReplay::GoToNextLevel()
{
while (status < 2)
{
GoToNextFrame();
}
}
bool SystemReplay::Initialize()
{
// TODO: Read input coordinates from file and push to list
Context* cont = engine->GetContext();
coordinates coord;
string file = cont->GetInputFile();
ifstream infile;
infile.open(file, ios::in);
while(infile >> coord.x_player_1 >> coord.y_player_1 >> coord.x_player_2 >> coord.y_player_2 >> coord.x_ball >> coord.y_ball){
cs.push_back(coord);
}
infile.close();
// TODO: Initialize all component pointers (optional)
EntityStream* es = engine->GetEntityStream();
set<Entity*> entities_player = es->WithTag(Component::PLAYER);
set<Entity*> entity_ball = es->WithTag(Component::BALL);
set<Entity*>::iterator itr;
itr = entities_player.begin();
cspr_player_1 = dynamic_cast<ComponentSprite*>((*itr)->GetComponent(Component::SPRITE));
itr++;
cspr_player_2 = dynamic_cast<ComponentSprite*>((*itr)->GetComponent(Component::SPRITE));
cspr_ball = dynamic_cast<ComponentSprite*>((*entity_ball.begin())->GetComponent(Component::SPRITE));
return true;
}