-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforest.cpp
More file actions
365 lines (324 loc) · 10.5 KB
/
Copy pathforest.cpp
File metadata and controls
365 lines (324 loc) · 10.5 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "raylib.h"
#include <stdio.h>
struct v2 {
float x;
float y;
};
struct tile_position {
int x;
int y;
};
struct world_position {
int screen;
tile_position tile;
};
enum entity_type {
Type_NONE,
Type_BLOCK,
Type_PLAYER,
Type_ENEMY,
Type_GOLD,
};
struct entity {
entity_type type;
world_position position;
};
enum gold_status {
NONE,
NOT_COLLECTED,
COLLECTED,
};
struct gold {
gold_status status;
world_position position;
};
struct game_state
{
bool game_over;
int gold_collected;
};
int Absolute(int A)
{
int Result = A;
if (Result < 0)
{
Result *= -1;
}
return(Result);
}
int main()
{
game_state State = {};
State.game_over = false;
State.gold_collected = 0;
InitWindow(480, 640, "Forest");
int ScreenWidth = GetScreenWidth();
int ScreenHeight = GetScreenHeight();
float TilesPerWidth = 10;
float TilesPerHeight = 13;
float TileWidth = ((ScreenWidth * 1.0f) / TilesPerWidth);
float TileHeight = ((ScreenHeight * 1.0f) / TilesPerHeight);
int ScreenIndex = 0;
world_position BlockP[] = {
{1, {3, 4}}, {1, {2, 4}}, {1, {3, 2}}, {1, {2, 2}}, {1, {2, 3}},
};
int BlockCount = sizeof(BlockP)/sizeof(BlockP[0]);
world_position DoorP[] = {
{0, {5, 0}},
{1, {5, 12}},
{1, {9, 5}},
{2, {0, 5}},
};
int DoorCount = sizeof(DoorP)/sizeof(DoorP[0]);
tile_position PlayerP = {5, 10};
gold GoldPP[] = {
{ NOT_COLLECTED, { 0, { 2, 2 } } },
{ NOT_COLLECTED, { 0, { 7, 2 } } },
{ NOT_COLLECTED, { 1, { 3, 3 } } },
};
tile_position GoldP[] = {
{2, 2},
{7, 2},
};
int GoldCount = sizeof(GoldPP)/sizeof(GoldPP[0]);
world_position EnemyP[] = {
{ 1, {5, 5} },
};
int EnemyCount = sizeof(EnemyP)/sizeof(EnemyP[0]);
bool DisplayEditorInfo = false;
while (!WindowShouldClose())
{
int MouseX = GetMouseX();
int MouseY = GetMouseY();
tile_position Mouse;
entity_type HoverObject = Type_NONE;
for (int I = 0; I < TilesPerWidth; ++I)
{
for (int J = 0; J < TilesPerHeight; ++J)
{
if (MouseX > (I*TileWidth) && (MouseX - (I*TileWidth) < TileWidth) && MouseY > (J*TileHeight) && (MouseY - (J*TileHeight) < TileHeight))
{
Mouse.x = I;
Mouse.y = J;
}
}
}
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
}
if (IsKeyPressed(KEY_TAB))
{
DisplayEditorInfo = !DisplayEditorInfo;
}
// Physics
int KeyPressed = GetCharPressed();
if (KeyPressed != 0)
{
tile_position NewPlayerP = PlayerP;
if (KeyPressed == 119)
{
// Up
NewPlayerP.y--;
}
if (KeyPressed == 115)
{
// Down
NewPlayerP.y++;
}
if (KeyPressed == 97)
{
// Left
NewPlayerP.x--;
}
if (KeyPressed == 100)
{
// Right
NewPlayerP.x++;
}
bool CollidedWithTiles = false;
for (int I = 0; I < TilesPerWidth; ++I)
{
for (int J = 0; J < TilesPerHeight; ++J)
{
if (I == 0 || J == 0 || I == (TilesPerWidth - 1) || J == (TilesPerHeight - 1))
{
bool DoorPresentHere = false;
for (int DoorIndex = 0; DoorIndex < DoorCount; ++DoorIndex)
{
if (ScreenIndex == DoorP[DoorIndex].screen && DoorP[DoorIndex].tile.x == I && DoorP[DoorIndex].tile.y == J)
{
DoorPresentHere = true;
}
}
if (!DoorPresentHere)
{
if (NewPlayerP.x == I && NewPlayerP.y == J)
{
CollidedWithTiles = true;
}
}
}
}
}
for (int Index = 0; Index < BlockCount && !CollidedWithTiles; ++Index)
{
if (BlockP[Index].screen == ScreenIndex)
{
if (NewPlayerP.x == BlockP[Index].tile.x && NewPlayerP.y == BlockP[Index].tile.y)
{
CollidedWithTiles = true;
}
}
}
if (!CollidedWithTiles)
{
PlayerP = NewPlayerP;
}
bool CollidesWithEnemy = false;
for (int Index = 0; Index < EnemyCount && !CollidesWithEnemy; ++Index)
{
if (EnemyP[Index].screen == ScreenIndex)
{
if (PlayerP.x == EnemyP[Index].tile.x && PlayerP.y == EnemyP[Index].tile.y)
{
CollidesWithEnemy = true;
}
}
}
if (CollidesWithEnemy)
{
State.game_over = true;
}
for (int Index = 0; Index < GoldCount; ++Index)
{
gold _Gold = GoldPP[Index];
world_position _GoldP = _Gold.position;
if (_Gold.status == NOT_COLLECTED && (ScreenIndex == _GoldP.screen) && (PlayerP.x == _GoldP.tile.x && PlayerP.y == _GoldP.tile.y))
{
GoldPP[Index].status = COLLECTED;
State.gold_collected++;
}
}
if (PlayerP.y < 0)
{
ScreenIndex++;
PlayerP.y = TilesPerHeight - 1;
}
else if (PlayerP.y > (TilesPerHeight - 1))
{
ScreenIndex--;
PlayerP.y = 0;
}
if (PlayerP.x < 0)
{
ScreenIndex--;
PlayerP.x = TilesPerWidth - 1;
}
else if (PlayerP.x > (TilesPerWidth - 1))
{
ScreenIndex++;
PlayerP.x = 0;
}
}
// Rendering
BeginDrawing();
ClearBackground(DARKGRAY);
for (int I = 0; I < TilesPerWidth; ++I)
{
for (int J = 0; J < TilesPerHeight; ++J)
{
if (I == 0 || J == 0 || I == (TilesPerWidth - 1) || J == (TilesPerHeight - 1))
{
bool DoorPresentHere = false;
for (int DoorIndex = 0; DoorIndex < DoorCount; ++DoorIndex)
{
if (ScreenIndex == DoorP[DoorIndex].screen && DoorP[DoorIndex].tile.x == I && DoorP[DoorIndex].tile.y == J)
{
DoorPresentHere = true;
}
}
if (!DoorPresentHere)
{
DrawRectangle(I*TileWidth, J*TileHeight, TileWidth, TileHeight, BLACK);
}
}
}
}
for (int Index = 0; Index < BlockCount; ++Index)
{
if (BlockP[Index].screen == ScreenIndex)
{
if (BlockP[Index].tile.x == Mouse.x && BlockP[Index].tile.y == Mouse.y)
{
HoverObject = Type_BLOCK;
}
DrawRectangle(BlockP[Index].tile.x*TileWidth, BlockP[Index].tile.y*TileHeight, TileWidth, TileHeight, BLACK);
}
}
if (PlayerP.x == Mouse.x && PlayerP.y == Mouse.y)
{
HoverObject = Type_PLAYER;
}
if (State.game_over)
{
DrawText("Game Over", TileWidth*2.4f, TileHeight*5.4f, 50, GOLD);
}
else
{
for (int Index = 0; Index < GoldCount; ++Index)
{
gold _Gold = GoldPP[Index];
world_position _GoldP = _Gold.position;
if (_GoldP.screen == ScreenIndex)
{
if (_Gold.status == NOT_COLLECTED)
{
if (_GoldP.tile.x == Mouse.x && _GoldP.tile.y == Mouse.y)
{
HoverObject = Type_GOLD;
}
DrawRectangle(_GoldP.tile.x*TileWidth, _GoldP.tile.y*TileHeight, TileWidth, TileHeight, GOLD);
}
}
}
for (int Index = 0; Index < EnemyCount; ++Index)
{
if (EnemyP[Index].screen == ScreenIndex)
{
DrawRectangle(EnemyP[Index].tile.x*TileWidth, EnemyP[Index].tile.y*TileHeight, TileWidth, TileHeight, RED);
}
}
DrawRectangle(PlayerP.x*TileWidth, PlayerP.y*TileHeight, TileWidth, TileHeight, PURPLE);
}
if (DisplayEditorInfo)
{
DrawRectangleLines(Mouse.x*TileWidth, Mouse.y*TileHeight, TileWidth, TileHeight, BEIGE);
char MousePositionText[1024];
char HoverObjectText[512];
if (HoverObject == Type_NONE)
{
snprintf(HoverObjectText, sizeof(HoverObjectText), "NONE");
}
else if (HoverObject == Type_GOLD)
{
snprintf(HoverObjectText, sizeof(HoverObjectText), "Gold");
}
else if (HoverObject == Type_PLAYER)
{
snprintf(HoverObjectText, sizeof(HoverObjectText), "Player");
}
else if (HoverObject == Type_BLOCK)
{
snprintf(HoverObjectText, sizeof(HoverObjectText), "Block");
}
snprintf(MousePositionText, sizeof(MousePositionText), "%d, %d\nScreen: %d\nObject: %s", Mouse.x, Mouse.y, ScreenIndex, HoverObjectText);
DrawText(MousePositionText, TileWidth*(Mouse.x + 1.2f), TileHeight*(Mouse.y + 1.2f), 20, BEIGE);
}
char GoldCollectedText[1024];
snprintf(GoldCollectedText, sizeof(GoldCollectedText), "Golds Collected: %d", State.gold_collected);
DrawText(GoldCollectedText, TileWidth*5.4f, TileHeight*12.4f, 20, GOLD);
DrawFPS(TileWidth*0.4f, TileHeight*0.4f);
EndDrawing();
}
return(0);
}