Explorar o código

:truck: Extract color logic from brick

- Move color computation to a dedicated utility
- Move color modulation into main sprite
DricomDragon %!s(int64=3) %!d(string=hai) anos
pai
achega
66c6eb76de

+ 3 - 18
godot/component/entity/brick/Brick.gd

@@ -4,6 +4,7 @@ extends StaticBody2D
 
 signal kace_broken
 signal kace_damaged
+signal life_changed
 
 
 var life: int
@@ -13,7 +14,7 @@ func setup(x: int, y: int, max_life: int):
 	position.x = x * 40
 	position.y = y * 20
 	life = randi() % max_life + 1
-	_refresh_look()
+	emit_signal("life_changed", life)
 
 
 func damage():
@@ -22,7 +23,7 @@ func damage():
 	if life < 1:
 		blow_up()
 	else:
-		_refresh_look()
+		emit_signal("life_changed", life)
 
 
 func blow_up():
@@ -32,19 +33,3 @@ func blow_up():
 
 func impact(pos: Vector2):
 	damage()
-
-
-static func color_from_life(n: int) -> Color:
-	var x = float(n % 256) / 255
-	if n < 256:
-		return Color(x, 1.0, 1.0 - x)
-	elif n < 512:
-		return Color(1.0, 1.0 - x, x)
-	elif n < 768:
-		return Color(1.0, 0.0, 1.0 - x)
-	else:
-		return Color(1.0, 0.0, 0.0)
-
-func _refresh_look() -> void:
-	modulate = color_from_life(life)
-	update() # For custom drawing

+ 5 - 1
godot/component/entity/brick/Brick.tscn

@@ -1,7 +1,8 @@
-[gd_scene load_steps=4 format=2]
+[gd_scene load_steps=5 format=2]
 
 [ext_resource path="res://component/entity/brick/brik.png" type="Texture" id=1]
 [ext_resource path="res://component/entity/brick/Brick.gd" type="Script" id=2]
+[ext_resource path="res://component/entity/brick/BrickMainSprite.gd" type="Script" id=3]
 
 [sub_resource type="RectangleShape2D" id=1]
 extents = Vector2( 20, 10 )
@@ -14,7 +15,10 @@ script = ExtResource( 2 )
 [node name="Sprite" type="Sprite" parent="."]
 texture = ExtResource( 1 )
 centered = false
+script = ExtResource( 3 )
 
 [node name="Shape" type="CollisionShape2D" parent="."]
 position = Vector2( 20, 10 )
 shape = SubResource( 1 )
+[connection signal="kace_damaged" from="." to="Sprite" method="_on_Brick_kace_damaged"]
+[connection signal="life_changed" from="." to="Sprite" method="_on_Brick_life_changed"]

+ 12 - 0
godot/component/entity/brick/BrickMainSprite.gd

@@ -0,0 +1,12 @@
+extends Sprite
+
+
+const ColorUtil = preload("res://utility/ColorUtil.gd")
+
+
+func _refresh_look(life: int) -> void:
+	modulate = ColorUtil.color_from_life(life)
+
+
+func _on_Brick_life_changed(life: int):
+	_refresh_look(life)

+ 6 - 0
godot/project.godot

@@ -24,6 +24,11 @@ _global_script_classes=[ {
 "language": "GDScript",
 "path": "res://component/entity/catcher/Catcher.gd"
 }, {
+"base": "Reference",
+"class": "ColorUtil",
+"language": "GDScript",
+"path": "res://utility/ColorUtil.gd"
+}, {
 "base": "Panel",
 "class": "MessagePanel",
 "language": "GDScript",
@@ -38,6 +43,7 @@ _global_script_class_icons={
 "Ball": "",
 "Brick": "",
 "Catcher": "",
+"ColorUtil": "",
 "MessagePanel": "",
 "NumberPanel": ""
 }

+ 13 - 0
godot/utility/ColorUtil.gd

@@ -0,0 +1,13 @@
+class_name ColorUtil
+
+
+static func color_from_life(n: int) -> Color:
+	var x = float(n % 256) / 255
+	if n < 256:
+		return Color(x, 1.0, 1.0 - x)
+	elif n < 512:
+		return Color(1.0, 1.0 - x, x)
+	elif n < 768:
+		return Color(1.0, 0.0, 1.0 - x)
+	else:
+		return Color(1.0, 0.0, 0.0)