-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathFPSActor.cpp
More file actions
129 lines (116 loc) · 3.29 KB
/
FPSActor.cpp
File metadata and controls
129 lines (116 loc) · 3.29 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
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
// ----------------------------------------------------------------
// 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 "FPSActor.h"
#include "MoveComponent.h"
#include "SDL/SDL_scancode.h"
#include "Renderer.h"
#include "AudioSystem.h"
#include "Game.h"
#include "AudioComponent.h"
#include "FPSCamera.h"
#include "MeshComponent.h"
FPSActor::FPSActor(Game* game)
:Actor(game)
{
mMoveComp = new MoveComponent(this);
mAudioComp = new AudioComponent(this);
mLastFootstep = 0.0f;
mFootstep = mAudioComp->PlayEvent("event:/Footstep");
mFootstep.SetPaused(true);
mCameraComp = new FPSCamera(this);
mFPSModel = new Actor(game);
mFPSModel->SetScale(0.75f);
mMeshComp = new MeshComponent(mFPSModel);
mMeshComp->SetMesh(game->GetRenderer()->GetMesh("Assets/Rifle.gpmesh"));
}
void FPSActor::UpdateActor(float deltaTime)
{
Actor::UpdateActor(deltaTime);
// Play the footstep if we're moving and haven't recently
mLastFootstep -= deltaTime;
if (!Math::NearZero(mMoveComp->GetForwardSpeed()) && mLastFootstep <= 0.0f)
{
mFootstep.SetPaused(false);
mFootstep.Restart();
mLastFootstep = 0.5f;
}
// Update position of FPS model relative to actor position
const Vector3 modelOffset(Vector3(10.0f, 10.0f, -10.0f));
Vector3 modelPos = GetPosition();
modelPos += GetForward() * modelOffset.x;
modelPos += GetRight() * modelOffset.y;
modelPos.z += modelOffset.z;
mFPSModel->SetPosition(modelPos);
// Initialize rotation to actor rotation
Quaternion q = GetRotation();
// Rotate by pitch from camera
q = Quaternion::Concatenate(q, Quaternion(GetRight(), mCameraComp->GetPitch()));
mFPSModel->SetRotation(q);
}
void FPSActor::ActorInput(const uint8_t* keys)
{
float forwardSpeed = 0.0f;
float strafeSpeed = 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])
{
strafeSpeed -= 400.0f;
}
if (keys[SDL_SCANCODE_D])
{
strafeSpeed += 400.0f;
}
mMoveComp->SetForwardSpeed(forwardSpeed);
mMoveComp->SetStrafeSpeed(strafeSpeed);
// Mouse movement
// Get relative movement from SDL
int x, y;
SDL_GetRelativeMouseState(&x, &y);
// Assume mouse movement is usually between -500 and +500
const int maxMouseSpeed = 500;
// Rotation/sec at maximum speed
const float maxAngularSpeed = Math::Pi * 8;
float angularSpeed = 0.0f;
if (x != 0)
{
// Convert to ~[-1.0, 1.0]
angularSpeed = static_cast<float>(x) / maxMouseSpeed;
// Multiply by rotation/sec
angularSpeed *= maxAngularSpeed;
}
mMoveComp->SetAngularSpeed(angularSpeed);
// Compute pitch
const float maxPitchSpeed = Math::Pi * 8;
float pitchSpeed = 0.0f;
if (y != 0)
{
// Convert to ~[-1.0, 1.0]
pitchSpeed = static_cast<float>(y) / maxMouseSpeed;
pitchSpeed *= maxPitchSpeed;
}
mCameraComp->SetPitchSpeed(pitchSpeed);
}
void FPSActor::SetFootstepSurface(float value)
{
// Pause here because the way I setup the parameter in FMOD
// changing it will play a footstep
mFootstep.SetPaused(true);
mFootstep.SetParameter("Surface", value);
}
void FPSActor::SetVisible(bool visible)
{
mMeshComp->SetVisible(visible);
}