当前位置:网站首页>1 Introduction to command mode
1 Introduction to command mode
2022-07-28 20:55:00 【W.C.Zeng】
Game Programming Patterns The book introduces common design patterns
Command mode
Reference reading 1
Reference reading 2
Reference definition
Encapsulate a request as an object, thereby letting users parameterize clients with different requests, queue or log requests, and support undoable operations.
Command mode is in our normal use Press the button – Trigger character movement In the middle of the logic ,( Use a command class ) Add a parsing layer , Handle the control logic of different keys , It can also conveniently realize the cancellation function of commands
Take the command line program as an example , Simulate the situation of key control character movement
Command class Command I'll add later
Analog character controller class PlayerController
Program Basic logic : Read user input 、 Print information
PlayerController Member method of : Handle the execution or revocation of the input to the command Input 、 Parse command GetCommand 、 Operation contents of specific commands Fire MoveX etc.
// command Command mode
// Commands are methods wrapped in objects , Simulate closures in languages without closures
// http://gameprogrammingpatterns.com/command.html
using System;
using System.Threading;
using System.Collections.Generic;
namespace command
{
class Program
{
public static bool gameEnd = false;
static void Main()
{
PlayerController player = new PlayerController();
while (true)
{
// simulation fixupdate 0.02 Seconds to update
Thread.Sleep(2);
player.Input();
Console.WriteLine("----pos: " + player.pos[0].ToString() + " , " + player.pos[1].ToString() + " mono: " + player.mono);
if (gameEnd)
{
break;
}
}
}
}
public class PlayerController
{
public int[] pos = {
0, 0 };
public int mono = 10;
Stack<Command> cmdHistory = new Stack<Command>();
// Command mode Eliminates the coupling of direct method calls , Pass the data that needs operation to other classes
public void Input()
{
ConsoleKeyInfo key = Console.ReadKey();
Command? cmd = GetCommand(key.Key);
// If there's an order
if (cmd != null)
{
// Directly call the corresponding command execution method in the command class
cmd.execute();
// Revocable 2 Time
if (cmdHistory.Count < 2)
{
cmdHistory.Push(cmd);
}
}
}
public Command? GetCommand(ConsoleKey key)
{
if (key == ConsoleKey.A)
{
// Generate and initialize a move command
return new MoveXCommand(this, -1);
}
else if (key == ConsoleKey.D)
{
return new MoveXCommand(this, 1);
}
else if (key == ConsoleKey.Spacebar)
{
// Call directly
// Fire();
// Return the firing command class derived from the base class of the command
return new FireCommand(this, 3);
}
else if (key == ConsoleKey.Q)
{
return new ExitCommand();
}
else if (key == ConsoleKey.Z)
{
if (cmdHistory.Count > 0)
{
cmdHistory.Pop().undo();
}
return null;
}
else
{
return null;
}
}
public void MoveTo(int x, int y)
{
pos[0] = x;
pos[1] = y;
}
public void Fire(int amount)
{
mono -= amount;
}
}
}
Command base class : The execution command is declared 、 Virtual method of revoking an order
Command class : Declared the move 、 FireStarter 、 Exit command
namespace command
{
public class Command
{
public virtual void execute() {
}
public virtual void undo() {
}
}
class MoveXCommand : Command
{
PlayerController player;
int beforeX = 0;
int beforeY = 0;
int dx = 0;
public MoveXCommand(PlayerController player, int x)
{
this.player = player;
this.dx = x * 3;
}
public override void execute()
{
base.execute();
beforeX = player.pos[0];
beforeY = player.pos[1];
player.MoveTo(player.pos[0] + dx, player.pos[1]);
}
// Undo move
public override void undo()
{
base.undo();
player.MoveTo(beforeX, beforeY);
}
}
class FireCommand : Command
{
PlayerController player;
int useNum;
public FireCommand(PlayerController player, int useNum)
{
this.player = player;
this.useNum = useNum;
}
public override void execute()
{
base.execute();
player.Fire(useNum);
}
}
class ExitCommand : Command
{
public override void execute()
{
base.execute();
Program.gameEnd = true;
}
}
}
After a middle layer of processing , We isolate the player's input from the command to be executed .
边栏推荐
猜你喜欢

《软件设计师考试》易混淆知识点

Prize essay solicitation | 2022 cloud native programming challenge draft activity opens

Introduction to redis I: redis practical reading notes

"When you are no longer a programmer, many things will get out of control" -- talk to SUSE CTO, the world's largest independent open source company

“当你不再是程序员,很多事会脱离掌控”—— 对话全球最大独立开源公司SUSE CTO...

远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作

The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution

js图表散点图例子

作业 ce

【服务器数据恢复】HP StorageWorks系列存储RAID5两块盘故障离线的数据恢复案例
随机推荐
Teach you unity scene switching progress bar production hand in hand
What is "security"? Volvo tells you with its unique understanding and action
Explain the life cycle function in unity in detail
Dynamic planning: code summary of knapsack problem template
如何用Redis实现事物以及锁?
C # basic 1-events and commissions
GIS数据漫谈(六)— 投影坐标系统
Redis入门一:Redis实战读书笔记
Mongoose condition queries the data of a certain time period
云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
研发效能的思考总结
System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
Explain the imported 3D model in unity
Integrating database Ecology: using eventbridge to build CDC applications
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
既要便捷、安全+智能,也要颜值,萤石发布北斗星人脸锁DL30F和极光人脸视频锁Y3000FV
How can enterprises successfully complete cloud migration?
微信小程序的分包加载
Space shooting Lesson 11: sound and music
3D激光SLAM:LeGO-LOAM论文解读---简介部分