From f1ca11d8872130773c7573270f05da81cf1806cc Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 16 Apr 2024 05:08:25 +0200 Subject: [PATCH] Create `Sampler`s --- behaviours/sampler.gd | 11 ++++++++ behaviours/sampler_priority.gd | 8 ++++++ behaviours/sampler_priority.tscn | 6 ++++ behaviours/sampler_random.gd | 14 ++++++++++ behaviours/sampler_random.tscn | 6 ++++ behaviours/sampler_weighted.gd | 30 ++++++++++++++++++++ behaviours/sampler_weighted.tscn | 6 ++++ behaviours/spawner_choice.gd | 41 ---------------------------- behaviours/spawner_choice.svg | 41 ---------------------------- behaviours/spawner_choice.svg.import | 37 ------------------------- behaviours/spawner_choice.tscn | 6 ---- 11 files changed, 81 insertions(+), 125 deletions(-) create mode 100644 behaviours/sampler.gd create mode 100644 behaviours/sampler_priority.gd create mode 100644 behaviours/sampler_priority.tscn create mode 100644 behaviours/sampler_random.gd create mode 100644 behaviours/sampler_random.tscn create mode 100644 behaviours/sampler_weighted.gd create mode 100644 behaviours/sampler_weighted.tscn delete mode 100644 behaviours/spawner_choice.gd delete mode 100644 behaviours/spawner_choice.svg delete mode 100644 behaviours/spawner_choice.svg.import delete mode 100644 behaviours/spawner_choice.tscn diff --git a/behaviours/sampler.gd b/behaviours/sampler.gd new file mode 100644 index 0000000..1b208a5 --- /dev/null +++ b/behaviours/sampler.gd @@ -0,0 +1,11 @@ +extends Node +class_name Sampler + + +## Abstract base class for sampling a certain reference among multiple. + + +## Get a reference. +func sample() -> Object: + Log.e(self, "Not implemented.") + return null diff --git a/behaviours/sampler_priority.gd b/behaviours/sampler_priority.gd new file mode 100644 index 0000000..4aafaf5 --- /dev/null +++ b/behaviours/sampler_priority.gd @@ -0,0 +1,8 @@ +extends Sampler +class_name SamplerPriority + + +## Always sample the object with the highest priority in the queue. + + +## TODO \ No newline at end of file diff --git a/behaviours/sampler_priority.tscn b/behaviours/sampler_priority.tscn new file mode 100644 index 0000000..6865ca7 --- /dev/null +++ b/behaviours/sampler_priority.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://djcwis8ycrq85"] + +[ext_resource type="Script" path="res://behaviours/sampler_priority.gd" id="1_jy5r5"] + +[node name="SamplerPriority" type="Node"] +script = ExtResource("1_jy5r5") diff --git a/behaviours/sampler_random.gd b/behaviours/sampler_random.gd new file mode 100644 index 0000000..5380d3e --- /dev/null +++ b/behaviours/sampler_random.gd @@ -0,0 +1,14 @@ +extends Sampler +class_name SamplerRandom + + +## Sample a random reference from the array. + + +@export var possibilities: Array = [] + + +func sample() -> Object: + if len(possibilities) == 0: + return null + return Random.sample(possibilities) diff --git a/behaviours/sampler_random.tscn b/behaviours/sampler_random.tscn new file mode 100644 index 0000000..5b1695f --- /dev/null +++ b/behaviours/sampler_random.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://dtiuw54ss376d"] + +[ext_resource type="Script" path="res://behaviours/sampler_random.gd" id="1_geacn"] + +[node name="SamplerRandom" type="Node"] +script = ExtResource("1_geacn") diff --git a/behaviours/sampler_weighted.gd b/behaviours/sampler_weighted.gd new file mode 100644 index 0000000..cc81bc0 --- /dev/null +++ b/behaviours/sampler_weighted.gd @@ -0,0 +1,30 @@ +extends Sampler +class_name SamplerWeighted + + +## Sample a random reference from the array, considering the given weights. + + +@export var possibilities: Array = [] +@export var weights: Array[int] = [] + + +func sample() -> Object: + var total = compute_total_weight() + if total == 0: + return null + var sampled = Random.rng.randi_range(0, total - 1) + var idx = -1 + while sampled >= 0: + idx += 1 + sampled -= weights[idx] + return possibilities[idx] + +func compute_total_weight() -> int: + var total = 0 + for weight in weights: + if weight < 0: + Log.e(self, "Weight with negative value detected.") + return 0 + total += weight + return total diff --git a/behaviours/sampler_weighted.tscn b/behaviours/sampler_weighted.tscn new file mode 100644 index 0000000..fddbae7 --- /dev/null +++ b/behaviours/sampler_weighted.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://d2ftjun7wdif0"] + +[ext_resource type="Script" path="res://behaviours/sampler_weighted.gd" id="1_hhnbk"] + +[node name="SamplerWeighted" type="Node"] +script = ExtResource("1_hhnbk") diff --git a/behaviours/spawner_choice.gd b/behaviours/spawner_choice.gd deleted file mode 100644 index 8af2a02..0000000 --- a/behaviours/spawner_choice.gd +++ /dev/null @@ -1,41 +0,0 @@ -@icon("res://behaviours/spawner_choice.svg") -extends Node -class_name SpawnerChoice - -## Randomly choose a [Spawner] to use from [field spawners] considering [field weights]. -## -## If a null [Spawner] is selected, nothing is spawned. - - -@export var spawners: Array[Spawner] = [] -@export var weights: Array[int] = [] - - -func compute_total_weight() -> int: - var total = 0 - for weight in weights: - if weight < 0: - Log.e(self, "Weight with negative value detected.") - total += weight - return total - -func select_spawner() -> Spawner: - var total = compute_total_weight() - var sampled = Random.rng.randi_range(0, total - 1) - var idx = -1 - while sampled >= 0: - idx += 1 - sampled -= weights[idx] - return spawners[idx] - -func spawn(): - var spawner = select_spawner() - if spawner != null: - spawner.spawn() - - -func _ready(): - if len(spawners) != len(weights): - Log.e(self, "Spawners and weights are different lengths.") - if compute_total_weight() <= 0: - Log.e(self, "Weight total is less or equal to 0.") diff --git a/behaviours/spawner_choice.svg b/behaviours/spawner_choice.svg deleted file mode 100644 index 6665d20..0000000 --- a/behaviours/spawner_choice.svg +++ /dev/null @@ -1,41 +0,0 @@ - - diff --git a/behaviours/spawner_choice.svg.import b/behaviours/spawner_choice.svg.import deleted file mode 100644 index ad1beae..0000000 --- a/behaviours/spawner_choice.svg.import +++ /dev/null @@ -1,37 +0,0 @@ -[remap] - -importer="texture" -type="CompressedTexture2D" -uid="uid://c0f5nbe7lhib6" -path="res://.godot/imported/spawner_choice.svg-91bce9cbbb63e3eb7738ffe03834ed28.ctex" -metadata={ -"vram_texture": false -} - -[deps] - -source_file="res://behaviours/spawner_choice.svg" -dest_files=["res://.godot/imported/spawner_choice.svg-91bce9cbbb63e3eb7738ffe03834ed28.ctex"] - -[params] - -compress/mode=0 -compress/high_quality=false -compress/lossy_quality=0.7 -compress/hdr_compression=1 -compress/normal_map=0 -compress/channel_pack=0 -mipmaps/generate=false -mipmaps/limit=-1 -roughness/mode=0 -roughness/src_normal="" -process/fix_alpha_border=true -process/premult_alpha=false -process/normal_map_invert_y=false -process/hdr_as_srgb=false -process/hdr_clamp_exposure=false -process/size_limit=0 -detect_3d/compress_to=1 -svg/scale=1.0 -editor/scale_with_editor_scale=false -editor/convert_colors_with_editor_theme=false diff --git a/behaviours/spawner_choice.tscn b/behaviours/spawner_choice.tscn deleted file mode 100644 index b11a7b0..0000000 --- a/behaviours/spawner_choice.tscn +++ /dev/null @@ -1,6 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://cokdfsk6rgeup"] - -[ext_resource type="Script" path="res://behaviours/spawner_choice.gd" id="1_jkwdb"] - -[node name="SpawnerChoice" type="Node"] -script = ExtResource("1_jkwdb")