博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 命令行模式(Command Pattern)
阅读量:20798 次
发布时间:2019-12-03

本文共 5706 字,大约阅读时间需要 19 分钟。

头文件相关:

// 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)
    {
        actor.Run();
    }
};

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

目录结构:

你可能感兴趣的文章
资料: vs2005IDE操作
查看>>
整理: 关于程序员的漫画
查看>>
goto and lable
查看>>
linux sh : get os info, gcc version, glibc version
查看>>
linux sh : check sh function is exist; multiple line comment;
查看>>
linux sh : get ini config file's section key's value
查看>>
alg : 3个量杯的问题1
查看>>
alg : 3个量杯的问题2
查看>>
alg : 3个量杯的问题3
查看>>
linux sh : print color text
查看>>
linux sh : print color text, show format string have space
查看>>
linux sh : 由未实现函数引起bash读取2进制附加数据引起的报错
查看>>
msdn原版纯c实现的截屏代码
查看>>
new指针后,地址相同
查看>>
由map.insert使用不当引起的内存泄漏
查看>>
用屏蔽法找bug的总结
查看>>
debug : linux C, 由rename API引起的内存被吃光
查看>>
使用一个Makefile, 动态,静态连接libpcap
查看>>
linux C : 子进程监听父进程使用的socket端口问题
查看>>
how_to_build_chromium_project_for_win
查看>>