someone can tell me if there something of right?
teoretically is more “call” and less “polling” , the function of character pass the call to the state directly.
this call can cuse the change of state (note the change of state is completely managed from the state itself)
it work, anyway are 300 to move a cube how is possible ?:o
is normal ? anyway with polling is long too … the problem of the length is when there these damn states :ba:
can someone give me the right diection …
from mathutils import Vector
import bge
""" state -> animation
Waiting -> waiting
WalkingForward -> walking_forward
WalkingBackward -> walking_backward
Jumping -> jumping
Falling -> falling
"""
"""state transitions
Waiting
Falling
Jumping
WalkingForward
WalkingBackward
WalkingForward
Jumping
Falling
Waiting
WalkingBackward
Jumping
Falling
Waiting
Jumping
Falling
Waiting
Falling
Waiting
"""
class State:
def __init__(self, char):
self.char = char
self.start()
def start(self): pass
def forward(self): pass
def backward(self): pass
def left(self): pass
def right(self): pass
def jump(self): pass
def update(self): pass
def attack(self): pass
class Waiting(State):
def start(self):
self.char.animator.play("wait")
def forward(self):
self.char.state = WalkingForward(self.char)
def backward(self):
self.char.state = WalkingBackward(self.char)
def left(self):
self.char.box.applyRotation((0,0,0.1),1)
def right(self):
self.char.box.applyRotation((0,0,-0.1),1)
def jump(self):
self.char.state = Jumping(self.char)
def update(self):
if not self.char.on_ground:
self.char.state = Falling(self.char)
class WalkingForward(State):
def start(self):
self.char.animator.play("walk_forward")
self.forward()
def forward(self):
self.char.box.localLinearVelocity.xy = 0, 2
def backward(self):
self.char.state = Waiting(self.char)
def left(self):
self.char.box.applyRotation((0,0,0.1),1)
def right(self):
self.char.box.applyRotation((0,0,-0.1),1)
def jump(self):
self.char.state = Jumping(self.char)
def update(self):
if not self.char.on_ground:
self.char.state = Falling(self.char)
else:
if self.char.box.localLinearVelocity.y < 1.0:
self.char.state = Waiting(self.char)
class WalkingBackward(State):
def start(self):
self.char.animator.play("walk_backward")
self.backward()
def forward(self):
self.char.state = Waiting(self.char)
def backward(self):
self.char.box.localLinearVelocity.xy = 0, -2
def left(self):
self.char.box.applyRotation((0,0,0.1),1)
def right(self):
self.char.box.applyRotation((0,0,-0.1),1)
def jump(self):
self.char.state = Jumping(self.char)
def update(self):
if not self.char.on_ground:
self.char.state = Falling(self.char)
else:
if self.char.box.localLinearVelocity.y > 0.5:
self.char.state = Waiting(self.char)
class Jumping(State):
def start(self):
self.char.animator.play("jumping")
self.char.box.worldLinearVelocity.z = 5
def update(self):
if self.char.on_ground:
self.char.state = Waiting(self.char)
elif self.char.box.localLinearVelocity.z < 0.0:
self.char.state = Falling(self.char)
class Falling(State):
def start(self):
self.char.animator.play("falling")
def update(self):
if self.char.on_ground or self.char.box.localLinearVelocity.z > 0.0:
self.char.state = Waiting(self.char)
class Animator:
def play(self, name=""):
pass
class Character:
def __init__(self, box, armature=None):
self.box = box
self.animator = Animator()
self.state = Waiting(self)
@property
def on_ground(self):
return self.box.rayCast(self.box.worldPosition + Vector((0, 0, -1.1)), self.box)[0] != None
def forward(self):
self.state.forward()
def backward(self):
self.state.backward()
def left(self):
self.state.left()
def right(self):
self.state.right()
def jump(self):
self.state.jump()
def attack(self):
self.state.attack()
def update(self):
self.state.update()
class Player(Character):
pass
def init(cont):
box = cont.owner
box["player"] = p = Player(box)
def update(cont):
own = cont.owner
if not "player" in own:
init(cont)
p = own["player"]
p.update()
own["state_name"] = p.state.__class__.__name__
kev = bge.logic.keyboard.events
w,s,a,d,space = [kev[getattr(bge.events, k.upper()+"KEY")] in [1,2] for k in ["w","s","a","d","space"]]
if w:
p.forward()
if s:
p.backward()
if a:
p.left()
if d:
p.right()
if space:
p.jump()
it should work with always true, and a cube dynamic , -> [module.update]