player.gd 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. extends RigidBody
  2. # member variables here, example:
  3. # var a=2
  4. # var b="textvar"
  5. #var dir=Vector3()
  6. const ANIM_FLOOR = 0
  7. const ANIM_AIR_UP = 1
  8. const ANIM_AIR_DOWN = 2
  9. const SHOOT_TIME = 1.5
  10. const SHOOT_SCALE = 2
  11. const CHAR_SCALE = Vector3(0.3,0.3,0.3)
  12. var facing_dir = Vector3(1, 0, 0)
  13. var movement_dir = Vector3()
  14. var jumping=false
  15. var turn_speed=40
  16. var keep_jump_inertia = true
  17. var air_idle_deaccel = false
  18. var accel=19.0
  19. var deaccel=14.0
  20. var sharp_turn_threshhold = 140
  21. var max_speed=3.1
  22. var on_floor = false
  23. var prev_shoot = false
  24. var last_floor_velocity = Vector3()
  25. var shoot_blend = 0
  26. func adjust_facing(p_facing, p_target,p_step, p_adjust_rate,current_gn):
  27. var n = p_target # normal
  28. var t = n.cross(current_gn).normalized()
  29. var x = n.dot(p_facing)
  30. var y = t.dot(p_facing)
  31. var ang = atan2(y,x)
  32. if (abs(ang)<0.001): # too small
  33. return p_facing
  34. var s = sign(ang)
  35. ang = ang * s
  36. var turn = ang * p_adjust_rate * p_step
  37. var a
  38. if (ang<turn):
  39. a=ang
  40. else:
  41. a=turn
  42. ang = (ang - a) * s
  43. return ((n * cos(ang)) + (t * sin(ang))) * p_facing.length()
  44. func _integrate_forces( state ):
  45. var lv = state.get_linear_velocity() # linear velocity
  46. var g = state.get_total_gravity()
  47. var delta = state.get_step()
  48. var d = 1.0 - delta*state.get_total_density()
  49. if (d<0):
  50. d=0
  51. lv += g * delta #apply gravity
  52. var anim = ANIM_FLOOR
  53. var up = -g.normalized() # (up is against gravity)
  54. var vv = up.dot(lv) # vertical velocity
  55. var hv = lv - (up*vv) # horizontal velocity
  56. var hdir = hv.normalized() # horizontal direction
  57. var hspeed = hv.length() #horizontal speed
  58. var floor_velocity
  59. var onfloor = false
  60. if (state.get_contact_count() == 0):
  61. floor_velocity = last_floor_velocity
  62. else:
  63. for i in range(state.get_contact_count()):
  64. if (state.get_contact_local_shape(i) != 1):
  65. continue
  66. onfloor = true
  67. floor_velocity = state.get_contact_collider_velocity_at_pos(i)
  68. break
  69. var dir = Vector3() #where does the player intend to walk to
  70. var cam_xform = get_node("target/camera").get_global_transform()
  71. if (Input.is_action_pressed("move_forward")):
  72. dir+=-cam_xform.basis[2]
  73. if (Input.is_action_pressed("move_backwards")):
  74. dir+=cam_xform.basis[2]
  75. if (Input.is_action_pressed("move_left")):
  76. dir+=-cam_xform.basis[0]
  77. if (Input.is_action_pressed("move_right")):
  78. dir+=cam_xform.basis[0]
  79. var jump_attempt = Input.is_action_pressed("jump")
  80. var shoot_attempt = Input.is_action_pressed("shoot")
  81. var target_dir = (dir - up*dir.dot(up)).normalized()
  82. if (onfloor):
  83. var sharp_turn = hspeed > 0.1 and rad2deg(acos(target_dir.dot(hdir))) > sharp_turn_threshhold
  84. if (dir.length()>0.1 and !sharp_turn) :
  85. if (hspeed > 0.001) :
  86. #linear_dir = linear_h_velocity/linear_vel
  87. #if (linear_vel > brake_velocity_limit and linear_dir.dot(ctarget_dir)<-cos(Math::deg2rad(brake_angular_limit)))
  88. # brake=true
  89. #else
  90. hdir = adjust_facing(hdir,target_dir,delta,1.0/hspeed*turn_speed,up)
  91. facing_dir = hdir
  92. else:
  93. hdir = target_dir
  94. if (hspeed<max_speed):
  95. hspeed+=accel*delta
  96. else:
  97. hspeed-=deaccel*delta
  98. if (hspeed<0):
  99. hspeed=0
  100. hv = hdir*hspeed
  101. var mesh_xform = get_node("Armature").get_transform()
  102. var facing_mesh=-mesh_xform.basis[0].normalized()
  103. facing_mesh = (facing_mesh - up*facing_mesh.dot(up)).normalized()
  104. facing_mesh = adjust_facing(facing_mesh,target_dir,delta,1.0/hspeed*turn_speed,up)
  105. var m3 = Matrix3(-facing_mesh,up,-facing_mesh.cross(up).normalized()).scaled( CHAR_SCALE )
  106. get_node("Armature").set_transform(Transform(m3,mesh_xform.origin))
  107. if (not jumping and jump_attempt):
  108. vv = 7.0
  109. jumping = true
  110. get_node("sfx").play("jump")
  111. else:
  112. if (vv>0):
  113. anim=ANIM_AIR_UP
  114. else:
  115. anim=ANIM_AIR_DOWN
  116. var hs
  117. if (dir.length()>0.1):
  118. hv += target_dir * (accel * 0.2) * delta
  119. if (hv.length() > max_speed):
  120. hv = hv.normalized() * max_speed
  121. else:
  122. if (air_idle_deaccel):
  123. hspeed = hspeed - (deaccel * 0.2) * delta
  124. if (hspeed<0):
  125. hspeed=0
  126. hv = hdir*hspeed
  127. if (jumping and vv < 0):
  128. jumping=false
  129. lv = hv+up*vv
  130. if (onfloor):
  131. movement_dir = lv
  132. #lv += floor_velocity
  133. last_floor_velocity = floor_velocity
  134. else:
  135. if (on_floor) :
  136. #if (keep_jump_inertia):
  137. # lv += last_floor_velocity
  138. pass
  139. last_floor_velocity = Vector3()
  140. movement_dir = lv
  141. on_floor = onfloor
  142. state.set_linear_velocity(lv)
  143. if (shoot_blend>0):
  144. shoot_blend -= delta * SHOOT_SCALE
  145. if (shoot_blend<0):
  146. shoot_blend=0
  147. if (shoot_attempt and not prev_shoot):
  148. shoot_blend = SHOOT_TIME
  149. var bullet = preload("res://bullet.scn").instance()
  150. bullet.set_transform( get_node("Armature/bullet").get_global_transform().orthonormalized() )
  151. get_parent().add_child( bullet )
  152. bullet.set_linear_velocity( get_node("Armature/bullet").get_global_transform().basis[2].normalized() * 20 )
  153. PS.body_add_collision_exception( bullet.get_rid(), get_rid() ) #add it to bullet
  154. get_node("sfx").play("shoot")
  155. prev_shoot = shoot_attempt
  156. if (onfloor):
  157. get_node("AnimationTreePlayer").blend2_node_set_amount("walk",hspeed / max_speed)
  158. get_node("AnimationTreePlayer").transition_node_set_current("state",anim)
  159. get_node("AnimationTreePlayer").blend2_node_set_amount("gun",min(shoot_blend,1.0))
  160. # state.set_angular_velocity(Vector3())
  161. func _ready():
  162. # Initalization here
  163. get_node("AnimationTreePlayer").set_active(true)
  164. pass