Skip to content

Instantly share code, notes, and snippets.

@Kronos11
Created January 24, 2012 06:16
Show Gist options
  • Save Kronos11/1668316 to your computer and use it in GitHub Desktop.
Save Kronos11/1668316 to your computer and use it in GitHub Desktop.
Different approach to PSM
#-------------------------------------------------------------------------------
# Name: AbstractState
# Purpose:
#
# Author: Kyle
#
# Created: 23/01/2012
# Copyright: (c) Kyle 2012
# Licence: GPL2 ANHSTUDIOS
#-------------------------------------------------------------------------------
class AbstractState():
""" Abstract State """
def __init__(self):
self.id = 0
self.transitionList = []
self.blocked = 0 # false
print('Abstract State')
def Block(self):
self.blocked = 1
def Unblock(self):
self.blocked = 0
def CanTransition(self, targetState):
print('abstract state can transition')
if self.blocked == 1:
return 0 # Transiton blocked
if targetState.GetId(self) in transitionList:
return 1 # Valid Transition
else:
return 0
def GetId(self):
return self.id
def Enter(actorState):
return
def Exit(actorState):
return
#-------------------------------------------------------------------------------
# Name: ActionState
# Purpose:
#
# Author: Kyle
#
# Created: 23/01/2012
# Copyright: (c) Kyle 2012
# Licence: GPL2 ANHSTUDIOS
#-------------------------------------------------------------------------------
import AbstractState
class ActionState(AbstractState.AbstractState):
def __init__(self):
self.id = 0
self.transitionList = []
self.blocked = 0 # false
def CanTransition(targetState):
print('action state can transition')
if self.blocked == 1:
return 0 # Transiton blocked
if targetState.GetId(self) in self.transitionList:
return 1 # Valid Transition
elif '*' in self.transitionList:
return 1
else:
return 0
def GetId(self):
return self.id
def Enter(self, actorState):
return
def Exit(self, actorState):
return
#-------------------------------------------------------------------------------
# Name: ActorState
# Purpose:
#
# Author: Kyle
#
# Created: 20/01/2012
# Copyright: (c) Kyle 2012
# Licence: GPL ANH
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import ActionState as As
import LocomotionState as Ls
import PostureState as Ps
class ActorState:
def __init__(self):
self.actionStateInvalid = As.Invalid()
self.actionStateCover = As.Cover()
self.actionStateCombat = As.Combat()
self.actionStatePeace = As.Peace()
self.actionStateAiming = As.Aiming()
self.locomotionStateStanding = Ls.Standing()
self.locomotionStateWalking = Ls.Walking()
self.lcoomotionStateRunning = Ls.Running()
self.locomotionStateKneeling = Ls.Kneeling()
self.locmotionStateProne = Ls.Prone()
self.locomotionStateIncapacitated = Ls.Incap()
self.locomotionStateDead = Ls.Dead()
self.Upright = Ps.Upright()
self.Crouched = Ps.Crouched()
self.Prone = Ps.Prone()
self.Sneaking = Ps.Sneaking()
self.Blocking = Ps.Blocking()
self.Climbing = Ps.Climbing()
self.Flying = Ps.Flying()
self.LyingDown = Ps.LyingDown()
self.Sitting = Ps.Sitting()
self.SkillAnimating = Ps.SkillAnimating()
self.DrivingVehicle = Ps.DrivingVehicle()
self.RidingCreature = Ps.RidingCreature()
self.KnockedDown = Ps.KnockedDown()
self.Incapacitated = Ps.Incapacitated()
self.Dead = Ps.Dead()
# current states
self.currentActionState = self.actionStateInvalid
self.currentLocomotionState = self.locomotionStateStanding
def SetCurrentState(self, targetState):
self.currentState.Exit(self)
self.currentState = targetState
self.currentState.Enter(self)
#-------------------------------------------------------------------------------
# Name: PostureState
# Purpose:
#
# Author: Kyle
#
# Created: 23/01/2012
# Copyright: (c) Kyle 2012
# Licence: GPL2 ANHSTUDIOS
#-------------------------------------------------------------------------------
import AbstractState
class PostureState(AbstractState.AbstractState):
def __init__(self):
self.id = 0
self.transitionList = []
self.blocked = 0 # false
def CanTransition(self,targetState):
if self.blocked == 1:
return 0 # Transiton blocked
if targetState.GetId(self) in self.transitionList:
return 1 # Valid Transition
else:
return 0
def GetId(self):
return self.id
## def Enter(self, actorState):
## return
##
## def Exit(self, actorState):
## return
import PostureStates
class Upright(PostureState):
"Upright is the default posture"
def __init__(self):
self.id = 0
self.transitionList = [
PostureState.Crouched.GetId(),
PostureState.Prone.GetId(),
PostureState.Sneaking.GetId(),
PostureState.LyingDown.GetId(),
PostureState.Sitting.GetId(),
PostureState.SkillAnimating.GetId(),
PostureState.KnockedDown.GetId()
]
self.blocked = 0
## def Enter(self, actorState):
## return
##
## def Exit(self, actorState):
## return
#-------------------------------------------------------------------------------
# Name: StateManager
# Purpose:
#
# Author: Kyle
#
# Created: 20/01/2012
# Copyright: (c) Kyle 2012
# Licence: GPL ANH
#-------------------------------------------------------------------------------
import ActorState
import ActionState as As
import LocomotionState as Ls
import PostureState as Ps
class StateManager:
def __init__(self):
self.currentActionState = As.Idle()
self.currentLocomotionState = Ls.Upright()
self.currentPostureState = Ps.Stand()
def SetActionState(self, ActorState, targetState):
if self.currentActionState.CanTransition(targetState):
self.currentActionState.Exit(ActorState)
self.currentActionState = targetState
self.currentActionState.Enter(ActorState)
return true # Successful Transition
else:
return false
def SetLocomotionState(self, ActorState, targetState):
if self.currentLocomotionState.CanTransition(targetState):
self.currentLocomotionState.Exit(ActorState)
self.currentLocomotionState = targetState
self.currentLocomotionState.Enter(ActorState)
return true # Successful Transition
else:
return false
def SetPostureState(self, ActorState, targetState):
if self.currentPostureState.CanTransition(targetState):
self.currentPostureState.Exit(ActorState)
self.currentPostureState = targetState
self.currentPostureState.Enter(ActorState)
return true # Successful Transition
else:
return false
def BlockAll(self, ActorState):
As.Block(ActorState)
Ls.Block(ActorState)
Ps.Block(ActorState)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment