snap pieces to grid
This commit is contained in:
		
							parent
							
								
									bcb8dfe26f
								
							
						
					
					
						commit
						0f8ddd4d71
					
				
					 3 changed files with 27 additions and 21 deletions
				
			
		| 
						 | 
				
			
			@ -122,5 +122,5 @@ __meta__ = {
 | 
			
		|||
 | 
			
		||||
[node name="GameState" type="Node" parent="."]
 | 
			
		||||
script = ExtResource( 3 )
 | 
			
		||||
[connection signal="state_changed" from="GameState" to="HSplitContainer/Taquin" method="_on_GameState_state_changed"]
 | 
			
		||||
[connection signal="state_changed" from="GameState" to="." method="_on_GameState_state_changed"]
 | 
			
		||||
[connection signal="state_changed" from="GameState" to="HSplitContainer/Taquin" method="_on_GameState_state_changed"]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,8 @@ class_name Piece
 | 
			
		|||
export var size: int = 160
 | 
			
		||||
 | 
			
		||||
var number: int = 0
 | 
			
		||||
var taquin_position = Vector2(0, 0)
 | 
			
		||||
var taquin_position = Vector2.ZERO
 | 
			
		||||
var taquin_index = Vector2.ZERO
 | 
			
		||||
var piece_scale = Vector2(0.25, 0.25)
 | 
			
		||||
 | 
			
		||||
func _ready() -> void:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,6 +26,7 @@ var swipe = Vector2(0, 0)
 | 
			
		|||
var is_sliding = false
 | 
			
		||||
var current_sliding_piece: Piece = null
 | 
			
		||||
var current_origin = Vector2.ZERO
 | 
			
		||||
var current_goal = Vector2.ZERO
 | 
			
		||||
var current_axis = Vector2.ZERO
 | 
			
		||||
var current_slide = Vector2.ZERO
 | 
			
		||||
var local_min_position = Vector2.ZERO
 | 
			
		||||
| 
						 | 
				
			
			@ -40,11 +41,11 @@ func compute_piece_size() -> int:
 | 
			
		|||
	return int(min(w_size, h_size))
 | 
			
		||||
 | 
			
		||||
func compute_padding(piece_size: int) -> Vector2:
 | 
			
		||||
	var padding = Vector2(0, 0)
 | 
			
		||||
	padding.x = width - columns * piece_size - (columns - 1) * interpiece
 | 
			
		||||
	padding.y = height - rows * piece_size - (rows - 1) * interpiece
 | 
			
		||||
	padding = padding / Vector2(2, 2)
 | 
			
		||||
	return padding
 | 
			
		||||
	var p = Vector2(0, 0)
 | 
			
		||||
	p.x = width - columns * piece_size - (columns - 1) * interpiece
 | 
			
		||||
	p.y = height - rows * piece_size - (rows - 1) * interpiece
 | 
			
		||||
	p = p / Vector2(2, 2)
 | 
			
		||||
	return p
 | 
			
		||||
 | 
			
		||||
func _ready() -> void:
 | 
			
		||||
	$AnimationPlayer/MockPiece.visible = false
 | 
			
		||||
| 
						 | 
				
			
			@ -67,6 +68,7 @@ func _ready() -> void:
 | 
			
		|||
			piece.size = piece_size
 | 
			
		||||
			piece.position = position_for_index(Vector2(c, r), piece.size)
 | 
			
		||||
			piece.number = 1 + c + r * columns
 | 
			
		||||
			piece.taquin_index = Vector2(c, r)
 | 
			
		||||
			piece.piece_scale = Vector2((float(piece_size) / width), (float(piece_size) / height))
 | 
			
		||||
			piece.taquin_position = Vector2(float(piece.position.x) / width, float(piece.position.y) / height)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -128,6 +130,7 @@ func _input(event):
 | 
			
		|||
				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_goal = current_sliding_piece.position + local_end_position
 | 
			
		||||
		current_slide += swipe
 | 
			
		||||
		if current_sliding_piece != null:
 | 
			
		||||
			var delta = current_slide.project(current_axis)
 | 
			
		||||
| 
						 | 
				
			
			@ -139,26 +142,26 @@ func _input(event):
 | 
			
		|||
#		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)
 | 
			
		||||
			if current_sliding_piece != null:
 | 
			
		||||
				var current_position = current_sliding_piece.position
 | 
			
		||||
				if current_position.distance_to(current_origin) > current_position.distance_to(current_goal):
 | 
			
		||||
					current_sliding_piece.position = current_goal
 | 
			
		||||
					var origin_taquin_position = current_sliding_piece.taquin_index
 | 
			
		||||
					swap(missing_piece, origin_taquin_position)
 | 
			
		||||
					missing_piece = origin_taquin_position
 | 
			
		||||
				else:
 | 
			
		||||
					current_sliding_piece.position = current_origin
 | 
			
		||||
 | 
			
		||||
func debug_print_direction(direction: int):
 | 
			
		||||
	match direction:
 | 
			
		||||
		Direction.UP:
 | 
			
		||||
			print("Direction UP")
 | 
			
		||||
			print("Direction ⬆️ UP")
 | 
			
		||||
		Direction.DOWN:
 | 
			
		||||
			print("Direction DOWN")
 | 
			
		||||
			print("Direction ⬇️ DOWN")
 | 
			
		||||
		Direction.LEFT:
 | 
			
		||||
			print("Direction LEFT")
 | 
			
		||||
			print("Direction ⬅️ LEFT")
 | 
			
		||||
		Direction.RIGHT:
 | 
			
		||||
			print("Direction RIGHT")
 | 
			
		||||
			print("Direction ➡️ RIGHT")
 | 
			
		||||
		_:
 | 
			
		||||
			assert(false)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -202,7 +205,7 @@ func sliding_piece_for_direction(direction) -> Piece:
 | 
			
		|||
	
 | 
			
		||||
	if (destination.x < 0 || destination.x >= columns
 | 
			
		||||
	|| destination.y < 0 || destination.y >= rows):
 | 
			
		||||
		print("impossible move")
 | 
			
		||||
		print("\/!\\ Impossible move")
 | 
			
		||||
		return null
 | 
			
		||||
	return pieces[destination.x][destination.y]
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -255,7 +258,9 @@ func move_piece(direction) -> bool:
 | 
			
		|||
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[src.x][src.y].taquin_index = src
 | 
			
		||||
	pieces[dst.x][dst.y] = tmp
 | 
			
		||||
	tmp.taquin_position = dst
 | 
			
		||||
#	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…
	
	Add table
		Add a link
		
	
		Reference in a new issue