godot-shooter / meteor / files / World.gd
Branch meteor · default branch · all branches
World.gd
5b18ed86 2023-11-01 Pavel Safronov | 1 | extends Node2D |
| 2 | ||
| 3 | const Utils = preload("res://Utils.gd") | |
| 4 | const Enemy = preload("res://Enemy.tscn") | |
f398dcb9 2023-11-02 Pavel Safronov | 5 | export var enemy_count = 4 |
f108d953 2023-11-02 Pavel Safronov | 6 | var destination: Vector2 |
| 7 | var is_destination_set = false | |
0c1938a6 2023-11-01 Pavel Safronov | 8 | |
e9f8f8f2 2023-11-01 Pavel Safronov | 9 | func _ready() -> void: |
| 10 | for n in enemy_count: | |
| 11 | add_new_enemy(true) | |
5b18ed86 2023-11-01 Pavel Safronov | 12 | |
| 13 | func _on_enemy_killed() -> void: | |
| 14 | $CanvasLayer/HUD.score += 1 | |
e9f8f8f2 2023-11-01 Pavel Safronov | 15 | # use call_deferred to ensure that this happens after the collision has been processed |
| 16 | call_deferred("add_new_enemy") | |
5b18ed86 2023-11-01 Pavel Safronov | 17 | |
e9f8f8f2 2023-11-01 Pavel Safronov | 18 | func add_new_enemy(should_be_visible: bool = false) -> void: |
5b18ed86 2023-11-01 Pavel Safronov | 19 | var pos_angle = randi() % 360 |
| 20 | var unit_vector = Vector2.UP.rotated(pos_angle) | |
e9f8f8f2 2023-11-01 Pavel Safronov | 21 | var screen_radius = get_viewport().get_visible_rect().size.length() / 2 + 10 |
d5d1043b 2023-11-02 Pavel Safronov | 22 | var enemy_spawn_dist = 400 if should_be_visible else screen_radius |
5b18ed86 2023-11-01 Pavel Safronov | 23 | var pos = unit_vector * enemy_spawn_dist + $Player.global_position |
| 24 | ||
| 25 | var enemy = Enemy.instance() | |
| 26 | enemy.connect("enemy_killed", self, "_on_enemy_killed") | |
| 27 | enemy.position = pos | |
f398dcb9 2023-11-02 Pavel Safronov | 28 | enemy.add_to_group("enemies") |
5b18ed86 2023-11-01 Pavel Safronov | 29 | add_child(enemy) |
f108d953 2023-11-02 Pavel Safronov | 30 | |
a71b5173 2023-11-02 Pavel Safronov | 31 | func _input(event: InputEvent) -> void: |
| 32 | if event is InputEventScreenTouch: | |
| 33 | if event.pressed && !$Player.dead: | |
ad6f22a5 2023-11-02 Pavel Safronov | 34 | Utils.log("event position", event.position) |
| 35 | Utils.log("to_global(event.position)", to_global(event.position)) | |
ded27fa1 2023-11-06 Pavel Safronov | 36 | Utils.log("player", $Player.position) |
| 37 | var result = event.position + $Player.position - Vector2(500, 300) | |
| 38 | set_destination(result) | |
a71b5173 2023-11-02 Pavel Safronov | 39 | |
8c2ffd3f 2023-11-02 Pavel Safronov | 40 | func _process(_delta: float) -> void: |
f398dcb9 2023-11-02 Pavel Safronov | 41 | if Input.is_action_just_pressed("click") and !$Player.dead: |
f108d953 2023-11-02 Pavel Safronov | 42 | var pos = get_global_mouse_position() |
a71b5173 2023-11-02 Pavel Safronov | 43 | set_destination(pos) |
52220002 2023-11-06 Pavel Safronov | 44 | if Input.is_action_just_pressed("pause"): |
| 45 | get_tree().paused = true | |
| 46 | $CanvasLayer/PauseMenu.show() | |
f108d953 2023-11-02 Pavel Safronov | 47 | |
| 48 | if is_destination_set: | |
52220002 2023-11-06 Pavel Safronov | 49 | var diff = destination - $Player.global_position |
f108d953 2023-11-02 Pavel Safronov | 50 | var unit = diff.normalized() |
| 51 | $Player.velocity = unit * $Player.move_speed | |
| 52 | $Destination.visible = true | |
| 53 | else: | |
| 54 | $Destination.visible = false | |
f398dcb9 2023-11-02 Pavel Safronov | 55 | $Player.velocity = Vector2.ZERO |
f108d953 2023-11-02 Pavel Safronov | 56 | |
a71b5173 2023-11-02 Pavel Safronov | 57 | func set_destination(pos: Vector2) -> void: |
ad6f22a5 2023-11-02 Pavel Safronov | 58 | Utils.log("setting destination to ", pos) |
a71b5173 2023-11-02 Pavel Safronov | 59 | destination = pos |
| 60 | is_destination_set = true | |
52220002 2023-11-06 Pavel Safronov | 61 | $Destination.global_position = pos |
a71b5173 2023-11-02 Pavel Safronov | 62 | $Destination.visible = true |
| 63 | $Player. global_rotation = global_position.direction_to(get_global_mouse_position()).angle() + PI/2.0 | |
| 64 | ||
6e5e56fe 2023-11-02 Pavel Safronov | 65 | func _on_Destination_body_entered(body: Node) -> void: |
| 66 | if body == $Player: | |
| 67 | is_destination_set = false | |
f398dcb9 2023-11-02 Pavel Safronov | 68 | |
| 69 | func sort_enemies_ascending_distance(a: Enemy,b: Enemy): | |
52220002 2023-11-06 Pavel Safronov | 70 | var distA = $Player.global_position.distance_to(a.global_position) |
| 71 | var distB = $Player.global_position.distance_to(b.global_position) | |
| 72 | var result = distA < distB | |
| 73 | return result | |
| 74 | ||
| 75 | func filter_invalid_enemies(enemy: Enemy) -> bool: | |
| 76 | if enemy.dead: | |
| 77 | return false | |
| 78 | return true | |
f398dcb9 2023-11-02 Pavel Safronov | 79 | |
| 80 | func _on_ShootTimer_timeout() -> void: | |
| 81 | if $Player.dead: | |
| 82 | return | |
| 83 | var enemies = get_tree().get_nodes_in_group("enemies") | |
52220002 2023-11-06 Pavel Safronov | 84 | for e in enemies: |
| 85 | var enemy: Enemy = e | |
| 86 | enemy.is_targeted = false | |
| 87 | ||
| 88 | var filteredEnemies = Utils.filter(funcref(self,"filter_invalid_enemies"), enemies) | |
| 89 | if filteredEnemies.size() > 0: | |
| 90 | filteredEnemies.sort_custom(self, "sort_enemies_ascending_distance") | |
| 91 | var closest_enemy: Enemy = filteredEnemies[0] | |
1d4ba5a6 2023-11-02 Pavel Safronov | 92 | closest_enemy.is_targeted = true |
52220002 2023-11-06 Pavel Safronov | 93 | $Player.shoot_at(closest_enemy.global_position) |
| 94 | $PlayerToTarget.points[0] = $Player.global_position | |
| 95 | $PlayerToTarget.points[1] = closest_enemy.global_position | |
| 96 | ||
| 97 | func _on_Button_pressed() -> void: | |
| 98 | get_tree().paused = false | |
| 99 | $CanvasLayer/PauseMenu.hide() |