From 6963a56c5cd9a315e02541d27a4696530dd0d2ee Mon Sep 17 00:00:00 2001 From: Tamir Atias Date: Sun, 15 Apr 2012 00:59:12 +0300 Subject: [PATCH] [Add] Player and NPC now inherit Actor. --- Bin/VC10/VC10.vcxproj | 2 + Bin/VC10/VC10.vcxproj.filters | 6 +++ src/Actor/Actor.cpp | 45 +++++++++++++++++++--- src/Actor/Actor.h | 21 +++++++---- src/Actor/NPC.cpp | 13 ++++--- src/Actor/NPC.h | 7 ++-- src/Actor/Player.cpp | 71 ++++++++--------------------------- src/Actor/Player.h | 25 ++---------- src/Main/Game.cpp | 17 ++++++--- 9 files changed, 104 insertions(+), 103 deletions(-) diff --git a/Bin/VC10/VC10.vcxproj b/Bin/VC10/VC10.vcxproj index e81bbd1..7223d26 100644 --- a/Bin/VC10/VC10.vcxproj +++ b/Bin/VC10/VC10.vcxproj @@ -107,6 +107,7 @@ + @@ -146,6 +147,7 @@ + diff --git a/Bin/VC10/VC10.vcxproj.filters b/Bin/VC10/VC10.vcxproj.filters index aaab8eb..629b8d9 100644 --- a/Bin/VC10/VC10.vcxproj.filters +++ b/Bin/VC10/VC10.vcxproj.filters @@ -168,6 +168,9 @@ Font + + System + @@ -269,5 +272,8 @@ Font + + System + \ No newline at end of file diff --git a/src/Actor/Actor.cpp b/src/Actor/Actor.cpp index 4f84f46..45f563c 100644 --- a/src/Actor/Actor.cpp +++ b/src/Actor/Actor.cpp @@ -1,19 +1,54 @@ -#include "Actor.h" +#include -Actor::Actor(void) : VELOCITY(10.0f) { +#include "Actor.h" +#include "../Sound/SoundEffect.h" + +Actor::Actor(void) { _actor = new Sprite(); + + _stepSFX[0] = sfxManager.Load("../Data/SFX/step_cloth1.wav"); + _stepSFX[1] = sfxManager.Load("../Data/SFX/step_cloth2.wav"); + _stepSFX[2] = sfxManager.Load("../Data/SFX/step_cloth3.wav"); + _stepSFX[3] = sfxManager.Load("../Data/SFX/step_cloth4.wav"); + _lastStepSFXPlayed = -1; + + _velocity = 4.0f; } Actor::~Actor(void) { + for(int i = 0; i < 4; i++) { + if(_stepSFX[i]) { + sfxManager.Destroy(_stepSFX[i]); + _stepSFX[i] = NULL; + } + } delete _actor; } -void Actor::LoadSprite(const char* filename, float w, float h) { - //static const char +void Actor::LoadSprite(const char* filename) { + _actor->LoadSprite(filename); + w = _actor->GetWidth(); + h = _actor->GetHeight(); } -void Actor::Update(void) { +void Actor::Update(float dt) { + float oldX = x = _actor->GetX(); + float oldY = y = _actor->GetY(); + Move(dt); + + if(x != oldX || y != oldY) { + if(!SoundEffect::IsPlaying(1)) { + int sfxIndex; + do { + sfxIndex = rand() % 4; + } while(sfxIndex == _lastStepSFXPlayed); + + SoundEffect::Play(_stepSFX[sfxIndex], 1, 0); + + _lastStepSFXPlayed = sfxIndex; + } + } } void Actor::Render(void) { diff --git a/src/Actor/Actor.h b/src/Actor/Actor.h index 2e29a40..53b4b22 100644 --- a/src/Actor/Actor.h +++ b/src/Actor/Actor.h @@ -4,14 +4,17 @@ #include "../Sprite/Sprite.h" #include "../Math/Vec2.h" +class SoundEffect; + class Actor { public: Actor(void); ~Actor(void); - void LoadSprite(const char* filename, float w, float h); - void Update(void); - void Render(void); + void LoadSprite(const char* filename); + + virtual void Update(float dt); + virtual void Render(void); float GetX(void) { return x; } float GetY(void) { return y; } @@ -24,8 +27,9 @@ public: void SetDirection(int direction) { _direction = direction; } protected: - const float VELOCITY; + virtual void Move(float dt) = 0; + float _velocity; int _direction; static const int ANIM_LEFT_FOOT = 0; @@ -33,14 +37,17 @@ protected: static const int ANIM_RIGHT_FOOT = 2; static const int ANIM_ATTACK = 3; -private: + Sprite* _actor; + float x; float y; + +private: float w; float h; - Sprite* _actor; - SDL_Surface* texture; Vec2 _spriteVector[4][4]; + SoundEffect* _stepSFX[4]; + int _lastStepSFXPlayed; }; diff --git a/src/Actor/NPC.cpp b/src/Actor/NPC.cpp index f2b2ed1..c344ab1 100644 --- a/src/Actor/NPC.cpp +++ b/src/Actor/NPC.cpp @@ -1,17 +1,18 @@ #include "NPC.h" -NPC::NPC(void) { - _NPC = new Sprite(); +NPC::NPC(void) : Actor() { } NPC::~NPC(void) { - delete _NPC; } -void NPC::Update(void) { - +void NPC::Update(float dt) { + Actor::Update(dt); } void NPC::Render(void) { - _NPC->Draw(); + Actor::Render(); +} + +void NPC::Move(float dt) { } diff --git a/src/Actor/NPC.h b/src/Actor/NPC.h index f283294..3f96100 100644 --- a/src/Actor/NPC.h +++ b/src/Actor/NPC.h @@ -1,14 +1,15 @@ #pragma once #include "../Sprite/Sprite.h" +#include "../Actor/Actor.h" -class NPC { +class NPC : public Actor { public: NPC(void); ~NPC(void); - void Update(void); + void Update(float dt); void Render(void); private: - Sprite* _NPC; + void Move(float dt); }; diff --git a/src/Actor/Player.cpp b/src/Actor/Player.cpp index 022c2ca..9d69817 100644 --- a/src/Actor/Player.cpp +++ b/src/Actor/Player.cpp @@ -1,82 +1,43 @@ -#include - #include "Player.h" -#include "../Sound/SoundEffect.h" +#include "../IO/Input.h" -Player::Player(void) { - PLAYER_SPEED = 5; - _rotationAngle = 0.0f; - _player = new Sprite(); - _player->LoadSprite("../Data/Img/Player.png"); - - _stepSFX[0] = sfxManager.Load("../Data/SFX/step_cloth1.wav"); - _stepSFX[1] = sfxManager.Load("../Data/SFX/step_cloth2.wav"); - _stepSFX[2] = sfxManager.Load("../Data/SFX/step_cloth3.wav"); - _stepSFX[3] = sfxManager.Load("../Data/SFX/step_cloth4.wav"); - _lastStepSFXPlayed = -1; +Player::Player(void) : Actor() { } Player::~Player(void) { - for(int i = 0; i < 4; i++) { - if(_stepSFX[i]) { - sfxManager.Destroy(_stepSFX[i]); - _stepSFX[i] = NULL; - } - } - delete _player; } void Player::Update(float dt) { - // Process events here. - ProcessEvents(dt); + Actor::Update(dt); } void Player::Render(void) { - _player->SetRotation(_rotationAngle); - _player->Draw(); + Actor::Render(); } -void Player::ProcessEvents(float dt) { - float oldX = x = _player->GetX(); - float oldY = y = _player->GetY(); +void Player::Move(float dt) { if(KeyStillDown(SDLK_w) || KeyStillDown(SDLK_UP)) { - y -= PLAYER_SPEED; - _player->SetY(y); + y -= _velocity * 60 * dt; + _actor->SetY(y); } if(KeyStillDown(SDLK_s) || KeyStillDown(SDLK_DOWN)) { - y += PLAYER_SPEED; - _player->SetY(y); + y += _velocity * 60 * dt; + _actor->SetY(y); } if(KeyStillDown(SDLK_a) || KeyStillDown(SDLK_LEFT)) { - x -= PLAYER_SPEED; - _player->SetX(x); + x -= _velocity * 60 * dt; + _actor->SetX(x); } if(KeyStillDown(SDLK_d) || KeyStillDown(SDLK_RIGHT)) { - x += PLAYER_SPEED; - _player->SetX(x); + x += _velocity * 60 * dt; + _actor->SetX(x); } + if(KeyDown(SDLK_LSHIFT)) { // Run! - PLAYER_SPEED += 3; - Debug::logger->message("Speed: %f", PLAYER_SPEED * 16 * dt); + _velocity += 3; } if(KeyUp(SDLK_LSHIFT)) { - PLAYER_SPEED -= 3; - Debug::logger->message("Speed: %f", PLAYER_SPEED * 16 * dt); - } - if(x != oldX || y != oldY) { - if(!SoundEffect::IsPlaying(1)) { - int sfxIndex; - do { - sfxIndex = rand() % 4; - } while(sfxIndex == _lastStepSFXPlayed); - - SoundEffect::Play(_stepSFX[sfxIndex], 1, 0); - - _lastStepSFXPlayed = sfxIndex; - } + _velocity -= 3; } } - -int Player::GetWidth(void) { return _player->GetWidth(); } -int Player::GetHeight(void) { return _player->GetWidth(); } diff --git a/src/Actor/Player.h b/src/Actor/Player.h index 55b8e3c..3a4e67c 100644 --- a/src/Actor/Player.h +++ b/src/Actor/Player.h @@ -1,14 +1,11 @@ #pragma once +#include "../Actor/Actor.h" #include "../Sprite/Sprite.h" #include "../Global/Globals.h" #include "../System/Debug.h" #include "../IO/Input.h" -class Sprite; -class SoundEffect; - -// We will derive from an Actor class at some point. -class Player { +class Player : public Actor{ public: Player(void); ~Player(void); @@ -16,22 +13,6 @@ public: void Update(float dt); void Render(void); - // --- Collision stuff. - - void ProcessEvents(float dt); - - int GetX(void) { return x; } - int GetY(void) { return y; } - int GetWidth(void); - int GetHeight(void); - private: - float x; - float y; - float PLAYER_SPEED; - Sprite* _player; - float _rotationAngle; - - SoundEffect* _stepSFX[4]; - int _lastStepSFXPlayed; + void Move(float dt); }; diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp index df2b1a0..1a40b67 100644 --- a/src/Main/Game.cpp +++ b/src/Main/Game.cpp @@ -17,11 +17,17 @@ Game::Game(void) { _player = new Player(); - //_NPC = new NPC(); + _NPC = new NPC(); _level = new Level(); - //_rotationAngle = 0.0f; + + _player->LoadSprite("../Data/Img/Player.png"); + _NPC->LoadSprite("../Data/Img/Player.png"); + + _NPC->SetXY(30.0f, 30.0f); + _testFont = new Font(); _testFont->Load("../Data/Font/Fairydust.ttf"); + _testFont->SetColor(0.0f, 1.0f, 1.0f, 1.0f); } Game::~Game(void) { @@ -73,23 +79,24 @@ void Game::Render(void) { // Render our shit.. _level->Draw(xOffset, yOffset); _player->Render(); - _testFont->SetColor(0.0f, 1.0f, 1.0f, 1.0f); + _NPC->Render(); _testFont->DrawText( _player->GetX() - 5, _player->GetY() - _testFont->GetLineSkip() - 2, "Miss D"); - //_NPC->Render(); } void Game::Shutdown(void) { Debug::logger->message("\n ----- Cleaning Engine -----"); delete _testFont; + delete _NPC; delete _player; delete _level; } void Game::ProcessEvents(float dt) { - _player->ProcessEvents(dt); + _player->Update(dt); + _NPC->Update(dt); } void Game::OnResize(int width, int height) {