No Description
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.gd 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. extends KinematicBody2D
  2. var speed = Vector2.ZERO
  3. export var jump_speed = 1100.0
  4. export var gravity = 60.0
  5. export var jump_time = 0.2
  6. export var max_lives = 1
  7. enum {
  8. RUN,
  9. JUMP,
  10. IDLE
  11. }
  12. enum {
  13. SCORE,
  14. TIME
  15. }
  16. var jump_key = "dialogic_next"
  17. var player_enabled = false
  18. var state = RUN
  19. var au_sol = true
  20. var current_lives = max_lives
  21. var pressing_jump = false
  22. onready var animation := $AnimatedSprite
  23. onready var bonus_texture := $BonusControl/Sprite
  24. onready var bonus_tween := $BonusControl/Tween
  25. onready var bonus_timer := $BonusControl/Timer
  26. onready var jump_timer := $JumpTimer
  27. signal hit()
  28. signal die()
  29. signal score()
  30. func _ready():
  31. jump_timer.wait_time = jump_time
  32. stop()
  33. func _physics_process(delta):
  34. if player_enabled:
  35. match state:
  36. RUN:
  37. animation.play("man_run")
  38. JUMP:
  39. speed = Vector2.ZERO
  40. speed.y -= jump_speed
  41. animation.play("man_jump")
  42. if not pressing_jump:
  43. state = IDLE
  44. IDLE:
  45. pass
  46. speed.y += gravity
  47. move_and_collide(speed*delta)
  48. func _input(event: InputEvent):
  49. if player_enabled and state == RUN and event.is_action_pressed(jump_key):
  50. jump_timer.start()
  51. pressing_jump = true
  52. state = JUMP
  53. elif event.is_action_released(jump_key):
  54. jump_timer.stop()
  55. pressing_jump = false
  56. func _on_Area2D_body_entered(body):
  57. if body is StaticBody2D:
  58. state = RUN
  59. func _on_Area2D_body_exited(body):
  60. if body is StaticBody2D:
  61. state = JUMP
  62. func start():
  63. show()
  64. player_enabled = true
  65. func stop():
  66. hide()
  67. bonus_texture.modulate = Color(1, 1, 1, 0)
  68. player_enabled = false
  69. func hit():
  70. current_lives -= 1
  71. if current_lives > 0:
  72. emit_signal("hit")
  73. else:
  74. current_lives = 0
  75. emit_signal("die")
  76. func play_bonus_anim():
  77. bonus_texture.show()
  78. bonus_tween.stop_all()
  79. bonus_timer.stop()
  80. bonus_tween.interpolate_property(bonus_texture, "modulate", null, Color(1, 1, 1, 1), 0.3, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
  81. bonus_tween.interpolate_property(bonus_texture, "position", Vector2(bonus_texture.position.x, 0), Vector2(bonus_texture.position.x, bonus_texture.position.y), 0.3, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
  82. bonus_tween.start()
  83. bonus_timer.start()
  84. func bonus():
  85. play_bonus_anim()
  86. emit_signal("score")
  87. func _on_Timer_timeout() -> void:
  88. bonus_tween.stop_all()
  89. bonus_tween.interpolate_property(bonus_texture, "modulate", null, Color(1, 1, 1, 0), 0.3, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
  90. bonus_tween.start()
  91. func _on_JumpTimer_timeout() -> void:
  92. pressing_jump = false