Skip to content

Commit 7d13730

Browse files
committed
Add Flower decoration scene
This is a flavour of Decoration which chooses a random texture from a series of directories. The files in those directories are the flowers (and a mushroom) that are currently manually placed in Fray's End, plus a new flower drawn by a learner (who agrees to license it under CC-BY-SA-4.0, as does their adult (me)). The random selection is done based on the node's path in the scene, which means that the choice will remain the same between runs so long as the list remains the same. #556
1 parent 1aaaa50 commit 7d13730

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://dt4lx6310tcoy"
6+
path="res://.godot/imported/lcjt-flower.png-d6fabfbb70fcabe42e0f32acab468c79.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://scenes/game_elements/props/decoration/flower/assets/lcjt-flower.png"
14+
dest_files=["res://.godot/imported/lcjt-flower.png-d6fabfbb70fcabe42e0f32acab468c79.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SPDX-FileCopyrightText: The Threadbare Authors
2+
# SPDX-License-Identifier: MPL-2.0
3+
@tool
4+
class_name Flower
5+
extends Decoration
6+
## A decoration which deterministically picks its texture from a hardcoded list of directories.
7+
##
8+
## The goal here is to be able to add new flowers to a flowerbed in Fray's End by adding a file to
9+
## the repository, without touching any scene in a conflict-prone way.
10+
11+
const TEXTURE_DIRECTORIES := [
12+
"res://scenes/game_elements/props/decoration/crochenthemum/",
13+
"res://scenes/game_elements/props/decoration/mushroom/",
14+
"res://scenes/game_elements/props/decoration/flower/assets/",
15+
]
16+
static var _textures: Array[Texture2D]
17+
18+
19+
static func _load_textures_from(directory_path: String) -> void:
20+
var files := ResourceLoader.list_directory(directory_path)
21+
files.sort()
22+
for file: String in files:
23+
var path := directory_path.path_join(file)
24+
var resource := ResourceLoader.load(path)
25+
if resource is Texture2D:
26+
_textures.append(resource)
27+
else:
28+
push_warning("%s is not a Texture2D" % path)
29+
30+
31+
# Initialises the static _textures list, if not already done
32+
static func _initialise_textures() -> void:
33+
if _textures:
34+
return
35+
36+
for directory_path: String in TEXTURE_DIRECTORIES:
37+
_load_textures_from(directory_path)
38+
39+
40+
func _validate_property(property: Dictionary) -> void:
41+
if property.name == "texture":
42+
# Prevent modifying texture property in inspector: it would be re-set next time the scene
43+
# loads.
44+
property.usage |= PROPERTY_USAGE_READ_ONLY
45+
property.usage |= PROPERTY_USAGE_NO_INSTANCE_STATE
46+
47+
# Don't store the texture in the scene file: it would be overwritten when new flower images
48+
# are added.
49+
property.usage &= ~PROPERTY_USAGE_STORAGE
50+
51+
52+
func _init() -> void:
53+
_initialise_textures()
54+
55+
56+
func _enter_tree() -> void:
57+
# Assign a texture to this instance of the flower, in a fashion that is deterministic so long
58+
# as the set of flowers does not change.
59+
var tree := get_tree()
60+
var current_scene := tree.edited_scene_root if Engine.is_editor_hint() else tree.current_scene
61+
62+
# Don't save a texture in flower.tscn's Sprite2D
63+
if Engine.is_editor_hint() and self == current_scene:
64+
return
65+
66+
var path := current_scene.get_path_to(self)
67+
texture = _textures[path.hash() % _textures.size()]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://eenl0b7aoxuq
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://c0104ickpm3ru"]
2+
3+
[ext_resource type="Script" uid="uid://eenl0b7aoxuq" path="res://scenes/game_elements/props/decoration/flower/components/flower.gd" id="1_8t1d6"]
4+
5+
[node name="Flower" type="Node2D"]
6+
script = ExtResource("1_8t1d6")
7+
8+
[node name="Sprite2D" type="Sprite2D" parent="."]
9+
unique_name_in_owner = true

0 commit comments

Comments
 (0)