浏览代码

:tada: Create a ball spawner

DricomDragon 2 年之前
父节点
当前提交
8da007a920
共有 3 个文件被更改,包括 43 次插入5 次删除
  1. 14 2
      labs/cascade/Arena1.tscn
  2. 4 3
      labs/cascade/component/ball/Ball.tscn
  3. 25 0
      labs/cascade/component/spawner/Spawner.gd

+ 14 - 2
labs/cascade/Arena1.tscn

@@ -1,10 +1,17 @@
-[gd_scene load_steps=3 format=2]
+[gd_scene load_steps=4 format=2]
 
 [ext_resource path="res://labs/cascade/component/bar/Bar.tscn" type="PackedScene" id=1]
 [ext_resource path="res://labs/cascade/component/ball/Ball.tscn" type="PackedScene" id=2]
+[ext_resource path="res://labs/cascade/component/spawner/Spawner.gd" type="Script" id=3]
 
 [node name="Arena1" type="Node"]
 
+[node name="Spawner" type="Position2D" parent="."]
+position = Vector2( 844.767, 116.097 )
+script = ExtResource( 3 )
+dispersion_scale = Vector2( 10, 10 )
+delay = 0.1
+
 [node name="Bar" parent="." instance=ExtResource( 1 )]
 position = Vector2( 794.105, 268.158 )
 
@@ -21,4 +28,9 @@ position = Vector2( 770.105, 344.158 )
 rotation = -0.88848
 
 [node name="Ball" parent="." instance=ExtResource( 2 )]
-position = Vector2( 855, -1 )
+position = Vector2( 685.103, 159.459 )
+
+[node name="Camera2D" type="Camera2D" parent="."]
+position = Vector2( 784.4, 232.396 )
+current = true
+zoom = Vector2( 0.4, 0.4 )

+ 4 - 3
labs/cascade/component/ball/Ball.tscn

@@ -1,14 +1,15 @@
-[gd_scene load_steps=3 format=2]
+[gd_scene load_steps=4 format=2]
 
 [ext_resource path="res://labs/cascade/component/ball/firefox-logo.png" type="Texture" id=1]
 
-
-
+[sub_resource type="PhysicsMaterial" id=2]
+bounce = 0.22
 
 [sub_resource type="CircleShape2D" id=1]
 radius = 4.0
 
 [node name="Ball" type="RigidBody2D"]
+physics_material_override = SubResource( 2 )
 
 [node name="FirefoxSprite" type="Sprite" parent="."]
 scale = Vector2( 0.064, 0.064 )

+ 25 - 0
labs/cascade/component/spawner/Spawner.gd

@@ -0,0 +1,25 @@
+extends Position2D
+
+var Ball: PackedScene = preload("res://labs/cascade/component/ball/Ball.tscn")
+
+export(Vector2) var dispersion_scale = Vector2.ONE
+export(float) var delay = 1
+
+
+func _ready():
+	spawn()
+
+
+func spawn():
+	print("spawn")
+	var ball: PhysicsBody2D = Ball.instance()
+	var dispersion = Vector2(randf() * dispersion_scale.x, randf() * dispersion_scale.y)
+	ball.position = dispersion
+
+	add_child(ball)
+
+	get_tree().create_timer(delay).connect("timeout", self, "_on_delay_time_out")
+
+
+func _on_delay_time_out():
+	spawn()