-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteleports.cpp
More file actions
104 lines (100 loc) · 2.83 KB
/
teleports.cpp
File metadata and controls
104 lines (100 loc) · 2.83 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
#include "teleports.h"
Teleports::Teleports(Snake snake){
init(snake);
}
void Teleports::init(Snake snake){
portal_bmp = SDL_LoadBMP("./bitmaps/portal.bmp");
for (int i = 0;i<N_OF_TELEPORTS;i++){
teleports[i].num = i;
position_teleport(snake,i,1);
position_teleport(snake,i,2);
}
}
void Teleports::position_teleport(Snake snake,int num,int which){
int x,y,pos_ok = 1;
int tries = 0;
int positions_x = SCREEN_WIDTH/24;
int positions_y = SCREEN_HEIGHT/24;
do{
tries++;
pos_ok = 1;
x = rand()%(positions_x-1);
y = rand()%(positions_y-2)+1;
x = x*24 + 12;
y = y*24 + 36;
for (int i = 0;i<num;i++){
if ((teleports[i].one.x == x && teleports[i].one.y == y) ||
(teleports[i].two.x == x && teleports[i].two.y == y)){
pos_ok = 0;
}
}
if (which == 2 && (teleports[num].one.x == x && teleports[num].one.y == y)){
pos_ok = 0;
}
for (int i = 0;i<snake.length;i++){
if (snake.snake_parts[i].x == x && snake.snake_parts[i].y == y) {
pos_ok = 0;
}
}
}while(!pos_ok && tries <1000);
if (pos_ok){
if (which == 1){
teleports[num].one = {x,y};
}
else if (which == 2){
teleports[num].two = {x,y};
}
}
}
void Teleports::set_window_size(int width,int height){
screen_width = width;
screen_height = height;
}
void Teleports::DrawTeleports(Renderer renderer){
char number_str[10] = "";
int w,h;
SDL_Rect dest;
w = portal_bmp->w;
h = portal_bmp->h;
dest.w = w;
dest.h = h;
for (int i = 0;i<N_OF_TELEPORTS;i++){
sprintf(number_str, "%d",teleports[i].num);
dest.x = teleports[i].one.x - w / 2;
dest.y = teleports[i].one.y - h / 2;
SDL_BlitSurface(portal_bmp, NULL, renderer.screen, &dest);
renderer.DrawString(teleports[i].one.x, teleports[i].one.y, number_str);
sprintf(number_str, "%d",teleports[i].num);
dest.x = teleports[i].two.x - w / 2;
dest.y = teleports[i].two.y - h / 2;
SDL_BlitSurface(portal_bmp, NULL, renderer.screen, &dest);
renderer.DrawString(teleports[i].two.x, teleports[i].two.y, number_str);
}
}
void Teleports::teleport_snake(Snake* snake){
for (int i = 0;i<N_OF_TELEPORTS;i++){
if (snake->snake_parts[0].x == teleports[i].one.x && snake->snake_parts[0].y == teleports[i].one.y){
snake->posx = teleports[i].two.x;
snake->posy = teleports[i].two.y;
}
else if (snake->snake_parts[0].x == teleports[i].two.x && snake->snake_parts[0].y == teleports[i].two.y){
snake->posx = teleports[i].one.x;
snake->posy = teleports[i].one.y;
}
}
}
void Teleports::save(FILE* fptr){
for (int i = 0;i<N_OF_TELEPORTS;i++){
fprintf(fptr,"%d,%d,%d,%d ",teleports[i].one.x,teleports[i].one.y,
teleports[i].two.x,teleports[i].two.y);
}
}
int Teleports::load(FILE* fptr){
for (int i = 0;i<N_OF_TELEPORTS;i++){
if (!fscanf(fptr,"%d,%d,%d,%d ",&teleports[i].one.x,&teleports[i].one.y,
&teleports[i].two.x,&teleports[i].two.y)){
return 0;
}
}
return 1;
}