Shuvit game master repo. http://shuvit.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

player.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import bge
  2. import FSM
  3. from mathutils import Vector
  4. dict = bge.logic.globalDict
  5. class Player:
  6. def __init__(self, cont):
  7. self.cont = cont
  8. self.obj = cont.owner
  9. self.FSM = FSM.PlayerFSM(self)
  10. self.walking = True
  11. dict['walking'] = True
  12. #print(self.obj.childrenRecursive)
  13. #self.arm = self.obj.childrenRecursive['Char4']
  14. self.arm = None
  15. def update(self):
  16. self.FSM.Execute()
  17. def get_ground_ray(self):
  18. v = self.obj.worldOrientation.col[0]
  19. tv = v.normalized()
  20. axis = 2
  21. distance = -5
  22. start = self.obj.worldPosition.copy()
  23. #front_start = start - 0.30 * tvfront_start = start - 0.30 * tv
  24. back_start = start + 0.15 * tv
  25. start.z += .1
  26. end = Vector.copy(self.obj.worldPosition + (self.obj.worldOrientation.col[axis] * distance))
  27. ray = self.obj.rayCast(end, back_start, 0, 'ground', 1, 0, 0)
  28. v = self.obj.linearVelocity.copy()
  29. v = self.obj.worldOrientation.col[0]
  30. tv = v.normalized()
  31. front_start = start - 0.30 * tv
  32. start.z += .1
  33. front_end = Vector(front_start + (self.obj.worldOrientation.col[axis] * distance))
  34. frontray = self.obj.rayCast(front_end, front_start, 0, 'ground', 1, 1, 0, 53247)
  35. bge.render.drawLine(front_start, front_end, [1,0,0])
  36. #print(ray)
  37. return [ray, frontray]
  38. def get_ground_dist(self, groundray):
  39. gr = groundray[0]
  40. fgr = groundray[1]
  41. #when player is on the ground, dist is .25
  42. d = None
  43. if gr[0] != None:
  44. p_z = self.obj.worldPosition.z
  45. g_z = gr[1].z
  46. distance_to_ground = p_z - g_z
  47. p2 = None
  48. if fgr[0] != None:
  49. p2 = p_z - fgr[1].z
  50. if p2 < distance_to_ground:
  51. #front to ground is bigger than back to ground
  52. #val = p2 - distance_to_ground
  53. #val *= .1
  54. print(fgr[0], 'ray hit obj')
  55. distance_to_ground = p2
  56. return distance_to_ground
  57. def set_walk_z(self, dist):
  58. if dist < .6:
  59. target_dist = .3
  60. #z_targ = self.obj.worldPosition.z + (target_dist - dist)
  61. self.obj.worldPosition.z += (target_dist - dist)
  62. self.obj.linearVelocity.z = 0
  63. def walk_movement(self):
  64. moving = False
  65. o = self.obj
  66. o.linearVelocity.y *= .9
  67. o.linearVelocity.x *= .95
  68. if dict['kb_w'] == 2 or dict['lUD'] < -.05:
  69. #walk_blend(self)
  70. #run
  71. if (dict['kb_lsh'] == 2 or dict['aBut'] == 1) and o.linearVelocity.x > -dict['max_run_vel']:
  72. #self.run_speed = 1.5
  73. #o.applyForce([-dict['run_force'], 0, 0], True)
  74. o.linearVelocity.x -= .2
  75. #walk
  76. elif o.linearVelocity.x > -dict['max_walk_vel']:
  77. #o.applyForce([-dict['walk_force'], 0, 0], True)
  78. o.linearVelocity.x -= .1
  79. if dict['kb_s'] == 2 or dict['lUD'] > .06:
  80. #o.applyForce([10, 0, 0], True)
  81. o.linearVelocity.x -= 1
  82. #print('w')
  83. if dict['kb_a'] == 2 or dict['lLR'] < -.03:
  84. o.applyRotation([0, 0, dict['walk_turn_amt']], True)
  85. #print('w')
  86. if dict['kb_d'] == 2 or dict['lLR'] > .03:
  87. o.applyRotation([0, 0, -dict['walk_turn_amt']], True)
  88. #print('linvel', o.linearVelocity)
  89. return moving
  90. def update(cont):
  91. #print('-------------player updating----------------')
  92. dict = bge.logic.globalDict
  93. #own = cont.owner
  94. if 'player_class' not in dict:
  95. #dict['playerFSM'] = FSM.PlayerFSM(own)
  96. dict['player_class'] = Player(cont)
  97. #dict['playerFSM'].Execute()
  98. dict['player_class'].update()