Add basic illumination features

This commit is contained in:
Steffo 2024-09-22 23:35:50 +02:00
parent 79b8c1059e
commit be049a07c2
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
14 changed files with 245 additions and 5 deletions

View file

@ -0,0 +1,14 @@
extends Illuminable
class_name BooleanIlluminable
## Whether the parent object has been hit by an [Illuminator].
var is_lit := false
func light(illuminator: Illuminator) -> void:
if not is_lit:
is_lit = true
super.light(illuminator)
else:
Log.trace(self, "Refusing to light up, already lit")

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://w26hyy8v5u3i"]
[ext_resource type="Script" path="res://behaviours/illuminable/boolean_illuminable.gd" id="1_tow2r"]
[node name="BooleanIlluminable" type="Node"]
script = ExtResource("1_tow2r")

View file

@ -0,0 +1,12 @@
@icon("./illuminable.svg")
extends Node
class_name Illuminable
## Emitted when the illuminable gets lit.
signal lit
func light(illuminator: Illuminator) -> void:
Log.trace(self, "Lit up by: %s" % illuminator)
lit.emit()

View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
viewBox="0 0 16 16"
version="1.1"
id="svg1"
sodipodi:docname="illuminable.svg"
inkscape:version="1.3.2 (091e20ef0f, 2023-11-25, custom)"
width="16"
height="16"
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="#505050"
bordercolor="#ffffff"
borderopacity="1"
inkscape:showpageshadow="0"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="1"
inkscape:deskcolor="#505050"
inkscape:zoom="1.5644531"
inkscape:cx="192.0799"
inkscape:cy="256"
inkscape:window-width="1920"
inkscape:window-height="1020"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="svg1" />
<!--! Font Awesome Pro 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2024 Fonticons, Inc. -->
<path
d="m 10.5,12 c 0.3,-0.996875 0.921875,-1.846875 1.5375,-2.69375 v 0 C 12.2,9.084375 12.3625,8.8625 12.51875,8.6375 13.1375,7.746875 13.5,6.66875 13.5,5.503125 13.5,2.4625 11.0375,0 8,0 4.9625,0 2.5,2.4625 2.5,5.5 2.5,6.665625 2.8625,7.746875 3.48125,8.634375 3.6375,8.859375 3.8,9.08125 3.9625,9.303125 v 0 c 0.61875,0.846875 1.240625,1.7 1.5375,2.69375 h 5 z M 8,16 c 1.38125,0 2.5,-1.11875 2.5,-2.5 V 13 h -5 v 0.5 C 5.5,14.88125 6.61875,16 8,16 Z M 5.5,5.5 C 5.5,5.775 5.275,6 5,6 4.725,6 4.5,5.775 4.5,5.5 4.5,3.565625 6.065625,2 8,2 8.275,2 8.5,2.225 8.5,2.5 8.5,2.775 8.275,3 8,3 6.61875,3 5.5,4.11875 5.5,5.5 Z"
id="path1"
style="fill:#e0e0e0;stroke-width:0.03125" />
</svg>

