Browse Source

:art: Colorize brick depending of life

DricomDragon 3 years ago
parent
commit
5cf1215392
2 changed files with 26 additions and 2 deletions
  1. 20 2
      godot/component/entity/brick/Brick.gd
  2. 6 0
      godot/project.godot

+ 20 - 2
godot/component/entity/brick/Brick.gd

@@ -1,3 +1,4 @@
+class_name Brick
 extends StaticBody2D
 extends StaticBody2D
 
 
 
 
@@ -12,15 +13,16 @@ func setup(x: int, y: int, max_life: int):
 	position.x = x * 40
 	position.x = x * 40
 	position.y = y * 20
 	position.y = y * 20
 	life = randi() % max_life + 1
 	life = randi() % max_life + 1
-	update() # For custom drawing
+	_refresh_look()
 
 
 
 
 func damage():
 func damage():
 	life = life - 1
 	life = life - 1
 	emit_signal("kace_damaged")
 	emit_signal("kace_damaged")
-	update() # For custom drawing
 	if life < 1:
 	if life < 1:
 		blow_up()
 		blow_up()
+	else:
+		_refresh_look()
 
 
 
 
 func blow_up():
 func blow_up():
@@ -30,3 +32,19 @@ func blow_up():
 
 
 func impact(pos: Vector2):
 func impact(pos: Vector2):
 	damage()
 	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

+ 6 - 0
godot/project.godot

@@ -14,6 +14,11 @@ _global_script_classes=[ {
 "language": "GDScript",
 "language": "GDScript",
 "path": "res://component/entity/ball/Ball.gd"
 "path": "res://component/entity/ball/Ball.gd"
 }, {
 }, {
+"base": "StaticBody2D",
+"class": "Brick",
+"language": "GDScript",
+"path": "res://component/entity/brick/Brick.gd"
+}, {
 "base": "Area2D",
 "base": "Area2D",
 "class": "Catcher",
 "class": "Catcher",
 "language": "GDScript",
 "language": "GDScript",
@@ -31,6 +36,7 @@ _global_script_classes=[ {
 } ]
 } ]
 _global_script_class_icons={
 _global_script_class_icons={
 "Ball": "",
 "Ball": "",
+"Brick": "",
 "Catcher": "",
 "Catcher": "",
 "MessagePanel": "",
 "MessagePanel": "",
 "NumberPanel": ""
 "NumberPanel": ""