游戏编程模式 - 状态模式




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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值