游戏编程模式 - 状态模式
来源:互联网 发布:怎么申请软件版权 编辑:程序博客网 时间:2024/06/06 08:35
using UnityEngine;using System.Collections;public enum LogicState{ STAND, JUMP, DUCK, DIVE}public enum LogicInput{ PRESS_JUMP, PRESS_DOWN, RELEASE_DOWN}namespace finiteStateMachine{ public interface IState { IState handleInput(LogicEntity _logic, LogicInput _input); void enter(LogicEntity _logic); void update(LogicEntity _logic); void exit(LogicEntity _logic); } public class DuckState : IState { private float chargeTime_ = 0f; public IState handleInput(LogicEntity _logic, LogicInput _input) { if (LogicInput.RELEASE_DOWN == _input) { return LogicEntity.standState; } else { return null; } } public void update(LogicEntity _logic, float gameFrameLength) { chargeTime_ += gameFrameLength; if (chargeTime_ >= 5f) { // dosomething _logic.setState(LogicEntity.jumpState); } } public void enter(LogicEntity _logic) { chargeTime_ = 0f; _logic.setAnimation("animate_duck"); } public void exit(LogicEntity _logic) { } } public class JumpState : IState { public IState handleInput(LogicEntity _logic, LogicInput _input) { if (LogicInput.PRESS_DOWN == _input) { return LogicEntity.diveState; } else { return null; } } public void update(LogicEntity _logic, float gameFrameLength) { _logic.yVelocity -= gameFrameLength * LogicEntity.GRAVITY; _logic.yPosition += _logic.yVelocity * gameFrameLength; if (_logic.yPosition <= 0f) { _logic.yPosition = 0f; _logic.setState(LogicEntity.standState); } } public void enter(LogicEntity _logic) { _logic.setAnimation("animate_jump"); } public void exit(LogicEntity _logic) { } } public class StandState : IState{ public IState handleInput(LogicEntity _logic, LogicInput _input){ if (LogicInput.PRESS_JUMP == _input){ return LogicEntity.jumpState; } else if(LogicInput.PRESS_DOWN == _input){ return ( new DuckState() ); } else{ return null; } } public void update(LogicEntity _logic, float gameFrameLength){ } public void enter(LogicEntity _logic){ _logic.setAnimation("animate_stand"); } public void exit(LogicEntity _logic){ } } public class DiveState : IState { public IState handleInput(LogicEntity _logic, LogicInput _input) { return null; } public void update(LogicEntity _logic, float gameFrameLength) { _logic.yVelocity -= gameFrameLength * LogicEntity.GRAVITY; _logic.yPosition += _logic.yVelocity * gameFrameLength; if (_logic.yPosition <= 0f) { _logic.yPosition = 0f; _logic.setState(LogicEntity.standState); } } public void enter(LogicEntity _logic) { _logic.setAnimation("animate_dive"); } public void exit(LogicEntity _logic) { } } public class LogicEntity { public static readonly float GRAVITY = 10f; public static readonly float JUMP_VELOCITY = 10f; public static DiveState diveState = new DiveState(); public static JumpState jumpState = new JumpState(); public static StandState standState = new StandState(); public string animateName; public float yVelocity; public float yPosition; IState state_; public void handleInput(LogicInput inputKey) { IState _nextState = state_.handleInput(this, inputKey); if (null != _nextState) { setState(_nextState); } } public void update() { state_.update(this); } public void setState(IState _nextState) { state_.exit(this); state_ = _nextState; state_.enter(this); } public void setAnimation(string _animateName) { animateName = _animateName; } }}namespace test{ public class LogicEntity { public static readonly float JUMP_VELOCITY = 10f; public float yVelocity; public bool isDiving; public bool isJumping; public bool isDucking; public void handleInput(LogicInput inputKey) { if (LogicInput.PRESS_JUMP == inputKey) { if (!isJumping && !isDucking && !isDiving) { isJumping = true; yVelocity = JUMP_VELOCITY; setState(LogicState.JUMP); } } else if (LogicInput.PRESS_DOWN == inputKey) { if (!isJumping && !isDiving) { isDucking = true; setState(LogicState.DUCK); } } else if (LogicInput.RELEASE_DOWN == inputKey) { if (isDucking && !isJumping && !isDiving) { isDucking = false; setState(LogicState.STAND); } else if (isJumping && !isDucking) { isDiving = true; setState(LogicState.DIVE); } } else { Debug.LogError("not exist input:" + inputKey); } } public void setState(LogicState curState) { } public void nextState() { } } public class LogicEntity2 { public static readonly float JUMP_VELOCITY = 10f; public float yVelocity; public LogicState state_; public void handleInput(LogicInput inputKey) { switch (state_) { case LogicState.STAND: { if (LogicInput.PRESS_JUMP == inputKey) { yVelocity = JUMP_VELOCITY; setState(LogicState.JUMP); } else if (LogicInput.PRESS_DOWN == inputKey) { setState(LogicState.DUCK); } break; } case LogicState.JUMP: { if (LogicInput.PRESS_DOWN == inputKey) { setState(LogicState.DIVE); } break; } case LogicState.DUCK: { if (LogicInput.RELEASE_DOWN == inputKey) { setState(LogicState.STAND); } break; } case LogicState.DIVE: { break; } default: { Debug.LogError("not exist state:" + state_); break; } } } private void setState(LogicState curState) { } }}
0 0
- 游戏编程模式 - 状态模式
- 状态模式--游戏开发
- 游戏编程模式:命令模式(Part I)
- 游戏编程模式:命令模式(Part II)
- 游戏编程模式:命令模式(Part III)
- 《游戏编程模式》(6)
- 《游戏编程模式》(8)
- 《游戏编程模式》(6)
- 《游戏编程模式》(8)
- 状态同步模式下的游戏掉线重连
- 状态同步模式下的游戏掉线重连
- Java设计模式编程中的状态模式和策略模式
- 游戏中的设计模式——状态者模式
- Unity游戏设计模式之状态模式实现
- (学习笔记)设计模式之状态模式(游戏场景切换)
- 游戏开发中的设计模式——1.状态模式
- 面向对象编程-状态模式(js)
- 面向对象编程思想-状态模式
- Android备忘录
- Linux-Test(测试指令)
- STM32 SPI配置
- 优秀Java程序员的编程风格
- EasyUI总结三
- 游戏编程模式 - 状态模式
- 第七届蓝桥杯软件类省赛真题-C-A-2_平方怪圈
- LeetCode 11. Container With Most Water
- [Leetcode] #77 Combinations
- Starling2.x SuperTextureAtlas 扩展支持4096以上的纹理集图片
- 浏览器是如何渲染页面和加载页面
- 魅族5.0以上支付宝开发程序崩溃,异常ActivityNotFoundException,其实是flyme6的安全模式搞的鬼
- 全面分析 Spring 的编程式事务管理及声明式事务管理
- 趣味职场俚语笔记(02_Our Cutting Edge Hi-Tech Line)