-
Notifications
You must be signed in to change notification settings - Fork 0
/
Agente.cpp
319 lines (316 loc) · 6.46 KB
/
Agente.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <string.h>
#include "Agente.hpp"
#include <stdlib.h>
static Image Agent;
void AgenteCls::drawstatic(Graphics& agent_pkm, Animation& anim)
{
agent_pkm.DrawImage2D(_posX,_posY,_spritesize,_spritesize,anim.AnimAgente(_direc,0));
}
AgenteCls::AgenteCls()
{
_scale = 0.75f;
_posX = (posInicialX+1) * DFLTSIZE + MyZeroX;
_posY = (posInicialY+1) * DFLTSIZE + MyZeroY;
_spritesize = DFLTSIZE;
adjust_scale(_scale,DFLTSIZE, &_posX, &_posY, &_spritesize);
_state = -1;
_health = 100;
_gold = 0;
_kills = 0;
_spdX = 0;
_spdY = 0;
_durwalk = 0;
AGENT_STATE = 0;
_direc = 3;
_dist = 0;
_walked = 0;
_frame = 0;
_nframes = 0;
_splitposY=0;
_splitposX=0;
_passos = 0;
_getgold = 0;
_getpu = 0;
_bullets = 0;
_checkgetitem = false;
//passa_mapa(a_map);
}
char AgenteCls::checa_casa_atual(void)
{
int indexX;
int indexY;
indexY = pos2index(_posX,0);
indexX = pos2index(_posY,1);
if(GAME_STATE == 0)
{
if(AGENT_STATE == 0)
{
atualiza_agente(indexX,indexY);
}
}
return UltimaCasaAcessada;
}
int AgenteCls::pos2index(int pos, int axis)
{
if(axis == 0) return ((pos - MyZeroX)/DFLTSIZE)-1;
else if(axis == 1) return ((pos - MyZeroY)/DFLTSIZE)-1;
return -1;
}
int AgenteCls::checkWin(void)
{
if(_gold == 3 && UltimaCasaAcessada == 'E') return 1;
return 0;
}
void AgenteCls::load(void)
{
Agent.LoadPNGImage("Sprites\\Agente\\AgentSD.png");
}
void AgenteCls::inicia_mvmt(int direc,int dist)
{
//printf("INICIANDO MOVIMENTO!!\n");
_passos++;
GAME_STATE = 0;
AGENT_STATE = 1; //Agente em movimento
_direc = direc;
_dist = DFLTSIZE;
_walked = 0;
_frame = 0;
_nframes = 6;
_spdX = (_dist / _nframes);
_spdY = (_dist / _nframes);
_splitposX = _posX;
_splitposY = _posY;
if(direc == 1)//Up
{
_spdX = 0;
_splitposY = _posY;
}
else if(direc == 2)//Left
{
_spdY = 0;
_spdX *= (-1);
_splitposX = _posX;
}
else if(direc == 3)//Down
{
_spdY *= (-1);
_spdX = 0;
_splitposY = _posY;
}
else if(direc == 4)//Right
{
_spdY = 0;
_splitposX = _posX;
}
}
void AgenteCls::finaliza_mvmt(void)
{
AGENT_STATE = 0;
_dist = 0;
_spdX = 0;
_spdY = 0;
_splitposY=0;
_splitposX=0;
//printf("FINALIZANDO MOVIMENTO!!\n");
}
void AgenteCls::walk(int direc, int *x, int *y)
{
int dist = DFLTSIZE;
int indexX, indexY;
indexX = pos2index(_posX,0);
indexY = pos2index(_posY,1);
if(direc == 1)//UP
{
if(indexY < LN-1)
{
inicia_mvmt(direc,dist);
*y += dist;
}
}
else if(direc == 2) //Left
{
if(indexX > 0)
{
inicia_mvmt(direc,dist);
*x -= dist;
}
}
else if(direc == 3) //Down
{
if(indexY > 0)
{
inicia_mvmt(direc,dist);
*y -= dist;
}
}
else if(direc == 4) //Right
{
if(indexX < COL-1)
{
inicia_mvmt(direc,dist);
*x += dist;
}
}
}
void AgenteCls::drawmvmt(Graphics& agent_pkm, int timer, Animation& anim)
{
int time;
int posX, posY;
time = timer;
_frame++;
if(_direc == 1 || _direc == 3) //vertical
{
_splitposY += _spdY;
}
else if(_direc == 2 || _direc == 4) //horizontal
{
_splitposX += _spdX;
}
posX = _splitposX;
posY = _splitposY;
agent_pkm.DrawImage2D(posX,posY,_spritesize,_spritesize,anim.AnimAgente(_direc,_frame));
if(_frame == _nframes)
{
finaliza_mvmt();
}
}
int AgenteCls::get_AGENT_STATE(void)
{
return AGENT_STATE;
}
int AgenteCls::metade_anim(void)
{
if( (_frame*2) == _nframes) return 1;
return 0;
}
int AgenteCls::RDMpos(int x)
{
int index;
int desloc = (x == 0)?MyZeroX:MyZeroY;
index = (GetTickCount()%(LN));
printf("Index %d e %d\n",index, x);
valid_pos_ind(index);
return ((index*DFLTSIZE)+ desloc);
}
void AgenteCls::atualiza_agente(int indexX, int indexY)
{
int aux;
char a_map[LN][COL];
passa_mapa(a_map);
aux = DFLTSIZE;
if(a_map[indexX][indexY] != '.' && a_map[indexX][indexY] != 'E')
{
GAME_STATE = 1;
}
UltimaCasaAcessada = a_map[indexX][indexY];
if(UltimaCasaAcessada == 'P')
{
_health = 0;
}
else if(UltimaCasaAcessada == 'D')
{
//_health -= 20;
}
else if(UltimaCasaAcessada == 'O')
{
_gold++;
deleta_ponto(indexX,indexY);
//a_map[indexX][indexY] = CasaVazia;
}
else if(UltimaCasaAcessada == 'U')
{
int hp_up = 30;
if(_health < 100)
{
_health += hp_up;
if(_health > 100)_health = 100;
deleta_ponto(indexX,indexY);
}
}
else if(a_map[indexX][indexY] == 'd')
{
//_health -= 10;
}
else if(UltimaCasaAcessada == 'T')
{
/*
_posX = RDMpos(0);
valid_pos_ind(pos2index(_posY,1));
//printf("C: Agnt\tF: att_agnt\tPosX:\n");
_posY = RDMpos(1);
valid_pos_ind(pos2index(_posX,0));
//printf("C: Agnt\tF: att_agnt\tPosY:\n");
adjust_scale(_scale, DFLTSIZE, &_posX, &_posY, &aux);
//printf("C: Agnt\tF: att_agnt\tPosZ:\n");*/
}
else if(UltimaCasaAcessada == 'E')
{
if(checkWin() == 1)
{
GAME_STATE = -1;
}
}
if(_health <= 0)
{
GAME_STATE = -1;
}
}
void AgenteCls::valid_pos_ind(int index)
{
if(index <= (-1) || index > 11)
{
printf("AgenteCls::Valid_pos_ind\nIndex Invalido no valor de: %d\n", index);
log_erro();
exit(1);
}
}
void AgenteCls::log_erro(void)
{
FILE *arq;
char hora[10];
char minuto[10];
char ponto[2] = "_";
char path[200] = "Logs\\log_erro_AGNT_";
char sufix[6] = ".txt";
time_t now = time(0);
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
sprintf(hora,"%d", timeinfo->tm_hour);
sprintf(minuto,"%d", timeinfo->tm_min);
strcat(path,hora);
strcat(path,ponto);
strcat(path,minuto);
strcat(path,sufix);
arq = fopen(path,"w");
fprintf(arq,"Game State %d\n", GAME_STATE);
fprintf(arq,"Agnt State %d\n", AGENT_STATE);
fprintf(arq,"PosX %d\n", _posX);
fprintf(arq,"PosY %d\n", _posY);
fprintf(arq,"IndexX: %d\n", pos2index(_posX,0));
fprintf(arq,"IndexY: %d\n", pos2index(_posY,1));
fprintf(arq,"Elemento: %c\n", UltimaCasaAcessada);
fprintf(arq,"SPDx %d\n", _spdX);
fprintf(arq,"SPDy %d\n", _spdY);
fprintf(arq,"Direct: %d\n", _direc);
fprintf(arq,"Dist: %d\n", _dist);
fprintf(arq,"Walked: %d\n", _walked);
fprintf(arq,"Frame: %d\n", _frame);
fprintf(arq,"SplitPosX: %d\n", _splitposX);
fprintf(arq,"SplitPosY: %d\n", _splitposY);
fclose(arq);
}
int AgenteCls::index2pos(int index, int axis)
{
int value;
if(axis == 0)
{
value = ((index+1)*DFLTSIZE)+MyZeroX;
}
else
{
value = ((index+1)*DFLTSIZE)+MyZeroY;
}
return value + 8;
}