-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulletUpdateComponent.cpp
More file actions
86 lines (73 loc) · 2.06 KB
/
Copy pathBulletUpdateComponent.cpp
File metadata and controls
86 lines (73 loc) · 2.06 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
#include "BulletUpdateComponent.h"
#include "WorldState.h"
#include "Random.h"
using namespace std;
using namespace sf;
void BulletUpdateComponent::spawnForPlayer(Vector2f spawnPosition)
{
m_MovingUp = true;
m_BelongsToPlayer = true;
m_IsSpawned = true;
m_TC->getLocation().x = spawnPosition.x;
m_TC->getLocation().y = spawnPosition.y - m_TC->getSize().y;
// Update collider
m_RCC->setOrMoveCollider(
m_TC->getLocation().x,
m_TC->getLocation().y,
m_TC->getSize().x,
m_TC->getSize().y
);
}
void BulletUpdateComponent::spawnForInvader(Vector2f spawnPosition)
{
m_MovingUp = false;
m_BelongsToPlayer = false;
m_IsSpawned = true;
// srand((int)time(0)) ran on every spawn, so every invader bullet fired
// within the same second travelled at exactly the same speed, and the
// re-seeding discarded the generator's state each time.
m_AlienBulletSpeedModifier =
Random::inRange(0, m_ModifierRandomComponent - 1) + m_MinimumAdditionalModifier;
m_TC->getLocation().x = spawnPosition.x;
m_TC->getLocation().y = spawnPosition.y + m_TC->getSize().y;
// Update collider
m_RCC->setOrMoveCollider(
m_TC->getLocation().x,
m_TC->getLocation().y,
m_TC->getSize().x,
m_TC->getSize().y
);
}
void BulletUpdateComponent::deSpawn()
{
m_IsSpawned = false;
}
bool BulletUpdateComponent::isMovingUp()
{
return m_MovingUp;
}
void BulletUpdateComponent::update(float dt)
{
if (m_IsSpawned)
{
if (m_MovingUp)
{
m_TC->getLocation().y -= m_Speed * dt;
}
else
{
m_TC->getLocation().y += m_Speed / m_AlienBulletSpeedModifier * dt;
}
if (m_TC->getLocation().y > WorldState::WORLD_HEIGHT || m_TC->getLocation().y < -2)
{
deSpawn();
}
// Update the collider
m_RCC->setOrMoveCollider(
m_TC->getLocation().x,
m_TC->getLocation().y,
m_TC->getSize().x,
m_TC->getSize().y
);
}
}