-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgem.cpp
More file actions
52 lines (47 loc) · 1.25 KB
/
gem.cpp
File metadata and controls
52 lines (47 loc) · 1.25 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
#include "gem.h"
#include "template.h"
namespace Tmpl8
{
Gem::Gem() : gemSprite(new Surface("assets/gem.png"), 1) {}
// Come up with 5 random locations for gems
void Gem::Init()
{
for (int i = 0; i < 5; i++)
{
x[i] = IRand(800);
y[i] = IRand(512);
}
}
// Draw gem sprite
void Gem::Draw(Tmpl8::Surface* screen, int x, int y)
{
gemSprite.Draw(screen, x, y);
}
// Check the collision of gems and player
bool Gem::CheckCollision(int x, int y, int playerWidth, int playerHeight)
{
for (int i = 0; i < 5; i++)
{
if (x < this->x[i] + gemSprite.GetWidth() &&
x + playerWidth > this->x[i] &&
y < this->y[i] + gemSprite.GetHeight() &&
y + playerHeight > this->y[i])
{
Respawn(i);
return true;
}
}
return false;
}
// If gem is interacted with, a new one respawns
void Gem::Respawn(int index)
{
for (int j = index; j < 4; j++)
{
x[j] = x[j + 1];
y[j] = y[j + 1];
}
x[4] = IRand(774); //minus gem image size
y[4] = IRand(485); //minus gem image size
}
}