Browse Source

Replace tile map bricks by dedicated entities

DricomDragon 3 years ago
parent
commit
6bdcfd847b

+ 27 - 0
godot/component/entity/brick/Brick.gd

@@ -0,0 +1,27 @@
+extends StaticBody2D
+
+
+signal kace_broken
+signal kace_damaged
+
+
+var life: int
+
+
+func setup(x: int, y: int, max_life: int):
+	position.x = x * 40
+	position.y = y * 20
+	life = randi() % max_life + 1
+	update() # For custom drawing
+
+
+func damage():
+	life = life - 1
+	emit_signal("kace_damaged")
+	update() # For custom drawing
+	if life < 1:
+		blow_up()
+
+
+func blow_up():
+	emit_signal("kace_broken")

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

@@ -1,11 +1,15 @@
-[gd_scene load_steps=3 format=2]
+[gd_scene load_steps=4 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]
 
 [sub_resource type="RectangleShape2D" id=1]
 extents = Vector2( 20, 10 )
 
 [node name="Brick" type="StaticBody2D"]
+collision_layer = 4
+collision_mask = 9
+script = ExtResource( 2 )
 
 [node name="Sprite" type="Sprite" parent="."]
 texture = ExtResource( 1 )

+ 20 - 27
godot/component/entity/brick/Grid.gd

@@ -1,12 +1,9 @@
-extends TileMap
+extends Node2D
 
 signal kace_broken
 signal kace_damaged
 signal has_moved
 
-enum Tile {
-	BRICK = 0,
-}
 
 enum State {
 	DONE,
@@ -14,10 +11,14 @@ enum State {
 }
 
 
+const Brick = preload("res://component/entity/brick/Brick.tscn")
+
+const BRICK_SIZE = 20
+
+
 export (float) var moving_time = 1.0
 
 
-onready var life_container = $LifeContainer
 onready var tween = $MoveDown
 
 
@@ -43,27 +44,17 @@ func generate_cell(x, y):
 
 
 func build_brick(x, y):
-	set_cell(x, y, Tile.BRICK)
-	life_container.spawn_brick(x, y, current_row * current_row)
-
-
-func damage_cell(tile_pos : Vector2):
-	var cell_id = get_cellv(tile_pos)
-	if cell_id != TileMap.INVALID_CELL and cell_id == Tile.BRICK:
-		emit_signal("kace_damaged")
-		if life_container.may_die_by_taking_this(tile_pos, 1):
-			break_cell(tile_pos)
-
-
-func break_cell(tile_pos):
-	set_cellv(tile_pos, -1)
-	emit_signal("kace_broken")
+	var brick = Brick.instance()
+	brick.setup(x, y, current_row * current_row)
+	brick.connect("kace_broken", self, "_on_Brick_kace_broken")
+	brick.connect("kace_damaged", self, "_on_Brick_kace_damaged")
+	add_child(brick)
 
 
 func start_moving():
 	current_row += 1
 	tween.interpolate_property(self, "position", position,
-		position + Vector2(0, cell_size.y), moving_time,
+		position + Vector2(0, BRICK_SIZE), moving_time,
 		Tween.TRANS_QUART, Tween.EASE_IN_OUT)
 	tween.start()
 
@@ -78,15 +69,17 @@ func _ready():
 	generate_and_move()
 
 
-func _on_Ball_kace_contact(contact_pos:Vector2):
-	var local_position = to_local(contact_pos)
-	var tile_pos = world_to_map(local_position)
-	damage_cell(tile_pos)
-
-
 func _on_Launcher_is_full():
 	generate_and_move()
 
 
 func _on_MoveDown_tween_all_completed():
 	finish_moving()
+
+
+func _on_Brick_kace_broken():
+	emit_signal("kace_broken")
+
+
+func _on_Brick_kace_damaged():
+	emit_signal("kace_damaged")

+ 2 - 10
godot/component/entity/brick/Grid.tscn

@@ -1,19 +1,11 @@
-[gd_scene load_steps=4 format=2]
+[gd_scene load_steps=3 format=2]
 
 [ext_resource path="res://component/ui/LifeContainer.gd" type="Script" id=1]
-[ext_resource path="res://component/entity/brick/brick_tileset.tres" type="TileSet" id=2]
 [ext_resource path="res://component/entity/brick/Grid.gd" type="Script" id=3]
 
-[node name="Grid" type="TileMap" groups=[
+[node name="Grid" type="Node2D" groups=[
 "grid",
 ]]
-mode = 2
-tile_set = ExtResource( 2 )
-cell_size = Vector2( 40, 20 )
-cell_custom_transform = Transform2D( 40, 0, 0, 20, 0, 0 )
-collision_layer = 4
-collision_mask = 8
-format = 1
 script = ExtResource( 3 )
 
 [node name="LifeContainer" type="Node2D" parent="."]