chessgame/Sprite.gd

89 lines
1.9 KiB
GDScript3
Raw Normal View History

2023-02-17 11:37:23 -08:00
extends Sprite
signal clicked
var CELL_W = 128
var CELL_H = 128
export var piece = 'pawn'
export var team = 0
2023-02-18 13:41:27 -08:00
export var has_moved = false
export var in_check = false
export var killed = false # queue_free() isn't fast enough; get_nodes.by_group("piece") will still find this so mark explictly
2023-02-18 13:41:27 -08:00
2023-02-17 11:37:23 -08:00
func get_move_pattern():
return self.move_patterns.get(piece)
enum piece_color {
black = 0,
white = 128
grey = 256,
litegrey = 384,
pink = 512,
red = 640,
orange = 768,
yellow = 896,
green = 1024,
blue = 1152,
teal = 1280,
purple = 1408
}
enum piece_map {
pawn = 0,
knight = 128,
bishop = 256,
rook = 384,
queen = 512,
king = 640
}
func _ready():
#print("Trying Red Pawn... Color: %s, Piece: " % piece_color.red, piece_map.pawn)
#var atlas = get_texture()
#atlas.set_region(Rect2(piece_map.king, piece_color.red, CELL_W, CELL_H))
#print(atlas.get_region())
#get_texture().set_region(Rect2(piece_map.king, piece_color.red, CELL_W, CELL_H))
#position = Vector2(500,500)
pass
func init(new_piece, color, new_team = 0):
get_texture().set_region(Rect2(new_piece, color, CELL_W, CELL_H))
2023-02-19 01:06:56 -08:00
self.set_piece(self.get_piece_name_by_region(), self.get_piece_color_by_region())
self.set_team(new_team)
2023-02-17 11:37:23 -08:00
func _on_Area2D_input_event(_viewport, event, _shape_idx):
2023-02-17 11:37:23 -08:00
if event.is_action_pressed("mouse1"):
emit_signal("clicked")
2023-02-19 01:06:56 -08:00
func set_piece(new_piece, color):
2023-02-17 11:37:23 -08:00
piece = new_piece
2023-02-19 01:06:56 -08:00
get_texture().set_region(Rect2(self.piece_map.get(new_piece), self.piece_color.get(color), CELL_W, CELL_H))
2023-02-17 11:37:23 -08:00
func get_piece():
return piece
func get_team():
return team
func set_team(new_team):
team = new_team
func get_piece_name_by_region():
var region = get_texture().get_region()
for map in piece_map:
if piece_map.get(map) == region.position.x:
return map
func get_piece_color_by_region():
var region = get_texture().get_region()
for map in piece_color:
if piece_color.get(map) == region.position.y:
return map
func kill():
killed = true
2023-02-17 11:37:23 -08:00
queue_free()