[Add] Player and NPC now inherit Actor.

This commit is contained in:
Tamir Atias 2012-04-15 00:59:12 +03:00
parent 5ecbeede07
commit 6963a56c5c
9 changed files with 104 additions and 103 deletions

View File

@ -107,6 +107,7 @@
<ClInclude Include="..\..\Src\Sound\SoundEffect.h" /> <ClInclude Include="..\..\Src\Sound\SoundEffect.h" />
<ClInclude Include="..\..\src\Sprite\Sprite.h" /> <ClInclude Include="..\..\src\Sprite\Sprite.h" />
<ClInclude Include="..\..\src\System\Debug.h" /> <ClInclude Include="..\..\src\System\Debug.h" />
<ClInclude Include="..\..\src\System\FileReader.h" />
<ClInclude Include="..\..\src\System\ResourceManager.h" /> <ClInclude Include="..\..\src\System\ResourceManager.h" />
<ClInclude Include="..\..\src\Texture\Texture.h" /> <ClInclude Include="..\..\src\Texture\Texture.h" />
<ClInclude Include="..\..\src\TMXParser\base64.h" /> <ClInclude Include="..\..\src\TMXParser\base64.h" />
@ -146,6 +147,7 @@
<ClCompile Include="..\..\Src\Sound\SoundEffect.cpp" /> <ClCompile Include="..\..\Src\Sound\SoundEffect.cpp" />
<ClCompile Include="..\..\src\Sprite\Sprite.cpp" /> <ClCompile Include="..\..\src\Sprite\Sprite.cpp" />
<ClCompile Include="..\..\src\System\Debug.cpp" /> <ClCompile Include="..\..\src\System\Debug.cpp" />
<ClCompile Include="..\..\src\System\FileReader.cpp" />
<ClCompile Include="..\..\src\Texture\Texture.cpp" /> <ClCompile Include="..\..\src\Texture\Texture.cpp" />
<ClCompile Include="..\..\src\TMXParser\base64.cpp" /> <ClCompile Include="..\..\src\TMXParser\base64.cpp" />
<ClCompile Include="..\..\src\TMXParser\TmxImage.cpp" /> <ClCompile Include="..\..\src\TMXParser\TmxImage.cpp" />

View File

