display.gd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. extends Area2D
  2. class_name Display
  3. enum State {
  4. EMPTY,
  5. PLAYER,
  6. NOISE,
  7. CRACKED,
  8. }
  9. @onready var parent = get_parent()
  10. const speed : float = Globals.tile_size * 9
  11. const jump_height : float = Globals.tile_size * 2.25
  12. const jump_velocity : float = -sqrt(jump_height * 2 * Globals.gravity)
  13. var display : Sprite2D
  14. var noise : Sprite2D
  15. var player : Sprite2D
  16. var cracked_display : Sprite2D
  17. var index : Label
  18. @export var state : State : set = _set_state
  19. static var box_count = 0
  20. static var numbers_displayed = false
  21. var current_box_index = 0
  22. static func reset() -> void:
  23. box_count = 0
  24. numbers_displayed = false
  25. func _set_state(new_state: State) -> void:
  26. state = new_state
  27. #workaround for onready setters
  28. if not is_inside_tree():
  29. await ready
  30. match new_state:
  31. State.EMPTY:
  32. display.visible = true
  33. noise.visible = false
  34. player.visible = false
  35. cracked_display.visible = false
  36. input_pickable = true
  37. State.PLAYER:
  38. display.visible = true
  39. noise.visible = false
  40. player.visible = true
  41. cracked_display.visible = false
  42. input_pickable = false
  43. State.NOISE:
  44. display.visible = true
  45. noise.visible = true
  46. player.visible = false
  47. cracked_display.visible = false
  48. input_pickable = false
  49. State.CRACKED:
  50. display.visible = true
  51. noise.visible = false
  52. player.visible = false
  53. cracked_display.visible = true
  54. input_pickable = false
  55. func _ready() -> void:
  56. EventBus.player_moving_to_box.connect(move_to_box)
  57. display = $Sprites/Display
  58. noise = $Sprites/Noise
  59. player = $Sprites/Player
  60. cracked_display = $Sprites/CrackedDisplay
  61. index = $Sprites/Index
  62. if state == State.EMPTY or state == State.PLAYER:
  63. box_count += 1
  64. current_box_index = box_count
  65. index.text = str(current_box_index)
  66. func _physics_process(_delta: float) -> void:
  67. if state != State.PLAYER:
  68. return
  69. if numbers_displayed:
  70. return
  71. if Input.is_action_just_pressed("player_jump") and parent.is_on_floor():
  72. parent.velocity.y = jump_velocity
  73. var direction := Input.get_axis("player_left", "player_right")
  74. if direction:
  75. parent.velocity.x = direction * speed
  76. parent.getting_force = true
  77. else:
  78. parent.getting_force = false
  79. func move_to_box(box : Display) -> void:
  80. if state == State.PLAYER:
  81. if box.index.visible:
  82. box.index.visible = false
  83. index.visible = true
  84. state = State.EMPTY
  85. parent.velocity.x = 0
  86. box.state = State.PLAYER
  87. func _on_input_event(_viewport, event: InputEvent, _shape) -> void:
  88. if event is InputEventMouseButton and event.pressed:
  89. EventBus.player_moving_to_box.emit(self)
  90. func _on_area_entered(_area: Area2D) -> void:
  91. if state == State.EMPTY:
  92. state = State.NOISE
  93. elif state == State.PLAYER:
  94. state = State.NOISE
  95. await get_tree().create_timer(0.5).timeout
  96. SceneManager.change_to_scene("CURRENT")
  97. func _on_area_exited(_area: Area2D) -> void:
  98. if state == State.NOISE:
  99. state = State.EMPTY
  100. func _unhandled_key_input(event: InputEvent) -> void:
  101. event = event as InputEventKey
  102. if event.keycode == KEY_TAB and event.is_pressed() and !event.is_echo():
  103. if state == State.EMPTY:
  104. index.visible = !index.visible
  105. if OS.get_keycode_string(event.keycode) == str(current_box_index):
  106. if state == State.EMPTY:
  107. EventBus.player_moving_to_box.emit(self)