头文件相关:
// AActor.h
//TODO 事实上,可以把AActor做成抽象类的,把具体的实现下发到子类中去,把AActor作为一个接口,个人只是为了练习,也就随便写写了
class AActor
{
public:
AActor();
~AActor();
virtual bool Jump();
virtual bool Run();
virtual bool Walk();
virtual bool Attack();
virtual bool Idle();
};
//Attack.h
#include"Command.h"
class AttackCommand :public Command
{
public:
virtual void Execute(AActor& actor)
{
actor.Attack();
}
};
//BaseLibrary.h
#include"Command.h"
class AttackCommand :public Command
{
public:
virtual void Execute(AActor& actor)
{
actor.Attack();
}
};
// Command.h
#include "Actor.h"
class Command
{
public :
virtual ~Command() {};
virtual void Execute(AActor& actor) = 0;
};
//IdleCommand.h
#include"Command.h"
class IdleCommand :public Command
{
public:
virtual void Execute(AActor& actor)
{
actor.Idle();
}
};
//InputManager.h
#include "Command.h"
class KeyPress;
class InputManager
{
public:
Command* HandleInput();
static InputManager* GetInputManager();
static KeyPress* GetKeyPress();
private:
InputManager();
InputManager(const InputManager&);
InputManager& operator=(const InputManager&);
~InputManager();
Command* JumpButton;
Command* WalkButton;
Command* IdleButton;
Command* RunButton;
Command* AttackButon;
static KeyPress* mKeyPress;
static InputManager* mInputManager;
};
//JumpCommand.h
#include"Command.h"
class JumpCommand :public Command
{
public :
virtual void Execute(AActor& actor)
{
actor.Jump();
}
};
//KeyPress.h
class KeyPress
{
public:
static bool JumpPressed;
static bool WalkPressed;
static bool RunPressed;
static bool IdlePressed;
static bool AttackPressed;
void SetJumpPressed(bool press);
void SetWalkPressed(bool press);
void SetRunPressed(bool press);
void SetIdlePressed(bool press);
void SetAttackPressed(bool press);
bool GetJumpPressed();
bool GetWalkPressed();
bool GetRunPressed();
bool GetIdlePressed();
bool GetAttackPressed();
bool IsPress(bool isKeyPressed);
};
//RunCommand.h
#include"Command.h"
class RunCommand :public Command
{
public:
virtual void Execute(AActor& actor)
{
www.nigeriaembassy.cn();
}
};
//WalkCommand.h
#include"Command.h"
class WalkCommand :public Command
{
public:
virtual void Execute(AActor& actor)
{
actor.Walk();
}
};
cpp文件相关:
//Actor.cpp
#include "Actor.h"
#include "BaseLibrary.h"
using namespace std;
AActor::AActor()
{
}
AActor::~AActor()
{
}
bool AActor::Jump()
{
cout << "It Is Basic Jump Funtion Runnging!" << endl;
return true;
}
bool AActor::Run()
{
cout << "It Is Basic Run Funtion Runnging!" << endl;
return true;
}
bool AActor::Walk()
{
cout << "It Is Basic Walk Funtion Runnging!" << endl;
return true;
}
bool AActor::Attack()
{
cout << "It Is Basic Attack Funtion Runnging!" << endl;
return true;
}
bool AActor::Idle()
{
cout << "It Is Basic Idle Funtion Runnging!" << endl;
return true;
}
//InputManager.cpp
#include "InputManager.h"
#include "KeyPress.h"
#include "IdleCommand.h"
#include "JumpCommand.h"
#include "RunCommand.h"
#include "WalkCommand.h"
#include "Attack.h"
InputManager* InputManager::mInputManager = new InputManager();
KeyPress* InputManager::mKeyPress = new KeyPress();
InputManager* InputManager::GetInputManager()
{
return mInputManager;
}
KeyPress* InputManager::GetKeyPress()
{
return mKeyPress;
}
InputManager::InputManager(const InputManager& input)
{
}
InputManager& InputManager::operator=(const InputManager& input)
{
return *mInputManager;
}
Command* InputManager::HandleInput()
{
if (mKeyPress->JumpPressed)
return JumpButton;
if (mKeyPress->AttackPressed)
return AttackButon;
if (mKeyPress->RunPressed)
return RunButton;
if (mKeyPress->WalkPressed)
return WalkButton;
if (mKeyPress->IdlePressed)
return IdleButton;
return nullptr;
}
InputManager::InputManager()
{
JumpButton = new JumpCommand();
WalkButton = new WalkCommand();
RunButton = new RunCommand();
AttackButon = new AttackCommand();
IdleButton = new IdleCommand();
}
InputManager::~InputManager()
{
if (mKeyPress != nullptr)
delete mKeyPress;
if (JumpButton != nullptr)
delete JumpButton;
if (WalkButton != nullptr)
delete WalkButton;
if (RunButton != nullptr)
delete RunButton;
if (IdleButton != nullptr)
delete AttackButon;
if (AttackButon != nullptr)
delete AttackButon;
mKeyPress = nullptr;
JumpButton = nullptr;
WalkButton = nullptr;
RunButton = nullptr;
IdleButton = nullptr;
AttackButon = nullptr;
}
//KeyPress.cpp
#include "KeyPress.h"
bool KeyPress::JumpPressed = false;
bool KeyPress::WalkPressed=false;
bool KeyPress::RunPressed = false;
bool KeyPress::IdlePressed=false;
bool KeyPress::AttackPressed=false;
bool KeyPress::IsPress(bool isKeyPressed)
{
if (isKeyPressed)
return true;
else
return false;
}
void KeyPress::SetJumpPressed(bool press)
{
JumpPressed = press;
}
void KeyPress::SetWalkPressed(bool press)
{
WalkPressed = press;
}
void KeyPress::SetRunPressed(bool press)
{
RunPressed = press;
}
void KeyPress::SetIdlePressed(bool press)
{
IdlePressed = press;
}
void KeyPress::SetAttackPressed(bool press)
{
AttackPressed = press;
}
bool KeyPress::GetJumpPressed()
{
return JumpPressed;
}
bool KeyPress::GetWalkPressed()
{
return WalkPressed;
}
bool KeyPress::GetRunPressed()
{
return RunPressed;
}
bool KeyPress::GetIdlePressed()
{
return IdlePressed;
}
bool KeyPress::GetAttackPressed()
{
return AttackPressed;
}
//Test.cpp
#include "BaseLibrary.h"
#include "InputManager.h"
#include "Command.h"
#include "Actor.h"
#include "KeyPress.h"
using namespace std;
int main()
{
AActor actor;
InputManager::GetKeyPress()->SetAttackPressed(true);
Command* cd = InputManager::GetInputManager()->HandleInput();
if (cd)
cd->Execute(actor);
InputManager::GetKeyPress()->SetIdlePressed(true);
InputManager::GetKeyPress()->SetAttackPressed(false);
cd = InputManager::GetInputManager()->HandleInput();
if (cd)
cd->Execute(actor);
system("pause");
return 0;
}
目录结构: