Просмотр исходного кода

Added Exercise 7.2 camera code

Sanjay Madhav 8 лет назад
Родитель
Сommit
3a90a2faa9
2 измененных файлов с 113 добавлено и 0 удалено
  1. 84 0
      Exercises/7.2/CameraActor.cpp
  2. 29 0
      Exercises/7.2/CameraActor.h

+ 84 - 0
Exercises/7.2/CameraActor.cpp

@@ -0,0 +1,84 @@
+// ----------------------------------------------------------------
+// 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 "CameraActor.h"
+#include "MoveComponent.h"
+#include "SDL/SDL_scancode.h"
+#include "Renderer.h"
+#include "AudioSystem.h"
+#include "Game.h"
+#include "AudioComponent.h"
+#include "MeshComponent.h"
+
+CameraActor::CameraActor(Game* game)
+	:Actor(game)
+{
+	mMoveComp = new MoveComponent(this);
+	mAudioComp = new AudioComponent(this);
+	MeshComponent* mc = new MeshComponent(this);
+	mc->SetMesh(game->GetRenderer()->GetMesh("Assets/Sphere.gpmesh"));
+	mLastFootstep = 0.0f;
+	mFootstep = mAudioComp->PlayEvent("event:/Footstep");
+	mFootstep.SetPaused(true);
+}
+
+void CameraActor::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;
+	}
+
+	// Compute new camera from this actor
+	mCameraPos = GetPosition() - GetForward() * 200.0f + Vector3::UnitZ * 100.0f;
+	Vector3 target = GetPosition() + GetForward() * 100.0f;
+	Vector3 up = Vector3::UnitZ;
+	Matrix4 view = Matrix4::CreateLookAt(mCameraPos, target, up);
+	GetGame()->GetRenderer()->SetViewMatrix(view);
+	GetGame()->GetAudioSystem()->SetListener(view);
+}
+
+void CameraActor::ActorInput(const uint8_t* keys)
+{
+	float forwardSpeed = 0.0f;
+	float angularSpeed = 0.0f;
+	// wasd movement
+	if (keys[SDL_SCANCODE_W])
+	{
+		forwardSpeed += 300.0f;
+	}
+	if (keys[SDL_SCANCODE_S])
+	{
+		forwardSpeed -= 300.0f;
+	}
+	if (keys[SDL_SCANCODE_A])
+	{
+		angularSpeed -= Math::TwoPi;
+	}
+	if (keys[SDL_SCANCODE_D])
+	{
+		angularSpeed += Math::TwoPi;
+	}
+
+	mMoveComp->SetForwardSpeed(forwardSpeed);
+	mMoveComp->SetAngularSpeed(angularSpeed);
+}
+
+void CameraActor::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);
+}

+ 29 - 0
Exercises/7.2/CameraActor.h

@@ -0,0 +1,29 @@
+// ----------------------------------------------------------------
+// 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.
+// ----------------------------------------------------------------
+
+#pragma once
+#include "Actor.h"
+#include "SoundEvent.h"
+
+class CameraActor : public Actor
+{
+public:
+	CameraActor(class Game* game);
+
+	void UpdateActor(float deltaTime) override;
+	void ActorInput(const uint8_t* keys) override;
+
+	void SetFootstepSurface(float value);
+	const Vector3& GetCameraPosition() const { return mCameraPos; }
+private:
+	class MoveComponent* mMoveComp;
+	class AudioComponent* mAudioComp;
+	Vector3 mCameraPos;
+	SoundEvent mFootstep;
+	float mLastFootstep;
+};