用23种设计模式打造一个cocos creator的游戏框架----(十二)状态模式

1、模式标准

模式名称:状态模式

模式分类:行为型

模式意图:允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。

结构图:

适用于:

1、一个对象的行为决定于它的状态,并且它必须在运行时刻根据状态改变它的行为。

2、一个操作中含有庞大的多分支的条件语句,且这些分支依赖丁该对象的状态。这个状态常用一个或多个枚举常量表示。通常,有多个操作包含这一相同的条件结构。State模式将每一个条件分支放入一个独立的类中。这使得开发者可以根据对象自身的情况将对象的状态作为一个对象,这一对象可以不依赖于其他对象独立变化。

主要成员:

  • 上下文(Context):它定义了客户端感兴趣的接口,并且维护一个指向当前状态的实例变量。
  • 状态抽象(State):这是一个接口或者抽象类,它定义了每个状态必须实现的方法。
  • 具体状态(Concrete States):它们是实现状态接口的类,每个类对应一种状态,且包含了该状态下的行为实现。

2、分析与设计  

在一般的游戏开发中状态值通常是一个枚举值,但在状态模式中,状态值是一个通过实现了 IUnitState 接口的对象表示的。这种方法的优点是它更加灵活和强大,因为这个状态值不仅仅是一个值,它还是一组行为的集合(即方法实现)。这允许您在不同的状态之间切换行为,而不是仅仅改变一个表示状态的值。

在游戏中的单位一般有以下几种状态:站立,移动,攻击,释放技能中,眩晕中,死亡。比较常见的是单位正在释放一个技能,这个时候一个飞锤飞过来,将他击晕了,他停止了技能的释放。

接下来我们修改一下我们的意图

意图:允许一个对象(单位)在其内部状态改变时(由其状态对象来)改变它的行为。对象看起来似乎修改了它的类(实际是状态对象干的)。

3、开始打造

