Browse Source

Make Grid manage breaking cells

DricomDragon 3 years ago
parent
commit
37c940c276
4 changed files with 29 additions and 19 deletions
  1. 11 8
      godot/scenes/main.tscn
  2. 2 8
      godot/scripts/Ball.gd
  3. 14 0
      godot/scripts/Grid.gd
  4. 2 3
      godot/scripts/Launcher.gd

+ 11 - 8
godot/scenes/main.tscn

@@ -1,10 +1,11 @@
-[gd_scene load_steps=12 format=2]
+[gd_scene load_steps=13 format=2]
 
 [ext_resource path="res://image/launcher.png" type="Texture" id=1]
 [ext_resource path="res://image/brik.png" type="Texture" id=2]
 [ext_resource path="res://scripts/Catcher.gd" type="Script" id=3]
 [ext_resource path="res://scripts/Launcher.gd" type="Script" id=4]
 [ext_resource path="res://image/visor.png" type="Texture" id=5]
+[ext_resource path="res://scripts/Grid.gd" type="Script" id=6]
 
 [sub_resource type="PhysicsMaterial" id=1]
 friction = 0.0
@@ -16,13 +17,13 @@ extents = Vector2( 50, 300 )
 [sub_resource type="RectangleShape2D" id=3]
 extents = Vector2( 300, 50 )
 
-[sub_resource type="RectangleShape2D" id=6]
+[sub_resource type="RectangleShape2D" id=4]
 extents = Vector2( 200, 50 )
 
-[sub_resource type="ConvexPolygonShape2D" id=4]
+[sub_resource type="ConvexPolygonShape2D" id=5]
 points = PoolVector2Array( 0, 0, 40, 0, 40, 20, 0, 20 )
 
-[sub_resource type="TileSet" id=5]
+[sub_resource type="TileSet" id=6]
 0/name = "brik.png 0"
 0/texture = ExtResource( 2 )
 0/tex_offset = Vector2( 0, 0 )
@@ -35,7 +36,7 @@ points = PoolVector2Array( 0, 0, 40, 0, 40, 20, 0, 20 )
 "autotile_coord": Vector2( 0, 0 ),
 "one_way": false,
 "one_way_margin": 1.0,
-"shape": SubResource( 4 ),
+"shape": SubResource( 5 ),
 "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 )
 } ]
 0/z_index = 0
@@ -62,18 +63,19 @@ position = Vector2( 200, 470 )
 script = ExtResource( 3 )
 
 [node name="Area" type="CollisionShape2D" parent="Catcher"]
-shape = SubResource( 6 )
+shape = SubResource( 4 )
 
 [node name="Launcher" type="Node2D" parent="."]
 script = ExtResource( 4 )
 
 [node name="Grid" type="TileMap" parent="Launcher"]
 mode = 2
-tile_set = SubResource( 5 )
+tile_set = SubResource( 6 )
 cell_size = Vector2( 40, 20 )
 cell_custom_transform = Transform2D( 40, 0, 0, 20, 0, 0 )
 format = 1
-tile_data = PoolIntArray( 65540, 0, 0, 65541, 0, 0, 393217, 0, 0, 393218, 0, 0, 393223, 0, 0, 393224, 0, 0, 458753, 0, 0, 458754, 0, 0, 458759, 0, 0, 458760, 0, 0, 524289, 0, 0, 524290, 0, 0, 524295, 0, 0, 524296, 0, 0, 720899, 0, 0, 720900, 0, 0, 720901, 0, 0, 720902, 0, 0 )
+tile_data = PoolIntArray( 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 65536, 0, 0, 65537, 0, 0, 65538, 0, 0, 65539, 0, 0, 65540, 0, 0, 65541, 0, 0, 65542, 0, 0, 65543, 0, 0, 65544, 0, 0, 65545, 0, 0 )
+script = ExtResource( 6 )
 
 [node name="Target" type="Sprite" parent="Launcher"]
 position = Vector2( 200, 400 )
@@ -88,4 +90,5 @@ wait_time = 0.3
 one_shot = true
 [connection signal="body_entered" from="Catcher" to="Catcher" method="_on_Catcher_body_entered"]
 [connection signal="get_firing" from="Launcher" to="Launcher" method="_on_Launcher_get_firing"]
+[connection signal="kace_broken" from="Launcher/Grid" to="Launcher" method="_on_Grid_kace_broken"]
 [connection signal="timeout" from="Launcher/ShootDelay" to="Launcher" method="_on_ShootDelay_timeout"]

+ 2 - 8
godot/scripts/Ball.gd

@@ -1,17 +1,11 @@
 class_name Ball
 extends RigidBody2D
 
-signal kace_broken
-
-var grid:TileMap
+signal kace_contact
 
 func _integrate_forces(state :  Physics2DDirectBodyState):
 	for i in range(state.get_contact_count()):
 		var contact_pos = state.get_contact_local_position(i)
-		var tile_pos = grid.world_to_map(contact_pos)
-		var cell = grid.get_cellv(tile_pos)
-		if cell != TileMap.INVALID_CELL and cell == 0:
-			grid.set_cellv(tile_pos, -1)
-			emit_signal("kace_broken")
+		emit_signal("kace_contact", contact_pos)
 
 

+ 14 - 0
godot/scripts/Grid.gd

@@ -0,0 +1,14 @@
+extends TileMap
+
+signal kace_broken
+
+enum Tile {
+	BRICK = 0,
+}
+
+func _on_Ball_kace_contact(contact_pos:Vector2):
+	var tile_pos = world_to_map(contact_pos)
+	var cell = get_cellv(tile_pos)
+	if cell != TileMap.INVALID_CELL and cell == Tile.BRICK:
+		set_cellv(tile_pos, -1)
+		emit_signal("kace_broken")

+ 2 - 3
godot/scripts/Launcher.gd

@@ -40,10 +40,9 @@ func shoot():
 		bullet.position = source.position
 		bullet.linear_velocity = (target.position - source.position).normalized()
 		bullet.linear_velocity *= BULLET_SPEED
-		bullet.grid = grid
 		add_child(bullet)
 		bullet.connect("tree_exited", self, "_on_Ball_tree_exited")
-		bullet.connect("kace_broken", self, "_on_Ball_kace_broken")
+		bullet.connect("kace_contact", grid, "_on_Ball_kace_contact")
 
 
 func loadBullet(n:int):
@@ -72,5 +71,5 @@ func _on_Ball_tree_exited():
 		nbStorage = 0
 
 
-func _on_Ball_kace_broken():
+func _on_Grid_kace_broken():
 	loadBullet(1)