From 8158c96dad1bc60bb50108d014cb9488608b81f9 Mon Sep 17 00:00:00 2001 From: Moe Poi ~ Date: Fri, 6 Oct 2023 19:25:13 +0700 Subject: [PATCH] Optimize enemy script --- scenes/bullets/bullet-1.gd | 2 +- scenes/enemies/enemy-1.tscn | 20 ++++++------------ scenes/stages/stage 1/stage-1.tscn | 9 +++++--- scripts/enemies/enemy-1.gd | 19 ----------------- scripts/enemies/enemy.gd | 31 +++++++++++++++++++++++++++ scripts/stages/stage 1/Config.gd | 15 +++++++++++++ scripts/stages/stage 1/summon.gd | 34 +++++++++++++++++------------- scripts/stages/stage 1/walk.gd | 11 ---------- 8 files changed, 79 insertions(+), 62 deletions(-) delete mode 100644 scripts/enemies/enemy-1.gd create mode 100644 scripts/enemies/enemy.gd delete mode 100644 scripts/stages/stage 1/walk.gd diff --git a/scenes/bullets/bullet-1.gd b/scenes/bullets/bullet-1.gd index fd9cf10..0c519b3 100644 --- a/scenes/bullets/bullet-1.gd +++ b/scenes/bullets/bullet-1.gd @@ -13,5 +13,5 @@ func _on_VisibilityNotifier2D_screen_exited(): func _on_Bullet_body_entered(body): if body.is_in_group("Enemy"): - body.hit(damage) + body.get_parent().hit(damage) queue_free() diff --git a/scenes/enemies/enemy-1.tscn b/scenes/enemies/enemy-1.tscn index 7672603..41b0648 100644 --- a/scenes/enemies/enemy-1.tscn +++ b/scenes/enemies/enemy-1.tscn @@ -1,7 +1,6 @@ -[gd_scene load_steps=15 format=3 uid="uid://clceqphyig48x"] +[gd_scene load_steps=14 format=3 uid="uid://bdkpehb7sykww"] -[ext_resource type="Script" path="res://scripts/stages/stage 1/walk.gd" id="1_ic50n"] -[ext_resource type="Script" path="res://scripts/enemies/enemy-1.gd" id="2_2ldr2"] +[ext_resource type="Script" path="res://scripts/enemies/enemy.gd" id="1_oeuts"] [ext_resource type="Texture2D" uid="uid://b046l0oy0hyj2" path="res://assets/spritesheets/enemy-1.png" id="2_ndkpn"] [sub_resource type="AtlasTexture" id="AtlasTexture_17kv3"] @@ -82,23 +81,22 @@ height = 39.999 [node name="PathFollow2D" type="PathFollow2D"] rotation = 0.00699289 loop = false -script = ExtResource("1_ic50n") +script = ExtResource("1_oeuts") -[node name="enemy" type="CharacterBody2D" parent="." groups=["Enemy"]] -script = ExtResource("2_2ldr2") +[node name="Enemy" type="CharacterBody2D" parent="." groups=["Enemy"]] -[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="enemy"] +[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="Enemy"] position = Vector2(9.15527e-05, 0.00012207) scale = Vector2(0.176347, 0.175058) sprite_frames = SubResource("SpriteFrames_kipn1") autoplay = "default" frame_progress = 0.735051 -[node name="CollisionShape2D" type="CollisionShape2D" parent="enemy"] +[node name="CollisionShape2D" type="CollisionShape2D" parent="Enemy"] position = Vector2(-0.992983, 1.00696) shape = SubResource("CapsuleShape2D_5hy1j") -[node name="HealthBar" type="ProgressBar" parent="enemy"] +[node name="HealthBar" type="ProgressBar" parent="Enemy"] modulate = Color(0.431373, 0.815686, 0, 1) offset_left = -15.1814 offset_top = -25.8945 @@ -106,7 +104,3 @@ offset_right = 86.8186 offset_bottom = 1.10553 scale = Vector2(0.3, 0.3) show_percentage = false - -[node name="_Timer_66182" type="Timer" parent="."] -wait_time = 0.2 -one_shot = true diff --git a/scenes/stages/stage 1/stage-1.tscn b/scenes/stages/stage 1/stage-1.tscn index f10986f..9d999db 100644 --- a/scenes/stages/stage 1/stage-1.tscn +++ b/scenes/stages/stage 1/stage-1.tscn @@ -1,10 +1,9 @@ -[gd_scene load_steps=12 format=3 uid="uid://btdewn34d67m"] +[gd_scene load_steps=11 format=3 uid="uid://btdewn34d67m"] [ext_resource type="Texture2D" uid="uid://cnxj0mf3luxo" path="res://assets/tileset/ashlands/tf_A5_ashlands_3.png" id="1_rhsei"] [ext_resource type="Script" path="res://scripts/stages/stage 1/GameManager.gd" id="1_yugmb"] [ext_resource type="Texture2D" uid="uid://danbfdqmqu23q" path="res://assets/tileset/ashlands/tf_B_ashlands_3.png" id="2_ao304"] [ext_resource type="Script" path="res://scripts/stages/stage 1/summon.gd" id="3_wqx3u"] -[ext_resource type="PackedScene" uid="uid://clceqphyig48x" path="res://scenes/enemies/enemy-1.tscn" id="4_iivll"] [ext_resource type="PackedScene" uid="uid://cd5r1v6f8hahx" path="res://scenes/ui/tower-menu.tscn" id="6_p2t31"] [ext_resource type="PackedScene" uid="uid://bvoenhbaqiqj2" path="res://scenes/towers/placement/placement.tscn" id="8_ftqhi"] @@ -542,7 +541,9 @@ layer_1/tile_data = PackedInt32Array(196628, 262146, 6, 131089, 2, 13, 720899, 3 [node name="Path2D" type="Path2D" parent="."] curve = SubResource("Curve2D_csucy") script = ExtResource("3_wqx3u") -enemy = ExtResource("4_iivll") + +[node name="Timer" type="Timer" parent="Path2D"] +one_shot = true [node name="CanvasLayer" type="CanvasLayer" parent="."] @@ -559,3 +560,5 @@ position = Vector2(676, 192) [node name="placement3" parent="Placements" instance=ExtResource("8_ftqhi")] position = Vector2(844, 393) + +[connection signal="timeout" from="Path2D/Timer" to="Path2D" method="_on_timer_timeout"] diff --git a/scripts/enemies/enemy-1.gd b/scripts/enemies/enemy-1.gd deleted file mode 100644 index d09f076..0000000 --- a/scripts/enemies/enemy-1.gd +++ /dev/null @@ -1,19 +0,0 @@ -extends CharacterBody2D - -@export var health : int = 100 - -func _process(delta): - update_healthbar() - if health <= 0: - queue_free() - -func update_healthbar(): - $HealthBar.value = health - - if health >= 100: - $HealthBar.visible = false - else: - $HealthBar.visible = true - -func hit(damage : int): - health = health - damage diff --git a/scripts/enemies/enemy.gd b/scripts/enemies/enemy.gd new file mode 100644 index 0000000..95065c3 --- /dev/null +++ b/scripts/enemies/enemy.gd @@ -0,0 +1,31 @@ +extends PathFollow2D + +var config = null +var enemy_id: int = 0 +var health: int = 0 +var speed: int = 0 + +func set_config(config_path): + config = load(config_path).new() + +func set_enemy(id: int): + enemy_id = id + health = config.enemy[enemy_id]['health'] + speed = config.enemy[enemy_id]['speed'] + +func _process(delta): + update_healthbar() + if health <= 0: + queue_free() + set_progress(get_progress() + speed * delta) + +func update_healthbar(): + $Enemy/HealthBar.value = health + + if health >= 100: + $Enemy/HealthBar.visible = false + else: + $Enemy/HealthBar.visible = true + +func hit(damage : int): + health = health - damage diff --git a/scripts/stages/stage 1/Config.gd b/scripts/stages/stage 1/Config.gd index 8ac58d2..1a36a2a 100644 --- a/scripts/stages/stage 1/Config.gd +++ b/scripts/stages/stage 1/Config.gd @@ -2,6 +2,11 @@ extends Node class_name Config +var game = { + "spawn_timer": 3.0, + "offset": [-30.0, 30.0] +} + var menu = [ { "name": "Cannon", @@ -85,3 +90,13 @@ var tower = [ "damage": 40 } ] + +var enemy = [ + { + "name": "Demon", + "asset": "preload()", + "health": 100, + "speed": 30, + "damage": 1 + } +] diff --git a/scripts/stages/stage 1/summon.gd b/scripts/stages/stage 1/summon.gd index 03f8bcb..6b234cf 100644 --- a/scripts/stages/stage 1/summon.gd +++ b/scripts/stages/stage 1/summon.gd @@ -1,24 +1,28 @@ extends Path2D -var timer = 0 -@export var spawnTime = 3 +@onready var timer = $Timer + +var config_path = "res://scripts/stages/stage 1/Config.gd" +var config = load(config_path).new() + var spawn_first: bool = true -@export var enemy: PackedScene = null +var enemy: PackedScene = preload("res://scenes/enemies/enemy-1.tscn") -# Called when the node enters the scene tree for the first time. func _ready(): - pass # Replace with function body. + summon() + timer.wait_time = config.game['spawn_timer'] + timer.start() +func _on_timer_timeout(): + summon() + timer.start() -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - timer += delta +func summon(): + var rng = RandomNumberGenerator.new() - if spawn_first: - add_child(enemy.instantiate()) - spawn_first = false - - if timer > spawnTime: - add_child(enemy.instantiate()) - timer = 0 + var enemy_instance = enemy.instantiate() + add_child(enemy_instance) + enemy_instance.set_config(config_path) + enemy_instance.set_enemy(0) + enemy_instance.set_v_offset(rng.randf_range(config.game['offset'][0], config.game['offset'][1])) diff --git a/scripts/stages/stage 1/walk.gd b/scripts/stages/stage 1/walk.gd deleted file mode 100644 index 19f50da..0000000 --- a/scripts/stages/stage 1/walk.gd +++ /dev/null @@ -1,11 +0,0 @@ -extends PathFollow2D - -@export var speed = 30 - -func _ready(): - var rng = RandomNumberGenerator.new() - set_v_offset(rng.randf_range(-30.0, 30.0)) - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - set_progress(get_progress() + speed * delta)