I'm working on my school project about my first top down shooting game. Anyway after I did my project by following a tutorial from youtube I desire to add more enemies. But I've no idea how to do it?
I've tried it by using the same code with the first enemy I did but it's not working as I expect. The first enemy that I followed from tutorial works really fine. But The second enemy I do it by copy from the first one's only move forward not following where the player go, when the player touch it's harm nothing to player and the animations like walking, attacking or dead doesn't play as it should be. Here's my enemy code:
extends CharacterBody2D
@onready var main = get_node("/root/Main")
@onready var player = get_node("/root/Main/Player")
var explosion_scene := preload("res://scenes/explosion.tscn")
var item_scene := preload("res://scenes/item.tscn")
signal hit_player
var alive : bool
var entered : bool
var speed : int = 100
var direction : Vector2
const DROP_CHANCE : float = 0.1
func _ready():
var screen_rect = get_viewport_rect()
alive = true
entered = false
#pick a direction for the entrance
var dist = screen_rect.get_center() - position
#check if need to move horizontally or vertically
if abs(dist.x) > abs(dist.y):
#move horizontally
direction.x = dist.x
direction.y = 0
else:
#move vertically
direction.x = 0
direction.y = dist.y
func _physics_process(_delta):
if alive:
$AnimatedSprite2D.animation = "run"
if entered:
direction = (player.position - position)
direction = direction.normalized()
velocity = direction * speed
move_and_slide()
if velocity.x != 0:
$AnimatedSprite2D.flip_h = velocity.x < 0
else:
pass
func die():
alive = false
$AnimatedSprite2D.stop()
$AnimatedSprite2D.animation = "dead"
$Area2D/CollisionShape2D.set_deferred("disabled", true)
if randf() <= DROP_CHANCE:
drop_item()
var explosion = explosion_scene.instantiate()
explosion.position = position
main.add_child(explosion)
explosion.process_mode = Node.PROCESS_MODE_ALWAYS
func drop_item():
var item = item_scene.instantiate()
item.position = position
item.item_type = randi_range(0, 2)
main.call_deferred("add_child", item)
item.add_to_group("items")
func _on_entrance_timer_timeout():
entered = true
func _on_area_2d_body_entered(_body):
hit_player.emit()
Also it will sent the signal to enemy_spawner to spawn it on Marker2D This is my enemy_spawner code:
extends Node2D
@onready var main = get_node("/root/Main")
signal hit_p
var goblin_scene := preload("res://scenes/goblin.tscn")
var spawn_points := []
# Called when the node enters the scene tree for the first time.
func _ready():
for i in get_children():
if i is Marker2D:
spawn_points.append(i)
func _on_timer_timeout():
#check how many enemies have already been created
var enemies = get_tree().get_nodes_in_group("enemies")
if enemies.size() < get_parent().max_enemies:
#pick random spawn point
var spawn = spawn_points[randi() % spawn_points.size()]
#spawn enemy
var goblin = goblin_scene.instantiate()
goblin.position = spawn.position
goblin.hit_player.connect(hit)
main.add_child(goblin)
goblin.add_to_group("enemies")
func hit():
hit_p.emit()
- Thank you -
enemy_spawner
code has an if statement, which executes only one time, and I don't see other parts where you "spawn" enemies. "Also it will sent the signal to enemy_spawner to spawn it on Marker2D" Who? The enemy send a signal to spawn? Please, clarify