-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
166 lines (165 loc) · 2.98 KB
/
game.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
#include "main.h"
game::game(){
consoleOn();
loadMaps();
map1=level1;
running=true;
currLevel=1;
}
void game::execute(){
message messageCopy("You are in a dark cavern with only flickering crystals to guide your path. A familliar yet haunting voice summons you forward from 2 levels above.");
currMessage=messageCopy;
consoleClear();
p1.setStart(map1);
currMessage.draw();
while(running){
render();
pollEvent();
Sleep(1);
}
}
void game::pollEvent(){
int input,res,random,random2;
int validinput=-1;
srand(time(NULL));
while(validinput==-1){
input=consoleGetKey();
switch(input){
case LEFT:
validinput=p1.move(-1,0,map1);
break;
case RIGHT:
validinput=p1.move(1,0,map1);
break;
case UP:
validinput=p1.move(0,-1,map1);
break;
case DOWN:
validinput=p1.move(0,1,map1);
break;
case END:
case ESC:
running=false;
validinput=0;
break;
}
}
if (validinput != ' '){
switch(validinput){
case 'x':
case 'X':
changeLevel(1);
if (validinput=='X')
p1.setStart(map1);
else if (validinput=='x')
p1.setStart(map1,2);
break;
case 'e':
case 'E':
if (currLevel>1){
changeLevel(-1);
if (validinput=='E')
{ p1.setStart(map1,3); }
else if (validinput=='e')
{ p1.setStart(map1,4); }
}
break;
case 'i':
if(p1.getItem(currLevel))
{ map1.edit(p1.getx(),p1.gety(),' '); }
break;
case 'S':
encounter[0].selmys();
break;
case 'W':
res=encounter[1].weaver(p1);
switch(res){
case -1:
case -2:
changeLevel(-4);
p1.setStart(map1);
break;
case -3:
random=rand() % 4 + 2;
changeLevel(3-random);
srand(time(NULL));
if(random==2 || random == 3)
random2=rand() % 3 + 1;
else
random2=rand() % 2 +1;
p1.setStart(map1,random2);
break;
case -4:
p1.removeItem(5);
level5.readmap();
break;
case 2:
victory();
break;
}
break;
}
}
}
void game::loadMaps(){
map copyMap1("level1.txt"),copyMap2("level2.txt"),copyMap3("level3.txt"),copyMap4("level4.txt"),copyMap5("level5.txt");
level1=copyMap1;
level2=copyMap2;
level3=copyMap3;
level4=copyMap4;
level5=copyMap5;
}
void game::changeLevel(int change){
if(change==0)
return;
switch (currLevel){
case 1:
level1=map1;
break;
case 2:
level2=map1;
break;
case 3:
level3=map1;
break;
case 4:
level4=map1;
break;
case 5:
level5=map1;
break;
}
currLevel=currLevel+change;
if (currLevel<1)
{ currLevel=1; }
switch (currLevel){
case 1:
map1=level1;
break;
case 2:
map1=level2;
break;
case 3:
map1=level3;
break;
case 4:
map1=level4;
break;
case 5:
map1=level5;
break;
}
}
void game::render(){
consoleClear();
map1.visibleMap(p1.getx(),p1.gety(), p1.getTorch());
p1.draw();
}
void game::victory(){
message msgOut("Congratulations! You have defeated the wizard Weaver! You will go down in OOP history, and C++ students everywhere will rejoice in your name!");
msgOut.draw();
running=false;
}
game::~game(){
consoleOff();
}