博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 命令行模式(Command Pattern)
阅读量:20799 次
发布时间: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;
}

目录结构:

你可能感兴趣的文章
composer安装YII
查看>>
Sublime text3快捷键演示
查看>>
sublime text3 快捷键修改
查看>>
关于PHP几点建议
查看>>
硬盘的接口、协议
查看>>
VLAN与子网划分区别
查看>>
Cisco Packet Tracer教程
查看>>
02. 交换机的基本配置和管理
查看>>
03. 交换机的Telnet远程登陆配置
查看>>
微信小程序-调用-腾讯视频-解决方案
查看>>
phpStudy安装yaf扩展
查看>>
密码 加密 加盐 常用操作记录
查看>>
TP 分页后,调用指定页。
查看>>
Oracle数据库中的(+)连接
查看>>
java-oracle中几十个实用的PL/SQL
查看>>
PLSQL常用方法汇总
查看>>
几个基本的 Sql Plus 命令 和 例子
查看>>
PLSQL单行函数和组函数详解
查看>>
Oracle PL/SQL语言初级教程之异常处理
查看>>
Oracle PL/SQL语言初级教程之游标
查看>>