/
Create.c
138 lines (120 loc) · 2.74 KB
/
Create.c
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
#include "Functions.h"
/*
declares a 2D char array 'map' to hold the level.
Calls functions to let the user build a map and write it to a file.
*/
void Create(void) {
char map[22][80];
initscr();
noecho();
Pop(map);
Draw(map);
BuildMap(map);
endwin();
WriteToFile(map);
}
/*
takes in a 2D array of char, populates it with '.'
(i.e. the ground texture of the map.)
*/
void Pop(char map[22][80]) {
int i;
int j;
/*nested for-loops to iterate through every element of
the array*/
for(i = 0; i < 22; i++) {
for(j = 0; j < 80; j++) {
map[i][j] = '.';
}
}
}
/*
takes in a 2D array of char, prints the contents to the screen.
In other words, displays the map.
*/
void Draw(char map[22][80]) {
int i;
int j;
initscr();
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
for(i = 0; i < 22; i++) {
for(j = 0; j < 80; j++) {
mvaddch(i, j, map[i][j]);
}
}
/*printing the legend*/
mvwprintw(stdscr, 22, 0, " + B Y S o O ");
mvwprintw(stdscr, 23, 0, " Path Farmer Rifle Farmer Shotgun Farmer Start End");
refresh();
}
/*
takes in a 2D array of char, prints it to a .txt file.
*/
void WriteToFile(char map[22][80]) {
char inp[20];
char level[25];
FILE *fp;
int i;
int j;
/*read in desired name for level, concatenate .txt on the end.*/
fgets(inp, 19, stdin);
strtok(inp, "\n");
strncpy(level, inp, 19);
strcat(level, ".txt");
fp = fopen(level, "w");
/*iterate through every element, print it to file.*/
for(i = 0; i < 22; i++) {
for(j = 0; j < 80; j++) {
fputc(map[i][j], fp);
/*put a newline in the file if j has reached the end of a line.*/
if(j == 79)
fputc('\n', fp);
}
}
fclose(fp);
printf("New Level Created: %s\n", level);
exit(0);
}
/*
takes in a 2D array of char, allows the user to navigate it with wasd.
*/
void BuildMap (char map[22][80]) {
char in = 'a';
char put = 'a';
int y;
int x;
while(in != 'q' && in != 'Q') {
getyx(stdscr, y, x);
in = getch();
/*move cursor according to user input*/
switch(in) {
case 'w':
move(y - 1, x);
break;
case 'a':
move(y, x - 1);
break;
case 's':
move(y + 1, x);
break;
case 'd':
move(y, x + 1);
break;
case 'e':
/*if the user selected 'e', archive the character their cursor is on.*/
if((char)mvinch(y, x) == '+' || (char)mvinch(y, x) == 'B' || (char)mvinch(y, x) == 'Y' || (char)mvinch(y, x) == 'S' || (char)mvinch(y, x) == 'o' || (char)mvinch(y, x) == 'O')
put = (char)mvinch(y, x);
break;
case 'r':
/*if the user selected 'r', place the archived character where their cursor is.*/
if((char)mvinch(y, x) == '.') {
mvaddch(y, x, put);
map[y][x] = put;
}
move(y, x);
break;
}
refresh();
}
}