JocDame/Node.gd

272 lines
12 KiB
GDScript

extends Node
@export var piesa_neagra_scene: PackedScene
@export var piesa_alba_scene: PackedScene
@export var cadran_negru_scene: PackedScene
var player : int
var pasi : int
var piesa : int
var board_size : int
var cell_size : int
var grid_pos : Vector2i
var grid_data : Array # matrice 8 pe 8
var start_pos : Vector2i # retine primul click dat / piesa pe care se doreste a muta
var start_piesa : int
var temp_marker
# Called when the node enters the scene tree for the first time.
func _ready():
board_size = 788
# imparte board_size la 8 pentru a obtine dimensiunea fiecarei celule
cell_size = board_size / 8
new_game()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _input(event):
if event is InputEventMouseButton :
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed :
#verifica daca mouse-ul e pe tabla de joc
if event.position.x < board_size:
#convertire pozitie mouse in locatie grid
grid_pos = Vector2i(event.position / cell_size) # grid_pos memoreza [coloana, linie]
if player == 1 :
if grid_data[grid_pos.y][grid_pos.x][0] == 1 and pasi == 1 :
start_piesa = grid_data[grid_pos.y][grid_pos.x][0]
start_pos = grid_pos
print("player", player, "a apasat pe chenarul ", grid_pos )
print("a fost selectata piesa ", start_piesa)
pasi = 2
# cmiscare pe diagonala un patratel piesa neagra
elif grid_pos.x == start_pos.x + 2 and grid_pos.y == start_pos.y + 2 : capturare_diagonala_jos_negru()
elif grid_pos.x == start_pos.x + 2 and grid_pos.y == start_pos.y - 2 : capturare_diagonala_sus_negru()
else : deplasare_diagonala_negru()
elif player == -1 :
if grid_data[grid_pos.y][grid_pos.x][0] < 0 and pasi == 1 :
start_piesa = grid_data[grid_pos.y][grid_pos.x][0]
start_pos = grid_pos
print("player ", player, " a apasat pe chenarul ", grid_pos )
print("a fost selectata piesa ", start_piesa)
pasi = 2
elif grid_pos.x == start_pos.x - 2 and grid_pos.y == start_pos.y - 2 : capturare_diagonala_jos_alb()
elif grid_pos.x == start_pos.x - 2 and grid_pos.y == start_pos.y + 2 : capturare_diagonala_sus_alb()
else : deplasare_diagonala_alb()
#capturare piesa de catre pion negru pe diagonala
elif grid_pos != start_pos and grid_data[grid_pos.y][grid_pos.x][0] >= 0 :
if grid_pos.x == start_pos.x - 1 and grid_pos.y == start_pos.y + 1 or grid_pos.x == start_pos.x - 1 and grid_pos.y == start_pos.y - 1 :
if pasi == 2:
print("trece de primul if")
#marcare noua pozitie a piesei
grid_data[grid_pos.y][grid_pos.x][0] = start_piesa
create_marker(-1, grid_pos * cell_size + Vector2i(cell_size /2, cell_size /2 + 5 ))
#demarcare vechea pozitie a piesei
create_marker(grid_data[start_pos.y][start_pos.x][1], start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y][start_pos.x][0] = 0
print(grid_pos)
print(grid_data)
pasi = 1
player = 1
func new_game():
player = 1
# [5,1] a doua valoare marcheaza culoarea chenarului tablei de joc, 7 = negru, 8 = alb
grid_data = [
[[1,7], [0,8], [0,7], [0,8], [0,7], [0,8], [-1,7], [0,8]],
[[0,8], [1,7], [0,8], [0,7], [0,8], [0,7], [0,8], [-1,7]],
[[1,7], [0,8], [0,7], [0,8], [0,7], [0,8], [-1,7], [0,8]],
[[0,8], [1,7], [0,8], [0,7], [0,8], [0,7], [0,8], [-1,7]],
[[1,7], [0,8], [0,7], [0,8], [0,7], [0,8], [-1,7], [0,8]],
[[0,8], [1,7], [0,8], [0,7], [0,8], [0,7], [0,8], [-1,7]],
[[1,7], [0,8], [0,7], [0,8], [0,7], [0,8], [-1,7], [0,8]],
[[0,8], [1,7], [0,8], [0,7], [0,8], [0,7], [0,8], [-1,7]]
]
pasi = 1
#punerea pieselor negre (player1)
create_marker(1, Vector2i(0,0) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(1, Vector2i(0,2) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(1, Vector2i(0,4) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(1, Vector2i(0,6) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(1, Vector2i(1,1) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(1, Vector2i(1,3) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(1, Vector2i(1,5) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(1, Vector2i(1,7) * cell_size + Vector2i(cell_size /2, cell_size /2))
#punerea pieselor albe(player2)
create_marker(-1, Vector2i(6,0) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(-1, Vector2i(6,2) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(-1, Vector2i(6,4) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(-1, Vector2i(6,6) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(-1, Vector2i(7,1) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(-1, Vector2i(7,3) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(-1, Vector2i(7,5) * cell_size + Vector2i(cell_size /2, cell_size /2))
create_marker(-1, Vector2i(7,7) * cell_size + Vector2i(cell_size /2, cell_size /2))
func create_marker(piesa, position) :
#creaza un marcker node si il adauga ca un child
if piesa==1:
var piesa_neagra = piesa_neagra_scene.instantiate()
piesa_neagra.position=position
add_child(piesa_neagra)
if piesa==-1:
var piesa_alba=piesa_alba_scene.instantiate()
piesa_alba.position=position
add_child(piesa_alba)
if piesa == 7:
var cadran_negru = cadran_negru_scene.instantiate()
cadran_negru.position = position
add_child(cadran_negru)
func deplasare_diagonala_negru():
if grid_pos != start_pos and grid_data[grid_pos.y][grid_pos.x][0] == 0 :
if grid_pos.x == start_pos.x + 1 and grid_pos.y == start_pos.y + 1 or grid_pos.x == start_pos.x + 1 and grid_pos.y == start_pos.y - 1 :
if pasi == 2:
print("trece de primul if")
#marcare noua pozitie a piesei
grid_data[grid_pos.y][grid_pos.x][0] = start_piesa
create_marker(1, grid_pos * cell_size + Vector2i(cell_size /2, cell_size /2 + 5 ))
#demarcare vechea pozitie a piesei
create_marker(grid_data[start_pos.y][start_pos.x][1], start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y][start_pos.x][0] = 0
print(grid_pos)
print(grid_data)
pasi = 1
player = -1
func capturare_diagonala_jos_negru():
print("punct control1")
if grid_pos != start_pos and grid_data[grid_pos.y][grid_pos.x][0] == 0 :
print("punct control 2")
if grid_pos.x == start_pos.x + 2 and grid_pos.y == start_pos.y + 2 :
print("punct control 3")
print(start_pos.x + 1, start_pos.y + 1)
if grid_data[start_pos.y + 1][start_pos.x + 1][0] <= 0 :
print("punct control 4")
if pasi == 2:
print("trece de primul if")
#marcare noua pozitie a piesei
grid_data[grid_pos.y][grid_pos.x][0] = start_piesa
create_marker(1, grid_pos * cell_size + Vector2i(cell_size /2, cell_size /2 + 5 ))
#demarcare vechea pozitie a piesei
create_marker(grid_data[start_pos.y][start_pos.x][1], start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y][start_pos.x][0] = 0
#demarcare piesa capturata
start_pos.x +=1
start_pos.y +=1
create_marker(7, start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y + 1][start_pos.x + 1][0] = 0
print(grid_pos)
print(grid_data)
pasi = 1
player = -1
func capturare_diagonala_sus_negru():
print("punct control1")
if grid_pos != start_pos and grid_data[grid_pos.y][grid_pos.x][0] == 0 :
print("punct control 2")
if grid_pos.x == start_pos.x + 2 and grid_pos.y == start_pos.y - 2 :
print("punct control 3")
print(start_pos.x + 1, start_pos.y - 1)
if grid_data[start_pos.y - 1][start_pos.x + 1][0] <= 0 :
print("punct control 4")
if pasi == 2:
print("trece de primul if")
#marcare noua pozitie a piesei
grid_data[grid_pos.y][grid_pos.x][0] = start_piesa
create_marker(1, grid_pos * cell_size + Vector2i(cell_size /2, cell_size /2 + 5 ))
#demarcare vechea pozitie a piesei
create_marker(grid_data[start_pos.y][start_pos.x][1], start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y][start_pos.x][0] = 0
#demarcare piesa capturata
start_pos.x +=1
start_pos.y -=1
create_marker(7, start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y -1 ][start_pos.x + 1][0] = 0
print(grid_pos)
print(grid_data)
pasi = 1
player = -1
func capturare_diagonala_sus_alb():
print("punct control1")
if grid_pos != start_pos and grid_data[grid_pos.y][grid_pos.x][0] == 0 :
print("punct control 2")
if grid_pos.x == start_pos.x - 2 and grid_pos.y == start_pos.y + 2 :
print("punct control 3")
print(start_pos.x - 1, start_pos.y + 1)
if grid_data[start_pos.y + 1][start_pos.x - 1][0] > 0 :
print("punct control 4")
if pasi == 2:
print("trece de primul if")
#marcare noua pozitie a piesei
grid_data[grid_pos.y][grid_pos.x][0] = start_piesa
create_marker(-1, grid_pos * cell_size + Vector2i(cell_size /2, cell_size /2 + 5 ))
#demarcare vechea pozitie a piesei
create_marker(grid_data[start_pos.y][start_pos.x][1], start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y][start_pos.x][0] = 0
#demarcare piesa capturata
start_pos.x -=1
start_pos.y +=1
create_marker(7, start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y + 1][start_pos.x - 1][0] = 0
print(grid_pos)
print(grid_data)
pasi = 1
player = 1
func capturare_diagonala_jos_alb():
print("punct control1")
print(grid_data[grid_pos.y][grid_pos.x][0])
if grid_pos != start_pos and grid_data[grid_pos.y][grid_pos.x][0] == 0 :
print("punct control 2")
if grid_pos.x == start_pos.x - 2 and grid_pos.y == start_pos.y - 2 :
print("punct control 3")
print(start_pos.x - 1, start_pos.y - 1)
if grid_data[start_pos.y - 1][start_pos.x - 1][0] > 0 :
print("punct control 4")
if pasi == 2:
print("trece de primul if")
#marcare noua pozitie a piesei
grid_data[grid_pos.y][grid_pos.x][0] = start_piesa
create_marker(-1, grid_pos * cell_size + Vector2i(cell_size /2, cell_size /2 + 5 ))
#demarcare vechea pozitie a piesei
create_marker(grid_data[start_pos.y][start_pos.x][1], start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y][start_pos.x][0] = 0
#demarcare piesa capturata
start_pos.x -=1
start_pos.y -=1
create_marker(7, start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y + 1][start_pos.x + 1][0] = 0
print(grid_pos)
print(grid_data)
pasi = 1
player = 1
func deplasare_diagonala_alb():
if grid_pos != start_pos and grid_data[grid_pos.y][grid_pos.x][0] == 0 :
if grid_pos.x == start_pos.x - 1 and grid_pos.y == start_pos.y - 1 or grid_pos.x == start_pos.x - 1 and grid_pos.y == start_pos.y + 1 :
if pasi == 2:
print("trece de primul if")
#marcare noua pozitie a piesei
grid_data[grid_pos.y][grid_pos.x][0] = start_piesa
create_marker(-1, grid_pos * cell_size + Vector2i(cell_size /2, cell_size /2 + 5 ))
#demarcare vechea pozitie a piesei
create_marker(grid_data[start_pos.y][start_pos.x][1], start_pos * cell_size + Vector2i(cell_size /2, cell_size /2) )
grid_data[start_pos.y][start_pos.x][0] = 0
print(grid_pos)
print(grid_data)
pasi = 1
player = 1