-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.cpp
101 lines (94 loc) · 3.21 KB
/
level.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
//======================================================================================
//Source code for cLevel class
//
//(c) Patrick Dickinson, University of Lincoln, School of Computer Science, 2020
//======================================================================================
#include "level.h"
#include "bots.h"
#include <stdio.h>
//======================================================================================
//Globals
//======================================================================================
cLevel gLevel;
//======================================================================================
//Valid if in range and position is traversable
//======================================================================================
bool cLevel::isValid(int x, int y) const
{
if ((x < 0) || (x > 39)) return false;
if ((y < 0) || (y > 39)) return false;
int m = map[x][y];
if(m != 0) return false;
return true;
}
//======================================================================================
//Constructor
//======================================================================================
cLevel::cLevel()
{
for (int i = 0; i < GRIDHEIGHT; i++)
{
for (int j = 0; j < GRIDWIDTH; j++)
{
map[i][j] = 0;
}
}
}
//======================================================================================
//Load map from ascii text file
//======================================================================================
void cLevel::Load(const char* fname)
{
for (int i = 0; i < GRIDHEIGHT; i++)
{
for (int j = 0; j < GRIDWIDTH; j++)
{
map[i][j] = 0;
}
}
FILE *file = fopen(fname,"rt");
for (int j = 0; j < GRIDHEIGHT; j++)
{
char line[128];
fread(line, 1, 41, file);
for (int i = 0; i < GRIDWIDTH; i++)
{
if(line[i]=='#') map[i][j] = 1;
else map[i][j] = 0;
}
}
fclose(file);
}
//======================================================================================
//Draw grid
//======================================================================================
void cLevel::Draw(void)
{
SDL_Rect position;
position.h = position.w = 16;
for (int i = 0; i < GRIDHEIGHT; i++)
{
for (int j = 0; j < GRIDWIDTH; j++)
{
position.x = i*16;
position.y = j*16;
/*
if (map[i][j] == 0)
{
SDL_RenderCopy(gMainRenderer, tileTexture, NULL, &position);
}
else SDL_RenderCopy(gMainRenderer, tileBlockedTexture, NULL, &position);
*/
if (map[i][j] == 0)
{
//if ((gDijkstra.completed) && (gDijkstra.getInPath(i, j) == true)) SDL_RenderCopy(gMainRenderer, tileRouteTexture, NULL, &position);
//else if ((gDijkstra.completed) && (gDijkstra.getClosed(i, j) == true)) SDL_RenderCopy(gMainRenderer, tileClosedTexture, NULL, &position);
//else SDL_RenderCopy(gMainRenderer, tileTexture, NULL, &position);
if ((gAStar.completed) && (gAStar.getInPath(i, j) == true)) SDL_RenderCopy(gMainRenderer, tileRouteTexture, NULL, &position);
else if ((gAStar.completed) && (gAStar.getClosed(i, j) == true)) SDL_RenderCopy(gMainRenderer, tileClosedTexture, NULL, &position);
else SDL_RenderCopy(gMainRenderer, tileTexture, NULL, &position);
}
else SDL_RenderCopy(gMainRenderer, tileBlockedTexture, NULL, &position);
}
}
}