Skip to content

Commit 3a90a2f

Browse files
committed
Added Exercise 7.2 camera code
1 parent 2809ff2 commit 3a90a2f

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

Exercises/7.2/CameraActor.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
9+
#include "CameraActor.h"
10+
#include "MoveComponent.h"
11+
#include "SDL/SDL_scancode.h"
12+
#include "Renderer.h"
13+
#include "AudioSystem.h"
14+
#include "Game.h"
15+
#include "AudioComponent.h"
16+
#include "MeshComponent.h"
17+
18+
CameraActor::CameraActor(Game* game)
19+
:Actor(game)
20+
{
21+
mMoveComp = new MoveComponent(this);
22+
mAudioComp = new AudioComponent(this);
23+
MeshComponent* mc = new MeshComponent(this);
24+
mc->SetMesh(game->GetRenderer()->GetMesh("Assets/Sphere.gpmesh"));
25+
mLastFootstep = 0.0f;
26+
mFootstep = mAudioComp->PlayEvent("event:/Footstep");
27+
mFootstep.SetPaused(true);
28+
}
29+
30+
void CameraActor::UpdateActor(float deltaTime)
31+
{
32+
Actor::UpdateActor(deltaTime);
33+
34+
// Play the footstep if we're moving and haven't recently
35+
mLastFootstep -= deltaTime;
36+
if (!Math::NearZero(mMoveComp->GetForwardSpeed()) && mLastFootstep <= 0.0f)
37+
{
38+
mFootstep.SetPaused(false);
39+
mFootstep.Restart();
40+
mLastFootstep = 0.5f;
41+
}
42+
43+
// Compute new camera from this actor
44+
mCameraPos = GetPosition() - GetForward() * 200.0f + Vector3::UnitZ * 100.0f;
45+
Vector3 target = GetPosition() + GetForward() * 100.0f;
46+
Vector3 up = Vector3::UnitZ;
47+
Matrix4 view = Matrix4::CreateLookAt(mCameraPos, target, up);
48+
GetGame()->GetRenderer()->SetViewMatrix(view);
49+
GetGame()->GetAudioSystem()->SetListener(view);
50+
}
51+
52+
void CameraActor::ActorInput(const uint8_t* keys)
53+
{
54+
float forwardSpeed = 0.0f;
55+
float angularSpeed = 0.0f;
56+
// wasd movement
57+
if (keys[SDL_SCANCODE_W])
58+
{
59+
forwardSpeed += 300.0f;
60+
}
61+
if (keys[SDL_SCANCODE_S])
62+
{
63+
forwardSpeed -= 300.0f;
64+
}
65+
if (keys[SDL_SCANCODE_A])
66+
{
67+
angularSpeed -= Math::TwoPi;
68+
}
69+
if (keys[SDL_SCANCODE_D])
70+
{
71+
angularSpeed += Math::TwoPi;
72+
}
73+
74+
mMoveComp->SetForwardSpeed(forwardSpeed);
75+
mMoveComp->SetAngularSpeed(angularSpeed);
76+
}
77+
78+
void CameraActor::SetFootstepSurface(float value)
79+
{
80+
// Pause here because the way I setup the parameter in FMOD
81+
// changing it will play a footstep
82+
mFootstep.SetPaused(true);
83+
mFootstep.SetParameter("Surface", value);
84+
}

Exercises/7.2/CameraActor.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
9+
#pragma once
10+
#include "Actor.h"
11+
#include "SoundEvent.h"
12+
13+
class CameraActor : public Actor
14+
{
15+
public:
16+
CameraActor(class Game* game);
17+
18+
void UpdateActor(float deltaTime) override;
19+
void ActorInput(const uint8_t* keys) override;
20+
21+
void SetFootstepSurface(float value);
22+
const Vector3& GetCameraPosition() const { return mCameraPos; }
23+
private:
24+
class MoveComponent* mMoveComp;
25+
class AudioComponent* mAudioComp;
26+
Vector3 mCameraPos;
27+
SoundEvent mFootstep;
28+
float mLastFootstep;
29+
};

0 commit comments

Comments
 (0)