-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathC_InteractWithObjects.cpp
196 lines (131 loc) · 5.24 KB
/
C_InteractWithObjects.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
189
190
191
192
193
194
195
196
#include "C_InteractWithObjects.hpp"
#include "Object.hpp"
#include "S_Quests.hpp"
#include "SceneDungeon.hpp"
C_InteractWithObjects::C_InteractWithObjects(Object* owner) : Component(owner), interactionDistance(100.f) {}
void C_InteractWithObjects::Awake()
{
direction = owner->GetComponent<C_Direction>();
animation = owner->GetComponent<C_Animation>();
}
void C_InteractWithObjects::Update(float deltaTime)
{
if (owner->context->player->userMovementEnabled)
{
/* ENABLE THIS TO DO CONSTANT SIGHT CHECKS
//draw a line out from the player
sf::Vector2i heading = direction->GetHeading(); //returns vector with only one direction set
const sf::Vector2f& startPoint = owner->transform->GetPosition();
sf::Vector2f endPoint;
endPoint.x = startPoint.x + (heading.x * interactionDistance * 1.5);
endPoint.y = startPoint.y + (heading.y * interactionDistance * 1.5);
Debug::DrawLine(startPoint, endPoint, sf::Color::Red);
BoxcastResult result = owner->context->boxcast->Cast(startPoint, endPoint, owner->instanceID->Get());
*/
if (owner->context->currentScene->inDialog())
{
//skip a frame
}
else // process normally
{
if (owner->context->input->IsKeyDown(Input::Key::R))
{
Debug::Log("BoxCast Called");
//draw a line out from the player
sf::Vector2i heading = direction->GetHeading(); //returns vector with only one direction set
const sf::Vector2f& startPoint = owner->transform->GetPosition();
sf::Vector2f endPoint;
endPoint.x = startPoint.x + (heading.x * interactionDistance );
endPoint.y = startPoint.y + (heading.y * interactionDistance );
Debug::DrawLine(startPoint, endPoint, sf::Color::Red);
BoxcastResult result = owner->context->boxcast->Cast(startPoint, endPoint, interactionDistance *2 , owner->instanceID->Get());
}
if (owner->context->input->IsKeyDown(Input::Key::SPACE))
{
sf::Vector2i heading = direction->GetHeading();
const sf::Vector2f& startPoint = owner->transform->GetPosition();
sf::Vector2f endPoint;
endPoint.x = startPoint.x + (heading.x * interactionDistance);
endPoint.y = startPoint.y + (heading.y * interactionDistance);
BoxcastResult result = owner->context->boxcast->Cast(startPoint, endPoint, interactionDistance *2, owner->instanceID->Get());
//RaycastResult result = owner->context->raycast->Cast(startPoint, endPoint, owner->instanceID->Get());
bool defaultInteraction = true;
bool attackInteraction = true;
if (result.collision != nullptr)
{
int TargetID = result.collision->instanceID->Get();
Debug::Log("Raycast result hit Object: " + std::to_string(TargetID));
// Retrieve all interactable components
auto interactables = result.collision->GetComponents<C_Interactable>();
// check if the collision is friendly if not we could default to attack
// we would want to check the quests here
std::list<S_Quests*>* listQuests = owner->context->listQuests;
std::list<S_Quests*>::iterator it;
for (it = listQuests->begin(); it != listQuests->end(); it++)
{
S_Quests* tempQuest = *it;
Debug::Log("On InteractWithObject - Quest Check " + tempQuest->sName);
if (tempQuest->OnInteration(owner->context->objects, result.collision, owner, owner->context->levelName, S_Quests::TALK))
{
defaultInteraction = false;
attackInteraction = false;
//m_inDialog = true;
break;
}
}
if (defaultInteraction) // nothing above called interaction so lets look at the default
{
// we will check for default interactions now
for (auto& interactable : interactables)
{
if (interactable->OnInteraction(owner))
{
attackInteraction = false;
break;
//m_inDialog = true;
}
}
}
// we could probably check map interactions too
//nothing else to do so attack
if (attackInteraction)
{
int hardcodedDamage = 10; // for now damage is hardcoded
auto targetHealth = result.collision->GetComponent<C_EnemyHealth>();
// do an attack - not quite sure how LOL
Debug::Log("Attack reached - Target: " + to_string(TargetID) );
animation->SetAnimationState(AnimationState::Slash);
if (targetHealth)
{
if (targetHealth->Damage(hardcodedDamage) == false)
{
// target is not dead
result.collision->transform->Pushback(heading);
//Debug::Log(" Attack Hit! Target now at " + to_string(targetHealth->Get()) + " total health");
}
else
{
//Debug::Log("C_InteractWithObjects: Attack Hit. Target Dead!");
//result.collision->QueueForRemoval(); // this removes them completely
}
}
}
}
else
{
animation->SetAnimationState(AnimationState::Slash);
/*else if (owner->context->input->IsKeyUp(Input::Key::SPACE))
{
animation->SetAnimationState(AnimationState::Idle);
}*/
}
}
if (animation->IsFinished())
{
animation->SetAnimationState(AnimationState::Idle);
}
}
// we skipped a frame - set this back to false
owner->context->currentScene->SetDialog(false);
}// no interaction if we have disabled player movement
}