Player.gd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. extends KinematicBody2D
  2. const AutoRotate = true # Autorotate player to move direction
  3. const DefaultSpeedValue = 200 # Player speed value
  4. var Speed = DefaultSpeedValue # Player speed value (for processing)
  5. var Velocity = Vector2() # Player velocity
  6. var Health = 60 # Player's health
  7. var Power = 100 # Player's power
  8. var second_jump = true # Flag for double jump (Is allow second jump?)
  9. var release_up = true # Flag for double jump (The space was released?)
  10. var is_squat = false # Is squat state?
  11. var allow_power = true
  12. var shot_button_release = true # Flag for shot key
  13. const Floor = Vector2(0, -1) # Floor
  14. const Gravity = 2500 # Gravity value
  15. const JumpPowerUp = 800 # Jump power value
  16. const ShiftPower = 2.5 # Shift turbo value
  17. const SitPower = -150 # Sit down power
  18. # Load Plasma
  19. const Plasma = preload("res://assets/Bullet/Bullet.tscn")
  20. func _ready():
  21. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  22. func step():
  23. if Input.is_action_pressed("ui_left"):
  24. Velocity.x = -Speed
  25. if AutoRotate:
  26. $AnimatedSprite.flip_h = true
  27. $AnimatedSprite.play("Walk")
  28. elif Input.is_action_pressed("ui_right"):
  29. Velocity.x = Speed
  30. if AutoRotate:
  31. $AnimatedSprite.flip_h = false
  32. $AnimatedSprite.play("Walk")
  33. else:
  34. Velocity.x = 0
  35. Speed = DefaultSpeedValue
  36. $AnimatedSprite.stop()
  37. $AnimatedSprite.play("posible")
  38. func jump():
  39. # Jump buttons processing
  40. if Input.is_action_pressed("ui_up"):
  41. if is_on_floor():
  42. Velocity.y = -JumpPowerUp
  43. $AnimatedSprite.play("jump")
  44. release_up = false
  45. elif second_jump and release_up:
  46. Velocity.y -= JumpPowerUp
  47. second_jump = false
  48. release_up = false
  49. func run_and_squat():
  50. # Run
  51. if Input.is_action_pressed("ui_acceleration") and \
  52. !is_squat and Power > 0 and allow_power:
  53. Speed = DefaultSpeedValue * ShiftPower
  54. # Squat
  55. elif Input.is_action_pressed("ui_squat"):
  56. Speed = DefaultSpeedValue + SitPower
  57. $AnimatedSprite.play("SquatAction")
  58. $Collision.set_disabled(true)
  59. $CollisionSit.set_disabled(false)
  60. is_squat = true
  61. else:
  62. Speed = DefaultSpeedValue
  63. $Collision.set_disabled(false)
  64. $CollisionSit.set_disabled(true)
  65. is_squat = false
  66. if Power == 0:
  67. $RecoveryPower.start()
  68. allow_power = false
  69. func reset_flags():
  70. # Reset flags
  71. if is_on_floor():
  72. second_jump = true
  73. if !Input.is_action_pressed("ui_up"):
  74. release_up = true
  75. if !Input.is_action_pressed("ui_shot"):
  76. shot_button_release = true
  77. func shot_check():
  78. # Shot key proccesing
  79. if Input.is_action_pressed("ui_shot") and shot_button_release:
  80. var plasma = Plasma.instance()
  81. plasma.position.x = $GunPosition.global_position.x
  82. plasma.position.y = $GunPosition.global_position.y
  83. if $AnimatedSprite.flip_h: # Left player direction
  84. plasma.SPEED = -plasma.SPEED
  85. get_parent().add_child(plasma)
  86. shot_button_release = false
  87. func add_health(quantity):
  88. if Health < 100:
  89. Health += quantity
  90. func _physics_process(_delta):
  91. step()
  92. jump()
  93. run_and_squat()
  94. shot_check()
  95. reset_flags()
  96. # Calculate the physics and move the player
  97. Velocity.y += (Gravity * _delta)
  98. Velocity = move_and_slide(Velocity, Floor)
  99. func _on_RunTimer_timeout():
  100. if Speed == DefaultSpeedValue * ShiftPower:
  101. Power -= 25
  102. elif Power < 100:
  103. Power += 20
  104. func _on_RecoveryPower_timeout():
  105. allow_power = true
  106. $RecoveryPower.stop()