basic interface and no success trying to use them in a Tool script
This commit is contained in:
62
scenes/spawners/Spawner.cs
Normal file
62
scenes/spawners/Spawner.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
[Tool]
|
||||
public partial class Spawner : Node3D
|
||||
{
|
||||
[Export(PropertyHint.NodeType)]
|
||||
public PackedScene SceneToSpawn { get; set; }
|
||||
|
||||
[Export]
|
||||
public Resource Inputs;
|
||||
[Export]
|
||||
public Node3D Target { get; set; }
|
||||
|
||||
[Export(PropertyHint.Range, "0.1, 100, 0.1, or_greater")]
|
||||
public float SpawnInterval { get; set; } = 1.0f;
|
||||
[Export]
|
||||
public bool IsActiveOnStart { get; set; } = true;
|
||||
|
||||
private float _spawnTimer;
|
||||
private bool _isActive;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_isActive = IsActiveOnStart && !Engine.IsEditorHint(); // Keep it inactive in Editor
|
||||
_spawnTimer = SpawnInterval;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (!_isActive) return;
|
||||
|
||||
_spawnTimer -= (float) delta;
|
||||
if (_spawnTimer <= 0) Spawn();
|
||||
}
|
||||
|
||||
public void Spawn()
|
||||
{
|
||||
if (SceneToSpawn == null || !SceneToSpawn.CanInstantiate()) return;
|
||||
|
||||
var spawnedInstance = SceneToSpawn.Instantiate() as FirstEnemy;
|
||||
if (spawnedInstance == null) return;
|
||||
|
||||
spawnedInstance.Inputs = Inputs as EnemyInitInputs;
|
||||
spawnedInstance.Target = Target;
|
||||
GetTree().GetCurrentScene().AddChild(spawnedInstance);
|
||||
spawnedInstance.GlobalPosition = GlobalPosition;
|
||||
|
||||
_spawnTimer = SpawnInterval;
|
||||
}
|
||||
|
||||
public void StartSpawning()
|
||||
{
|
||||
_isActive = true;
|
||||
_spawnTimer = SpawnInterval;
|
||||
}
|
||||
|
||||
public void StopSpawning()
|
||||
{
|
||||
_isActive = false;
|
||||
}
|
||||
}
|
||||
1
scenes/spawners/Spawner.cs.uid
Normal file
1
scenes/spawners/Spawner.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://djyspwixi01j5
|
||||
10
scenes/spawners/spawner.tscn
Normal file
10
scenes/spawners/spawner.tscn
Normal file
@@ -0,0 +1,10 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://c305mfrtumcyq"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://djyspwixi01j5" path="res://scenes/spawners/Spawner.cs" id="1_2otbo"]
|
||||
[ext_resource type="PackedScene" uid="uid://dxt0e2ugmttqq" path="res://scenes/enemies/first_enemy.tscn" id="2_2otbo"]
|
||||
|
||||
[node name="Spawner" type="Node3D"]
|
||||
script = ExtResource("1_2otbo")
|
||||
SceneToSpawn = ExtResource("2_2otbo")
|
||||
|
||||
[node name="Marker3D" type="Marker3D" parent="."]
|
||||
Reference in New Issue
Block a user