move pieces instantaneously and without sound at init
This commit is contained in:
		
							parent
							
								
									6d788465ab
								
							
						
					
					
						commit
						111b2f7125
					
				
					 2 changed files with 36 additions and 31 deletions
				
			
		| 
						 | 
				
			
			@ -44,6 +44,7 @@ func _on_NewGamePanel_about_to_show():
 | 
			
		|||
		"hard":
 | 
			
		||||
			$Panel/HBoxContainer/Hard.pressed = true
 | 
			
		||||
		_:
 | 
			
		||||
			assert("Invalid value")
 | 
			
		||||
			$Panel/HBoxContainer/Normal.pressed = true
 | 
			
		||||
 | 
			
		||||
	$Panel/Start.grab_focus()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -81,8 +81,7 @@ func _ready() -> void:
 | 
			
		|||
	var hidden_piece = rows * columns # Last piece is hidden
 | 
			
		||||
	
 | 
			
		||||
	init(pieces_order, hidden_piece)
 | 
			
		||||
 | 
			
		||||
	shuffle(difficulty)
 | 
			
		||||
	shuffle(difficulty, 0.0)
 | 
			
		||||
 | 
			
		||||
func _input(event):
 | 
			
		||||
	if $AnimationPlayer.is_playing():
 | 
			
		||||
| 
						 | 
				
			
			@ -101,13 +100,13 @@ func _input(event):
 | 
			
		|||
	# Handle keyboard input
 | 
			
		||||
	#
 | 
			
		||||
	if event.is_action_pressed("ui_up"):
 | 
			
		||||
		move_piece(Direction.UP)
 | 
			
		||||
		move_piece(Direction.UP, 1.0)
 | 
			
		||||
	if event.is_action_pressed("ui_down"):
 | 
			
		||||
		move_piece(Direction.DOWN)
 | 
			
		||||
		move_piece(Direction.DOWN, 1.0)
 | 
			
		||||
	if event.is_action_pressed("ui_left"):
 | 
			
		||||
		move_piece(Direction.LEFT)
 | 
			
		||||
		move_piece(Direction.LEFT, 1.0)
 | 
			
		||||
	if event.is_action_pressed("ui_right"):
 | 
			
		||||
		move_piece(Direction.RIGHT)
 | 
			
		||||
		move_piece(Direction.RIGHT, 1.0)
 | 
			
		||||
 | 
			
		||||
	#
 | 
			
		||||
	# Handle touch input
 | 
			
		||||
| 
						 | 
				
			
			@ -144,7 +143,7 @@ func _input(event):
 | 
			
		|||
				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
 | 
			
		||||
					commit_slide()
 | 
			
		||||
					commit_slide(true)
 | 
			
		||||
				else:
 | 
			
		||||
					reset_position(current_sliding_piece)
 | 
			
		||||
					reset_slide()
 | 
			
		||||
| 
						 | 
				
			
			@ -210,12 +209,13 @@ func sliding_piece_for_direction(direction) -> Piece:
 | 
			
		|||
	
 | 
			
		||||
	return piece
 | 
			
		||||
 | 
			
		||||
func move_piece(direction) -> void:
 | 
			
		||||
func move_piece(direction, speed: float) -> void:
 | 
			
		||||
	current_sliding_piece = sliding_piece_for_direction(direction)
 | 
			
		||||
	if current_sliding_piece == null:
 | 
			
		||||
		reset_slide()
 | 
			
		||||
		return
 | 
			
		||||
 | 
			
		||||
	if speed > 0.0:
 | 
			
		||||
		var moving_piece_animation: Animation = $AnimationPlayer.get_animation("MovingPiece")
 | 
			
		||||
	
 | 
			
		||||
		assert(moving_piece_animation != null)
 | 
			
		||||
