move pieces around
This commit is contained in:
parent
53780532bf
commit
076969704d
|
@ -40,10 +40,46 @@ func _ready() -> void:
|
|||
|
||||
func _input(event):
|
||||
if event.is_action_pressed("ui_up"):
|
||||
print("up")
|
||||
move_piece(Direction.UP)
|
||||
if event.is_action_pressed("ui_down"):
|
||||
print("down")
|
||||
move_piece(Direction.DOWN)
|
||||
if event.is_action_pressed("ui_left"):
|
||||
print("left")
|
||||
move_piece(Direction.LEFT)
|
||||
if event.is_action_pressed("ui_right"):
|
||||
print("right")
|
||||
move_piece(Direction.RIGHT)
|
||||
|
||||
enum Direction { UP, DOWN, LEFT, RIGHT }
|
||||
|
||||
func move_piece(direction) -> bool:
|
||||
var destination: Vector2 = missing_piece
|
||||
match direction:
|
||||
Direction.UP:
|
||||
print("up")
|
||||
destination.y -= 1
|
||||
Direction.DOWN:
|
||||
destination.y += 1
|
||||
print("down")
|
||||
Direction.LEFT:
|
||||
destination.x -= 1
|
||||
print("left")
|
||||
Direction.RIGHT:
|
||||
destination.x += 1
|
||||
print("right")
|
||||
|
||||
print(destination)
|
||||
if (destination.x < 0 || destination.x >= columns
|
||||
|| destination.y < 0 || destination.y >= rows):
|
||||
print("impossible move")
|
||||
return false
|
||||
|
||||
swap(missing_piece, destination)
|
||||
missing_piece = destination
|
||||
update()
|
||||
return true
|
||||
|
||||
func swap(src: Vector2, dst: Vector2) -> void:
|
||||
var tmp: Piece = pieces[src.x][src.y]
|
||||
pieces[src.x][src.y] = pieces[dst.x][dst.y]
|
||||
pieces[dst.x][dst.y] = tmp
|
||||
pieces[src.x][src.y].position = position_for_index(src, tmp.size)
|
||||
pieces[dst.x][dst.y].position = position_for_index(dst, tmp.size)
|
Loading…
Reference in a new issue