0 votes

I have a game that looks like this
enter image description here

I want to spawn the enemy mob in the middle of the screen, and then go try to hit the player, if they miss, they go offscreen and get deleted.

E.g.
enter image description here

How can I do this with my current code?

Mob.gd

extends RigidBody2D


export var speed = 250
var velocity = Vector2(1, 0)

func _process(delta):
    global_position += velocity.rotated(rotation) * speed * delta

func _ready():
var mob_types = $AnimatedSprite.frames.get_animation_names()
$AnimatedSprite.animation = mob_types[randi() % mob_types.size()]

func _on_VisibilityNotifier2D_screen_exited():
 queue_free()

Main.gd (The "Arena" or "Stage" Scene)

extends Node2D

export var speed = 150
var velocity = Vector2()
var screen_size  # Size of the game window.
var center

var mob = preload("res://Scenes/Mob.tscn")

func _ready():
    randomize()

    screen_size = get_viewport_rect().size
    center = screen_size * 0.5

func _process(delta):
    velocity.x = int(rand_range(0, 1))
    velocity.y = int(rand_range(0, 1))

    velocity = velocity.normalized()

    global_position += speed * velocity * delta

    Global.instance_node(mob, global_position, get_parent())

Global.gd

extends Node

func instance_node(node, location, parent):
    var node_instance = node.instance()
    parent.add_child(node_instance)
    node_instance.global_position = location
    return node_instance
Godot version 3.4.2
in Engine by (40 points)
edited by

1 Answer

0 votes
Best answer
by (650 points)
selected by

Doesn't seem to work sadly. Perhaps my timers are broken?
enter image description here

Main.gd

extends Node2D

export var speed = 150
export (PackedScene) var Mob

var velocity = Vector2()

var score

func _ready():
    randomize()

func _process(delta):
    pass

func game_over():
    $ScoreTimer.stop()
    $MobTimer.stop()

func new_game():
    score = 0
    $Player.start($StartPosition.position)
    $StartTimer.start()


func _on_MobTimer_timeout():
    var mob = Mob.instance()
    owner.add_child(mob)
    mob.transform = $HoleFromHell.global_transform


func _on_ScoreTimer_timeout():
    score += 1


func _on_StartTimer_timeout():
    $MobTimer.start()
    $ScoreTimer.start()

Mob.gd

extends Area2D


export var speed = 250

func _physics_process(delta):
    position += transform.x * speed * delta

func _ready():
    var mob_types = $AnimatedSprite.frames.get_animation_names()
    $AnimatedSprite.animation = mob_types[randi() % mob_types.size()]

func _on_Bullet_body_entered(body):
    if body.is_in_group("player"):
        body.queue_free()
    queue_free()

Okay, I figured out the problem, however, I'm still confused about the second post about the enemy following the player, I want the Position2D to rotate to where the player was to target the Mobs.

I think this should be split to a new question, but thanks for the help on this.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.