-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
157 lines (147 loc) · 5.66 KB
/
main.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
#include "raylib.h"
#include "raymath.h"
#include "main.h"
#include "Character.h"
#include "Prop.h"
#include "Enemy.h"
#include <string>
#include <iostream>
int DEBUG_MODE = 0;
#define ENEMY_COUNT 4
#define PROP_COUNT 4
#define MAP_COUNT 4
// Draw
void draw(const float deltaTime,
const int screenWidth,
const int screenHeight,
Character &knight,
Enemy (&goblins)[ENEMY_COUNT],
Prop (&props)[PROP_COUNT],
Texture2D (&map)[MAP_COUNT],
Vector2 &mapPos,
float &runningTime,
int ¤tFrame)
{
const int frameCount = 4;
const float frameTime = 1.0f / 6.f;
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw Map
runningTime += deltaTime;
if (runningTime > frameTime)
{
currentFrame++;
runningTime = 0;
if (currentFrame >= frameCount)
currentFrame = 0;
}
DrawTextureEx(map[currentFrame], mapPos, 0, 4, WHITE);
for (Prop prop : props)
{
prop.draw(knight.getWorldPos());
}
if (!knight.getAlive())
{
DrawText("You died!", screenWidth / 2 - MeasureText("You died!", 20) / 2, screenHeight / 2, 20, RED);
EndDrawing();
return;
}
else
{
std::string text = "HP: ";
text.append(std::to_string(knight.getHealth()));
DrawText(text.c_str(), 10, 10, 20, BLACK);
}
knight.draw();
for (auto &goblin : goblins)
{
goblin.draw();
}
EndDrawing();
}
// Update
void update(const float deltaTime,
const int screenWidth,
const int screenHeight,
Character &knight,
Enemy (&goblins)[ENEMY_COUNT],
Prop (&props)[PROP_COUNT],
Texture2D (&map)[MAP_COUNT],
Vector2 &mapPos)
{
knight.update(deltaTime);
for (auto &&goblin : goblins)
{
goblin.update(deltaTime);
}
Vector2 knightWorldPos = knight.getWorldPos();
// Check if the knight is going out of the map
if (knightWorldPos.x < 0 || knightWorldPos.y < 0 || knightWorldPos.x + screenWidth > map[0].width * 4.f || knightWorldPos.y + screenHeight > map[0].height * 4.f)
{
knight.undoMovement();
}
// Check if the knight is colliding with a prop
for (auto prop : props)
{
if (CheckCollisionRecs(knight.getCollisionRectangle(), prop.getCollisionRectangle(knightWorldPos)))
knight.undoMovement();
}
if (knight.isAttacking())
{
for (auto &&goblin : goblins)
{
if (CheckCollisionRecs(knight.getWeaponCollisionRec(), goblin.getCollisionRectangle()))
{
goblin.setAlive(false);
}
}
}
mapPos = Vector2Scale(knight.getWorldPos(), -1.f);
}
int main(int argc, char const *argv[])
{
const int screenWidth = 384;
const int screenHeight = 384;
const int targetFPS = 60;
float runningTime = 0;
int currentFrame = 0;
InitWindow(screenWidth, screenHeight, "ClassyClash");
SetTargetFPS(targetFPS);
Vector2 mapPos{0, 0};
Texture2D map[MAP_COUNT]{LoadTexture("sproutLands/WorldMap30x30_1.png"),
LoadTexture("sproutLands/WorldMap30x30_2.png"),
LoadTexture("sproutLands/WorldMap30x30_3.png"),
LoadTexture("sproutLands/WorldMap30x30_4.png")};
Character knight{Vector2{screenWidth, screenHeight}, Vector2{555, 555}, Vector4{3, 3, 1, 1}};
Enemy goblins[ENEMY_COUNT]{
{Vector2{545, 2345}, LoadTexture("characters/goblin_idle_spritesheet.png"), LoadTexture("characters/goblin_run_spritesheet.png"), Vector4{1, 1, 1, 2}},
{Vector2{456, 126}, LoadTexture("characters/goblin_idle_spritesheet.png"), LoadTexture("characters/goblin_run_spritesheet.png"), Vector4{1, 1, 1, 2}},
{Vector2{454, 234}, LoadTexture("characters/slime_idle_spritesheet.png"), LoadTexture("characters/slime_run_spritesheet.png"), Vector4{1, 1, 1, 2}},
{Vector2{234, 987}, LoadTexture("characters/slime_idle_spritesheet.png"), LoadTexture("characters/slime_run_spritesheet.png"), Vector4{1, 1, 1, 2}}};
for (auto &&goblin : goblins)
{
goblin.setTarget(&knight);
}
Prop props[PROP_COUNT]{
Prop{Vector2{600.0f, 500.0f}, LoadTexture("sproutLands/Objects/Basic_Grass_Biom_things_1.png"), Rectangle{8 * 16, 16, 16, 16}, Vector4{1, 1, 2, 3}}, // Stone
Prop{Vector2{400.0f, 400.0f}, LoadTexture("sproutLands/Objects/Basic_Grass_Biom_things_1.png"), Rectangle{0, 0, 16, 32}, Vector4{1, 1, 0, 4}}, // Tall Tree
Prop{Vector2{700.0f, 450.0f}, LoadTexture("sproutLands/Objects/Basic_Grass_Biom_things_1.png"), Rectangle{16, 0, 32, 32}, Vector4{1, 1, 0, 4}}, // Fat Tree
Prop{Vector2{350.0f, 700.0f}, LoadTexture("sproutLands/Objects/Basic_Grass_Biom_things_1.png"), Rectangle{48, 0, 32, 32}, Vector4{1, 1, 0, 4}} // Fat Tree with Apples
};
// animation variables
while (!WindowShouldClose())
{
float deltaTime = GetFrameTime();
//----------------------------------------------------------------------------------
// Update
//----------------------------------------------------------------------------------
update(deltaTime, screenWidth, screenHeight, knight, goblins, props, map, mapPos);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
draw(deltaTime, screenWidth, screenHeight, knight, goblins, props, map, mapPos, runningTime, currentFrame);
}
for (auto mapTexture : map)
UnloadTexture(mapTexture);
return 0;
}