import bge dict = bge.logic.globalDict #==================================== State = type("State", (object,), {}) #==================================== class State(object): def __init__(self, FSM): self.FSM = FSM self.timer = 0 self.startTime = 0 def Enter(self): self.timer = 0 self.startTime = 0 def Execute(self): print('Executing') def Exit(self): print('Exiting') #==================================== class Example(State): def __init__(self,FSM): super(Example, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 super(Example, self).Enter() def Execute(self): self.FSM.stateLife += 1 #self.FSM.ToTransition('toExample') def Exit(self): pass #==================================== class Startup(State): def __init__(self,FSM): super(Startup, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 super(Startup, self).Enter() def Execute(self): self.FSM.stateLife += 1 if self.FSM.stateLife == 5: #Startup.main(self.FSM.owner['cont']) self.FSM.ToTransition('toWalk') print('player FSM') def Exit(self): pass #==================================== class Walk(State): def __init__(self,FSM): super(Walk, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 o = self.FSM.owner.obj c = self.FSM.owner if c.arm == None: print(o.childrenRecursive) c.arm = o.childrenRecursive['Char4'] self.walk_weight = 1 self.run_weight = 1 self.run_speed = 1.0 self.turn_weight = 0 super(Walk, self).Enter() def Execute(self): self.FSM.stateLife += 1 o = self.FSM.owner.obj c = self.FSM.owner #print('walking') if self.FSM.stateLife == 2: if o['stance']: o.applyRotation([0,0,3], True) o['stance'] = False o['requestAction'] = 'fak_offboard' print('request fak offboard') else: o['requestAction'] = 'reg_offboard' print('request reg offboard') if self.FSM.stateLife > 5: o['requestAction'] = 'reg_idle' self.check_onboard() self.check_jump() self.check_exit() ray = c.get_ground_ray() moving = c.walk_movement() self.idle_blend() self.get_walk_weight() #self.FSM.ToTransition('toLand') def check_onboard(self): if dict['walk'] == 0: self.FSM.ToTransition('toRoll') if dict['yBut'] == 1 and dict['last_yBut'] == 0: print('do the onboard') def check_jump(self): o = self.FSM.owner.obj #limit fall speed if o.linearVelocity.z < -10: o.linearVelocity.z = -10 if dict['xBut'] == True or dict['kb_space'] == 1: if dict['last_xBut'] == 0: o['requestAction'] ='reg_jump' JUMPHEIGHT = 1100 force = [ 0.0, 0.0, JUMPHEIGHT] # use local axis local = False # apply force -- limit jump speed if o.linearVelocity.z < 10: o.applyForce(force, local) o.linearVelocity.z += 5 def check_exit(self): pass #own['walk'] == 1 def idle_blend(self): arm = self.FSM.owner.arm arm.playAction('reg_idle1', 1,120, layer=6, play_mode=1, speed=1, blendin=10) if self.FSM.stateLife > 20: frame = arm.getActionFrame(7) + .5 * self.run_speed if frame > 30: frame = 0 arm.stopAction(7) arm.playAction('reg_nwalk2', 0,30, layer=7, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.walk_weight) arm.setActionFrame(frame, 7) #print(self.walk_weight, frame) frame = arm.getActionFrame(8) + .5 * self.run_speed if frame > 30: frame = 0 arm.stopAction(8) arm.playAction('reg_run.002', 0,30, layer=8, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.run_weight) arm.setActionFrame(frame, 8) #print(frame, 'frame') #print(self.run_weight, frame) # if self.turn_weight > 0: # tw = abs(self.turn_weight - 1) # #tw = self.turn_weight # arm.stopAction(4) # arm.playAction('bwalk_right', 1,60, layer=4, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=tw) # elif self.turn_weight < 0: # tw = abs(abs(self.turn_weight) - 1) # #tw = self.turn_weight # arm.stopAction(4) # arm.playAction('bwalk_left', 1,60, layer=4, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=tw) def get_walk_weight(self): #print(self.FSM.owner.linearVelocity.y) o = self.FSM.owner.obj w = abs(o.linearVelocity.x) #wt = 3.5 #yt = 8 wt = 1.1 yt = 3 out2 = 0 if w < wt: out = w / wt else: out = 1 #print('running', w) out2 = w / yt out = abs(1 - out) out2 = abs(1 - out2) tgww = round(out, 3) tgrw = round(out2, 3) incer = .05 if self.walk_weight < tgww: self.walk_weight += incer if self.walk_weight > tgww: self.walk_weight -= incer if self.run_weight <= tgrw: self.run_weight += incer if self.run_weight > tgrw: self.run_weight -= incer if self.walk_weight <= 0: self.walk_weight = 0 if self.walk_weight > .95: self.walk_weight = 1 if self.run_weight <= 0: self.run_weight = 0 if self.run_weight > .95: self.run_weight = 1 if dict['kb_lsh'] == 2 or dict['aBut'] == 1: self.run_speed = 1.3 else: self.run_speed = .8 print(self.run_speed, '---', self.walk_weight, 'walk weight', self.run_weight, 'run weight') def Exit(self): pass #==================================== class WalkAir(State): def __init__(self,FSM): super(WalkAir, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 super(WalkAir, self).Enter() def Execute(self): self.FSM.stateLife += 1 #self.FSM.ToTransition('toLand') def Exit(self): pass #==================================== class WalkJump(State): def __init__(self,FSM): super(WalkJump, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 super(WalkJump, self).Enter() def Execute(self): self.FSM.stateLife += 1 #self.FSM.ToTransition('toLand') def Exit(self): pass #==================================== class WalkLand(State): def __init__(self,FSM): super(WalkLand, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 super(WalkLand, self).Enter() def Execute(self): self.FSM.stateLife += 1 #self.FSM.ToTransition('toLand') def Exit(self): pass #==================================== class WalkHang(State): def __init__(self,FSM): super(WalkHang, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 super(WalkHang, self).Enter() def Execute(self): self.FSM.stateLife += 1 #self.FSM.ToTransition('toLand') def Exit(self): pass #==================================== class WalkClimb(State): def __init__(self,FSM): super(WalkClimb, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 super(WalkClimb, self).Enter() def Execute(self): self.FSM.stateLife += 1 #self.FSM.ToTransition('toLand') def Exit(self): pass #==================================== class WalkHurdle(State): def __init__(self,FSM): super(WalkHurdle, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 super(WalkHurdle, self).Enter() def Execute(self): self.FSM.stateLife += 1 #self.FSM.ToTransition('toLand') def Exit(self): pass #==================================== class Roll(State): def __init__(self,FSM): super(Roll, self).__init__(FSM) def Enter(self): self.FSM.stateLife = 1 super(Roll, self).Enter() def Execute(self): self.FSM.stateLife += 1 if dict['walk'] == 1: self.FSM.ToTransition('toWalk') print('rolling') #self.FSM.ToTransition('toLand') def Exit(self): pass #====================================