Player.gd 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. extends KinematicBody2D
  2. export (int) var SPEED = 200
  3. var velocity = Vector2()
  4. var shooting = true
  5. onready var timer = get_node("Timer")
  6. func _ready():
  7. # Called when the node is added to the scene for the first time.
  8. # Initialization here
  9. timer.stop()
  10. pass
  11. func get_input():
  12. velocity = Vector2()
  13. if Input.is_action_pressed('ui_right'):
  14. velocity.x += 1
  15. elif Input.is_action_pressed('ui_left'):
  16. velocity.x -= 1
  17. elif Input.is_action_pressed('ui_down'):
  18. velocity.y += 1
  19. elif Input.is_action_pressed('ui_up'):
  20. velocity.y -= 1
  21. velocity = velocity.normalized() * SPEED
  22. func _process(delta):
  23. var speed_x = 0
  24. position.x = position.x + speed_x
  25. if Input.is_action_pressed("ui_accept") :
  26. if timer.is_stopped():
  27. var fireball = load("res://bullet.tscn").instance()
  28. var spawner = get_node("Position2D")
  29. fireball.position = spawner.get_global_position()
  30. get_node("/root").add_child(fireball)
  31. restart_timer()
  32. pass
  33. func _physics_process(delta):
  34. get_input()
  35. var collision_info = move_and_collide(velocity * delta)
  36. if collision_info:
  37. collision_info.get_collider().free()
  38. free()
  39. #func _process(delta):
  40. # # Called every frame. Delta is time since last frame.
  41. # # Update game logic here.
  42. # pass
  43. func restart_timer():
  44. timer.start()
  45. timer.set_wait_time(0.5)
  46. func _on_Timer_timeout():
  47. timer.stop()