-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cpp
188 lines (150 loc) · 6.08 KB
/
Utils.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include "App.h"
#include "Game.h"
#include "Utils.h"
#include "TextureManager.h"
#include "Colour.h"
namespace typing
{
//////////////////////////////////////////////////////////////////////////
// Drawing utility funcs
//////////////////////////////////////////////////////////////////////////
void DrawTexturedRect(const std::string& texture, float x, float y, float width, float height)
{
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
TEXTURES.Bind(texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(x, y + height);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(x + width, y + height);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(x + width, y);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(x, y);
glEnd();
}
void DrawTexturedRect(const std::string& texture, const ColourRGBA& col, float x, float y, float width, float height)
{
glColor4f(col.GetRed(), col.GetGreen(), col.GetBlue(), col.GetAlpha());
TEXTURES.Bind(texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(x, y + height);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(x + width, y + height);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(x + width, y);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(x, y);
glEnd();
}
void DrawRect(ColourRGBA col, float x, float y, float width, float height)
{
glColor4f(col.GetRed(), col.GetGreen(), col.GetBlue(), col.GetAlpha());
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glVertex2f(x, y + height);
glVertex2f(x + width, y + height);
glVertex2f(x + width, y);
glVertex2f(x, y);
glEnd();
glEnable(GL_TEXTURE_2D);
}
void DrawLine(const ColourRGBA& col, float startX, float startY, float startZ, float endX, float endY, float endZ)
{
glColor4f(col.GetRed(), col.GetGreen(), col.GetBlue(), col.GetAlpha());
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES);
glVertex3f(startX, startY, startZ);
glVertex3f(endX, endY, endZ);
glEnd();
glEnable(GL_TEXTURE_2D);
}
void DrawLine(const ColourRGBA& col, const juzutil::Vector3& start, const juzutil::Vector3& end)
{
DrawLine(col, start[0], start[1], start[2], end[0], end[1], end[2]);
}
void DrawLine(const ColourRGBA& col, float startX, float startY, float endX, float endY)
{
DrawLine(col, startX, startY, 0.0f, endX, endY, 0.0f);
}
void DrawLine(const ColourRGBA& col, const juzutil::Vector2& start, const juzutil::Vector2& end)
{
DrawLine(col, start[0], start[1], 0.0f, end[0], end[1], 0.0f);
}
//////////////////////////////////////////////////////////////////////////
// Entity spawn utility funcs
//////////////////////////////////////////////////////////////////////////
// Selects a random spawn point for an enemy at the edge of the screen.
const juzutil::Vector2 SelectEdgeSpawnPoint(std::mt19937& random)
{
enum screenSide { SCREEN_LEFT, SCREEN_TOP, SCREEN_RIGHT, SCREEN_BOTTOM };
const float SIDE_OFFSET = 30.0f;
std::uniform_int_distribution<> sides(SCREEN_LEFT, SCREEN_BOTTOM);
screenSide side = static_cast<screenSide>(sides(random));
std::uniform_real_distribution<> distGen(0, 1);
float dist = static_cast<float>(distGen(random));
switch (side)
{
case SCREEN_LEFT:
return juzutil::Vector2(0.0f + SIDE_OFFSET, dist * APP.GetScreenHeight());
case SCREEN_TOP:
return juzutil::Vector2(dist * APP.GetScreenWidth(), 0.0f + SIDE_OFFSET);
case SCREEN_RIGHT:
return juzutil::Vector2(APP.GetScreenWidth() - SIDE_OFFSET, dist * APP.GetScreenHeight());
case SCREEN_BOTTOM:
return juzutil::Vector2(dist * APP.GetScreenWidth(), APP.GetScreenHeight() - SIDE_OFFSET);
}
return juzutil::Vector2();
}
// Selects a random spawn point for an entity in the middle of the screen.
const juzutil::Vector2 SelectMidSpawnPoint(std::mt19937& random)
{
const float EDGE_DEAD_ZONE = 50.0f;
const float MID_DEAD_ZONE = 50.0f;
std::uniform_real_distribution<> xGen(
EDGE_DEAD_ZONE,
APP.GetScreenWidth() - MID_DEAD_ZONE - EDGE_DEAD_ZONE);
float x = static_cast<float>(xGen(random));
if (x + EDGE_DEAD_ZONE * 2.0f > (APP.GetScreenWidth() - MID_DEAD_ZONE) / 2.0f)
{
x += MID_DEAD_ZONE;
}
std::uniform_real_distribution<> yGen(
EDGE_DEAD_ZONE,
APP.GetScreenHeight() - MID_DEAD_ZONE - EDGE_DEAD_ZONE);
float y = static_cast<float>(yGen(random));
if (y + EDGE_DEAD_ZONE * 2.0f > (APP.GetScreenHeight() - MID_DEAD_ZONE) / 2.0f)
{
y += MID_DEAD_ZONE;
}
return juzutil::Vector2(x, y);
}
// Selects a direction for an enemy to travel, such that at its closest point to the player,
// it is a distance 'dist' away from the player.
const juzutil::Vector2 SelectEnemyDirection(const juzutil::Vector3& spawnPoint, float dist)
{
// The direction from the player to the spawn point
juzutil::Vector2 dir(spawnPoint - GAME.GetPlayerOrigin());
dir.Normalize();
dir *= dist;
// There are two candidate points, one either side of the player
juzutil::Vector2 a(dir.GetY(), dir.GetX());
juzutil::Vector2 dirA = a + GAME.GetPlayerOrigin() - spawnPoint;
juzutil::Vector2 b(-dir.GetY(), dir.GetX());
juzutil::Vector2 dirB = b + GAME.GetPlayerOrigin() - spawnPoint;
// Pick the closer of the two candidate points.
if (dirB.SquareSize() >= dirA.SquareSize())
{
dirA.Normalize();
return dirA;
}
else
{
dirB.Normalize();
return dirB;
}
}
}