level.gd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. extends Spatial
  2. onready var player_green = get_tree().get_root().find_node("PlayerGreen", true, false)
  3. onready var player_blue = get_tree().get_root().find_node("PlayerBlue", true, false)
  4. onready var goal_green = get_tree().get_root().find_node("GoalGreen", true, false)
  5. onready var goal_blue = get_tree().get_root().find_node("GoalBlue", true, false)
  6. var active_player = ""
  7. var box_buttons_colored = null
  8. var box_buttons_green = null
  9. var box_buttons_blue = null
  10. var has_bb_green = false
  11. var has_bb_blue = false
  12. var all_bb_green_triggered = false
  13. var all_bb_blue_triggered = false
  14. func _ready():
  15. player_green.set_state(player_green.STATE.ACTIVE)
  16. active_player = "green"
  17. Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
  18. get_box_buttons()
  19. if (has_bb_green):
  20. change_goal_status("green", all_bb_green_triggered)
  21. if (has_bb_blue):
  22. change_goal_status("blue", all_bb_blue_triggered)
  23. #if (has_bb_green):
  24. # print ("has green bb")
  25. #if (has_bb_blue):
  26. # print ("has blue bb")
  27. func _process(delta):
  28. if player_green.current_state == player_green.STATE.FINISHED and player_blue.current_state == player_blue.STATE.FINISHED:
  29. get_tree().change_scene("res://UI/LevelDoneScreen.tscn")
  30. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  31. elif player_green.current_state == player_green.STATE.FINISHED and player_blue.current_state != player_blue.STATE.IN_FINISH_AREA:
  32. player_blue.set_state(player_blue.STATE.ACTIVE)
  33. elif player_blue.current_state == player_blue.STATE.FINISHED and player_green.current_state != player_green.STATE.IN_FINISH_AREA:
  34. player_green.set_state(player_green.STATE.ACTIVE)
  35. if Input.is_action_just_pressed("switch_player"):
  36. if !(player_green.current_state == player_green.STATE.FINISHED or player_blue.current_state == player_blue.STATE.FINISHED):
  37. if active_player == "green":
  38. active_player = "blue"
  39. player_green.set_state(player_green.STATE.INACTIVE)
  40. player_blue.set_state(player_blue.STATE.ACTIVE)
  41. elif active_player == "blue":
  42. active_player = "green"
  43. player_green.set_state(player_green.STATE.ACTIVE)
  44. player_blue.set_state(player_blue.STATE.INACTIVE)
  45. elif player_green.current_state == player_green.STATE.FINISHED and player_blue.current_state == player_blue.STATE.FINISHED:
  46. pass
  47. elif player_green.current_state == player_green.STATE.FINISHED:
  48. player_blue.set_state(player_blue.STATE.ACTIVE)
  49. elif player_blue.current_state == player_blue.STATE.FINISHED:
  50. player_green.set_state(player_green.STATE.ACTIVE)
  51. else:
  52. print ("what the heck happened here???")
  53. if Input.is_action_just_pressed("restart"):
  54. get_tree().reload_current_scene()
  55. func get_box_buttons():
  56. box_buttons_colored = get_tree().get_nodes_in_group("group_box_button")
  57. box_buttons_green = get_tree().get_nodes_in_group("group_box_button_green")
  58. box_buttons_blue = get_tree().get_nodes_in_group("group_box_button_blue")
  59. has_bb_green = !box_buttons_green.empty()
  60. has_bb_blue = !box_buttons_blue.empty()
  61. for box in box_buttons_colored:
  62. box.connect("box_button_status_changed", self, "_on_box_button_changed")
  63. func _on_box_button_changed():
  64. all_bb_green_triggered = true
  65. all_bb_blue_triggered = true
  66. for box_green in box_buttons_green:
  67. if !box_green.triggered:
  68. all_bb_green_triggered = false
  69. for box_blue in box_buttons_blue:
  70. if !box_blue.triggered:
  71. all_bb_blue_triggered = false
  72. #print ("all green triggered?: " + str(all_bb_green_triggered))
  73. #print ("all blue triggered?: " + str(all_bb_blue_triggered))
  74. change_goal_status("green", all_bb_green_triggered)
  75. change_goal_status("blue", all_bb_blue_triggered)
  76. #FIXME: check if status changed
  77. func change_goal_status(color, status):
  78. #print ("change goal status: " + str(color) + " = " + str(status))
  79. if (color == "green"):
  80. goal_green.enable_goal(status)
  81. if (color == "blue"):
  82. goal_blue.enable_goal(status)