From 1eaae060402b4f451d1a8d85aa52cc9212e662bb Mon Sep 17 00:00:00 2001 From: DerTyp7 Date: Wed, 20 Sep 2023 16:45:48 +0200 Subject: [PATCH] Fix collider and add waves --- Bullet.gd | 10 ++++++++++ Bullet.tscn | 15 +++++++++++++-- Enemy.gd | 19 +++++++++++++++---- Enemy.tscn | 5 +++-- EnemySpawner.gd | 44 ++++++++++++++++++++++++++++++++++++++++++++ Enemy_spawner.tscn | 6 ++++++ main.tscn | 14 ++++++++------ 7 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 EnemySpawner.gd create mode 100644 Enemy_spawner.tscn diff --git a/Bullet.gd b/Bullet.gd index f6a8725..a039574 100644 --- a/Bullet.gd +++ b/Bullet.gd @@ -9,5 +9,15 @@ func _ready(): destroy_timer.start() destroy_timer.connect("timeout", _on_destroy_timer_timeout) + + func _on_destroy_timer_timeout(): queue_free() + + + +#func _on_body_entered(body): +# print("---------------") +# print(body.name) +# if "Bullet" not in body.name: +# queue_free() diff --git a/Bullet.tscn b/Bullet.tscn index a732e69..3bdd39f 100644 --- a/Bullet.tscn +++ b/Bullet.tscn @@ -1,13 +1,17 @@ -[gd_scene load_steps=4 format=3 uid="uid://day7or3kp8x5u"] +[gd_scene load_steps=5 format=3 uid="uid://day7or3kp8x5u"] [ext_resource type="Script" path="res://Bullet.gd" id="1_0l6ar"] [ext_resource type="Texture2D" uid="uid://dafsi3dqavgnb" path="res://Untitled.png" id="1_cjg2r"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_suj8m"] -size = Vector2(8, 8) +size = Vector2(4, 4) + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_uncb7"] +size = Vector2(6, 6) [node name="Bullet" type="RigidBody2D"] gravity_scale = 0.0 +continuous_cd = 1 script = ExtResource("1_0l6ar") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] @@ -17,3 +21,10 @@ shape = SubResource("RectangleShape2D_suj8m") position = Vector2(-2.38419e-07, -2.38419e-07) scale = Vector2(0.4, 0.4) texture = ExtResource("1_cjg2r") + +[node name="Area2D" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +shape = SubResource("RectangleShape2D_uncb7") + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/Enemy.gd b/Enemy.gd index c4ad54f..2307501 100644 --- a/Enemy.gd +++ b/Enemy.gd @@ -4,6 +4,7 @@ var speed: float = 100.0 var motion: Vector2 = Vector2.ZERO func _ready(): + set_meta("dead", false) pass func _physics_process(delta): @@ -11,12 +12,22 @@ func _physics_process(delta): var direction = (player.position - position).normalized() motion = direction * speed look_at(player.position) - + move_and_collide(motion * delta) -func _on_area_2d_body_entered(body): +#func _on_area_2d_body_entered(body): +# print("Bullet hit") +# print(body.name) +# if "Bullet" in body.name: +# set_meta("dead", true) +# queue_free() + + +func _on_area_2d_area_entered(area): print("Bullet hit") - print(body.name) - if "Bullet" in body.name: + print(area.get_parent().name) + if "Bullet" in area.get_parent().name: + set_meta("dead", true) + area.get_parent().queue_free() queue_free() diff --git a/Enemy.tscn b/Enemy.tscn index 9014d71..f07531f 100644 --- a/Enemy.tscn +++ b/Enemy.tscn @@ -11,6 +11,7 @@ size = Vector2(0.203555, 2.52986) [node name="Enemy" type="CharacterBody2D"] script = ExtResource("1_cnaxx") +metadata/dead = false [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("RectangleShape2D_aukek") @@ -22,9 +23,9 @@ texture = ExtResource("1_026jc") [node name="Area2D" type="Area2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] -position = Vector2(-2.38419e-07, 0) +position = Vector2(1.19209e-07, 5.96046e-08) rotation = -3.14159 scale = Vector2(58.952, -4.74335) shape = SubResource("RectangleShape2D_53hbt") -[connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"] +[connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"] diff --git a/EnemySpawner.gd b/EnemySpawner.gd new file mode 100644 index 0000000..67c9ee5 --- /dev/null +++ b/EnemySpawner.gd @@ -0,0 +1,44 @@ +extends Node + +var Enemy = preload("res://Enemy.tscn") + +var wave = 0; +var enemyList = [] +var spawnpoints = [[20, 80], [50, 120],[90, 170],[500, 50]] + +func _ready(): + nextWave() + pass # Replace with function body. + +func _process(delta): + var i = 0 + for enemy in enemyList: + if not enemy || enemy.get_meta("dead") == true: + enemyList.remove_at(i) + i = i + 1; + + if len(enemyList) == 0: + nextWave() + +func spawnEnemies(count): + var i = 0; + while i < count: + print("spawn enemy") + var enemyInstance = Enemy.instantiate() + enemyInstance.name = "Enemy" + str(randf()) + enemyInstance.position = Vector2(spawnpoints.pick_random()[0] + i, spawnpoints.pick_random()[1] + i) + get_parent().call_deferred("add_child", enemyInstance) + enemyList.append(enemyInstance) + i = i+1; + +func startWave(): + print("start wave") + if wave == 0: + spawnEnemies(1) + else: + spawnEnemies(wave * 3) + + +func nextWave(): + wave = wave+1; + startWave() diff --git a/Enemy_spawner.tscn b/Enemy_spawner.tscn new file mode 100644 index 0000000..6f17a2a --- /dev/null +++ b/Enemy_spawner.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://drc844kmvl3xa"] + +[ext_resource type="Script" path="res://EnemySpawner.gd" id="1_d3ir5"] + +[node name="EnemySpawner" type="Node"] +script = ExtResource("1_d3ir5") diff --git a/main.tscn b/main.tscn index a3c9f87..8cbb0f6 100644 --- a/main.tscn +++ b/main.tscn @@ -2,18 +2,23 @@ [ext_resource type="Script" path="res://Player.gd" id="1_6ke6x"] [ext_resource type="Texture2D" uid="uid://dafsi3dqavgnb" path="res://Untitled.png" id="2_w1il1"] -[ext_resource type="PackedScene" uid="uid://dkbgu7ghl048y" path="res://Enemy.tscn" id="3_geuud"] +[ext_resource type="PackedScene" uid="uid://drc844kmvl3xa" path="res://Enemy_spawner.tscn" id="4_vhwqp"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_fs1q0"] size = Vector2(10, 10) [sub_resource type="ShaderMaterial" id="ShaderMaterial_tqqca"] -[sub_resource type="RectangleShape2D" id="RectangleShape2D_my1x2"] +[sub_resource type="RectangleShape2D" id="RectangleShape2D_1cb75"] size = Vector2(14, 14) [node name="World" type="Node2D"] +[node name="Camera2D" type="Camera2D" parent="."] +zoom = Vector2(1.5, 1.5) + +[node name="EnemySpawner" parent="." instance=ExtResource("4_vhwqp")] + [node name="Player" type="CharacterBody2D" parent="."] script = ExtResource("1_6ke6x") @@ -33,9 +38,6 @@ offset = Vector2(45.275, 0) [node name="Area2D" type="Area2D" parent="Player"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Player/Area2D"] -shape = SubResource("RectangleShape2D_my1x2") - -[node name="Enemy" parent="." instance=ExtResource("3_geuud")] -position = Vector2(246, 135) +shape = SubResource("RectangleShape2D_1cb75") [connection signal="body_entered" from="Player/Area2D" to="Player" method="_on_area_2d_body_entered"]