157 lines
3.6 KiB
GDScript3
157 lines
3.6 KiB
GDScript3
![]() |
extends Spatial
|
||
|
|
||
|
var play = false
|
||
|
|
||
|
onready var engine = $Engine
|
||
|
|
||
|
export var tileSize = 40
|
||
|
export var blackColor = Color.black
|
||
|
export var whiteColor = Color.white
|
||
|
const tileCount = 8
|
||
|
|
||
|
export var selectionColor = Color.white
|
||
|
|
||
|
var tiles = []
|
||
|
var pieceTiles = []
|
||
|
|
||
|
var selectionTile = null
|
||
|
var selX = 0
|
||
|
var selZ = 0
|
||
|
|
||
|
var selectedIndex = -1
|
||
|
var inverse = -1
|
||
|
|
||
|
var bishop = preload("res://Minigames/Chess/pieces/bishop.png")
|
||
|
var king = preload("res://Minigames/Chess/pieces/king.png")
|
||
|
var pawn = preload("res://Minigames/Chess/pieces/pawn.png")
|
||
|
var queen = preload("res://Minigames/Chess/pieces/queen.png")
|
||
|
var rook = preload("res://Minigames/Chess/pieces/rook.png")
|
||
|
var knight = preload("res://Minigames/Chess/pieces/knight.png")
|
||
|
|
||
|
var pieceIconDict = {'r': rook, 'n': knight, 'b': bishop, 'q': queen,
|
||
|
'k': king, 'p': pawn, '.': null}
|
||
|
|
||
|
func _ready():
|
||
|
var vp = $Viewport
|
||
|
engine.setup()
|
||
|
for z in range(0,tileCount):
|
||
|
for x in range(0,tileCount):
|
||
|
var cr = ColorRect.new()
|
||
|
|
||
|
var val = x+z
|
||
|
|
||
|
if val % 2 == 0:
|
||
|
cr.color = blackColor
|
||
|
else:
|
||
|
cr.color = whiteColor
|
||
|
|
||
|
cr.margin_right = tileSize
|
||
|
cr.margin_bottom = tileSize
|
||
|
cr.rect_position.x = x * tileSize
|
||
|
cr.rect_position.y = z * tileSize
|
||
|
|
||
|
vp.add_child(cr)
|
||
|
tiles.append(cr)
|
||
|
|
||
|
var piece = TextureRect.new()
|
||
|
piece.margin_right = tileSize
|
||
|
piece.margin_bottom = tileSize
|
||
|
piece.rect_position.x = x * tileSize
|
||
|
piece.rect_position.y = z * tileSize
|
||
|
piece.expand = true
|
||
|
vp.add_child(piece)
|
||
|
pieceTiles.append(piece)
|
||
|
|
||
|
selectionTile = ColorRect.new()
|
||
|
selectionTile.color = selectionColor
|
||
|
selectionTile.margin_right = tileSize
|
||
|
selectionTile.margin_bottom = tileSize
|
||
|
selectionTile.rect_position.x = selX * tileSize
|
||
|
selectionTile.rect_position.y = (7-selZ) * tileSize
|
||
|
selectionTile.visible = false
|
||
|
vp.add_child(selectionTile)
|
||
|
|
||
|
#drawPieces()
|
||
|
|
||
|
func play():
|
||
|
play = true
|
||
|
selectionTile.visible = true
|
||
|
Spiel.player.lock = true
|
||
|
drawPieces()
|
||
|
|
||
|
func _process(_delta):
|
||
|
if not play:
|
||
|
return
|
||
|
|
||
|
if engine.isdraw():
|
||
|
playerDraw()
|
||
|
|
||
|
if engine.isaiturn():
|
||
|
if not engine.aimove():
|
||
|
playerLost()
|
||
|
drawPieces()
|
||
|
return
|
||
|
|
||
|
if Input.is_action_just_pressed("jump"):
|
||
|
selectPiece()
|
||
|
drawPieces()
|
||
|
if Input.is_action_just_pressed("ui_up"):
|
||
|
selZ = selZ + 1
|
||
|
elif Input.is_action_just_pressed("ui_down"):
|
||
|
selZ = selZ - 1
|
||
|
if Input.is_action_just_pressed("ui_left"):
|
||
|
selX = selX - 1
|
||
|
elif Input.is_action_just_pressed("ui_right"):
|
||
|
selX = selX + 1
|
||
|
|
||
|
selX = posmod(selX, 8)
|
||
|
selZ = posmod(selZ, 8)
|
||
|
|
||
|
selectionTile.rect_position.x = selX * tileSize
|
||
|
selectionTile.rect_position.y = (7-selZ) * tileSize
|
||
|
|
||
|
func selectPiece():
|
||
|
if selectedIndex == -1:
|
||
|
selectedIndex = selX + selZ * 8
|
||
|
inverse = selX + (7-selZ) * 8
|
||
|
return
|
||
|
|
||
|
if not engine.makemove(selectedIndex, selX + selZ * 8):
|
||
|
playerWon()
|
||
|
|
||
|
selectedIndex = -1
|
||
|
inverse = -1
|
||
|
|
||
|
func drawPieces():
|
||
|
var pieces = engine.getboard()
|
||
|
var i = 0
|
||
|
for c in pieces:
|
||
|
var p = c.to_lower()
|
||
|
if p == ' ' or p == '\n':
|
||
|
continue
|
||
|
|
||
|
pieceTiles[i].texture = pieceIconDict[p]
|
||
|
if i == inverse:
|
||
|
pieceTiles[i].modulate = Color.red
|
||
|
elif c != p:
|
||
|
pieceTiles[i].modulate = Color.cyan
|
||
|
else:
|
||
|
pieceTiles[i].modulate = Color.white
|
||
|
|
||
|
i = i + 1
|
||
|
|
||
|
func playerWon():
|
||
|
play = false
|
||
|
selectionTile.visible = false
|
||
|
Spiel.player.lock = false
|
||
|
|
||
|
var dialog = Dialogic.start("rat_chess_end")
|
||
|
dialog.connect("dialogic_signal", self, "dialog_listener")
|
||
|
add_child(dialog)
|
||
|
|
||
|
func playerLost():
|
||
|
engine.setup()
|
||
|
|
||
|
func playerDraw():
|
||
|
playerLost()
|