@ -168,6 +168,9 @@
<ClInclude Include="..\..\src\Font\Font.h"> <ClInclude Include="..\..\src\Font\Font.h">
<Filter>Font</Filter> <Filter>Font</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\System\FileReader.h">
<Filter>System</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\Main\main.cpp"> <ClCompile Include="..\..\src\Main\main.cpp">
@ -269,5 +272,8 @@
<ClCompile Include="..\..\src\Font\Font.cpp"> <ClCompile Include="..\..\src\Font\Font.cpp">
<Filter>Font</Filter> <Filter>Font</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\System\FileReader.cpp">
<Filter>System</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,19 +1,54 @@
#include "Actor.h" #include <stdlib.h>
Actor::Actor(void) : VELOCITY(10.0f) { #include "Actor.h"
#include "../Sound/SoundEffect.h"
Actor::Actor(void) {
_actor = new Sprite(); _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) { Actor::~Actor(void) {
for(int i = 0; i < 4; i++) {
if(_stepSFX[i]) {
sfxManager.Destroy(_stepSFX[i]);
_stepSFX[i] = NULL;
}
}
delete _actor; delete _actor;
} }
void Actor::LoadSprite(const char* filename, float w, float h) { void Actor::LoadSprite(const char* filename) {
//static const char _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) { void Actor::Render(void) {

View File

@ -4,14 +4,17 @@
#include "../Sprite/Sprite.h" #include "../Sprite/Sprite.h"
#include "../Math/Vec2.h" #include "../Math/Vec2.h"
class SoundEffect;
class Actor { class Actor {
public: public:
Actor(void); Actor(void);
~Actor(void); ~Actor(void);
void LoadSprite(const char* filename, float w, float h); void LoadSprite(const char* filename);
void Update(void);
void Render(void); virtual void Update(float dt);
virtual void Render(void);
float GetX(void) { return x; } float GetX(void) { return x; }
float GetY(void) { return y; } float GetY(void) { return y; }
@ -24,8 +27,9 @@ public:
void SetDirection(int direction) { _direction = direction; } void SetDirection(int direction) { _direction = direction; }
protected: protected:
const float VELOCITY; virtual void Move(float dt) = 0;
float _velocity;
int _direction; int _direction;
static const int ANIM_LEFT_FOOT = 0; static const int ANIM_LEFT_FOOT = 0;
@ -33,14 +37,17 @@ protected:
static const int ANIM_RIGHT_FOOT = 2; static const int ANIM_RIGHT_FOOT = 2;
static const int ANIM_ATTACK = 3; static const int ANIM_ATTACK = 3;
private: Sprite* _actor;
float x; float x;
float y; float y;
private:
float w; float w;
float h; float h;
Sprite* _actor;
SDL_Surface* texture;
Vec2 _spriteVector[4][4]; Vec2 _spriteVector[4][4];
SoundEffect* _stepSFX[4];
int _lastStepSFXPlayed;
}; };

View File

@ -1,17 +1,18 @@
#include "NPC.h" #include "NPC.h"
NPC::NPC(void) { NPC::NPC(void) : Actor() {
_NPC = new Sprite();
} }
NPC::~NPC(void) { NPC::~NPC(void) {
delete _NPC;
} }
void NPC::Update(void) { void NPC::Update(float dt) {
Actor::Update(dt);
} }
void NPC::Render(void) { void NPC::Render(void) {
_NPC->Draw(); Actor::Render();
}
void NPC::Move(float dt) {
} }

View File

@ -1,14 +1,15 @@
#pragma once #pragma once
#include "../Sprite/Sprite.h" #include "../Sprite/Sprite.h"
#include "../Actor/Actor.h"
class NPC { class NPC : public Actor {
public: public:
NPC(void); NPC(void);
~NPC(void); ~NPC(void);
void Update(void); void Update(float dt);
void Render(void); void Render(void);
private: private:
Sprite* _NPC; void Move(float dt);
}; };

View File

@ -1,82 +1,43 @@
#include <stdlib.h>
#include "Player.h" #include "Player.h"
#include "../Sound/SoundEffect.h" #include "../IO/Input.h"
Player::Player(void) { Player::Player(void) : Actor() {
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) { 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) { void Player::Update(float dt) {
// Process events here. Actor::Update(dt);
ProcessEvents(dt);
} }
void Player::Render(void) { void Player::Render(void) {
_player->SetRotation(_rotationAngle); Actor::Render();
_player->Draw();
} }
void Player::ProcessEvents(float dt) { void Player::Move(float dt) {
float oldX = x = _player->GetX();
float oldY = y = _player->GetY();
if(KeyStillDown(SDLK_w) || KeyStillDown(SDLK_UP)) { if(KeyStillDown(SDLK_w) || KeyStillDown(SDLK_UP)) {
y -= PLAYER_SPEED; y -= _velocity * 60 * dt;
_player->SetY(y); _actor->SetY(y);
} }
if(KeyStillDown(SDLK_s) || KeyStillDown(SDLK_DOWN)) { if(KeyStillDown(SDLK_s) || KeyStillDown(SDLK_DOWN)) {
y += PLAYER_SPEED; y += _velocity * 60 * dt;
_player->SetY(y); _actor->SetY(y);
} }
if(KeyStillDown(SDLK_a) || KeyStillDown(SDLK_LEFT)) { if(KeyStillDown(SDLK_a) || KeyStillDown(SDLK_LEFT)) {
x -= PLAYER_SPEED; x -= _velocity * 60 * dt;
_player->SetX(x); _actor->SetX(x);
} }
if(KeyStillDown(SDLK_d) || KeyStillDown(SDLK_RIGHT)) { if(KeyStillDown(SDLK_d) || KeyStillDown(SDLK_RIGHT)) {
x += PLAYER_SPEED; x += _velocity * 60 * dt;
_player->SetX(x); _actor->SetX(x);
} }
if(KeyDown(SDLK_LSHIFT)) { if(KeyDown(SDLK_LSHIFT)) {
// Run! // Run!
PLAYER_SPEED += 3; _velocity += 3;
Debug::logger->message("Speed: %f", PLAYER_SPEED * 16 * dt);
} }
if(KeyUp(SDLK_LSHIFT)) { if(KeyUp(SDLK_LSHIFT)) {
PLAYER_SPEED -= 3; _velocity -= 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;
} }
} }
}
int Player::GetWidth(void) { return _player->GetWidth(); }
int Player::GetHeight(void) { return _player->GetWidth(); }

View File

@ -1,14 +1,11 @@
#pragma once #pragma once
#include "../Actor/Actor.h"
#include "../Sprite/Sprite.h" #include "../Sprite/Sprite.h"
#include "../Global/Globals.h" #include "../Global/Globals.h"
#include "../System/Debug.h" #include "../System/Debug.h"
#include "../IO/Input.h" #include "../IO/Input.h"
class Sprite; class Player : public Actor{
class SoundEffect;
// We will derive from an Actor class at some point.
class Player {
public: public:
Player(void); Player(void);
~Player(void); ~Player(void);
@ -16,22 +13,6 @@ public:
void Update(float dt); void Update(float dt);
void Render(void); 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: private:
float x; void Move(float dt);
float y;
float PLAYER_SPEED;
Sprite* _player;
float _rotationAngle;
SoundEffect* _stepSFX[4];
int _lastStepSFXPlayed;
}; };

View File

@ -17,11 +17,17 @@
Game::Game(void) { Game::Game(void) {
_player = new Player(); _player = new Player();
//_NPC = new NPC(); _NPC = new NPC();
_level = new Level(); _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 = new Font();
_testFont->Load("../Data/Font/Fairydust.ttf"); _testFont->Load("../Data/Font/Fairydust.ttf");
_testFont->SetColor(0.0f, 1.0f, 1.0f, 1.0f);
} }
Game::~Game(void) { Game::~Game(void) {
@ -73,23 +79,24 @@ void Game::Render(void) {
// Render our shit.. // Render our shit..
_level->Draw(xOffset, yOffset); _level->Draw(xOffset, yOffset);
_player->Render(); _player->Render();
_testFont->SetColor(0.0f, 1.0f, 1.0f, 1.0f); _NPC->Render();
_testFont->DrawText( _testFont->DrawText(
_player->GetX() - 5, _player->GetX() - 5,
_player->GetY() - _testFont->GetLineSkip() - 2, _player->GetY() - _testFont->GetLineSkip() - 2,
"Miss D"); "Miss D");
//_NPC->Render();
} }
void Game::Shutdown(void) { void Game::Shutdown(void) {
Debug::logger->message("\n ----- Cleaning Engine -----"); Debug::logger->message("\n ----- Cleaning Engine -----");
delete _testFont; delete _testFont;
delete _NPC;
delete _player; delete _player;
delete _level; delete _level;
} }
void Game::ProcessEvents(float dt) { void Game::ProcessEvents(float dt) {
_player->ProcessEvents(dt); _player->Update(dt);
_NPC->Update(dt);
} }
void Game::OnResize(int width, int height) { void Game::OnResize(int width, int height) {