当前位置: 首页 > news >正文

5118站长工具seo国外推广软件

5118站长工具,seo国外推广软件,新疆建设厅造价网站,莱芜 做网站 公司1、模式标准 模式名称:状态模式 模式分类:行为型 模式意图:允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。 结构图: 适用于: 1、一个对象的行为决定于它的状态,并且它必须…

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;// 状态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, "单位准备进入死亡状态");this.unitItem.setState(new DeadState());}}

// 站立状态
export class StandingState extends BaseState {getType(): UnitStateType {return UnitStateType.Standing;}// 重写方法stand() {console.log(this.unitItem, "单位已经进入站立状态");}}// 移动状态
export class MovingState extends BaseState {getType(): UnitStateType {return UnitStateType.Moving;}// 重写方法move() {console.log(this.unitItem, "单位已经进入移动状态");}}// 攻击状态
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.setState(new StandingState());}}, attackDuration);}// 重写方法attack(): void {console.log(this.unitItem, "单位已经进入攻击状态");}}// 释放技能状态
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.setState(new StandingState());}}, attackDuration);}// 重写方法castSkill(): void {console.log(this.unitItem, "单位已经进入释放技能状态");}}// 击晕状态
export class StuningState extends BaseState {getType(): UnitStateType {return UnitStateType.Stuning;}enterState(unitItem: IUnitItem) {super.enterState(unitItem);this.stopCurrentAction();}// 重写方法stun(): void {console.log(this.unitItem, "单位已经进入击晕状态");}stopCurrentAction() {console.log(this.unitItem, "单位所有动作停止,因为被击晕");// 如果有正在进行的释放技能的操作,这里将其中断// 这可能包括清除技能计时器、动画等}}
// 死亡状态
export class DeadState extends BaseState {getType(): UnitStateType {return UnitStateType.Dead;}enterState(unitItem: IUnitItem) {super.enterState(unitItem);this.stopCurrentAction();}// 重写方法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;}
}

 在非状态对象类中使用时都是用以下的方式调用

this.getCurrentState().stand()
this.getCurrentState().move()

   在方法里面会执行从当前状态到下一个状态所需要的动作

在状态类中,如果需要到下一个状态就需要再状态类中new一个新的状态,如

    castSkill(): void {console.log(this.unitItem, "单位准备进入释放技能状态");this.unitItem.setState(new CastSkillingState());}

接着在下一个状态CastSkillingState中的enterState,方法内其他动作

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.mmbaike.com/news/72857.html

相关文章:

  • 教做香肠的网站seod的中文意思
  • 网站建设公司特点seo客服
  • 自己做网站都要什么软件免费推广软件 推广帮手
  • emlog转换wordpress东莞seo计费管理
  • 重庆企业的网站建设推广网站软文
  • 江山企业自适应网站建设首选线上推广策划方案
  • 学做蛋糕有哪些网站seo优化公司
  • 怎么网站是谁做的搜索引擎大全网站
  • wordpress 后台 添加菜单seo流量增长策略
  • 西安网站建设xamokj淘宝关键词排名查询网站
  • 福田网站建设论文结论网络营销课程学什么
  • 网站验证码怎么做推广方案流程
  • 网站在线客服聊天系统短视频营销策略有哪些
  • 富阳网站建设 优帮云杭州百度优化
  • wordpress 如何使用两个主题惠州seo按天计费
  • 如何选择一个优质网站建设公司手机网站排名优化软件
  • 做网站的岗位叫什么问题湖南网站推广优化
  • 好用的网站建设工具怎么制作个人网站
  • 最好的汽车科技网站建设品牌全网推广
  • wordpress一键分享代码合肥网站快速优化排名
  • mt7620a做网站seo网络优化招聘
  • 徐州英文网站seo淘宝关键词排名怎么查
  • 网站备案网站建设方案书潍坊seo培训
  • 如何做考试网站外包网站有哪些
  • 东莞公司高端网站建设广州网站制作实力乐云seo
  • 网站免费正能量直接进入在线推广软件一键发送
  • 网站建设 万网360优化大师官方最新
  • 毕业室内设计代做网站百度分公司
  • 宜春网站建设公司东莞网站建设推广品众
  • 丝袜做那个的视频网站51网站统计