BeanBunny

Main.gd, Board Game

Aug 22nd, 2025
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D
  2. class_name MainBoard
  3.  
  4. @onready var pink_piece : Sprite2D = $PinkPiece
  5. @onready var blue_piece: Sprite2D = $BluePiece
  6. @export var game_spaces : Array[Spot]
  7. @export var question_boxes : Array[Question]
  8. var place : int = 0
  9. var number_of_spaces : int
  10. @onready var dice := $CanvasLayer/Dice
  11. @onready var timer := $Timer
  12. @onready var canvas_layer: CanvasLayer = $CanvasLayer
  13. var score : int = 0
  14. @onready var score_label: Label = $"CanvasLayer/Score Label"
  15. var pink_piece_turn : bool = true
  16. @onready var turn_label: PanelContainer = $CanvasLayer/TurnLabel
  17. @onready var winner_screen: CenterContainer = $"CanvasLayer/Winner Screen"
  18. @onready var piece
  19. @onready var audio_stream_player: AudioStreamPlayer = $AudioStreamPlayer
  20. @onready var transition_camera: Camera2D = $TransitionCamera
  21. @export var board_number : int
  22. @export var bounceback_board : bool
  23.  
  24. const QUESTION_BOX = preload("res://Question Boxes/questionbox.tscn")
  25.  
  26. func _ready() -> void:
  27.     number_of_spaces = game_spaces.size()
  28.     print(number_of_spaces)
  29.     Events.question_box_gone.connect(on_question_box_gone)
  30.     Events.send_piece.connect(on_send_piece)
  31.     Fader.fade_in()
  32.     pink_piece.human = Events.pink_human_controlled
  33.     blue_piece.human = Events.blue_human_controlled
  34.     print(pink_piece.human)
  35.     print(blue_piece.human)
  36.    
  37. func on_send_piece(sent_piece):
  38.     pink_piece_turn = sent_piece
  39.     if pink_piece_turn:
  40.         pink_piece.camera_2d.enabled = true
  41.     else:
  42.         blue_piece.camera_2d.enabled = true
  43.     whose_turn_is_it()
  44.     timer.start()
  45.     await timer.timeout
  46.     if piece.human == false:
  47.         take_a_turn()
  48.    
  49. func _input(event: InputEvent) -> void:
  50.     whose_turn_is_it()
  51.     if event.is_action_pressed("ui_click") and dice.can_click == true:
  52.         if piece.human == true:
  53.             take_a_turn()
  54.    
  55. func take_a_turn():
  56.     print(piece)
  57.     if piece.i_won == false:
  58.         dice.roll()
  59.         print("The dice should roll because we hit this")
  60.     else:
  61.         pink_piece_turn = !pink_piece_turn
  62.         turn_label_switcher()
  63.  
  64. func _on_dice_dice_has_rolled(roll: Variant) -> void:
  65.     whose_turn_is_it()
  66.     #This line is for testing. Uncomment it and change roll to whatever you need.
  67.     #roll = 6
  68.     print(pink_piece.i_won)
  69.     print(blue_piece.i_won)
  70.     while roll != 0:
  71.         if piece.place < game_spaces.size():
  72.             #if we have not won
  73.             await(move(piece, piece.place))
  74.             piece.place += 1
  75.             roll -= 1
  76.             print(piece.place)
  77.         else:
  78.             #if we have won
  79.             if blue_piece.place >= game_spaces.size() and pink_piece.place >= game_spaces.size():
  80.                 #if both of these pieces are at the winner's circle
  81.                 win()
  82.                 break
  83.             elif piece.place >= game_spaces.size():
  84.                 #if just one is at the winner's circle
  85.                 if roll > 0 and bounceback_board == true:
  86.                     await(bounceback(roll))
  87.                     return
  88.                 piece.place = game_spaces.size()
  89.                 turn_switcher()
  90.                 turn_label_switcher()
  91.                 return
  92.     #this check tells us if we've stopped
  93.     where_are_we(roll)
  94.    
  95. func where_are_we(roll):
  96.     if roll == 0:
  97.         if (bounceback_board and piece.place == game_spaces.size()) or (piece.place >= game_spaces.size()):
  98.             check_for_win()
  99.             return
  100.         dice.can_click = true
  101.         await(move(piece, piece.place))
  102.         if pink_piece_turn:
  103.             print("pink piece's turn")
  104.             pink_piece_turn = false
  105.         else:
  106.             print("blue piece turn")
  107.             pink_piece_turn = true
  108.         if game_spaces[piece.place].direction == Direction.WhichWay.BACK:
  109.             await(backwards())
  110.         if game_spaces[piece.place].direction == Direction.WhichWay.FORWARD:
  111.             await(forwards())
  112.         if game_spaces[piece.place].direction == Direction.WhichWay.QUESTION:
  113.             question_boxes.shuffle()
  114.             #LOAD IT
  115.             #we already loaded it!
  116.             #INSTANCE IT
  117.             var question = QUESTION_BOX.instantiate()
  118.             #ADD IT
  119.             question.question_box_resource = question_boxes.front()
  120.             canvas_layer.add_child(question)
  121.             dice.can_click = false
  122.         if game_spaces[piece.place].direction == Direction.WhichWay.REGULAR:
  123.             turn_label_switcher()
  124.  
  125. func move(piece, place) -> void:
  126.     if piece.place < game_spaces.size():
  127.         #print("Move says piece.place is")
  128.         #print(piece.place)
  129.         dice.can_click = false
  130.         var tween = create_tween()
  131.         tween.tween_property(piece, "position", game_spaces[place].position, 1)
  132.         tween.finished.connect(audio_stream_player.play)
  133.         timer.start()
  134.         await timer.timeout
  135.    
  136. func on_question_box_gone(point):
  137.     if point == true:
  138.         if !pink_piece_turn:
  139.             pink_piece.score = pink_piece.score + 1
  140.             score_label.text = "Pink: " + str(pink_piece.score) + "\nBlue: " + str(blue_piece.score)
  141.         else:
  142.             blue_piece.score = blue_piece.score + 1
  143.             score_label.text = "Pink: " + str(pink_piece.score) + "\nBlue: " + str(blue_piece.score)
  144.     await get_tree().process_frame
  145.     dice.can_click = true
  146.     turn_label_switcher()
  147.  
  148. func turn_label_switcher():
  149.     if !pink_piece.i_won or !blue_piece.i_won:
  150.         turn_label.visible = true
  151.         #Calling whose turn is it is a thing you need to SCREENSHOT?!!?!?
  152.         whose_turn_is_it()
  153.         print(piece)
  154.         if pink_piece_turn:
  155.             turn_label.label.text = "Pink's turn"
  156.             await(transition_camera.transition_camera2D(blue_piece.camera_2d, pink_piece.camera_2d))
  157.             if pink_piece.human == false:
  158.                 print("computer detected!")
  159.                 dice.can_click = false
  160.                 take_a_turn()
  161.         else:
  162.             turn_label.label.text = "Blue's turn"
  163.             await(transition_camera.transition_camera2D(pink_piece.camera_2d, blue_piece.camera_2d))
  164.             if blue_piece.human == false:
  165.                 print("computer detected!")
  166.                 dice.can_click = false
  167.                 take_a_turn()
  168.         turn_label.timer.start()
  169.        
  170. func whose_turn_is_it():
  171.     #var other_piece
  172.     if pink_piece_turn:
  173.         piece = pink_piece
  174.         #other_piece = blue_piece
  175.     else:
  176.         piece = blue_piece
  177.         #other_piece = pink_piece
  178.     #piece.camera_2d.enabled = true
  179.     #other_piece.camera_2d.enabled = false
  180.  
  181. func bounceback(roll):
  182.     #we want to go backwards
  183.     var go_back = piece.place - (roll + 1)
  184.     while piece.place != go_back:
  185.         piece.place -=1
  186.         await(move(piece, piece.place))
  187.     where_are_we(0)
  188.  
  189. func win():
  190.     print("both won")
  191.     winner_screen.visible = true
  192.     winner_screen.board_that_called_me = board_number
  193.     if pink_piece.score > blue_piece.score:
  194.         winner_screen.label.text = "Pink won!"
  195.         winner_screen.texture_rect.texture = load("res://Art/pink piece.png")
  196.     elif blue_piece.score > pink_piece.score:
  197.         winner_screen.label.text = "Blue won!"
  198.         winner_screen.texture_rect.texture = load("res://Art/blue piece.png")
  199.     elif blue_piece.score == pink_piece.score:
  200.         winner_screen.label.text = "It's a tie!"
  201.         winner_screen.texture_rect.texture = load("res://Art/both.png")
  202.  
  203. func turn_switcher():
  204.     dice.can_click = true
  205.     pink_piece_turn = !pink_piece_turn
  206.     piece.i_won = true
  207.  
  208. func check_for_win():
  209.     turn_switcher()
  210.     print("check for win entered")
  211.     if pink_piece.i_won and blue_piece.i_won:
  212.         win()
  213.         print("both won")
  214.         return
  215.     whose_turn_is_it()
  216.     turn_label_switcher()
  217.  
  218. func backwards():
  219.     #we want to go backwards
  220.     var two_spaces_back = piece.place - 2
  221.     while piece.place != two_spaces_back:
  222.         piece.place -=1
  223.         await(move(piece, piece.place))
  224.     if piece.place == Direction.WhichWay.FORWARD:
  225.         await(forwards())
  226.        
  227. func forwards():
  228.     #we want to go forwards
  229.     var two_spaces_forward = piece.place + 2
  230.     while piece.place != two_spaces_forward:
  231.         piece.place += 1
  232.         await(move(piece, piece.place))
  233.     if piece.place == Direction.WhichWay.BACK:
  234.         await(backwards())
  235.  
Advertisement
Add Comment
Please, Sign In to add comment