slide piece in realtime
This commit is contained in:
parent
74de557b57
commit
bcb8dfe26f
|
@ -10,6 +10,8 @@ export var width: int = 512
|
|||
export var height: int = 512
|
||||
export var difficulty: int = 10
|
||||
|
||||
enum Direction { UP, DOWN, LEFT, RIGHT }
|
||||
|
||||
var interpiece: int = 4
|
||||
var min_padding = 15
|
||||
var padding = Vector2(min_padding, min_padding)
|
||||
|
@ -21,6 +23,13 @@ var rng = RandomNumberGenerator.new()
|
|||
var current_animation_path: String = "AnimationPlayer/MockPiece:position"
|
||||
|
||||
var swipe = Vector2(0, 0)
|
||||
var is_sliding = false
|
||||
var current_sliding_piece: Piece = null
|
||||
var current_origin = Vector2.ZERO
|
||||
var current_axis = Vector2.ZERO
|
||||
var current_slide = Vector2.ZERO
|
||||
var local_min_position = Vector2.ZERO
|
||||
var local_max_position = Vector2.ZERO
|
||||
|
||||
func position_for_index(index: Vector2, size: int) -> Vector2:
|
||||
return padding + Vector2(index.x * (size + interpiece), index.y * (size + interpiece))
|
||||
|
@ -88,6 +97,9 @@ func _input(event):
|
|||
GameState.State.GAME_OVER:
|
||||
return
|
||||
|
||||
#
|
||||
# Handle keyboard input
|
||||
#
|
||||
if event.is_action_pressed("ui_up"):
|
||||
move_piece(Direction.DOWN)
|
||||
if event.is_action_pressed("ui_down"):
|
||||
|
@ -97,21 +109,102 @@ func _input(event):
|
|||
if event.is_action_pressed("ui_right"):
|
||||
move_piece(Direction.LEFT)
|
||||
|
||||
#
|
||||
# Handle touch input
|
||||
#
|
||||
if event is InputEventScreenDrag:
|
||||
# print("screen drag")
|
||||
swipe = event.relative
|
||||
if event is InputEventScreenTouch:
|
||||
if not event.pressed: # Touch released
|
||||
if not is_sliding:
|
||||
is_sliding = true
|
||||
current_slide = Vector2(0, 0)
|
||||
var angle = swipe.angle()
|
||||
if angle < PI / 4 and angle >= - PI / 4:
|
||||
move_piece(Direction.LEFT)
|
||||
if angle >= PI / 4 and angle < PI - PI / 4:
|
||||
move_piece(Direction.UP)
|
||||
if angle >= - PI + PI / 4 and angle < - PI / 4:
|
||||
move_piece(Direction.DOWN)
|
||||
if angle >= PI - PI / 4 or angle < -PI + PI / 4:
|
||||
move_piece(Direction.RIGHT)
|
||||
var direction = direction_for_angle(angle)
|
||||
debug_print_direction(direction)
|
||||
current_sliding_piece = sliding_piece_for_direction(direction)
|
||||
if current_sliding_piece != null:
|
||||
current_origin = current_sliding_piece.position
|
||||
current_axis = axis_for_direction(direction)
|
||||
var local_end_position = current_axis * (current_sliding_piece.size + interpiece)
|
||||
local_min_position = Vector2(min(0, local_end_position.x), min(0, local_end_position.y))
|
||||
local_max_position = Vector2(max(0, local_end_position.x), max(0, local_end_position.y))
|
||||
current_slide += swipe
|
||||
if current_sliding_piece != null:
|
||||
var delta = current_slide.project(current_axis)
|
||||
delta.x = clamp(delta.x, local_min_position.x, local_max_position.x)
|
||||
delta.y = clamp(delta.y, local_min_position.y, local_max_position.y)
|
||||
current_sliding_piece.position = current_origin + delta
|
||||
|
||||
enum Direction { UP, DOWN, LEFT, RIGHT }
|
||||
if event is InputEventScreenTouch:
|
||||
# print("screen touch")
|
||||
if not event.pressed: # Touch released
|
||||
is_sliding = false
|
||||
# var angle = swipe.angle()
|
||||
# if angle < PI / 4 and angle >= - PI / 4:
|
||||
# move_piece(Direction.LEFT)
|
||||
# if angle >= PI / 4 and angle < PI - PI / 4:
|
||||
# move_piece(Direction.UP)
|
||||
# if angle >= - PI + PI / 4 and angle < - PI / 4:
|
||||
# move_piece(Direction.DOWN)
|
||||
# if angle >= PI - PI / 4 or angle < -PI + PI / 4:
|
||||
# move_piece(Direction.RIGHT)
|
||||
|
||||
func debug_print_direction(direction: int):
|
||||
match direction:
|
||||
Direction.UP:
|
||||
print("Direction UP")
|
||||
Direction.DOWN:
|
||||
print("Direction DOWN")
|
||||
Direction.LEFT:
|
||||
print("Direction LEFT")
|
||||
Direction.RIGHT:
|
||||
print("Direction RIGHT")
|
||||
_:
|
||||
assert(false)
|
||||
|
||||
func axis_for_direction(direction: int) -> Vector2:
|
||||
match direction:
|
||||
Direction.UP:
|
||||
return Vector2.UP
|
||||
Direction.DOWN:
|
||||
return Vector2.DOWN
|
||||
Direction.LEFT:
|
||||
return Vector2.LEFT
|
||||
Direction.RIGHT:
|
||||
return Vector2.RIGHT
|
||||
_:
|
||||
assert(false)
|
||||
return Vector2.ZERO
|
||||
|
||||
func direction_for_angle(angle: float) -> int:
|
||||
if angle < PI / 4 and angle >= - PI / 4:
|
||||
return Direction.RIGHT
|
||||
if angle >= PI / 4 and angle < PI - PI / 4:
|
||||
return Direction.DOWN
|
||||
if angle >= - PI + PI / 4 and angle < - PI / 4:
|
||||
return Direction.UP
|
||||
if angle >= PI - PI / 4 or angle < -PI + PI / 4:
|
||||
return Direction.LEFT
|
||||
assert(false)
|
||||
return Direction.DOWN
|
||||
|
||||
func sliding_piece_for_direction(direction) -> Piece:
|
||||
var destination: Vector2 = missing_piece
|
||||
match direction:
|
||||
Direction.UP:
|
||||
destination.y += 1
|
||||
Direction.DOWN:
|
||||
destination.y -= 1
|
||||
Direction.LEFT:
|
||||
destination.x += 1
|
||||
Direction.RIGHT:
|
||||
destination.x -= 1
|
||||
|
||||
if (destination.x < 0 || destination.x >= columns
|
||||
|| destination.y < 0 || destination.y >= rows):
|
||||
print("impossible move")
|
||||
return null
|
||||
return pieces[destination.x][destination.y]
|
||||
|
||||
func move_piece(direction) -> bool:
|
||||
var destination: Vector2 = missing_piece
|
||||
|
|
Loading…
Reference in a new issue