After

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -0,0 +1,37 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://dvkvtq0u3c2mt"
path="res://.godot/imported/illuminable.svg-cf5c5af44471ca08e093ec52ae78a1d1.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://behaviours/illuminable/illuminable.svg"
dest_files=["res://.godot/imported/illuminable.svg-cf5c5af44471ca08e093ec52ae78a1d1.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

View file

@ -0,0 +1,25 @@
@icon("./illuminator.svg")
extends Node
class_name Illuminator
## Tags an object as able to trigger [Illuminable] entities.
func _ready() -> void:
var parent: Node = get_parent()
if not parent is RigidBody2D:
Log.warning(self, "Not a parent of a RigidBody2D, can't connect.")
return
var rigidbody: RigidBody2D = parent
rigidbody.body_entered.connect(_parent_body_entered)
Log.trace(self, "Connected _parent_body_entered to: %s" % parent)
func _parent_body_entered(body: Node) -> void:
if body:
var illuminables: Array[Node] = body.find_children("*", "Illuminable")
if len(illuminables) == 0:
Log.trace(self, "No illuminables found on: %s" % body)
for illuminable in illuminables:
assert(illuminable is Illuminable, "Illuminable is not an illuminable.")
illuminable.light(self)

View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
viewBox="0 0 16 16"
version="1.1"
id="svg1"
sodipodi:docname="illuminator.svg"
width="16"
height="16"
inkscape:version="1.3.2 (091e20ef0f, 2023-11-25, custom)"
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="#505050"
bordercolor="#ffffff"
borderopacity="1"
inkscape:showpageshadow="0"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="1"
inkscape:deskcolor="#505050"
inkscape:zoom="17.699767"
inkscape:cx="12.853277"
inkscape:cy="21.949442"
inkscape:window-width="1920"
inkscape:window-height="1020"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="svg1" />
<!--! Font Awesome Pro 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2024 Fonticons, Inc. -->
<path
d="M 2.0011715,7.9984378 2.8916226,0.87482913 C 2.9541104,0.37492677 3.3790274,0 3.8851785,0 h 5.2677212 c 0.4686584,0 0.8467096,0.37805116 0.8467096,0.84670963 0,0.0999805 -0.018746,0.20308537 -0.053115,0.29681707 L 8.4999022,4.9990236 h 4.3522758 c 0.631126,0 1.146651,0.5124 1.146651,1.1466511 0,0.2312048 -0.06874,0.4561609 -0.199961,0.6467487 L 7.7937901,15.571959 C 7.6094511,15.840656 7.3063853,16 6.9845732,16 H 6.8939662 C 6.403437,16 6.0035151,15.600078 6.0035151,15.109549 c 0,-0.07186 0.00937,-0.143722 0.028119,-0.215583 L 7.5000975,8.9982425 H 3.0009762 c -0.553017,0 -0.9998047,-0.4467877 -0.9998047,-0.9998047 z"
id="path1"
style="stroke-width:0.0312439;fill:#e0e0e0" />
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

@ -0,0 +1,37 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cq815vk3hba0t"
path="res://.godot/imported/illuminator.svg-bf1c9f706bf7f8e28cfbc53ed5bdba6c.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://behaviours/illuminator/illuminator.svg"
dest_files=["res://.godot/imported/illuminator.svg-bf1c9f706bf7f8e28cfbc53ed5bdba6c.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

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://dsdi475dymkh7"]
[ext_resource type="Script" path="res://behaviours/illuminator/illuminator.gd" id="1_inei7"]
[node name="Illuminator" type="Node"]
script = ExtResource("1_inei7")

View file

@ -1,4 +1,6 @@
[gd_scene load_steps=3 format=3 uid="uid://ylvptuj0xnm8"] [gd_scene load_steps=4 format=3 uid="uid://ylvptuj0xnm8"]
[ext_resource type="PackedScene" uid="uid://dsdi475dymkh7" path="res://behaviours/illuminator/illuminator.tscn" id="1_c8jcw"]
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_1mhds"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_1mhds"]
bounce = 0.92 bounce = 0.92
@ -8,7 +10,12 @@ radius = 24.0
[node name="Ball" type="RigidBody2D"] [node name="Ball" type="RigidBody2D"]
physics_material_override = SubResource("PhysicsMaterial_1mhds") physics_material_override = SubResource("PhysicsMaterial_1mhds")
continuous_cd = 2
contact_monitor = true
max_contacts_reported = 4
[node name="Shape" type="CollisionShape2D" parent="."] [node name="Shape" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_xd6lq") shape = SubResource("CircleShape2D_xd6lq")
debug_color = Color(0.7, 0, 0, 0.419608) debug_color = Color(0.7, 0, 0, 0.419608)
[node name="Illuminator" parent="." instance=ExtResource("1_c8jcw")]

6
prefabs/peg.gd Normal file
View file

@ -0,0 +1,6 @@
extends StaticBody2D
class_name Peg
func _on_lit() -> void:
$"Shape".debug_color = Color(0, 0.69, 0, 0.41)

View file

@ -1,10 +1,18 @@
[gd_scene load_steps=2 format=3 uid="uid://bbtm3534mdeat"] [gd_scene load_steps=4 format=3 uid="uid://bbtm3534mdeat"]
[ext_resource type="PackedScene" uid="uid://w26hyy8v5u3i" path="res://behaviours/illuminable/boolean_illuminable.tscn" id="1_lpcqw"]
[ext_resource type="Script" path="res://prefabs/peg.gd" id="1_xl7sk"]
[sub_resource type="CircleShape2D" id="CircleShape2D_cj4kg"] [sub_resource type="CircleShape2D" id="CircleShape2D_cj4kg"]
radius = 24.0 radius = 24.0
[node name="Peg" type="StaticBody2D"] [node name="Peg" type="StaticBody2D"]
script = ExtResource("1_xl7sk")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="Shape" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_cj4kg") shape = SubResource("CircleShape2D_cj4kg")
debug_color = Color(0.7, 0.7, 0.7, 0.419608) debug_color = Color(0.7, 0.7, 0.7, 0.419608)
[node name="BooleanIlluminable" parent="." instance=ExtResource("1_lpcqw")]
[connection signal="lit" from="BooleanIlluminable" to="." method="_on_lit"]

View file

@ -6,7 +6,7 @@
[node name="Root" type="Node2D"] [node name="Root" type="Node2D"]
[node name="Ball" parent="." instance=ExtResource("1_d2cjj")] [node name="Ball" parent="." instance=ExtResource("1_d2cjj")]
position = Vector2(42, -281) position = Vector2(-44, -269)
[node name="Peg" parent="." instance=ExtResource("2_i427g")] [node name="Peg" parent="." instance=ExtResource("2_i427g")]
position = Vector2(77, -102) position = Vector2(77, -102)

View file

@ -34,6 +34,10 @@ func error(obj: Node, message: String) -> void:
func _log(level: int, obj: Node, message: String) -> void: func _log(level: int, obj: Node, message: String) -> void:
var color = _get_color(level) var color = _get_color(level)
var max_level = _get_max_level(obj) var max_level = _get_max_level(obj)
if level <= 10:
push_error("<%s> %s" % [obj, message])
elif level <= 20:
push_warning("<%s> %s" % [obj, message])
if level <= max_level: if level <= max_level:
print_rich("[color=%s]<%s> %s[/color]" % [color, obj, message]) print_rich("[color=%s]<%s> %s[/color]" % [color, obj, message])