-
Notifications
You must be signed in to change notification settings - Fork 0
/
Projectile.cpp
136 lines (105 loc) · 3.76 KB
/
Projectile.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
#include "precomp.h"
#include "Projectile.h"
#include "MySprite.h"
#include "PlayerData.h"
#include "TransformComponent.h"
#include "SpriteComponent.h"
#include "TimeManager.h"
#include "EntityManager.h"
#include "ColliderComponent.h"
#include "Military.h"
#include "TileMap.h"
#include "Quadtree.h"
#include "Explosion.h"
#include "Scene.h"
Projectile::Projectile(Scene& scene, string type) :
Entity(scene),
projData{Projectile::Data::GetData(type)}
{
AddComponent<TransformComponent>();
AddComponent<SpriteComponent>("projectiles");
}
Projectile::~Projectile()
{
}
void Projectile::Tick()
{
float deltaTime = TimeManager::GetDeltaTime();
float moveForwardBy = deltaTime * vel;
distanceLeft -= moveForwardBy;
if (distanceLeft <= 0.0f)
{
Hit();
return;
}
TransformComponent* myTransform = GetComponent<TransformComponent>();
myTransform->MoveForward(moveForwardBy);
SpriteComponent* mySprite = GetComponent<SpriteComponent>();
BoundingBox myBox = { myTransform->GetWorldPos(), make_float2(mySprite->GetSize()) };
Quadtree* quadTree = scene.quadTree.get();
if (!quadTree->GetMaxBounds().Contains(myBox))
{
Hit();
return;
}
float timePassed = TimeManager::GetTotalTimePassed();
const uint fps = 16;
const uint numOfFramesPerDir = 4;
uint animationOffset = static_cast<uint>(fmodf(timePassed, static_cast<float>(numOfFramesPerDir) / static_cast<float>(fps)) * fps);
const uint directions = 8;
uint rotationOffset = SpriteComponent::GetRotationOffset(myTransform->GetRotation(), directions);
uint frame = static_cast<uint>(rotationOffset * numOfFramesPerDir + animationOffset);
mySprite->GetSprite()->SetFrame(uint2{ frame, projData->spriteRow });
// Check for collisions
vector<ColliderComponent*> collisions = quadTree->Query(myBox, layer);
for (ColliderComponent* collider : collisions)
{
Military* colliderOwner = dynamic_cast<Military*>(collider->GetOwner());
if (colliderOwner == nullptr
|| colliderOwner->GetArmyId() == firedBy)
{
continue;
}
// Pixel collision check here
SpriteComponent* otherSprite = colliderOwner->GetComponent<SpriteComponent>();
if (otherSprite == nullptr
|| !mySprite->IsColliding(otherSprite))
{
continue;
}
// Should deal damage instead
colliderOwner->TakeDamage(projData->damage, this);
Hit();
return;
}
}
void Projectile::Hit()
{
EntityManager* entityManager = scene.entityManager.get();
float2 centre = GetComponent<TransformComponent>()->GetWorldPos() + make_float2(GetComponent<SpriteComponent>()->GetSize()) / 2.0f;
if (projData->explosionType != "none")
{
entityManager->AddEntity<Explosion>(projData->explosionType, centre);
}
entityManager->RemoveEntity(GetId());
}
void Projectile::Fire(float2 position, float rotation, ArmyId a_firedBy, CollisionLayer a_layer, float startingVel, float turretRange)
{
TransformComponent* transform = GetComponent<TransformComponent>();
SpriteComponent* sprite = GetComponent<SpriteComponent>();
uint2 centreOffset = sprite->GetSize() / 2u;
transform->SetWorldPos(position - make_float2(centreOffset));
transform->SetRotation(rotation);
origin = position;
vel = startingVel;
firedBy = a_firedBy;
layer = a_layer;
distanceLeft = turretRange;
}
Projectile::Data::Data(const string& type)
{
PlayerData playerData("entity.projectile." + type);
damage = stof(playerData.GetVariableValue("damage"));
explosionType = playerData.GetVariableValue("explosiontype");
spriteRow = static_cast<uint>(stoi(playerData.GetVariableValue("spriterow")));
}