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)
{
}
}
}