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

View File

@ -168,6 +168,9 @@
<ClInclude Include="..\..\src\Font\Font.h">
<Filter>Font</Filter>
</ClInclude>
<ClInclude Include="..\..\src\System\FileReader.h">
<Filter>System</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Main\main.cpp">
@ -269,5 +272,8 @@
<ClCompile Include="..\..\src\Font\Font.cpp">
<Filter>Font</Filter>
</ClCompile>
<ClCompile Include="..\..\src\System\FileReader.cpp">
<Filter>System</Filter>
</ClCompile>
</ItemGroup>
</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();
_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) {

View File

@ -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;
};

View File

@ -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) {
}

View File

@ -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);
};

View File

@ -1,82 +1,43 @@
#include <stdlib.h>
#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(); }

View File

@ -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);
};

View File

@ -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) {