|
| 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 | +} |
0 commit comments