From 275e11722ca4aec1ed3fbedac3508670b3e833c7 Mon Sep 17 00:00:00 2001 From: Moe Poi ~ Date: Fri, 20 Oct 2023 14:52:51 +0700 Subject: [PATCH] Rewrite Enemy Spawn Script --- scripts/stages/1/config.gd | 43 ++++++-------------------------------- scripts/stages/1/summon.gd | 34 ++++++++++++++++++++++++------ scripts/stages/2/config.gd | 40 ++++++++--------------------------- scripts/stages/2/summon.gd | 34 ++++++++++++++++++++++++------ scripts/stages/3/config.gd | 37 +++++++++++--------------------- scripts/stages/3/summon.gd | 34 ++++++++++++++++++++++++------ scripts/stages/4/config.gd | 34 +++++++++++++----------------- scripts/stages/4/summon.gd | 34 ++++++++++++++++++++++++------ scripts/stages/5/config.gd | 31 +++++++++++++++------------ scripts/stages/5/summon.gd | 34 ++++++++++++++++++++++++------ scripts/stages/6/config.gd | 28 ++++++++++++++++++------- scripts/stages/6/summon.gd | 34 ++++++++++++++++++++++++------ 12 files changed, 249 insertions(+), 168 deletions(-) diff --git a/scripts/stages/1/config.gd b/scripts/stages/1/config.gd index df932af..0ede201 100644 --- a/scripts/stages/1/config.gd +++ b/scripts/stages/1/config.gd @@ -5,7 +5,9 @@ var game = { "duration": 300, "health": 6, "resource": 100, - "spawn_timer": 3.0, + "spawn_timer": 10.0, + "spawn_count": 3, + "spawn_delay": 2, "offset": [-30.0, 30.0], "path_direction": [ [ @@ -131,44 +133,11 @@ var tower = [ var enemy = [ { "name": "Demon", + "asset": preload("res://scenes/enemies/demon.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 10 - }, - { - "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 + "reward": 10, + "spawn_rate": 1 } ] diff --git a/scripts/stages/1/summon.gd b/scripts/stages/1/summon.gd index f801f86..2051b2f 100644 --- a/scripts/stages/1/summon.gd +++ b/scripts/stages/1/summon.gd @@ -1,25 +1,47 @@ extends Path2D + @onready var timer = $Timer @export var type: int = 0 var config_path = "res://scripts/stages/1/config.gd" var config = load(config_path).new() -var spawn_first: bool = true - -var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn") +var enemies = [] +var probabilities = [] 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.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(): - summon() + summon_init() 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 enemy_instance = enemy.instantiate() diff --git a/scripts/stages/2/config.gd b/scripts/stages/2/config.gd index e9bb7ea..0b875eb 100644 --- a/scripts/stages/2/config.gd +++ b/scripts/stages/2/config.gd @@ -5,7 +5,9 @@ var game = { "duration": 300, "health": 6, "resource": 100, - "spawn_timer": 3.0, + "spawn_timer": 10.0, + "spawn_count": 2, + "spawn_delay": 2, "offset": [-40.0, 35.0], "path_direction": [ [ @@ -161,44 +163,20 @@ var tower = [ var enemy = [ { "name": "Demon", + "asset": preload("res://scenes/enemies/demon.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 10 + "reward": 10, + "spawn_rate": 0.80 }, { "name": "Orc", + "asset": preload("res://scenes/enemies/orc.tscn"), "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 + "reward": 20, + "spawn_rate": 0.20 } ] diff --git a/scripts/stages/2/summon.gd b/scripts/stages/2/summon.gd index 9300808..31584e1 100644 --- a/scripts/stages/2/summon.gd +++ b/scripts/stages/2/summon.gd @@ -1,25 +1,47 @@ extends Path2D + @onready var timer = $Timer @export var type: int = 0 var config_path = "res://scripts/stages/2/config.gd" var config = load(config_path).new() -var spawn_first: bool = true - -var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn") +var enemies = [] +var probabilities = [] 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.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(): - summon() + summon_init() 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 enemy_instance = enemy.instantiate() diff --git a/scripts/stages/3/config.gd b/scripts/stages/3/config.gd index a117967..acb7738 100644 --- a/scripts/stages/3/config.gd +++ b/scripts/stages/3/config.gd @@ -5,7 +5,9 @@ var game = { "duration": 300, "health": 6, "resource": 100, - "spawn_timer": 3.0, + "spawn_timer": 10.0, + "spawn_count": 3, + "spawn_delay": 2, "offset": [-40.0, 50.0], "path_direction": [ [ @@ -143,44 +145,29 @@ var tower = [ var enemy = [ { "name": "Demon", + "asset": preload("res://scenes/enemies/demon.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 10 + "reward": 10, + "spawn_rate": 0.60 }, { "name": "Orc", + "asset": preload("res://scenes/enemies/orc.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 20 + "reward": 20, + "spawn_rate": 0.30 }, { "name": "Cyclops", + "asset": preload("res://scenes/enemies/cyclops.tscn"), "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 + "reward": 30, + "spawn_rate": 0.10 } ] diff --git a/scripts/stages/3/summon.gd b/scripts/stages/3/summon.gd index 8722d17..84222d5 100644 --- a/scripts/stages/3/summon.gd +++ b/scripts/stages/3/summon.gd @@ -1,25 +1,47 @@ extends Path2D + @onready var timer = $Timer @export var type: int = 0 var config_path = "res://scripts/stages/3/config.gd" var config = load(config_path).new() -var spawn_first: bool = true - -var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn") +var enemies = [] +var probabilities = [] 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.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(): - summon() + summon_init() 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 enemy_instance = enemy.instantiate() diff --git a/scripts/stages/4/config.gd b/scripts/stages/4/config.gd index 5f99bf9..dea37f7 100644 --- a/scripts/stages/4/config.gd +++ b/scripts/stages/4/config.gd @@ -5,7 +5,9 @@ var game = { "duration": 300, "health": 6, "resource": 100, - "spawn_timer": 3.0, + "spawn_timer": 10.0, + "spawn_count": 2, + "spawn_delay": 2, "offset": [-20.0, 13.0], "path_direction": [ [ @@ -149,44 +151,38 @@ var tower = [ var enemy = [ { "name": "Demon", + "asset": preload("res://scenes/enemies/demon.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 10 + "reward": 10, + "spawn_rate": 0.45 }, { "name": "Orc", + "asset": preload("res://scenes/enemies/orc.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 20 + "reward": 20, + "spawn_rate": 0.35 }, { "name": "Cyclops", + "asset": preload("res://scenes/enemies/cyclops.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 30 + "reward": 30, + "spawn_rate": 0.15 }, { "name": "Demon Boar", + "asset": preload("res://scenes/enemies/demon_boar.tscn"), "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 + "reward": 40, + "spawn_rate": 0.05 } ] diff --git a/scripts/stages/4/summon.gd b/scripts/stages/4/summon.gd index 3dfcc40..49c1443 100644 --- a/scripts/stages/4/summon.gd +++ b/scripts/stages/4/summon.gd @@ -1,25 +1,47 @@ extends Path2D + @onready var timer = $Timer @export var type: int = 0 var config_path = "res://scripts/stages/4/config.gd" var config = load(config_path).new() -var spawn_first: bool = true - -var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn") +var enemies = [] +var probabilities = [] 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.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(): - summon() + summon_init() 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 enemy_instance = enemy.instantiate() diff --git a/scripts/stages/5/config.gd b/scripts/stages/5/config.gd index 9856492..088d15f 100644 --- a/scripts/stages/5/config.gd +++ b/scripts/stages/5/config.gd @@ -5,7 +5,9 @@ var game = { "duration": 300, "health": 6, "resource": 100, - "spawn_timer": 3.0, + "spawn_timer": 10.0, + "spawn_count": 3, + "spawn_delay": 2, "offset": [-20.0, 25.0], "path_direction": [ [ @@ -149,44 +151,47 @@ var tower = [ var enemy = [ { "name": "Demon", + "asset": preload("res://scenes/enemies/demon.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 10 + "reward": 10, + "spawn_rate": 0.30 }, { "name": "Orc", + "asset": preload("res://scenes/enemies/orc.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 20 + "reward": 20, + "spawn_rate": 0.25 }, { "name": "Cyclops", + "asset": preload("res://scenes/enemies/cyclops.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 30 + "reward": 30, + "spawn_rate": 0.20 }, { "name": "Demon Boar", + "asset": preload("res://scenes/enemies/demon_boar.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 40 + "reward": 40, + "spawn_rate": 0.15 }, { "name": "Demon Fungus", + "asset": preload("res://scenes/enemies/demon_fungus.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 50 - }, - { - "name": "Demon Shark", - "health": 100, - "speed": 30, - "damage": 1, - "reward": 60 + "reward": 50, + "spawn_rate": 0.10 } ] diff --git a/scripts/stages/5/summon.gd b/scripts/stages/5/summon.gd index 425f16f..0b61d1b 100644 --- a/scripts/stages/5/summon.gd +++ b/scripts/stages/5/summon.gd @@ -1,25 +1,47 @@ extends Path2D + @onready var timer = $Timer @export var type: int = 0 var config_path = "res://scripts/stages/5/config.gd" var config = load(config_path).new() -var spawn_first: bool = true - -var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn") +var enemies = [] +var probabilities = [] 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.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(): - summon() + summon_init() 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 enemy_instance = enemy.instantiate() diff --git a/scripts/stages/6/config.gd b/scripts/stages/6/config.gd index 2040d02..91bb3ab 100644 --- a/scripts/stages/6/config.gd +++ b/scripts/stages/6/config.gd @@ -5,7 +5,9 @@ var game = { "duration": 300, "health": 6, "resource": 100, - "spawn_timer": 3.0, + "spawn_timer": 10.0, + "spawn_count": 3, + "spawn_delay": 2, "offset": [-20.0, 20.0], "path_direction": [ [ @@ -191,44 +193,56 @@ var tower = [ var enemy = [ { "name": "Demon", + "asset": preload("res://scenes/enemies/demon.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 10 + "reward": 10, + "spawn_rate": 0.15 }, { "name": "Orc", + "asset": preload("res://scenes/enemies/orc.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 20 + "reward": 20, + "spawn_rate": 0.20 }, { "name": "Cyclops", + "asset": preload("res://scenes/enemies/cyclops.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 30 + "reward": 30, + "spawn_rate": 0.25 }, { "name": "Demon Boar", + "asset": preload("res://scenes/enemies/demon_boar.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 40 + "reward": 40, + "spawn_rate": 0.20 }, { "name": "Demon Fungus", + "asset": preload("res://scenes/enemies/demon_fungus.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 50 + "reward": 50, + "spawn_rate": 0.15 }, { "name": "Demon Shark", + "asset": preload("res://scenes/enemies/demon_shark.tscn"), "health": 100, "speed": 30, "damage": 1, - "reward": 60 + "reward": 60, + "spawn_rate": 0.05 } ] diff --git a/scripts/stages/6/summon.gd b/scripts/stages/6/summon.gd index a61025c..780f5ed 100644 --- a/scripts/stages/6/summon.gd +++ b/scripts/stages/6/summon.gd @@ -1,25 +1,47 @@ extends Path2D + @onready var timer = $Timer @export var type: int = 0 var config_path = "res://scripts/stages/6/config.gd" var config = load(config_path).new() -var spawn_first: bool = true - -var enemy: PackedScene = preload("res://scenes/enemies/demon.tscn") +var enemies = [] +var probabilities = [] 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.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(): - summon() + summon_init() 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 enemy_instance = enemy.instantiate()