player.gd 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. extends RigidBody2D
  2. # Character Demo, written by Juan Linietsky.
  3. #
  4. # Implementation of a 2D Character controller.
  5. # This implementation uses the physics engine for
  6. # controlling a character, in a very similar way
  7. # than a 3D character controller would be implemented.
  8. #
  9. # Using the physics engine for this has the main
  10. # advantages:
  11. # -Easy to write.
  12. # -Interaction with other physics-based objects is free
  13. # -Only have to deal with the object linear velocity, not position
  14. # -All collision/area framework available
  15. #
  16. # But also has the following disadvantages:
  17. #
  18. # -Objects may bounce a little bit sometimes
  19. # -Going up ramps sends the chracter flying up, small hack is needed.
  20. # -A ray collider is needed to avoid sliding down on ramps and
  21. # undesiderd bumps, small steps and rare numerical precision errors.
  22. # (another alternative may be to turn on friction when the character is not moving).
  23. # -Friction cant be used, so floor velocity must be considered
  24. # for moving platforms.
  25. var anim=""
  26. var siding_left=false
  27. var jumping=false
  28. var stopping_jump=false
  29. var shooting=false
  30. var WALK_ACCEL = 800.0
  31. var WALK_DEACCEL= 800.0
  32. var WALK_MAX_VELOCITY= 200.0
  33. var GRAVITY = 700.0
  34. var AIR_ACCEL = 200.0
  35. var AIR_DEACCEL= 200.0
  36. var JUMP_VELOCITY=460
  37. var STOP_JUMP_FORCE=900.0
  38. var MAX_FLOOR_AIRBORNE_TIME = 0.15
  39. var airborne_time=1e20
  40. var shoot_time=1e20
  41. var MAX_SHOOT_POSE_TIME = 0.3
  42. var bullet = preload("res://bullet.xml")
  43. var floor_h_velocity=0.0
  44. var enemy
  45. func _integrate_forces(s):
  46. var lv = s.get_linear_velocity()
  47. var step = s.get_step()
  48. var new_anim=anim
  49. var new_siding_left=siding_left
  50. # Get the controls
  51. var move_left = Input.is_action_pressed("move_left")
  52. var move_right = Input.is_action_pressed("move_right")
  53. var jump = Input.is_action_pressed("jump")
  54. var shoot = Input.is_action_pressed("shoot")
  55. var spawn = Input.is_action_pressed("spawn")
  56. if spawn:
  57. var e = enemy.instance()
  58. var p = get_pos()
  59. p.y = p.y - 100
  60. e.set_pos(p)
  61. get_parent().add_child(e)
  62. #deapply prev floor velocity
  63. lv.x-=floor_h_velocity
  64. floor_h_velocity=0.0
  65. # Find the floor (a contact with upwards facing collision normal)
  66. var found_floor=false
  67. var floor_index=-1
  68. for x in range(s.get_contact_count()):
  69. var ci = s.get_contact_local_normal(x)
  70. if (ci.dot(Vector2(0,-1))>0.6):
  71. found_floor=true
  72. floor_index=x
  73. # A good idea when impementing characters of all kinds,
  74. # Compensates for physics imprecission, as well as human
  75. # reaction delay.
  76. if (shoot and not shooting):
  77. shoot_time=0
  78. var bi = bullet.instance()
  79. var ss
  80. if (siding_left):
  81. ss=-1.0
  82. else:
  83. ss=1.0
  84. var pos = get_pos() + get_node("bullet_shoot").get_pos()*Vector2(ss,1.0)
  85. bi.set_pos(pos)
  86. get_parent().add_child(bi)
  87. bi.set_linear_velocity( Vector2(800.0*ss,-80) )
  88. get_node("sprite/smoke").set_emitting(true)
  89. get_node("sound").play("shoot")
  90. PS2D.body_add_collision_exception(bi.get_rid(),get_rid()) # make bullet and this not collide
  91. else:
  92. shoot_time+=step
  93. if (found_floor):
  94. airborne_time=0.0
  95. else:
  96. airborne_time+=step #time it spent in the air
  97. var on_floor = airborne_time < MAX_FLOOR_AIRBORNE_TIME
  98. # Process jump
  99. if (jumping):
  100. if (lv.y>0):
  101. #set off the jumping flag if going down
  102. jumping=false
  103. elif (not jump):
  104. stopping_jump=true
  105. if (stopping_jump):
  106. lv.y+=STOP_JUMP_FORCE*step
  107. if (on_floor):
  108. # Process logic when character is on floor
  109. if (move_left and not move_right):
  110. if (lv.x > -WALK_MAX_VELOCITY):
  111. lv.x-=WALK_ACCEL*step
  112. elif (move_right and not move_left):
  113. if (lv.x < WALK_MAX_VELOCITY):
  114. lv.x+=WALK_ACCEL*step
  115. else:
  116. var xv = abs(lv.x)
  117. xv-=WALK_DEACCEL*step
  118. if (xv<0):
  119. xv=0
  120. lv.x=sign(lv.x)*xv
  121. #Check jump
  122. if (not jumping and jump):
  123. lv.y=-JUMP_VELOCITY
  124. jumping=true
  125. stopping_jump=false
  126. get_node("sound").play("jump")
  127. #check siding
  128. if (lv.x < 0 and move_left):
  129. new_siding_left=true
  130. elif (lv.x > 0 and move_right):
  131. new_siding_left=false
  132. if (jumping):
  133. new_anim="jumping"
  134. elif (abs(lv.x)<0.1):
  135. if (shoot_time<MAX_SHOOT_POSE_TIME):
  136. new_anim="idle_weapon"
  137. else:
  138. new_anim="idle"
  139. else:
  140. if (shoot_time<MAX_SHOOT_POSE_TIME):
  141. new_anim="run_weapon"
  142. else:
  143. new_anim="run"
  144. else:
  145. # Process logic when the character is in the air
  146. if (move_left and not move_right):
  147. if (lv.x > -WALK_MAX_VELOCITY):
  148. lv.x-=AIR_ACCEL*step
  149. elif (move_right and not move_left):
  150. if (lv.x < WALK_MAX_VELOCITY):
  151. lv.x+=AIR_ACCEL*step
  152. else:
  153. var xv = abs(lv.x)
  154. xv-=AIR_DEACCEL*step
  155. if (xv<0):
  156. xv=0
  157. lv.x=sign(lv.x)*xv
  158. if (lv.y<0):
  159. if (shoot_time<MAX_SHOOT_POSE_TIME):
  160. new_anim="jumping_weapon"
  161. else:
  162. new_anim="jumping"
  163. else:
  164. if (shoot_time<MAX_SHOOT_POSE_TIME):
  165. new_anim="falling_weapon"
  166. else:
  167. new_anim="falling"
  168. #Update siding
  169. if (new_siding_left!=siding_left):
  170. if (new_siding_left):
  171. get_node("sprite").set_scale( Vector2(-1,1) )
  172. else:
  173. get_node("sprite").set_scale( Vector2(1,1) )
  174. siding_left=new_siding_left
  175. #Change animation
  176. if (new_anim!=anim):
  177. anim=new_anim
  178. get_node("anim").play(anim)
  179. shooting=shoot
  180. # Apply floor velocity
  181. if (found_floor):
  182. floor_h_velocity=s.get_contact_collider_velocity_at_pos(floor_index).x
  183. lv.x+=floor_h_velocity
  184. #Finally, apply gravity and set back the linear velocity
  185. lv+=s.get_total_gravity()*step
  186. s.set_linear_velocity(lv)
  187. func _ready():
  188. # Initalization here
  189. # if !Globals.has_singleton("Facebook"):
  190. # return
  191. # var Facebook = Globals.get_singleton("Facebook")
  192. # var link = Globals.get("facebook/link")
  193. # var icon = Globals.get("facebook/icon")
  194. # var msg = "I just sneezed on your wall! Beat my score and Stop the Running nose!"
  195. # var title = "I just sneezed on your wall!"
  196. # Facebook.post("feed", msg, title, link, icon)
  197. enemy = ResourceLoader.load("res://enemy.xml")
  198. pass