export enum UnitStateType {Standing,Moving,Attacking,CastSkilling,Stuning,Die
}
export interface IUnitState {enterState(unitItem: IUnitItem): void//stand(): void; // 站立move(): void; // 移动attack(): void; // 攻击castSkill(): void; // 释放技能stun(): void; // 击晕die(): void; // 死亡// getType(): UnitStateType
}
// 状态基类,包含一个指向Unit的引用
export abstract class BaseState implements IUnitState {protected unitItem: IUnitItem;enterState(unitItem: IUnitItem) {this.unitItem = unitItem;}// 获取状态的type值abstract getType(): UnitStateType;// 状态abstract stand(): void;abstract move(): void;abstract attack(): void;abstract castSkill(): void;abstract stun(): void;abstract die(): void;}

// 站立状态
export class StandingState extends BaseState {getType(): UnitStateType {return UnitStateType.Standing;}stand() {console.log(this.unitItem, "单位已经进入站立状态");}move() {console.log(this.unitItem, "单位准备进入移动状态");this.unitItem.setState(new MovingState());}attack(): void {console.log(this.unitItem, "单位准备进入攻击状态");this.unitItem.setState(new AttackingState());}castSkill(): void {console.log(this.unitItem, "单位准备进入释放技能状态");this.unitItem.setState(new CastSkillingState());}stun(): void {console.log(this.unitItem, "单位准备进入击晕状态");this.unitItem.setState(new StuningState());}die() {console.log("单位准备进入死亡状态");this.unitItem.setState(new DeadState());}}// 移动状态
export class MovingState extends BaseState {getType(): UnitStateType {return UnitStateType.Moving;}stand() {console.log(this.unitItem, "单位准备进入站立状态");this.unitItem.setState(new StandingState());}move() {console.log(this.unitItem, "单位已经进入移动状态");}attack(): void {console.log(this.unitItem, "单位准备进入攻击状态");this.unitItem.setState(new AttackingState());}castSkill(): void {console.log(this.unitItem, "单位准备进入释放技能状态");this.unitItem.setState(new CastSkillingState());}stun(): void {console.log(this.unitItem, "单位准备进入击晕状态");this.unitItem.setState(new StuningState());}die() {console.log(this.unitItem, "单位准备进入死亡状态");this.unitItem.setState(new DeadState());}}// 攻击状态
export class AttackingState extends BaseState {getType(): UnitStateType {return UnitStateType.Attacking;}enterState(unitItem: IUnitItem) {super.enterState(unitItem);this.doAction();}doAction() {// 执行攻击this.unitItem.role.attack(); // 攻击// 如果攻击顺利完成,进行清理并返回到正常状态// 例如,设置一个延时来模拟攻击动作的时间let attackDuration = 1000setTimeout(() => {if (this.unitItem.getCurrentState().getType() == UnitStateType.Attacking) {console.log('单位已从攻击状态到站立状态')this.unitItem.getCurrentState().stand()}}, attackDuration);}stand() {console.log(this.unitItem, "单位准备进入站立状态");this.unitItem.setState(new StandingState());}move() {console.log(this.unitItem, "单位准备进入移动状态");this.unitItem.setState(new MovingState());}attack(): void {console.log(this.unitItem, "单位已经进入攻击状态");}castSkill(): void {console.log(this.unitItem, "单位准备进入释放技能状态");this.unitItem.setState(new CastSkillingState());}stun(): void {console.log(this.unitItem, "单位准备进入击晕状态");this.unitItem.setState(new StuningState());}die() {console.log(this.unitItem, "单位准备进入死亡状态");this.unitItem.setState(new DeadState());}}// 释放技能状态
export class CastSkillingState extends BaseState {getType(): UnitStateType {return UnitStateType.CastSkilling;}enterState(unitItem: IUnitItem) {super.enterState(unitItem);this.doAction();}doAction() {// 执行攻击// this.unitItem.role.attack(); // 攻击// 如果攻击顺利完成,进行清理并返回到正常状态// 例如,设置一个延时来模拟攻击动作的时间let attackDuration = 1000setTimeout(() => {if (this.unitItem.getCurrentState().getType() == UnitStateType.CastSkilling) {console.log('单位已从技能释放状态到站立状态')this.unitItem.getCurrentState().stand()}}, attackDuration);}stand() {console.log(this.unitItem, "单位准备进入站立状态");this.unitItem.setState(new StandingState());}move() {console.log(this.unitItem, "单位准备进入移动状态");this.unitItem.setState(new MovingState());}attack(): void {console.log(this.unitItem, "单位准备进入攻击状态");this.unitItem.setState(new AttackingState());}castSkill(): void {console.log(this.unitItem, "单位已经进入释放技能状态");}stun(): void {console.log(this.unitItem, "单位准备进入击晕状态");this.unitItem.setState(new StuningState());}die() {console.log(this.unitItem, "单位准备进入死亡状态");this.unitItem.setState(new DeadState());}}// 击晕状态
export class StuningState extends BaseState {getType(): UnitStateType {return UnitStateType.Stuning;}enterState(unitItem: IUnitItem) {super.enterState(unitItem);this.stopCurrentAction();}stand() {console.log(this.unitItem, "单位准备进入站立状态");this.unitItem.setState(new StandingState());}move() {console.log(this.unitItem, "单位准备进入移动状态");this.unitItem.setState(new MovingState());}attack(): void {console.log(this.unitItem, "单位准备进入攻击状态");this.unitItem.setState(new AttackingState());}castSkill(): void {console.log(this.unitItem, "单位准备进入释放技能状态");this.unitItem.setState(new CastSkillingState());}stun(): void {console.log(this.unitItem, "单位已经进入击晕状态");}die() {console.log(this.unitItem, "单位准备进入死亡状态");this.unitItem.setState(new DeadState());}stopCurrentAction() {console.log(this.unitItem, "单位所有动作停止,因为被击晕");// 如果有正在进行的释放技能的操作,这里将其中断// 这可能包括清除技能计时器、动画等}}
// 死亡状态
export class DeadState extends BaseState {getType(): UnitStateType {return UnitStateType.Dead;}enterState(unitItem: IUnitItem) {super.enterState(unitItem);this.stopCurrentAction();}stand() {console.log(this.unitItem, "单位准备进入站立状态");this.unitItem.setState(new StandingState());}move() {console.log(this.unitItem, "单位准备进入移动状态");this.unitItem.setState(new MovingState());}attack(): void {console.log(this.unitItem, "单位准备进入攻击状态");this.unitItem.setState(new AttackingState());}castSkill(): void {console.log(this.unitItem, "单位准备进入释放技能状态");this.unitItem.setState(new CastSkillingState());}stun(): void {console.log(this.unitItem, "单位准备进入击晕状态");this.unitItem.setState(new StuningState());}die() {console.log(this.unitItem, "单位已经进入死亡状态");}stopCurrentAction() {console.log(this.unitItem, "单位所有动作停止,因为已死亡");// 如果有正在进行的释放技能的操作,这里将其中断// 这可能包括清除技能计时器、动画等}
}

接着是单位里的

export class UnitItem  extends Component implements IItem, IUnitItem {ad: number = 100;mp: number = 0;role: Fighter;private currentState: IUnitState = null;accept(visitor: IAttackVisitor) {visitor.visitUnitItem(this)}setRole(role: Fighter): void {this.role = role;}setState(state: IUnitState) {this.currentState = state;state.enterState(this);}getCurrentState(): IUnitState {if (this.currentState == null) {this.setState(new StandingState())}return this.currentState;}move() {this.getCurrentState().move()}idle() {this.getCurrentState().stand()}attack(unitItem: UnitItem<T>) {if (!this.canAttack()) {// 不能处理攻击的逻辑,可能是显示消息或者进入其他状态return;}// 尝试进入攻击状态this.getCurrentState().attack()let damage = this.adlet attackVisitor = new MonomerAttackVisitor(damage)unitItem.accept(attackVisitor)// 临时 todo 删除console.log('假装本次攻击带有击晕效果')unitItem.getCurrentState().stun()}skill() {if (!this.canSkill()) {// 不能处理攻击的逻辑,可能是显示消息或者进入其他状态return;}// 尝试进入攻击状态this.getCurrentState().castSkill()}die() {this.getCurrentState().die()}private canSkill(): boolean {// 检查单位是否可以进行技能攻击// 例如,单位是否处于晕眩状态或者攻击是否冷却中if (this.mp < 100) {console.log('不能处理skill攻击的逻辑,因为魔法值不足100')return false}if (this.getCurrentState().getType() == UnitStateType.CastSkilling) {console.log('不能处理skill攻击的逻辑,因为已经处于技能释放中')return false}if (this.getCurrentState().getType() == UnitStateType.Stuning) {console.log('不能处理skill攻击的逻辑,因为已经被击晕')return false}if (this.getCurrentState().getType() == UnitStateType.Dead) {console.log('不能处理skill攻击的逻辑,因为已经死亡')return false}return true;}private canAttack(): boolean {// 检查单位是否可以进行攻击// 例如,单位是否处于晕眩状态或者攻击是否冷却中if (this.getCurrentState().getType() == UnitStateType.Attacking) {console.log('不能处理攻击的逻辑,因为已经处于攻击中')return false}if (this.getCurrentState().getType() == UnitStateType.Stuning) {console.log('不能处理攻击的逻辑,因为已经被击晕')return false}if (this.getCurrentState().getType() == UnitStateType.Dead) {console.log('不能处理攻击的逻辑,因为已经死亡')return false}return true;}
}

4、开始使用

