From 12cf35695178ca1ed673b5e50d3382d9999eeae4 Mon Sep 17 00:00:00 2001 From: DerTyp7 Date: Tue, 19 Sep 2023 16:53:38 +0200 Subject: [PATCH] Add enemies and let them be shot --- Bullet.gd | 14 ++------------ Bullet.tscn | 14 +++----------- Enemy.gd | 22 ++++++++++++++++++++++ Enemy.tscn | 30 ++++++++++++++++++++++++++++++ Player.gd | 26 +++++++++++++++++++------- main.tscn | 10 ++++++++-- 6 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 Enemy.gd create mode 100644 Enemy.tscn diff --git a/Bullet.gd b/Bullet.gd index 39015d7..f6a8725 100644 --- a/Bullet.gd +++ b/Bullet.gd @@ -5,19 +5,9 @@ var destroy_timer := Timer.new() func _ready(): add_child(destroy_timer) destroy_timer.wait_time = 5.0 - destroy_timer.one_shot = true # Automatically stops after timing out - - # Start the timer + destroy_timer.one_shot = true destroy_timer.start() - - # Connect the "timeout" signal destroy_timer.connect("timeout", _on_destroy_timer_timeout) - # Set the wait time for the timer (5 seconds) - - - - func _on_destroy_timer_timeout(): - # This function will be called when the timer times out - queue_free() # Destroy the object + queue_free() diff --git a/Bullet.tscn b/Bullet.tscn index 0272750..a732e69 100644 --- a/Bullet.tscn +++ b/Bullet.tscn @@ -1,15 +1,12 @@ -[gd_scene load_steps=5 format=3 uid="uid://day7or3kp8x5u"] +[gd_scene load_steps=4 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(4, 4) +size = Vector2(8, 8) -[sub_resource type="RectangleShape2D" id="RectangleShape2D_lfity"] -size = Vector2(4, 4) - -[node name="RigidBody2D" type="RigidBody2D"] +[node name="Bullet" type="RigidBody2D"] gravity_scale = 0.0 script = ExtResource("1_0l6ar") @@ -20,8 +17,3 @@ 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_lfity") diff --git a/Enemy.gd b/Enemy.gd new file mode 100644 index 0000000..c4ad54f --- /dev/null +++ b/Enemy.gd @@ -0,0 +1,22 @@ +extends CharacterBody2D + +var speed: float = 100.0 +var motion: Vector2 = Vector2.ZERO + +func _ready(): + pass + +func _physics_process(delta): + var player = get_parent().get_node("Player") + 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): + print("Bullet hit") + print(body.name) + if "Bullet" in body.name: + queue_free() diff --git a/Enemy.tscn b/Enemy.tscn new file mode 100644 index 0000000..9014d71 --- /dev/null +++ b/Enemy.tscn @@ -0,0 +1,30 @@ +[gd_scene load_steps=5 format=3 uid="uid://dkbgu7ghl048y"] + +[ext_resource type="Texture2D" uid="uid://dafsi3dqavgnb" path="res://Untitled.png" id="1_026jc"] +[ext_resource type="Script" path="res://Enemy.gd" id="1_cnaxx"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_aukek"] +size = Vector2(10, 10) + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_53hbt"] +size = Vector2(0.203555, 2.52986) + +[node name="Enemy" type="CharacterBody2D"] +script = ExtResource("1_cnaxx") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_aukek") + +[node name="Sprite2D" type="Sprite2D" parent="."] +modulate = Color(0.94902, 0, 0, 1) +texture = ExtResource("1_026jc") + +[node name="Area2D" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +position = Vector2(-2.38419e-07, 0) +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"] diff --git a/Player.gd b/Player.gd index ed6a07b..2c3379a 100644 --- a/Player.gd +++ b/Player.gd @@ -19,10 +19,10 @@ func _on_shoot_timer_timeout(): func _physics_process(delta): look() - move() + move(delta) - if Input.is_action_pressed("shoot"): - shoot() +# if Input.is_action_pressed("shoot"): +# shoot() func shoot(): if can_shoot: @@ -41,6 +41,7 @@ func look(): look_direction.x -= 1 if Input.is_action_pressed("look_right"): look_direction.x += 1 + # Diagonal look if Input.is_action_pressed("look_up") && Input.is_action_pressed("look_left"): @@ -55,8 +56,11 @@ func look(): if look_direction != Vector2.ZERO: look_direction = look_direction.normalized() rotation = look_direction.angle() - -func move(): + + if Input.is_action_pressed("look_up") || Input.is_action_pressed("look_down") || Input.is_action_pressed("look_left") || Input.is_action_pressed("look_right"): + shoot(); + +func move(delta): velocity = Vector2.ZERO if Input.is_action_pressed("walk_up"): @@ -71,14 +75,14 @@ func move(): velocity = velocity.normalized() * movespeed move_and_slide() - + velocity.lerp(Vector2.ZERO, 0.1) func fire(): var bullet_instance = bullet.instantiate() # Create an instance of the bullet var offset = Vector2(20, 0).rotated(rotation) # Adjust the offset as needed bullet_instance.position = global_position + offset bullet_instance.rotation_degrees = rotation_degrees - + bullet_instance.name = "Bullet " + str(randf()) # Calculate the bullet direction based on the player's rotation var bullet_direction = Vector2(bullet_speed, 0).rotated(rotation) @@ -89,3 +93,11 @@ func fire(): get_tree().get_root().call_deferred("add_child", bullet_instance) +func kill(): + print("kill") + get_tree().reload_current_scene() + + +func _on_area_2d_body_entered(body): + if "Enemy" in body.name: + kill() diff --git a/main.tscn b/main.tscn index 58efbab..a3c9f87 100644 --- a/main.tscn +++ b/main.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=6 format=3 uid="uid://b0i261cp1ugkh"] +[gd_scene load_steps=7 format=3 uid="uid://b0i261cp1ugkh"] [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"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_fs1q0"] size = Vector2(10, 10) @@ -9,7 +10,7 @@ size = Vector2(10, 10) [sub_resource type="ShaderMaterial" id="ShaderMaterial_tqqca"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_my1x2"] -size = Vector2(12, 12) +size = Vector2(14, 14) [node name="World" type="Node2D"] @@ -33,3 +34,8 @@ offset = Vector2(45.275, 0) [node name="CollisionShape2D" type="CollisionShape2D" parent="Player/Area2D"] shape = SubResource("RectangleShape2D_my1x2") + +[node name="Enemy" parent="." instance=ExtResource("3_geuud")] +position = Vector2(246, 135) + +[connection signal="body_entered" from="Player/Area2D" to="Player" method="_on_area_2d_body_entered"]