extends Sprite

signal clicked

var CELL_W = 128
var CELL_H = 128

export var piece = 'pawn'
export var team = 0

export var has_moved = false
export var in_check = false

const move_patterns = {
	pawn = [[0,0,0],[2,0,0],[0,0,0]]
}

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(piece, color, team = 0):
	get_texture().set_region(Rect2(piece, color, CELL_W, CELL_H))
	self.set_piece(self.get_piece_name_by_region())
	self.set_team(team)

func _on_Area2D_input_event(viewport, event, shape_idx):
	if event.is_action_pressed("mouse1"):
		emit_signal("clicked")

func set_piece(new_piece):
	piece = new_piece

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():
	queue_free()