mirror of
https://github.com/Steffo99/hella-farm.git
synced 2024-11-21 15:44:23 +00:00
Create Sampler
s
This commit is contained in:
parent
69b08c09e7
commit
f1ca11d887
11 changed files with 81 additions and 125 deletions
11
behaviours/sampler.gd
Normal file
11
behaviours/sampler.gd
Normal file
|
@ -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
|
8
behaviours/sampler_priority.gd
Normal file
8
behaviours/sampler_priority.gd
Normal file
|
@ -0,0 +1,8 @@
|
|||
extends Sampler
|
||||
class_name SamplerPriority
|
||||
|
||||
|
||||
## Always sample the object with the highest priority in the queue.
|
||||
|
||||
|
||||
## TODO
|
6
behaviours/sampler_priority.tscn
Normal file
6
behaviours/sampler_priority.tscn
Normal file
|
@ -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")
|
14
behaviours/sampler_random.gd
Normal file
14
behaviours/sampler_random.gd
Normal file
|
@ -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)
|
6
behaviours/sampler_random.tscn
Normal file
6
behaviours/sampler_random.tscn
Normal file
|
@ -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")
|
30
behaviours/sampler_weighted.gd
Normal file
30
behaviours/sampler_weighted.gd
Normal file
|
@ -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
|
6
behaviours/sampler_weighted.tscn
Normal file
6
behaviours/sampler_weighted.tscn
Normal file
|
@ -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")
|
|
@ -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.")
|
|
@ -1,41 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<svg
|
||||
viewBox="0 0 640 640"
|
||||
version="1.1"
|
||||
id="svg1"
|
||||
sodipodi:docname="spawner_choice.svg"
|
||||
width="16"
|
||||
height="16"
|
||||
inkscape:version="1.3.2 (091e20ef0f, 2023-11-25, custom)"
|
||||
xml:space="preserve"
|
||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:svg="http://www.w3.org/2000/svg"><defs
|
||||
id="defs1" /><sodipodi:namedview
|
||||
id="namedview1"
|
||||
pagecolor="#ffffff"
|
||||
bordercolor="#000000"
|
||||
borderopacity="0.25"
|
||||
inkscape:showpageshadow="2"
|
||||
inkscape:pageopacity="0.0"
|
||||
inkscape:pagecheckerboard="0"
|
||||
inkscape:deskcolor="#d1d1d1"
|
||||
inkscape:zoom="26.34375"
|
||||
inkscape:cx="6.3582444"
|
||||
inkscape:cy="10.192171"
|
||||
inkscape:window-width="1920"
|
||||
inkscape:window-height="1020"
|
||||
inkscape:window-x="3200"
|
||||
inkscape:window-y="32"
|
||||
inkscape:window-maximized="1"
|
||||
inkscape:current-layer="svg1" /><!--! Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. --><path
|
||||
d="M 0,546.3 C 0,447.8 79.8,368 178.3,368 h 91.4 c 98.5,0 178.3,79.8 178.3,178.3 0,16.4 -13.3,29.7 -29.7,29.7 H 29.7 C 13.3,576 0,562.7 0,546.3 Z"
|
||||
id="path2"
|
||||
style="fill:#e0e0e0" /><path
|
||||
d="m 96,192 a 128,128 0 1 1 256,0 128,128 0 1 1 -256,0 z"
|
||||
id="path1"
|
||||
style="fill:#e0e0e0" /><path
|
||||
d="m 453.31589,247.44634 c 0,-26.67855 21.69051,-48.36908 48.36905,-48.36908 h 24.18458 c 26.67854,0 48.36905,21.69053 48.36905,48.36908 v 2.72078 c 0,16.47571 -8.389,31.81779 -22.21955,40.66023 l -31.89337,20.48129 c -19.0453,12.24343 -30.53295,33.32934 -30.53295,55.92674 v 1.13366 c 0,13.37704 10.80745,24.18458 24.18453,24.18458 13.37708,0 24.18453,-10.80754 24.18453,-24.18458 v -1.05808 c 0,-6.19727 3.17423,-11.94108 8.31346,-15.26646 l 31.89336,-20.48131 c 27.66105,-17.83607 44.43909,-48.44463 44.43909,-81.39607 v -2.72078 c 0,-53.43272 -43.30545,-96.73815 -96.73815,-96.73815 h -24.18458 c -53.4327,0 -96.73815,43.30543 -96.73815,96.73815 0,13.37708 10.80748,24.18454 24.18454,24.18454 13.37708,0 24.18456,-10.80746 24.18456,-24.18454 z m 60.46134,241.84541 a 30.230704,30.230704 0 1 0 0,-60.46127 30.230704,30.230704 0 1 0 0,60.46127 z"
|
||||
id="path1-7"
|
||||
style="fill:#e0e0e0;stroke-width:0.755768" /></svg>
|
Before Width: | Height: | Size: 2.5 KiB |
|
@ -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
|
|
@ -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")
|
Loading…
Reference in a new issue