Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- class_name MainBoard
- @onready var pink_piece : Sprite2D = $PinkPiece
- @onready var blue_piece: Sprite2D = $BluePiece
- @export var game_spaces : Array[Spot]
- @export var question_boxes : Array[Question]
- var place : int = 0
- var number_of_spaces : int
- @onready var dice := $CanvasLayer/Dice
- @onready var timer := $Timer
- @onready var canvas_layer: CanvasLayer = $CanvasLayer
- var score : int = 0
- @onready var score_label: Label = $"CanvasLayer/Score Label"
- var pink_piece_turn : bool = true
- @onready var turn_label: PanelContainer = $CanvasLayer/TurnLabel
- @onready var winner_screen: CenterContainer = $"CanvasLayer/Winner Screen"
- @onready var piece
- @onready var audio_stream_player: AudioStreamPlayer = $AudioStreamPlayer
- @onready var transition_camera: Camera2D = $TransitionCamera
- @export var board_number : int
- @export var bounceback_board : bool
- const QUESTION_BOX = preload("res://Question Boxes/questionbox.tscn")
- func _ready() -> void:
- number_of_spaces = game_spaces.size()
- print(number_of_spaces)
- Events.question_box_gone.connect(on_question_box_gone)
- Events.send_piece.connect(on_send_piece)
- Fader.fade_in()
- pink_piece.human = Events.pink_human_controlled
- blue_piece.human = Events.blue_human_controlled
- print(pink_piece.human)
- print(blue_piece.human)
- func on_send_piece(sent_piece):
- pink_piece_turn = sent_piece
- if pink_piece_turn:
- pink_piece.camera_2d.enabled = true
- else:
- blue_piece.camera_2d.enabled = true
- whose_turn_is_it()
- timer.start()
- await timer.timeout
- if piece.human == false:
- take_a_turn()
- func _input(event: InputEvent) -> void:
- whose_turn_is_it()
- if event.is_action_pressed("ui_click") and dice.can_click == true:
- if piece.human == true:
- take_a_turn()
- func take_a_turn():
- print(piece)
- if piece.i_won == false:
- dice.roll()
- print("The dice should roll because we hit this")
- else:
- pink_piece_turn = !pink_piece_turn
- turn_label_switcher()
- func _on_dice_dice_has_rolled(roll: Variant) -> void:
- whose_turn_is_it()
- #This line is for testing. Uncomment it and change roll to whatever you need.
- #roll = 6
- print(pink_piece.i_won)
- print(blue_piece.i_won)
- while roll != 0:
- if piece.place < game_spaces.size():
- #if we have not won
- await(move(piece, piece.place))
- piece.place += 1
- roll -= 1
- print(piece.place)
- else:
- #if we have won
- if blue_piece.place >= game_spaces.size() and pink_piece.place >= game_spaces.size():
- #if both of these pieces are at the winner's circle
- win()
- break
- elif piece.place >= game_spaces.size():
- #if just one is at the winner's circle
- if roll > 0 and bounceback_board == true:
- await(bounceback(roll))
- return
- piece.place = game_spaces.size()
- turn_switcher()
- turn_label_switcher()
- return
- #this check tells us if we've stopped
- where_are_we(roll)
- func where_are_we(roll):
- if roll == 0:
- if (bounceback_board and piece.place == game_spaces.size()) or (piece.place >= game_spaces.size()):
- check_for_win()
- return
- dice.can_click = true
- await(move(piece, piece.place))
- if pink_piece_turn:
- print("pink piece's turn")
- pink_piece_turn = false
- else:
- print("blue piece turn")
- pink_piece_turn = true
- if game_spaces[piece.place].direction == Direction.WhichWay.BACK:
- await(backwards())
- if game_spaces[piece.place].direction == Direction.WhichWay.FORWARD:
- await(forwards())
- if game_spaces[piece.place].direction == Direction.WhichWay.QUESTION:
- question_boxes.shuffle()
- #LOAD IT
- #we already loaded it!
- #INSTANCE IT
- var question = QUESTION_BOX.instantiate()
- #ADD IT
- question.question_box_resource = question_boxes.front()
- canvas_layer.add_child(question)
- dice.can_click = false
- if game_spaces[piece.place].direction == Direction.WhichWay.REGULAR:
- turn_label_switcher()
- func move(piece, place) -> void:
- if piece.place < game_spaces.size():
- #print("Move says piece.place is")
- #print(piece.place)
- dice.can_click = false
- var tween = create_tween()
- tween.tween_property(piece, "position", game_spaces[place].position, 1)
- tween.finished.connect(audio_stream_player.play)
- timer.start()
- await timer.timeout
- func on_question_box_gone(point):
- if point == true:
- if !pink_piece_turn:
- pink_piece.score = pink_piece.score + 1
- score_label.text = "Pink: " + str(pink_piece.score) + "\nBlue: " + str(blue_piece.score)
- else:
- blue_piece.score = blue_piece.score + 1
- score_label.text = "Pink: " + str(pink_piece.score) + "\nBlue: " + str(blue_piece.score)
- await get_tree().process_frame
- dice.can_click = true
- turn_label_switcher()
- func turn_label_switcher():
- if !pink_piece.i_won or !blue_piece.i_won:
- turn_label.visible = true
- #Calling whose turn is it is a thing you need to SCREENSHOT?!!?!?
- whose_turn_is_it()
- print(piece)
- if pink_piece_turn:
- turn_label.label.text = "Pink's turn"
- await(transition_camera.transition_camera2D(blue_piece.camera_2d, pink_piece.camera_2d))
- if pink_piece.human == false:
- print("computer detected!")
- dice.can_click = false
- take_a_turn()
- else:
- turn_label.label.text = "Blue's turn"
- await(transition_camera.transition_camera2D(pink_piece.camera_2d, blue_piece.camera_2d))
- if blue_piece.human == false:
- print("computer detected!")
- dice.can_click = false
- take_a_turn()
- turn_label.timer.start()
- func whose_turn_is_it():
- #var other_piece
- if pink_piece_turn:
- piece = pink_piece
- #other_piece = blue_piece
- else:
- piece = blue_piece
- #other_piece = pink_piece
- #piece.camera_2d.enabled = true
- #other_piece.camera_2d.enabled = false
- func bounceback(roll):
- #we want to go backwards
- var go_back = piece.place - (roll + 1)
- while piece.place != go_back:
- piece.place -=1
- await(move(piece, piece.place))
- where_are_we(0)
- func win():
- print("both won")
- winner_screen.visible = true
- winner_screen.board_that_called_me = board_number
- if pink_piece.score > blue_piece.score:
- winner_screen.label.text = "Pink won!"
- winner_screen.texture_rect.texture = load("res://Art/pink piece.png")
- elif blue_piece.score > pink_piece.score:
- winner_screen.label.text = "Blue won!"
- winner_screen.texture_rect.texture = load("res://Art/blue piece.png")
- elif blue_piece.score == pink_piece.score:
- winner_screen.label.text = "It's a tie!"
- winner_screen.texture_rect.texture = load("res://Art/both.png")
- func turn_switcher():
- dice.can_click = true
- pink_piece_turn = !pink_piece_turn
- piece.i_won = true
- func check_for_win():
- turn_switcher()
- print("check for win entered")
- if pink_piece.i_won and blue_piece.i_won:
- win()
- print("both won")
- return
- whose_turn_is_it()
- turn_label_switcher()
- func backwards():
- #we want to go backwards
- var two_spaces_back = piece.place - 2
- while piece.place != two_spaces_back:
- piece.place -=1
- await(move(piece, piece.place))
- if piece.place == Direction.WhichWay.FORWARD:
- await(forwards())
- func forwards():
- #we want to go forwards
- var two_spaces_forward = piece.place + 2
- while piece.place != two_spaces_forward:
- piece.place += 1
- await(move(piece, piece.place))
- if piece.place == Direction.WhichWay.BACK:
- await(backwards())
Advertisement
Add Comment
Please, Sign In to add comment