-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavComponent.cpp
More file actions
40 lines (32 loc) · 821 Bytes
/
NavComponent.cpp
File metadata and controls
40 lines (32 loc) · 821 Bytes
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
#include "NavComponent.h"
#include "Math.h"
#include "Actor.h"
NavComponent::NavComponent(Actor* owner) : MoveComponent(owner)
{}
void NavComponent::Update(float deltaTime)
{
//
Vector2 diff = mOwner->GetPosition() - mNextPoint;
if (diff.Length() <= 2.0f)
{
mNextPoint = GetNextPoint();
TurnTo(mNextPoint);
}
// This moves the actor forward
MoveComponent::Update(deltaTime);
}
void NavComponent::TurnTo(const Vector2& pos)
{
// vector from me to pos
Vector2 dir = pos - mOwner->GetPosition();
// New angle is just atan2 of this dir
// negate y because +y is down on screen
float angle = Math::Atan2(-dir.y, dir.x);
mOwner->SetRotation(angle);
}
Vector2 NavComponent::GetNextPoint()
{
// This is not the correct implementation.
// This is simply a placeholder
return mOwner->GetPosition();
}