TicTacToeExtreme/game.gd

200 lines
5.3 KiB
GDScript

extends Node2D
var current_player: int = 0
var player_states := ["Circle", "Cross"]
var won := ""
var sequence_needed = 4
var _sequence = []
@onready var board := $Board as Board
@onready var nextPlayer := $GUI/NextPlayer as Sprite2D
@onready var wonPlayer := $GUI/WonPlayer as Sprite2D
@onready var diceButton := $GUI/DiceButton as Button
@onready var diceSprite = $GUI/DiceSprite as Sprite2D
@onready var player1Sprite = $GUI/Player1Sprite as Sprite2D
@onready var player2Sprite = $GUI/Player2Sprite as Sprite2D
# Called when the node enters the scene tree for the first time.
func _ready():
current_player = 0
board.reset()
won = ""
wonPlayer.hide()
nextPlayer.region_rect.position.x = current_player * 300
nextPlayer.show()
_update_dice_sprite(0)
board.active = true
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
if won == "":
if Input.is_action_just_pressed("Click"):
var grid_pos = board.gridpos_at_mouse()
if board.get_state(Vector2(grid_pos.x, 0)) == board.empty_state:
var new_pos = board._calculate_drop_pos(grid_pos)
if board.set_state(new_pos, player_states[current_player]):
won = _check_win()
if won=="":
current_player = 1 - current_player
nextPlayer.region_rect.position.x = current_player * 300
else:
print("Winner: ", won)
wonPlayer.region_rect.position.x = player_states.find(won) * 300
if won == player_states[0]:
for tile in _sequence:
board.set_cell(1, tile, 1, Vector2i(0, 2), 0)
else:
for tile in _sequence:
board.set_cell(1, tile, 1, Vector2i(3, 2), 0)
wonPlayer.show()
nextPlayer.hide()
board.active = false
func _on_restart_button_pressed():
_ready()
var _checks = [
[Vector2i(-1,0), Vector2i(1,0)],
[Vector2i(0,-1), Vector2i(0,1)],
[Vector2i(-1,-1), Vector2i(1,1)],
[Vector2i(-1,1), Vector2i(1,-1)],
]
func _check_win() -> String:
for cell in board.get_used_cells(board.layer_by_name[board.state_layer]):
var state = board.get_state(cell)
_sequence.clear()
for check in _checks:
for i in range(3):
var same = true
for j in range(sequence_needed):
if board.get_state(cell + check[0] * (i + j)) != state:
same = false
break
_sequence.append(Vector2i(cell + check[0] * (i + j)))
if same:
return state
return ""
#Generiert die Zufallszahl von 1-6
func _roll_dice() -> int:
var zahl = randi() % 6 + 1
var animation_help
var zahl_davor
for i in range(6):
if animation_help == zahl_davor:
zahl_davor = randi() % 6 + 1
_update_dice_sprite(zahl_davor)
animation_help = zahl_davor
await get_tree().create_timer(0.25).timeout
_update_dice_sprite(zahl)
print(zahl) #Debug ausgabe, später löschen lol
return zahl
#Funktion um Knopf und Methode zu verbinden
func _on_dice_button_pressed():
_roll_dice()
#Sucht sich für die entsprechende Augenzahl
#die X und Y-Koordinaten in der Tilemap
func _update_dice_sprite(result :int):
diceSprite.region_rect.position.x = _get_dice_pos_x(result)
diceSprite.region_rect.position.y = _get_dice_pos_y(result)
#X-Koordinaten der Augenzahlen in der Tilemap
func _get_dice_pos_x(result: int) -> int:
match result:
1: return 8
2: return 108
3: return 208
4: return 308
5: return 108
6: return 208
return 208
#Y-Koordinaten in der Tilemap
func _get_dice_pos_y(result: int) -> int:
match result:
1: return 308
2: return 308
3: return 308
4: return 308
5: return 410
6: return 410
return 410
#Nach dem Glücksradereignis wird die Siegesbedingung auf 5 gewinnt geändert
func _five_wins() -> void:
print("5 Gewinnt aktiv") #Debug
sequence_needed = 5
#Nach dem Glücksradereignis tauschen die Spieler die Symbole
func _change_symbols():
print("tausch") #debug
current_player = 1 - current_player
nextPlayer.region_rect.position.x = current_player * 300
var tempX = player1Sprite.region_rect.position.x
var tempY = player1Sprite.region_rect.position.y
player1Sprite.region_rect.position.x = player2Sprite.region_rect.position.x
player1Sprite.region_rect.position.y = player2Sprite.region_rect.position.y
player2Sprite.region_rect.position.x = tempX
player2Sprite.region_rect.position.y = tempY
#Nach dem Glücksradereignis gewinnt der Spieler sofort
func _instant_win():
won = player_states[current_player]
print("Winner: ", won)
wonPlayer.region_rect.position.x = player_states.find(won) * 300
wonPlayer.show()
nextPlayer.hide()
board.active = false
#Zum Testen der Events
func _on_test_pressed():
board.delete_column(6)
func _spin_the_wheel(result: int) -> void:
if result <= 5:
_instant_win()
elif result <= 15:
print("1 ding löschen")#Debug
if Input.is_action_just_pressed("Click"):
var grid_pos = board.gridpos_at_mouse()
board._clear_enemy_tile(grid_pos)
elif result <= 30:
_change_symbols()
elif result <= 45:
_five_wins()
elif result <= 65:
var zeile_zum_loeschen = randi() % 6 + 1
print("delete column"+ zeile_zum_loeschen)#Debug
board.delete_column(zeile_zum_loeschen)
elif result <= 85:
var spalte_zum_loeschen = randi() % 6 + 1
print("delete row" + spalte_zum_loeschen)#Debug
board.delete_row(spalte_zum_loeschen)
elif result <= 100:
print("größer als 85")#Debug
#Am Glücksrad drehen
func _on_spin_the_wheel_pressed():
var zahl = randi() % 100 + 1
_spin_the_wheel(zahl)