| 
						 | 
				
			
			@ -232,8 +232,12 @@ func move_piece(direction) -> void:
 | 
			
		|||
		moving_piece_animation.track_set_key_value(moving_piece_track_index, 0, current_sliding_piece.position)
 | 
			
		||||
		moving_piece_animation.track_set_key_value(moving_piece_track_index, 1, missing_piece.position)
 | 
			
		||||
		$AnimationPlayer.play("MovingPiece")
 | 
			
		||||
	else:
 | 
			
		||||
		commit_slide(false)
 | 
			
		||||
		update()
 | 
			
		||||
		check_solved()
 | 
			
		||||
 | 
			
		||||
func commit_slide():
 | 
			
		||||
func commit_slide(audio: bool):
 | 
			
		||||
	assert(current_sliding_piece != null)
 | 
			
		||||
	assert(current_origin != Vector2.ZERO)
 | 
			
		||||
	
 | 
			
		||||
| 
						 | 
				
			
			@ -244,6 +248,7 @@ func commit_slide():
 | 
			
		|||
	swap_pieces(missing_piece, current_sliding_piece)
 | 
			
		||||
	reset_position(missing_piece)
 | 
			
		||||
 | 
			
		||||
	if audio:
 | 
			
		||||
		$AudioStreamPlayer.play()
 | 
			
		||||
 | 
			
		||||
	ensure_validity()
 | 
			
		||||
| 
						 | 
				
			
			@ -264,11 +269,11 @@ func swap_pieces(a: Piece, b: Piece) -> void:
 | 
			
		|||
	b.taquin_index = a_index
 | 
			
		||||
	pieces[a_index.x][a_index.y] = b
 | 
			
		||||
 | 
			
		||||
func shuffle(count: int) -> void:
 | 
			
		||||
func shuffle(count: int, speed: float) -> void:
 | 
			
		||||
	while count > 0:
 | 
			
		||||
		count -= 1
 | 
			
		||||
		var direction = rng.randi_range(Direction.UP, Direction.RIGHT)
 | 
			
		||||
		move_piece(direction)
 | 
			
		||||
		move_piece(direction, speed)
 | 
			
		||||
 | 
			
		||||
func check_solved() -> bool:
 | 
			
		||||
	for c in range(columns):
 | 
			
		||||
| 
						 | 
				
			
			@ -380,8 +385,6 @@ func init(pieces_order: Array, hidden_piece: int) -> void:
 | 
			
		|||
 | 
			
		||||
		pieces.append(pieces_row)
 | 
			
		||||
 | 
			
		||||
	shuffle(difficulty)
 | 
			
		||||
 | 
			
		||||
#
 | 
			
		||||
# Signals
 | 
			
		||||
#
 | 
			
		||||
| 
						 | 
				
			
			@ -391,7 +394,7 @@ func _on_Timer_timeout():
 | 
			
		|||
func _on_AnimationPlayer_animation_finished(anim_name):
 | 
			
		||||
	match anim_name:
 | 
			
		||||
		"MovingPiece":
 | 
			
		||||
			commit_slide()
 | 
			
		||||
			commit_slide(true)
 | 
			
		||||
			update()
 | 
			
		||||
			check_solved()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -411,6 +414,7 @@ func _on_NewGamePanel_start_triggered(preferences):
 | 
			
		|||
			columns = 5
 | 
			
		||||
			difficulty = 30
 | 
			
		||||
		_:
 | 
			
		||||
			assert("Invalid value")
 | 
			
		||||
			rows = 4
 | 
			
		||||
			columns = 4
 | 
			
		||||
			difficulty = 10
 | 
			
		||||
| 
						 | 
				
			
			@ -420,4 +424,4 @@ func _on_NewGamePanel_start_triggered(preferences):
 | 
			
		|||
		pieces_order.append(order)
 | 
			
		||||
	var hidden_piece = rows * columns # Last piece is hidden
 | 
			
		||||
	init(pieces_order, hidden_piece)
 | 
			
		||||
	shuffle(difficulty)
 | 
			
		||||
	shuffle(difficulty, 0.0)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue