Add enemies and let them be shot

This commit is contained in:
DerTyp7
2023-09-19 16:53:38 +02:00
parent f8b11b590b
commit 12cf356951
6 changed files with 84 additions and 32 deletions

View File

@@ -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()

View File

@@ -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")

22
Enemy.gd Normal file
View File

@@ -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()

30
Enemy.tscn Normal file
View File

@@ -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"]

View File

@@ -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()

View File

@@ -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"]