Rewrite Enemy Spawn Script

This commit is contained in:
Moe Poi ~ 2023-10-20 14:52:51 +07:00
parent 9e975bb9bf
commit 275e11722c
12 changed files with 249 additions and 168 deletions

View file

@ -5,7 +5,9 @@ var game = {
"duration": 300, "duration": 300,
"health": 6, "health": 6,
"resource": 100, "resource": 100,
"spawn_timer": 3.0, "spawn_timer": 10.0,
"spawn_count": 3,
"spawn_delay": 2,
"offset": [-30.0, 30.0], "offset": [-30.0, 30.0],
"path_direction": [ "path_direction": [
[ [
@ -131,44 +133,11 @@ var tower = [
var enemy = [ var enemy = [
{ {
"name": "Demon", "name": "Demon",
"asset": preload("res://scenes/enemies/demon.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 10 "reward": 10,
}, "spawn_rate": 1
{
"name": "Orc",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 20
},
{
"name": "Cyclops",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 30
},
{
"name": "Demon Boar",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 40
},
{
"name": "Demon Fungus",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 50
},
{
"name": "Demon Shark",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 60
} }
] ]

View file

@ -1,25 +1,47 @@
extends Path2D extends Path2D
@onready var timer = $Timer @onready var timer = $Timer
@export var type: int = 0 @export var type: int = 0
var config_path = "res://scripts/stages/1/config.gd" var config_path = "res://scripts/stages/1/config.gd"
var config = load(config_path).new() var config = load(config_path).new()
var spawn_first: bool = true var enemies = []
var probabilities = []
var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn")
func _ready(): func _ready():
summon() for enemy in config.enemy:
enemies.append(enemy['asset'])
probabilities.append(enemy['spawn_rate'])
summon_init()
timer.wait_time = config.game['spawn_timer'] timer.wait_time = config.game['spawn_timer']
timer.start() timer.start()
func weighted_random_choice(items, weights):
var total_weight = 0
for weight in weights:
total_weight += weight
var random_value = randf() * total_weight
for i in range(len(items)):
random_value -= weights[i]
if random_value <= 0:
return items[i]
func _on_timer_timeout(): func _on_timer_timeout():
summon() summon_init()
timer.start() timer.start()
func summon(): func summon_init():
for x in range(config.game['spawn_count']):
summon(weighted_random_choice(enemies, probabilities))
await get_tree().create_timer(config.game['spawn_delay']).timeout
func summon(enemy: PackedScene):
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
var enemy_instance = enemy.instantiate() var enemy_instance = enemy.instantiate()

View file

@ -5,7 +5,9 @@ var game = {
"duration": 300, "duration": 300,
"health": 6, "health": 6,
"resource": 100, "resource": 100,
"spawn_timer": 3.0, "spawn_timer": 10.0,
"spawn_count": 2,
"spawn_delay": 2,
"offset": [-40.0, 35.0], "offset": [-40.0, 35.0],
"path_direction": [ "path_direction": [
[ [
@ -161,44 +163,20 @@ var tower = [
var enemy = [ var enemy = [
{ {
"name": "Demon", "name": "Demon",
"asset": preload("res://scenes/enemies/demon.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 10 "reward": 10,
"spawn_rate": 0.80
}, },
{ {
"name": "Orc", "name": "Orc",
"asset": preload("res://scenes/enemies/orc.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 20 "reward": 20,
}, "spawn_rate": 0.20
{
"name": "Cyclops",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 30
},
{
"name": "Demon Boar",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 40
},
{
"name": "Demon Fungus",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 50
},
{
"name": "Demon Shark",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 60
} }
] ]

View file

@ -1,25 +1,47 @@
extends Path2D extends Path2D
@onready var timer = $Timer @onready var timer = $Timer
@export var type: int = 0 @export var type: int = 0
var config_path = "res://scripts/stages/2/config.gd" var config_path = "res://scripts/stages/2/config.gd"
var config = load(config_path).new() var config = load(config_path).new()
var spawn_first: bool = true var enemies = []
var probabilities = []
var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn")
func _ready(): func _ready():
summon() for enemy in config.enemy:
enemies.append(enemy['asset'])
probabilities.append(enemy['spawn_rate'])
summon_init()
timer.wait_time = config.game['spawn_timer'] timer.wait_time = config.game['spawn_timer']
timer.start() timer.start()
func weighted_random_choice(items, weights):
var total_weight = 0
for weight in weights:
total_weight += weight
var random_value = randf() * total_weight
for i in range(len(items)):
random_value -= weights[i]
if random_value <= 0:
return items[i]
func _on_timer_timeout(): func _on_timer_timeout():
summon() summon_init()
timer.start() timer.start()
func summon(): func summon_init():
for x in range(config.game['spawn_count']):
summon(weighted_random_choice(enemies, probabilities))
await get_tree().create_timer(config.game['spawn_delay']).timeout
func summon(enemy: PackedScene):
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
var enemy_instance = enemy.instantiate() var enemy_instance = enemy.instantiate()

View file

@ -5,7 +5,9 @@ var game = {
"duration": 300, "duration": 300,
"health": 6, "health": 6,
"resource": 100, "resource": 100,
"spawn_timer": 3.0, "spawn_timer": 10.0,
"spawn_count": 3,
"spawn_delay": 2,
"offset": [-40.0, 50.0], "offset": [-40.0, 50.0],
"path_direction": [ "path_direction": [
[ [
@ -143,44 +145,29 @@ var tower = [
var enemy = [ var enemy = [
{ {
"name": "Demon", "name": "Demon",
"asset": preload("res://scenes/enemies/demon.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 10 "reward": 10,
"spawn_rate": 0.60
}, },
{ {
"name": "Orc", "name": "Orc",
"asset": preload("res://scenes/enemies/orc.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 20 "reward": 20,
"spawn_rate": 0.30
}, },
{ {
"name": "Cyclops", "name": "Cyclops",
"asset": preload("res://scenes/enemies/cyclops.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 30 "reward": 30,
}, "spawn_rate": 0.10
{
"name": "Demon Boar",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 40
},
{
"name": "Demon Fungus",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 50
},
{
"name": "Demon Shark",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 60
} }
] ]

View file

@ -1,25 +1,47 @@
extends Path2D extends Path2D
@onready var timer = $Timer @onready var timer = $Timer
@export var type: int = 0 @export var type: int = 0
var config_path = "res://scripts/stages/3/config.gd" var config_path = "res://scripts/stages/3/config.gd"
var config = load(config_path).new() var config = load(config_path).new()
var spawn_first: bool = true var enemies = []
var probabilities = []
var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn")
func _ready(): func _ready():
summon() for enemy in config.enemy:
enemies.append(enemy['asset'])
probabilities.append(enemy['spawn_rate'])
summon_init()
timer.wait_time = config.game['spawn_timer'] timer.wait_time = config.game['spawn_timer']
timer.start() timer.start()
func weighted_random_choice(items, weights):
var total_weight = 0
for weight in weights:
total_weight += weight
var random_value = randf() * total_weight
for i in range(len(items)):
random_value -= weights[i]
if random_value <= 0:
return items[i]
func _on_timer_timeout(): func _on_timer_timeout():
summon() summon_init()
timer.start() timer.start()
func summon(): func summon_init():
for x in range(config.game['spawn_count']):
summon(weighted_random_choice(enemies, probabilities))
await get_tree().create_timer(config.game['spawn_delay']).timeout
func summon(enemy: PackedScene):
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
var enemy_instance = enemy.instantiate() var enemy_instance = enemy.instantiate()

View file

@ -5,7 +5,9 @@ var game = {
"duration": 300, "duration": 300,
"health": 6, "health": 6,
"resource": 100, "resource": 100,
"spawn_timer": 3.0, "spawn_timer": 10.0,
"spawn_count": 2,
"spawn_delay": 2,
"offset": [-20.0, 13.0], "offset": [-20.0, 13.0],
"path_direction": [ "path_direction": [
[ [
@ -149,44 +151,38 @@ var tower = [
var enemy = [ var enemy = [
{ {
"name": "Demon", "name": "Demon",
"asset": preload("res://scenes/enemies/demon.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 10 "reward": 10,
"spawn_rate": 0.45
}, },
{ {
"name": "Orc", "name": "Orc",
"asset": preload("res://scenes/enemies/orc.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 20 "reward": 20,
"spawn_rate": 0.35
}, },
{ {
"name": "Cyclops", "name": "Cyclops",
"asset": preload("res://scenes/enemies/cyclops.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 30 "reward": 30,
"spawn_rate": 0.15
}, },
{ {
"name": "Demon Boar", "name": "Demon Boar",
"asset": preload("res://scenes/enemies/demon_boar.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 40 "reward": 40,
}, "spawn_rate": 0.05
{
"name": "Demon Fungus",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 50
},
{
"name": "Demon Shark",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 60
} }
] ]

View file

@ -1,25 +1,47 @@
extends Path2D extends Path2D
@onready var timer = $Timer @onready var timer = $Timer
@export var type: int = 0 @export var type: int = 0
var config_path = "res://scripts/stages/4/config.gd" var config_path = "res://scripts/stages/4/config.gd"
var config = load(config_path).new() var config = load(config_path).new()
var spawn_first: bool = true var enemies = []
var probabilities = []
var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn")
func _ready(): func _ready():
summon() for enemy in config.enemy:
enemies.append(enemy['asset'])
probabilities.append(enemy['spawn_rate'])
summon_init()
timer.wait_time = config.game['spawn_timer'] timer.wait_time = config.game['spawn_timer']
timer.start() timer.start()
func weighted_random_choice(items, weights):
var total_weight = 0
for weight in weights:
total_weight += weight
var random_value = randf() * total_weight
for i in range(len(items)):
random_value -= weights[i]
if random_value <= 0:
return items[i]
func _on_timer_timeout(): func _on_timer_timeout():
summon() summon_init()
timer.start() timer.start()
func summon(): func summon_init():
for x in range(config.game['spawn_count']):
summon(weighted_random_choice(enemies, probabilities))
await get_tree().create_timer(config.game['spawn_delay']).timeout
func summon(enemy: PackedScene):
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
var enemy_instance = enemy.instantiate() var enemy_instance = enemy.instantiate()

View file

@ -5,7 +5,9 @@ var game = {
"duration": 300, "duration": 300,
"health": 6, "health": 6,
"resource": 100, "resource": 100,
"spawn_timer": 3.0, "spawn_timer": 10.0,
"spawn_count": 3,
"spawn_delay": 2,
"offset": [-20.0, 25.0], "offset": [-20.0, 25.0],
"path_direction": [ "path_direction": [
[ [
@ -149,44 +151,47 @@ var tower = [
var enemy = [ var enemy = [
{ {
"name": "Demon", "name": "Demon",
"asset": preload("res://scenes/enemies/demon.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 10 "reward": 10,
"spawn_rate": 0.30
}, },
{ {
"name": "Orc", "name": "Orc",
"asset": preload("res://scenes/enemies/orc.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 20 "reward": 20,
"spawn_rate": 0.25
}, },
{ {
"name": "Cyclops", "name": "Cyclops",
"asset": preload("res://scenes/enemies/cyclops.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 30 "reward": 30,
"spawn_rate": 0.20
}, },
{ {
"name": "Demon Boar", "name": "Demon Boar",
"asset": preload("res://scenes/enemies/demon_boar.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 40 "reward": 40,
"spawn_rate": 0.15
}, },
{ {
"name": "Demon Fungus", "name": "Demon Fungus",
"asset": preload("res://scenes/enemies/demon_fungus.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 50 "reward": 50,
}, "spawn_rate": 0.10
{
"name": "Demon Shark",
"health": 100,
"speed": 30,
"damage": 1,
"reward": 60
} }
] ]

View file

@ -1,25 +1,47 @@
extends Path2D extends Path2D
@onready var timer = $Timer @onready var timer = $Timer
@export var type: int = 0 @export var type: int = 0
var config_path = "res://scripts/stages/5/config.gd" var config_path = "res://scripts/stages/5/config.gd"
var config = load(config_path).new() var config = load(config_path).new()
var spawn_first: bool = true var enemies = []
var probabilities = []
var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn")
func _ready(): func _ready():
summon() for enemy in config.enemy:
enemies.append(enemy['asset'])
probabilities.append(enemy['spawn_rate'])
summon_init()
timer.wait_time = config.game['spawn_timer'] timer.wait_time = config.game['spawn_timer']
timer.start() timer.start()
func weighted_random_choice(items, weights):
var total_weight = 0
for weight in weights:
total_weight += weight
var random_value = randf() * total_weight
for i in range(len(items)):
random_value -= weights[i]
if random_value <= 0:
return items[i]
func _on_timer_timeout(): func _on_timer_timeout():
summon() summon_init()
timer.start() timer.start()
func summon(): func summon_init():
for x in range(config.game['spawn_count']):
summon(weighted_random_choice(enemies, probabilities))
await get_tree().create_timer(config.game['spawn_delay']).timeout
func summon(enemy: PackedScene):
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
var enemy_instance = enemy.instantiate() var enemy_instance = enemy.instantiate()

View file

@ -5,7 +5,9 @@ var game = {
"duration": 300, "duration": 300,
"health": 6, "health": 6,
"resource": 100, "resource": 100,
"spawn_timer": 3.0, "spawn_timer": 10.0,
"spawn_count": 3,
"spawn_delay": 2,
"offset": [-20.0, 20.0], "offset": [-20.0, 20.0],
"path_direction": [ "path_direction": [
[ [
@ -191,44 +193,56 @@ var tower = [
var enemy = [ var enemy = [
{ {
"name": "Demon", "name": "Demon",
"asset": preload("res://scenes/enemies/demon.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 10 "reward": 10,
"spawn_rate": 0.15
}, },
{ {
"name": "Orc", "name": "Orc",
"asset": preload("res://scenes/enemies/orc.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 20 "reward": 20,
"spawn_rate": 0.20
}, },
{ {
"name": "Cyclops", "name": "Cyclops",
"asset": preload("res://scenes/enemies/cyclops.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 30 "reward": 30,
"spawn_rate": 0.25
}, },
{ {
"name": "Demon Boar", "name": "Demon Boar",
"asset": preload("res://scenes/enemies/demon_boar.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 40 "reward": 40,
"spawn_rate": 0.20
}, },
{ {
"name": "Demon Fungus", "name": "Demon Fungus",
"asset": preload("res://scenes/enemies/demon_fungus.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 50 "reward": 50,
"spawn_rate": 0.15
}, },
{ {
"name": "Demon Shark", "name": "Demon Shark",
"asset": preload("res://scenes/enemies/demon_shark.tscn"),
"health": 100, "health": 100,
"speed": 30, "speed": 30,
"damage": 1, "damage": 1,
"reward": 60 "reward": 60,
"spawn_rate": 0.05
} }
] ]

View file

@ -1,25 +1,47 @@
extends Path2D extends Path2D
@onready var timer = $Timer @onready var timer = $Timer
@export var type: int = 0 @export var type: int = 0
var config_path = "res://scripts/stages/6/config.gd" var config_path = "res://scripts/stages/6/config.gd"
var config = load(config_path).new() var config = load(config_path).new()
var spawn_first: bool = true var enemies = []
var probabilities = []
var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn")
func _ready(): func _ready():
summon() for enemy in config.enemy:
enemies.append(enemy['asset'])
probabilities.append(enemy['spawn_rate'])
summon_init()
timer.wait_time = config.game['spawn_timer'] timer.wait_time = config.game['spawn_timer']
timer.start() timer.start()
func weighted_random_choice(items, weights):
var total_weight = 0
for weight in weights:
total_weight += weight
var random_value = randf() * total_weight
for i in range(len(items)):
random_value -= weights[i]
if random_value <= 0:
return items[i]
func _on_timer_timeout(): func _on_timer_timeout():
summon() summon_init()
timer.start() timer.start()
func summon(): func summon_init():
for x in range(config.game['spawn_count']):
summon(weighted_random_choice(enemies, probabilities))
await get_tree().create_timer(config.game['spawn_delay']).timeout
func summon(enemy: PackedScene):
var rng = RandomNumberGenerator.new() var rng = RandomNumberGenerator.new()
var enemy_instance = enemy.instantiate() var enemy_instance = enemy.instantiate()