summaryrefslogtreecommitdiff
path: root/pinball.gd
blob: 3a3a37cd8ba1dcae156d4415eb2d32d0899e7dda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
extends Node2D


var launch_speed := -35
var flipper_launch_speed := Vector2(
	1.0,
	-30,
)

var shake_tween: Tween
var camera_base_position: Vector2


func _ready() -> void:
	shake_tween = get_tree().create_tween()
	shake_tween.kill()
	
	camera_base_position = $Camera2D.position
	
	$FlipperLeft.rotation_degrees = 30
	$FlipperRight.rotation_degrees = -30
	
	launch_ball()


func _process(_delta: float) -> void:
	if Input.is_action_pressed("flipper_left"):
		var tween := get_tree().create_tween()
		tween.tween_property($FlipperLeft, "rotation_degrees", -45, 0.1)
	
	if Input.is_action_just_released("flipper_left"):
		var tween := get_tree().create_tween()
		tween.tween_property($FlipperLeft, "rotation_degrees", 30, 0.1)
	
	if Input.is_action_just_pressed("flipper_left"):
		if $FlipperLeft.is_colliding(%Ball):
			if %Ball.position.y < $FlipperLeft.get_lowest_point().y:
				%Ball.apply_central_impulse(
					Vector2(abs(%Ball.global_position.x - $FlipperLeft.get_center().x), 1.0)
					*
					flipper_launch_speed
				)
	
	
	if Input.is_action_pressed("flipper_right"):
		var tween := get_tree().create_tween()
		tween.tween_property($FlipperRight, "rotation_degrees", 45, 0.1)
	
	if Input.is_action_just_released("flipper_right"):
		var tween := get_tree().create_tween()
		tween.tween_property($FlipperRight, "rotation_degrees", -30, 0.1)
	
	if Input.is_action_just_pressed("flipper_right"):
		if $FlipperRight.is_colliding(%Ball):
			if %Ball.position.y < $FlipperRight.get_lowest_point().y:
				%Ball.apply_central_impulse(
					Vector2(-abs(%Ball.global_position.x - $FlipperRight.get_center().x), 1.0)
					*
					flipper_launch_speed
				)
	
	
	if Input.is_action_just_pressed("shake"):
		%Ball.apply_central_impulse(Vector2(randf_range(-1.5, 1.5), randf_range(-1.5, -0.5)))
		shake_tween.stop()
		shake_tween = get_tree().create_tween()
		var shake_direction = Vector2(randf_range(-1.0, 1.0), randf_range(-1.0, 1.0))
		shake_tween.tween_property($Camera2D, "position", camera_base_position + shake_direction, 0.05)
		shake_tween.tween_property($Camera2D, "position", camera_base_position, 0.05)
		
		# fail-safe for non-launched ball. TODO: use different action
		# TODO: make ball launch manual in general
		if $LaunchArea.get_overlapping_bodies().has(%Ball):
			launch_ball()
	
	
	if %Ball.position.y > get_viewport_rect().size.y:
		$Camera2D.position.y = get_viewport_rect().size.y + get_viewport_rect().size.y / 2.0
		camera_base_position.y = $Camera2D.position.y
	else:
		$Camera2D.position.y = get_viewport_rect().size.y / 2.0
		camera_base_position.y = $Camera2D.position.y


func launch_ball() -> void:
	$Wall.visible = false
	$Wall.process_mode = Node.PROCESS_MODE_DISABLED
	
	var tween := get_tree().create_tween()
	tween.tween_property(%Ball, "position:y", %Ball.position.y + 5, 0.5)
	await tween.finished
	%Ball.apply_central_impulse(Vector2(0, launch_speed))


func _on_outside_body_entered(_body: Node2D) -> void:
	%Ball.move_body($LaunchArea.global_position)
	await get_tree().create_timer(0.5).timeout
	launch_ball()


func _on_wall_area_body_exited(_body: Node2D) -> void:
	$Wall.visible = true
	$Wall.process_mode = Node.PROCESS_MODE_INHERIT