TicTacToeExtreme/game.gd

389 lines
10 KiB
GDScript
Raw Permalink Normal View History

2024-03-27 18:02:45 +01:00
extends Node2D
var current_player: int = 0
var player_states := ["Circle", "Cross"]
var won := ""
var sequence_needed = 4
var _sequence = []
var enable_remove = false
var move_allowed = true
var zuege = 0
var wuerfeln_erlaubt = false
var wheel_spin_allowed = false
var active = Theme.new()
var schnickLost = false
var schnickActive = false
@onready var gui := $GUI as Node2D
2024-03-27 18:02:45 +01:00
@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
@onready var ereignis = $GUI/Event as Label
@onready var sequenz = $GUI/Sequenz as Label
@onready var schnick = $Schnick
@onready var numbers = $Numbers as TileMap
@onready var blindBoard = $BlindBoard as TileMap
@onready var spinButton = $GUI/WheelSpin as Button
@onready var luckyWheel = $LuckyWheel as Control
2024-03-27 18:02:45 +01:00
# Called when the node enters the scene tree for the first time.
func _ready():
current_player = 0
zuege = 0
wuerfeln_erlaubt = false
move_allowed = true
wheel_spin_allowed = false
blindBoard.visible = false
sequence_needed = 4
2024-03-27 18:02:45 +01:00
board.reset()
won = ""
2024-04-01 23:09:32 +02:00
ereignis.text = ""
sequenz.text = "4 - Gewinnt!"
2024-03-27 18:02:45 +01:00
wonPlayer.hide()
nextPlayer.region_rect.position.x = current_player * 300
nextPlayer.show()
_update_dice_sprite(0)
2024-03-27 18:02:45 +01:00
board.active = true
schnickLost = false
luckyWheel.wheelActive = false
schnickActive = false
2024-03-27 18:02:45 +01:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
_check_dice_color()
move_allowed = move_allowed and !schnickLost and !luckyWheel.wheelActive and !schnickActive
2024-03-27 18:02:45 +01:00
if won == "":
if Input.is_action_just_pressed("Click") and move_allowed:
2024-03-27 18:02:45 +01:00
var grid_pos = board.gridpos_at_mouse()
2024-03-30 19:56:03 +01:00
if enable_remove:
board._clear_enemy_tile(grid_pos)
enable_remove = false
elif board.get_state(Vector2(grid_pos.x, 0)) == board.empty_state:
2024-03-27 21:35:52 +01:00
var new_pos = board._calculate_drop_pos(grid_pos)
if board.set_state(new_pos, player_states[current_player]):
if zuege >= 1:
2024-04-01 23:09:32 +02:00
move_allowed = false
wuerfeln_erlaubt = true
zuege += 1
blindBoard.visible = false
2024-04-01 23:09:32 +02:00
ereignis.text = ""
2024-03-27 18:02:45 +01:00
won = _check_win()
if won=="":
switch_player()
2024-03-27 18:02:45 +01:00
else:
wonPlayer.region_rect.position.x = player_states.find(won) * 300
if won == player_states[0]:
2024-04-01 23:09:32 +02:00
print(str(_sequence))
_sequence = _sort_array(_sequence)
print(str(_sequence))
var umgerechnet = _umrechnung(_sequence)
_sequence = _lol(umgerechnet, _sequence)
print(str(umgerechnet))
print(str(_sequence))
for tile in _sequence:
board.set_cell(1, tile, 1, Vector2i(0, 2), 0)
else:
2024-04-01 23:09:32 +02:00
print(str(_sequence))
_sequence = _sort_array(_sequence)
print(str(_sequence))
var umgerechnet = _umrechnung(_sequence)
_sequence = _lol(umgerechnet, _sequence)
print(str(umgerechnet))
print(str(_sequence))
for tile in _sequence:
board.set_cell(1, tile, 1, Vector2i(3, 2), 0)
2024-03-27 18:02:45 +01:00
wonPlayer.show()
nextPlayer.hide()
board.active = false
2024-03-27 18:02:45 +01:00
func switch_player():
current_player = 1 - current_player
nextPlayer.region_rect.position.x = current_player * 300
2024-03-27 18:02:45 +01:00
func _on_restart_button_pressed():
_ready()
2024-03-27 18:02:45 +01:00
var _checks = [
[Vector2i(-1,0), Vector2i(1,0)],
[Vector2i(0,-1), Vector2i(0,1)],
[Vector2i(-1,-1), Vector2i(1,1)],
2024-03-27 18:02:45 +01:00
[Vector2i(-1,1), Vector2i(1,-1)],
]
2024-03-27 18:02:45 +01:00
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()
2024-03-27 18:02:45 +01:00
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
elif board.get_state(cell + check[0] * (i + j)) == "Triangle2":
same = false
break
_sequence.append(Vector2i(cell + check[0] * (i + j)))
if same:
2024-04-01 23:09:32 +02:00
return state
2024-03-27 18:02:45 +01:00
return ""
2024-04-01 23:09:32 +02:00
func _sort_array(arr :Array) -> Array:
arr.sort_custom(func(a,b): return a < b)
return arr
func _umrechnung(arr :Array) -> Array:
var newArr = []
for i in range(1,arr.size()-1,1):
newArr.append(arr[i-1]-arr[i])
return newArr
#(-1, 0)_ (0,-1)| (-1,-1)\, (-1,1)/
func _lol(arr :Array, shortArr :Array) -> Array:
var bedingung
var vektoren = [Vector2i(-1,0),Vector2i(0,-1), Vector2i(-1,-1), Vector2i(-1,1)]
var newArr = []
for e in vektoren:
if arr.count(e) >= 2:
bedingung = e
for i in range(1,arr.size()+1,1):
if (shortArr[i-1] - shortArr[i]) == e:
newArr.append(shortArr[i-1])
newArr.append(shortArr[i])
return newArr
#Generiert die Zufallszahl von 1-6
func _roll_dice() -> int:
schnickLost = false
wuerfeln_erlaubt = false
move_allowed = false
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)
2024-04-01 23:09:32 +02:00
if zahl == 1:
switch_to_schnick()
if zahl == 3:
blindBoard.visible = true
if zahl == 6:
board.active = false
luckyWheel.is_spin = true
switch_to_wheel()
var number = randi() % 100 + 1
while luckyWheel.spun == false: #damit erst nach dem drehen 3s gewartet wird
await get_tree().create_timer(0.1).timeout
_spin_the_wheel(number)
await get_tree().create_timer(3.2).timeout
luckyWheel.spun = false
luckyWheel.hide()
switch_to_game()
board.active = true
move_allowed = true
return zahl
2024-03-29 18:09:51 +01:00
#Funktion um Knopf und Methode zu verbinden
func _on_dice_button_pressed():
if wuerfeln_erlaubt:
move_allowed = true
_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)
2024-03-27 18:02:45 +01:00
#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
2024-03-27 18:02:45 +01:00
#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:
sequence_needed = 5
#Nach dem Glücksradereignis tauschen die Spieler die Symbole
func _change_symbols():
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]
wonPlayer.region_rect.position.x = player_states.find(won) * 300
wonPlayer.show()
nextPlayer.hide()
board.active = false
2024-03-30 18:55:34 +01:00
func _instant_loose():
current_player = 1 - current_player
won = player_states[current_player]
wonPlayer.region_rect.position.x = player_states.find(won) * 300
wonPlayer.show()
nextPlayer.hide()
board.active = false
2024-03-29 18:09:51 +01:00
#Zum Testen der Events
func _on_test_pressed():
2024-04-01 23:09:32 +02:00
_spin_the_wheel(14)
func _spin_the_wheel(result: int) -> void:
wheel_spin_allowed = false
ereignis.text = "Ereignis: "
if result <= 3:
ereignis.text += "Instant win!"
_instant_win()
2024-03-30 18:55:34 +01:00
elif result <= 6:
ereignis.text += "Instant loose!"
_instant_loose()
elif result <= 15:
2024-03-30 19:56:03 +01:00
enable_remove = true
ereignis.text += "Du darfst einen Stein entfernen!"
elif result <= 30:
ereignis.text += "Symboltausch!"
_change_symbols()
elif result <= 45:
if sequence_needed == 5:
_spin_the_wheel(randi() % 100 + 1)
else:
ereignis.text += "Ab jetzt gilt 5 - Gewinnt!"
sequenz.text = "5 - Gewinnt!"
_five_wins()
elif result <= 65:
var zeile_zum_loeschen = randi() % 6 + 1
ereignis.text += "Zeile " + str(zeile_zum_loeschen) + " wird entfernt!"
await get_tree().create_timer(4).timeout
board.delete_column(zeile_zum_loeschen)
elif result <= 85:
var spalte_zum_loeschen = randi() % 6 + 1
ereignis.text += "Spalte " + str(spalte_zum_loeschen) + " wird entfernt!"
await get_tree().create_timer(4).timeout
board.delete_row(spalte_zum_loeschen)
elif result <= 100:
var spalte = randi() % 6 + 1
ereignis.text += "Totes Feld in Spalte " + str(spalte)
_dead_zone(spalte)
move_allowed = true
func _dead_zone(spalte :int):
var pos = board._calculate_drop_pos(Vector2(spalte-3,0))
if pos.y == 0:
_spin_the_wheel(randi() % 100 + 1)
else:
board.set_state(pos, "Triangle2")
#Am Glücksrad drehen
func _on_spin_the_wheel_pressed():
if wheel_spin_allowed:
var zahl = randi() % 100 + 1
_spin_the_wheel(zahl)
func hide_schnick():
schnick.hide()
schnickActive = false
move_allowed = true
func switch_to_schnick():
schnickActive = true
move_allowed = false
board.hide()
numbers.hide()
gui.hide()
schnick.show()
func switch_to_wheel():
luckyWheel.wheelActive = true
move_allowed = false
gui.hide()
numbers.hide()
board.hide()
luckyWheel.show()
func switch_to_game():
luckyWheel.wheelActive = false
move_allowed = true
gui.show()
numbers.show()
board.show()
func _on_gewonnen_pressed():
schnickLost = false
wuerfeln_erlaubt = false
hide_schnick()
switch_to_game()
func _on_verloren_pressed():
schnickLost = true
wuerfeln_erlaubt = true
switch_player()
hide_schnick()
switch_to_game()
func _check_dice_color():
if wuerfeln_erlaubt == false:
diceButton.add_theme_color_override("font_color", Color.RED)
diceButton.add_theme_color_override("font_hover_color", Color.RED)
diceButton.add_theme_color_override("font_pressed_color", Color.RED)
diceButton.add_theme_color_override("font_focus_color", Color.RED)
elif wuerfeln_erlaubt == true:
diceButton.add_theme_color_override("font_color", Color.SEA_GREEN)
diceButton.add_theme_color_override("font_hover_color", Color.SEA_GREEN)
diceButton.add_theme_color_override("font_pressed_color", Color.SEA_GREEN)
diceButton.add_theme_color_override("font_focus_color", Color.SEA_GREEN)