-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (96 loc) · 3.57 KB
/
main.cpp
File metadata and controls
123 lines (96 loc) · 3.57 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
#include "raylib.h"
#include "raymath.h" // Functions used Vector2Add, Vector2Subtract, Vector2Normalize, Vector2Scale
#include "Character.h"
#include "Prop.h"
#include "Enemy.h"
#include <string>
int main()
{
int windowDimensions[2]; // Array of window dimension
windowDimensions[0] = 384; // Window Width
windowDimensions[1] = 384; // Window Height
InitWindow(windowDimensions[0], windowDimensions[1], "Bash Gamez");
Texture2D map = LoadTexture("nature_tileset/WorldMap.png"); // Creating map texture
Vector2 mapPos{0.0, 0.0}; // screenPos, worldPos, mapPos
const float mapScale = 4.0f;
Character knight{windowDimensions[0], windowDimensions[1]}; // instance of the "Character" class called "knight"
// knight.setScreenPos(windowDimensions[0], windowDimensions[1]);
Prop props[2]{// Instance of the "Prop" class
Prop{Vector2{600.0f, 300.0f}, LoadTexture("nature_tileset/Rock.png")},
Prop{Vector2{400.f, 500.f}, LoadTexture("nature_tileset/Log.png")}};
Enemy goblin{// Instance of the "Enemy" class
Vector2{800.f, 300.f},
LoadTexture("characters/goblin_idle_spritesheet.png"),
LoadTexture("characters/goblin_run_spritesheet.png")};
Enemy slime{
Vector2{500.f, 700.f},
LoadTexture("characters/slime_idle_spritesheet.png"),
LoadTexture("characters/slime_run_spritesheet.png")};
Enemy *enemies[]{
&goblin,
&slime};
for (auto enemy : enemies)
{
enemy->setTarget(&knight);
}
SetTargetFPS(60);
while (!WindowShouldClose())
{
// Start drawing
BeginDrawing();
ClearBackground(WHITE);
mapPos = Vector2Scale(knight.getWorldPos(), -1.0f); // Moving the map and the not the worldPos
// Drawing map texture
DrawTextureEx(map, mapPos, 0.0, mapScale, WHITE); // scaling the map by a factor of 4
// draw props
for (auto prop : props)
{
prop.Render(knight.getWorldPos());
}
if (!knight.getAlive()) // Character is not alive
{
DrawText("Game Over!", 55.f, 45.f, 40, RED);
EndDrawing();
continue;
}
else // Character is alive
{
std::string knightsHealth = "Health: ";
knightsHealth.append(std::to_string(knight.getHealth()), 0, 5);
DrawText(knightsHealth.c_str(), 55.f, 45.f, 40, RED);
}
knight.tick(GetFrameTime());
// Map Bounds
if (knight.getWorldPos().x < 0.0f ||
knight.getWorldPos().y < 0.0f ||
knight.getWorldPos().x + windowDimensions[0] > map.width * mapScale ||
knight.getWorldPos().y + windowDimensions[1] > map.height * mapScale)
{
knight.undoMovement();
}
// Check Prop Collisions
for (auto prop : props)
{
if (CheckCollisionRecs(prop.getCollisionRec(knight.getWorldPos()), knight.GetCollisionRec()))
{
knight.undoMovement();
}
}
for (auto enemy : enemies)
{
enemy->tick(GetFrameTime());
}
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
for (auto enemy : enemies)
{
if (CheckCollisionRecs(enemy->GetCollisionRec(), knight.getWeaponCollisionRec()))
{
enemy->setAlive(false);
}
}
}
// Game Logic Ends
EndDrawing();
}
}