-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated functions on texture helper that were not static and added a …
…basic object pool to instantiate scenes
- Loading branch information
1 parent
f24104c
commit 245b658
Showing
4 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
@icon("res://components/behaviour/object_pool/object_pool.svg") | ||
class_name ObjectPool extends Node | ||
|
||
signal killed(spawned_object: Variant) | ||
|
||
@export var scene: PackedScene | ||
@export var create_objects_on_ready: bool = true | ||
@export var max_objects_in_pool: int = 100: | ||
set(value): | ||
if value != max_objects_in_pool: | ||
max_objects_in_pool = maxi(1, absi(value)) | ||
@export var process_mode_on_spawn: ProcessMode = Node.PROCESS_MODE_INHERIT | ||
|
||
var pool: Array[Variant] = [] | ||
var spawned: Array[Variant] = [] | ||
|
||
|
||
func _init( | ||
_scene: PackedScene, | ||
amount: int, | ||
create_on_ready: bool = true, | ||
_process_mode_on_spawn: ProcessMode = Node.PROCESS_MODE_INHERIT | ||
) -> void: | ||
scene = _scene | ||
max_objects_in_pool = amount | ||
create_objects_on_ready = create_on_ready | ||
process_mode_on_spawn = _process_mode_on_spawn | ||
|
||
|
||
func _ready() -> void: | ||
if create_objects_on_ready: | ||
create_pool(max_objects_in_pool) | ||
|
||
killed.connect(on_killed) | ||
|
||
|
||
func create_pool(amount: int) -> void: | ||
if scene == null: | ||
push_error("ObjectPool: The scene to spawn is not defined for this object pool") | ||
return | ||
|
||
amount = mini(amount, max_objects_in_pool - pool.size()) | ||
|
||
for i in amount: | ||
add_to_pool(scene.instantiate()) | ||
|
||
|
||
func add_to_pool(new_object: Variant) -> void: | ||
if pool.has(new_object): | ||
return | ||
|
||
new_object.process_mode = Node.PROCESS_MODE_DISABLED | ||
new_object.hide() | ||
pool.append(new_object) | ||
|
||
|
||
func kill(spawned_object) -> void: | ||
if spawned.has(spawned_object): | ||
spawned.erase(spawned_object) | ||
add_to_pool(spawned_object) | ||
|
||
|
||
func spawn() -> Variant: | ||
if pool.size() > 0: | ||
var pool_object: Variant = pool.pop_back() | ||
pool_object.process_mode = process_mode_on_spawn | ||
pool_object.show() | ||
spawned.append(pool_object) | ||
|
||
return pool_object | ||
|
||
return null | ||
|
||
|
||
func spawn_multiple(amount: int) -> Array[Variant]: | ||
amount = mini(amount, pool.size()) | ||
|
||
var spawned_objects: Array[Variant] = [] | ||
|
||
for i in amount: | ||
var spawned_object: Variant = spawn() | ||
|
||
if spawned_object == null: | ||
break | ||
|
||
spawned_objects.append(spawned_object) | ||
|
||
return spawned_objects | ||
|
||
|
||
func on_killed(spawned_object: Variant) -> void: | ||
kill(spawned_object) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
[remap] | ||
|
||
importer="texture" | ||
type="CompressedTexture2D" | ||
uid="uid://b4ukqec1un17f" | ||
path="res://.godot/imported/object_pool.svg-9cb4c9becd7d7a8ff4067b1122248dd8.ctex" | ||
metadata={ | ||
"vram_texture": false | ||
} | ||
|
||
[deps] | ||
|
||
source_file="res://components/behaviour/object_pool/object_pool.svg" | ||
dest_files=["res://.godot/imported/object_pool.svg-9cb4c9becd7d7a8ff4067b1122248dd8.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters