-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathFollowActor.cpp
More file actions
67 lines (60 loc) · 1.49 KB
/
FollowActor.cpp
File metadata and controls
67 lines (60 loc) · 1.49 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
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "FollowActor.h"
#include "MeshComponent.h"
#include "Game.h"
#include "Renderer.h"
#include "FollowCamera.h"
#include "MoveComponent.h"
FollowActor::FollowActor(Game* game)
:Actor(game)
{
mMeshComp = new MeshComponent(this);
mMeshComp->SetMesh(game->GetRenderer()->GetMesh("Assets/RacingCar.gpmesh"));
SetPosition(Vector3(0.0f, 0.0f, -100.0f));
mMoveComp = new MoveComponent(this);
mCameraComp = new FollowCamera(this);
mCameraComp->SnapToIdeal();
}
void FollowActor::ActorInput(const uint8_t* keys)
{
float forwardSpeed = 0.0f;
float angularSpeed = 0.0f;
// wasd movement
if (keys[SDL_SCANCODE_W])
{
forwardSpeed += 400.0f;
}
if (keys[SDL_SCANCODE_S])
{
forwardSpeed -= 400.0f;
}
if (keys[SDL_SCANCODE_A])
{
angularSpeed -= Math::Pi;
}
if (keys[SDL_SCANCODE_D])
{
angularSpeed += Math::Pi;
}
mMoveComp->SetForwardSpeed(forwardSpeed);
mMoveComp->SetAngularSpeed(angularSpeed);
// Adjust horizontal distance of camera based on speed
if (!Math::NearZero(forwardSpeed))
{
mCameraComp->SetHorzDist(500.0f);
}
else
{
mCameraComp->SetHorzDist(350.0f);
}
}
void FollowActor::SetVisible(bool visible)
{
mMeshComp->SetVisible(visible);
}