-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cpp
160 lines (144 loc) · 3.62 KB
/
map.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
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include "map.h"
#include "enums.h"
#include "actor.h"
#include "levels.h"
using namespace sf;
Map::Map(int level){
sizeX=10;
sizeY=10;
currLevel = level;
loadLevel(level);
loadTextures();
}
void Map::loadTextures(){
if (!grassTexture.LoadFromFile("textures/grass.png"))
{
std::cout << "Cannot load grass" << std::endl;
}
if (!wallTexture.LoadFromFile("textures/wall.png"))
{
std::cout << "Cannot load wall" << std::endl;
}
if (!boxTexture.LoadFromFile("textures/box.png"))
{
std::cout << "Cannot load box" << std::endl;
}
if (!exitTexture.LoadFromFile("textures/exit.png"))
{
std::cout << "Cannot load exit" << std::endl;
}
if (!playerTexture.LoadFromFile("textures/player.png"))
{
std::cout << "Cannot load player" << std::endl;
}
if (!exitBoxTexture.LoadFromFile("textures/boxExit.png"))
{
std::cout << "Cannot load exitBox" << std::endl;
}
}
void Map::draw(RenderWindow *window){
for(int i=0; i<sizeY; i++){
for(int j=0; j<sizeX; j++){
if(tile[i][j]==Wall){
Sprite wall;
wall.SetImage(wallTexture);
wall.Resize(RESOLUTION,RESOLUTION);
wall.SetPosition(j*RESOLUTION, i*RESOLUTION);
window->Draw(wall);
}
if(tile[i][j]==Grass){
Sprite grass;
grass.SetImage(grassTexture);
grass.Resize(RESOLUTION,RESOLUTION);
grass.SetPosition(j*RESOLUTION, i*RESOLUTION);
window->Draw(grass);
}
if(tile[i][j]==Box){
Sprite box;
box.SetImage(boxTexture);
box.Resize(RESOLUTION,RESOLUTION);
box.SetPosition(j*RESOLUTION, i*RESOLUTION);
window->Draw(box);
}
if(tile[i][j]==ExitBox){
Sprite exitBox;
exitBox.SetImage(exitBoxTexture);
exitBox.Resize(RESOLUTION,RESOLUTION);
exitBox.SetPosition(j*RESOLUTION, i*RESOLUTION);
window->Draw(exitBox);
}
if(tile[i][j]==Exit){
Sprite exit;
Sprite grass;
exit.SetImage(exitTexture);
grass.SetImage(grassTexture);
exit.Resize(RESOLUTION,RESOLUTION);
grass.Resize(RESOLUTION,RESOLUTION);
exit.SetPosition(j*RESOLUTION, i*RESOLUTION);
grass.SetPosition(j*RESOLUTION, i*RESOLUTION);
window->Draw(grass);
window->Draw(exit);
}
if(tile[i][j]==GrassPlayer){
Sprite player;
Sprite grass;
player.SetImage(playerTexture);
grass.SetImage(grassTexture);
player.Resize(RESOLUTION,RESOLUTION);
grass.Resize(RESOLUTION,RESOLUTION);
player.SetPosition(j*RESOLUTION, i*RESOLUTION);
grass.SetPosition(j*RESOLUTION, i*RESOLUTION);
window->Draw(grass);
window->Draw(player);
}
if(tile[i][j]==ExitPlayer){
Sprite exit;
Sprite player;
Sprite grass;
player.SetImage(playerTexture);
grass.SetImage(grassTexture);
exit.SetImage(exitTexture);
exit.Resize(RESOLUTION,RESOLUTION);
player.Resize(RESOLUTION,RESOLUTION);
grass.Resize(RESOLUTION,RESOLUTION);
player.SetPosition(j*RESOLUTION, i*RESOLUTION);
grass.SetPosition(j*RESOLUTION, i*RESOLUTION);
exit.SetPosition(j*RESOLUTION, i*RESOLUTION);
window->Draw(grass);
window->Draw(exit);
window->Draw(player);
}
}
}
}
int Map::getSizeX(){
return sizeX;
}
int Map::getSizeY(){
return sizeY;
}
void Map::loadLevel(int level){
Levels levels;
tile.clear();
std::cout << "Loading level " << level << "..." << std::endl;
levels.getLevel(&tile, level);
this->sizeY = tile.size();
this->sizeX = tile[0].size();
}
void Map::print(){
for(int i=0; i<sizeY; i++){
for(int j=0; j<sizeX; j++){
std::cout << tile[i][j];
}
std::cout << std::endl;
}
}
void Map::nextLevel(){
currLevel++;
loadLevel(currLevel);
}