import bge import FSM from mathutils import Vector dict = bge.logic.globalDict class Player: def __init__(self, cont): self.cont = cont self.obj = cont.owner self.FSM = FSM.PlayerFSM(self) self.walking = True dict['walking'] = True #print(self.obj.childrenRecursive) #self.arm = self.obj.childrenRecursive['Char4'] self.arm = None def update(self): self.FSM.Execute() def get_ground_ray(self): v = self.obj.worldOrientation.col[0] tv = v.normalized() axis = 2 distance = -5 start = self.obj.worldPosition.copy() #front_start = start - 0.30 * tvfront_start = start - 0.30 * tv back_start = start + 0.15 * tv start.z += .1 end = Vector.copy(self.obj.worldPosition + (self.obj.worldOrientation.col[axis] * distance)) ray = self.obj.rayCast(end, back_start, 0, 'ground', 1, 0, 0) v = self.obj.linearVelocity.copy() v = self.obj.worldOrientation.col[0] tv = v.normalized() front_start = start - 0.30 * tv start.z += .1 front_end = Vector(front_start + (self.obj.worldOrientation.col[axis] * distance)) frontray = self.obj.rayCast(front_end, front_start, 0, 'ground', 1, 1, 0, 53247) bge.render.drawLine(front_start, front_end, [1,0,0]) #print(ray) return [ray, frontray] def get_ground_dist(self, groundray): gr = groundray[0] fgr = groundray[1] #when player is on the ground, dist is .25 d = None if gr[0] != None: p_z = self.obj.worldPosition.z g_z = gr[1].z distance_to_ground = p_z - g_z p2 = None if fgr[0] != None: p2 = p_z - fgr[1].z if p2 < distance_to_ground: #front to ground is bigger than back to ground #val = p2 - distance_to_ground #val *= .1 print(fgr[0], 'ray hit obj') distance_to_ground = p2 return distance_to_ground def set_walk_z(self, dist): if dist < .6: target_dist = .3 #z_targ = self.obj.worldPosition.z + (target_dist - dist) self.obj.worldPosition.z += (target_dist - dist) self.obj.linearVelocity.z = 0 def walk_movement(self): moving = False o = self.obj o.linearVelocity.y *= .9 o.linearVelocity.x *= .95 if dict['kb_w'] == 2 or dict['lUD'] < -.05: #walk_blend(self) #run if (dict['kb_lsh'] == 2 or dict['aBut'] == 1) and o.linearVelocity.x > -dict['max_run_vel']: #self.run_speed = 1.5 #o.applyForce([-dict['run_force'], 0, 0], True) o.linearVelocity.x -= .2 #walk elif o.linearVelocity.x > -dict['max_walk_vel']: #o.applyForce([-dict['walk_force'], 0, 0], True) o.linearVelocity.x -= .1 if dict['kb_s'] == 2 or dict['lUD'] > .06: #o.applyForce([10, 0, 0], True) o.linearVelocity.x -= 1 #print('w') if dict['kb_a'] == 2 or dict['lLR'] < -.03: o.applyRotation([0, 0, dict['walk_turn_amt']], True) #print('w') if dict['kb_d'] == 2 or dict['lLR'] > .03: o.applyRotation([0, 0, -dict['walk_turn_amt']], True) #print('linvel', o.linearVelocity) return moving def update(cont): #print('-------------player updating----------------') dict = bge.logic.globalDict #own = cont.owner if 'player_class' not in dict: #dict['playerFSM'] = FSM.PlayerFSM(own) dict['player_class'] = Player(cont) #dict['playerFSM'].Execute() dict['player_class'].update()