|
| 1 | +# warning-ignore-all:return_value_discarded |
| 2 | +extends MarginContainer |
| 3 | + |
| 4 | +const END_MESSAGE := "You got %s points!" |
| 5 | +const COLOR_MESSAGE := "Pop a %s balloon" |
| 6 | +const STATUS_MESSAGE := "You'r in stage %s/10 and have %s points!" |
| 7 | + |
| 8 | +var balloon_scene := preload("res://games/testgame/balloon.tscn") |
| 9 | + |
| 10 | +var colors := { |
| 11 | + "Red": Color.red, |
| 12 | + "Green": Color.green, |
| 13 | + "Yellow": Color.yellow, |
| 14 | + "Blue": Color.blue, |
| 15 | + "Purple": Color.purple, |
| 16 | + "Orange": Color.orange, |
| 17 | +} |
| 18 | + |
| 19 | +var points := 0 |
| 20 | +var stage := 0 |
| 21 | +var search_color: Color |
| 22 | + |
| 23 | +onready var _timer := $Timer |
| 24 | +onready var _respawn_timer := $RespawnTimer |
| 25 | +onready var _color_label := $VBoxContainer/HBoxContainer/ColorLabel |
| 26 | +onready var _status_label := $VBoxContainer/HBoxContainer/StatusLabel |
| 27 | +onready var _area := $VBoxContainer/balloonArea |
| 28 | +onready var _rng := RandomNumberGenerator.new() |
| 29 | +onready var _particles := $VBoxContainer/Particles2D |
| 30 | + |
| 31 | + |
| 32 | +func _ready(): |
| 33 | + _rng.randomize() |
| 34 | + start() |
| 35 | + |
| 36 | + |
| 37 | +func start(): |
| 38 | + var i = _rng.randi_range(0, colors.size() - 1) |
| 39 | + _color_label.text = COLOR_MESSAGE % colors.keys()[i] |
| 40 | + search_color = colors.values()[i] |
| 41 | + call_deferred("_spawn") |
| 42 | + |
| 43 | + |
| 44 | +# Animates all balloons |
| 45 | +func _process(_delta): |
| 46 | + var offset := sin(_timer.time_left * 2 * PI) |
| 47 | + for balloon in _area.get_children(): |
| 48 | + balloon.rect_position.y += offset |
| 49 | + |
| 50 | + |
| 51 | +# spawns all balloons per round |
| 52 | +func _spawn(): |
| 53 | + var possible := colors.values() |
| 54 | + possible.erase(search_color) |
| 55 | + |
| 56 | + #spawn 1-3 balloons with the search color |
| 57 | + for _i in range(_rng.randi_range(1, 3)): |
| 58 | + _spawn_color(search_color) |
| 59 | + |
| 60 | + #spawn 3-7 balloons of any color exept the search color |
| 61 | + for _i in range(_rng.randi_range(3, 7)): |
| 62 | + _spawn_color(possible[_rng.randi_range(0, possible.size() - 1)]) |
| 63 | + |
| 64 | + stage += 1 |
| 65 | + _update_status() |
| 66 | + |
| 67 | + |
| 68 | +# spawns one balloon of the given color |
| 69 | +func _spawn_color(color: Color): |
| 70 | + var b: TextureButton = balloon_scene.instance() |
| 71 | + _area.add_child(b) |
| 72 | + # position |
| 73 | + var max_pos: Vector2 = _area.rect_size - b.rect_size * b.rect_scale |
| 74 | + b.rect_position.x = _rng.randf_range(0, max_pos.x) |
| 75 | + b.rect_position.y = _rng.randf_range(0, max_pos.y) |
| 76 | + |
| 77 | + # signals |
| 78 | + b.connect("pressed", self, "_on_destroy", [color, b]) |
| 79 | + b.connect("pressed", b, "queue_free") |
| 80 | + # modulate |
| 81 | + b.self_modulate = color |
| 82 | + |
| 83 | + |
| 84 | +func _on_destroy(color: Color, button = null): |
| 85 | + if color.is_equal_approx(search_color): |
| 86 | + points += 1 |
| 87 | + _delete_all() |
| 88 | + if button is TextureButton: |
| 89 | + _particles.global_position = ( |
| 90 | + button.rect_global_position |
| 91 | + + button.rect_size / 2 * button.rect_scale |
| 92 | + ) |
| 93 | + _particles.restart() |
| 94 | + _respawn_timer.start() |
| 95 | + else: |
| 96 | + points -= 1 |
| 97 | + if points < 0: |
| 98 | + points = 0 |
| 99 | + _update_status() |
| 100 | + |
| 101 | + |
| 102 | +func _delete_all(): |
| 103 | + for b in _area.get_children(): |
| 104 | + b.queue_free() |
| 105 | + |
| 106 | + |
| 107 | +# timer leaves a little time between stage end and the next stage start or game end |
| 108 | +func _on_RespawnTimer_timeout(): |
| 109 | + if stage >= 10: |
| 110 | + GameManager.end_game(END_MESSAGE % points) |
| 111 | + return |
| 112 | + _spawn() |
| 113 | + |
| 114 | + |
| 115 | +func _update_status(): |
| 116 | + _status_label.text = STATUS_MESSAGE % [stage, points] |
0 commit comments