-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ball.gd
68 lines (51 loc) · 2.1 KB
/
Ball.gd
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
extends Area2D
var INITIAL_SPEED = 150
var speed = 150
var direction
signal on_bounce(position)
# Called when the node enters the scene tree for the first time.
func _ready():
$AudioPlayer.add_sound("hit", ["res://sound/hit0.ogg","res://sound/hit1.ogg","res://sound/hit2.ogg","res://sound/hit3.ogg","res://sound/hit4.ogg"])
$AudioPlayer.add_sound("hit_slow", ["res://sound/hit_slow0.ogg"])
$AudioPlayer.add_sound("hit_medium", ["res://sound/hit_medium0.ogg"])
$AudioPlayer.add_sound("hit_fast", ["res://sound/hit_fast0.ogg"])
$AudioPlayer.add_sound("hit_veryfast", ["res://sound/hit_veryfast0.ogg"])
$AudioPlayer.add_sound("bounce", ["res://sound/bounce0.ogg","res://sound/bounce1.ogg","res://sound/bounce2.ogg","res://sound/bounce3.ogg","res://sound/bounce4.ogg"])
$AudioPlayer.add_sound("bounce_synth", ["res://sound/bounce_synth0.ogg"])
$AudioPlayer.add_sound("score", ["res://sound/score_goal0.ogg"])
restart(-1)
func restart(start_direction):
direction = Vector2(start_direction, 0).normalized()
position.x =get_viewport_rect().size.x/2
position.y =get_viewport_rect().size.y/2
speed = INITIAL_SPEED
func process(delta):
position = position + direction * speed * delta
speed = clamp(speed + 1 * delta, INITIAL_SPEED, 400)
if position.y > 470 || position.y<10:
direction.y = -direction.y
emit_signal("on_bounce", position)
func _on_area_entered(area):
if "Goal" in area.name:
goal()
return
emit_signal("on_bounce", position)
if "Player" in area.name:
$AudioPlayer.play_sound("hit")
if speed < 200:
$AudioPlayer.play_sound("hit_slow")
elif speed < 300:
$AudioPlayer.play_sound("hit_medium")
elif speed < 400:
$AudioPlayer.play_sound("hit_fast")
else:
$AudioPlayer.play_sound("hit_veryfast")
var hit_position = (area.position.y - position.y) / 128
direction = Vector2(-direction.x, clamp(direction.y + hit_position, -1, 1)).normalized()
if "Wall" in area.name:
$AudioPlayer.play_sound("bounce")
$AudioPlayer.play_sound("bounce_synth")
direction = Vector2(direction.x, -direction.y).normalized()
func goal():
$AudioPlayer.play_sound("score")
restart(direction.x)