2024-03-27 18:02:45 +01:00
|
|
|
extends Node2D
|
|
|
|
|
|
|
|
var current_player: int = 0
|
|
|
|
var player_states := ["Circle", "Cross"]
|
|
|
|
var won := ""
|
2024-03-28 00:16:22 +01:00
|
|
|
var tile_size : Vector2 = Vector2(32, 32) # Annahme: 32x32 Pixel pro Tile
|
|
|
|
var tileset_texture : Texture = preload("res://assets/sprites.png")
|
|
|
|
|
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
|
2024-03-28 00:16:22 +01:00
|
|
|
@onready var diceButton := $GUI/DiceButton as Button
|
|
|
|
@onready var diceSprite = $GUI/DiceSprite as Sprite2D
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
board.reset()
|
|
|
|
won = ""
|
|
|
|
wonPlayer.hide()
|
|
|
|
nextPlayer.region_rect.position.x = current_player * 300
|
|
|
|
nextPlayer.show()
|
|
|
|
board.active = true
|
|
|
|
|
|
|
|
|
2024-03-28 00:16:22 +01:00
|
|
|
|
|
|
|
|
2024-03-27 18:02:45 +01:00
|
|
|
# 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(grid_pos) == 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]):
|
2024-03-27 18:02:45 +01:00
|
|
|
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
|
|
|
|
wonPlayer.show()
|
|
|
|
nextPlayer.hide()
|
|
|
|
board.active = false
|
2024-03-27 21:35:52 +01:00
|
|
|
|
2024-03-27 18:02:45 +01:00
|
|
|
|
|
|
|
func _on_restart_button_pressed():
|
|
|
|
_ready()
|
|
|
|
|
|
|
|
var _checks = [
|
|
|
|
[Vector2i(-1,0), Vector2i(1,0)],
|
2024-03-27 20:39:10 +01:00
|
|
|
[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 20:39:10 +01:00
|
|
|
|
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)
|
|
|
|
for check in _checks:
|
2024-03-27 20:39:10 +01:00
|
|
|
for i in range(3):
|
|
|
|
var same = true
|
|
|
|
for j in range(4):
|
|
|
|
if board.get_state(cell + check[0] * (i + j)) != state:
|
|
|
|
same = false
|
|
|
|
break
|
|
|
|
if same:
|
|
|
|
return state
|
2024-03-27 18:02:45 +01:00
|
|
|
return ""
|
2024-03-27 20:39:10 +01:00
|
|
|
|
2024-03-28 00:16:22 +01:00
|
|
|
#Generiert die Zufallszahl von 1-6
|
|
|
|
func _roll_dice():
|
|
|
|
var zahl = randi() % 6 + 1
|
|
|
|
_update_dice_sprite(zahl)
|
|
|
|
print(zahl) #Debug ausgabe, später löschen lol
|
|
|
|
return zahl
|
2024-03-27 20:39:10 +01:00
|
|
|
|
2024-03-28 00:16:22 +01:00
|
|
|
#Funktion um Knop 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)
|
2024-03-27 18:02:45 +01:00
|
|
|
|
2024-03-28 00:16:22 +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 8
|
2024-03-27 18:02:45 +01:00
|
|
|
|
2024-03-28 00:16:22 +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 308
|
|
|
|
|