        let unitItem001 = xhgame.itemFactory.createUnitItem('kuloubing', UnitType.UnitSpine)let unitItem002 = xhgame.itemFactory.createUnitItem('kuloubing', UnitType.UnitSpine)unitItem001.idle()unitItem002.idle()unitItem002.skill()unitItem002.mp = 100;unitItem002.skill()unitItem001.setRole(new Cavalry(new Sword()));console.log('unitItem001(骑兵)准备使用【剑】对unitItem002发起了攻击')unitItem001.attack(unitItem002)unitItem001.setRole(new Cavalry(new Bow()));console.log('unitItem001(骑兵)准备使用【弓】对unitItem002发起了攻击')unitItem001.attack(unitItem002)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/211957.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C语言】字符串函数strcpystrcatstrcmpstrstr的使⽤和模拟实现

&#x1f308;write in front :&#x1f50d;个人主页 &#xff1a; 啊森要自信的主页 ✏️真正相信奇迹的家伙&#xff0c;本身和奇迹一样了不起啊&#xff01; 欢迎大家关注&#x1f50d;点赞&#x1f44d;收藏⭐️留言&#x1f4dd;>希望看完我的文章对你有小小的帮助&am…

电源小白入门学习1——电源系统架构和相关指标

电源小白入门学习1——电源系统架构和相关指标 电源系统架构电源系统的指标及测量方法电源的效率电源的静态电流输出电压调整率纹波测量的注意事项动态负载测试 在开始本期内容之气&#xff0c;我先简单介绍一下我们电源小白学习系列内容&#xff1a;首先我是一个嵌入式小白&am…

在VSCode中运行Python脚本文件时如何传参

以下实验所处的操作系统环境说明&#xff1a; OS版本MacOSMonterey 12.1VSCodeOctober 2023 (version 1.84.2) 一、背景 在 VSCode 中写好 Python 脚本后&#xff0c;如果要运行起来&#xff0c;可以怎么做呢&#xff1f; 一般有以下几种方式&#xff1a; 1、直接在 VSCode…

sentinel整合nacos配置中心持久化

在网上找了很多的资料&#xff0c;发现sentinel整合nacos持久化的博文和视频大多数都只有改造限流部分的教程&#xff0c;并且都需要修改前端&#xff0c;略显麻烦&#xff0c;至于剩下的熔断、热点流控、授权的更是没有相关的改造教程&#xff0c;最后在知乎的看到一篇文章后让…

百科词条可以删除吗?如何删除自己的百度百科?

近日&#xff0c;小马识途营销顾问接到不少客户删除自己百科词条的咨询&#xff0c;有不少人自己并没有去建立百科词条&#xff0c;但是网上已经有了&#xff0c;有的信息不正确&#xff0c;甚至有的信息是负能量的&#xff0c;对当事人自己造成一定的困扰&#xff0c;所以寻求…

PbootCMS 前台RCE漏洞复现

0x01 产品简介 PbootCMS是全新内核且永久开源免费的PHP企业网站开发建设管理系统,是一套高效、简洁、 强悍的可免费商用的PHP CMS源码,能够满足各类企业网站开发建设的需要 0x02 漏洞概述 PbootCMS v<=3.1.6版本中存在模板注入,攻击者可构造特定的链接利用该漏洞,执行…

线程及实现方式

一、线程 线程是一个基本的CPU执行单元&#xff0c;也是程序执行流的最小单位。引入线程之后&#xff0c;不仅是进程之间可以并发&#xff0c;进程内的各线程之间也可以并发&#xff0c;从而进一步提升了系统的并发度&#xff0c;使得一个进程内也可以并发处理各种任务&#x…

【Hive】——安装部署

1 MetaData&#xff08;元数据&#xff09; 2 MetaStore &#xff08;元数据服务&#xff09; 3 MetaStore配置方式 3.1 内嵌模式 3.2 本地模式 3.3 远程模式 4 安装前准备 <!-- 整合hive --><property><name>hadoop.proxyuser.root.hosts</name><v…

Java+Swing: 主界面组件布局 整理9

说明&#xff1a;这篇博客是在上一篇的基础上的&#xff0c;因为上一篇已经将界面的框架搭好了&#xff0c;这篇主要是将里面的组件完善。 分为三个部分&#xff0c;北边的组件、中间的组件、南边的组件 // 放置北边的组件layoutNorth(contentPane);// 放置中间的 Jtablelayou…

Tair(3):Tair入门demo

新建一个maven项目 1 导入依赖 <dependency><groupId>com.taobao.tair</groupId><artifactId>tair-client</artifactId><version>2.3.5</version></dependency><dependency><groupId>com.alibaba</groupId>…

Linux权限(用户角色+文件权限属性)

Linux权限 文章目录 Linux权限一.文件权限1.快速掌握修改权限的方法&#xff08;修改文件权限属性&#xff09;2.对比权限的有无&#xff0c;以及具体的体现3.修改权限的第二套方法&#xff08;修改用户角色&#xff09;4.文件类型&#xff08;Linux下一切皆文件&#xff09; 二…

049:VUE 引入jquery的方法和配置

第049个 查看专栏目录: VUE ------ element UI 专栏目标 在vue和element UI联合技术栈的操控下&#xff0c;本专栏提供行之有效的源代码示例和信息点介绍&#xff0c;做到灵活运用。 &#xff08;1&#xff09;提供vue2的一些基本操作&#xff1a;安装、引用&#xff0c;模板使…

springboot基础(80):redis geospatial的应用

文章目录 前言redis geospatial如何从地图上获取经纬度springboot 的相关方法调用准备redis服务器引用的依赖预设位置的keyGEOADD 添加位置GEORADIUS 获取指定经纬度附件的停车场&#xff08;deprecated&#xff09;GEORADIUS 获取指定成员附件的停车场&#xff08;deprecated&…

文心一言API(高级版)使用

文心一言API高级版使用 一、百度文心一言API(高级版)二、使用步骤1、接口2、请求参数3、请求参数示例4、接口 返回示例 三、 如何获取appKey和uid1、申请appKey:2、获取appKey和uid 四、重要说明 一、百度文心一言API(高级版) 基于百度文心一言语言大模型的智能文本对话AI机器…

归并排序--分治法

代码 #include<iostream> using namespace std;void merge(int arr[], int p, int q, int r, int temp[]) {int i p;int j q 1;int k 0;while (i < q && j < r){if (arr[i] < arr[j]){temp[k] arr[i];}else{temp[k] arr[j];}}while (i < q){t…

智能优化算法应用:基于蚁狮算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于蚁狮算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于蚁狮算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.蚁狮算法4.实验参数设定5.算法结果6.参考文献7.MA…

ptmalloc:从内存虚拟化说起

前言 本文并不局限于ptmalloc的原理&#xff0c;而是从linux的内存虚拟化和系统调用原理出发&#xff0c;结合各种语言实现&#xff0c;讲明内存分配方面的trade off&#xff0c;力图事无巨细&#xff0c;追根究底。本文内容包括但不限于&#xff1a;NIO原理、0拷贝原理、内存…

leetcode:643. 子数组最大平均数 I(滑动窗口)

一、题目 链接&#xff1a;643. 子数组最大平均数 I - 力扣&#xff08;LeetCode&#xff09; 函数原型&#xff1a; double findMaxAverage(int* nums, int numsSize, int k) 二、思路 滑动窗口&#xff1a; 先计算数组前k个元素总和&#xff0c;作为第一个窗口&#xff0c;默…

vlog如何降低重复率

大家好&#xff0c;今天来聊聊vlog如何降低重复率&#xff0c;希望能给大家提供一点参考。 以下是针对论文重复率高的情况&#xff0c;提供一些修改建议和技巧&#xff1a; vlog如何降低重复率 Vlog作为一种流行的视频日志形式&#xff0c;常常被人们用于记录日常生活、分享经…

pta模拟题——7-34 刮刮彩票

“刮刮彩票”是一款网络游戏里面的一个小游戏。如图所示&#xff1a; 每次游戏玩家会拿到一张彩票&#xff0c;上面会有 9 个数字&#xff0c;分别为数字 1 到数字 9&#xff0c;数字各不重复&#xff0c;并以 33 的“九宫格”形式排布在彩票上。 在游戏开始时能看见一个位置上…