Quellcode durchsuchen

Put visor on the first obstacle

Raycast up to first brick or wall.
DricomDragon vor 3 Jahren
Ursprung
Commit
a45b76b3ab
3 geänderte Dateien mit 34 neuen und 8 gelöschten Zeilen
  1. 8 2
      godot/scenes/main.tscn
  2. 7 6
      godot/scripts/Launcher.gd
  3. 19 0
      godot/scripts/TargetLine.gd

+ 8 - 2
godot/scenes/main.tscn

@@ -1,4 +1,4 @@
-[gd_scene load_steps=18 format=2]
+[gd_scene load_steps=19 format=2]
 
 [ext_resource path="res://image/launcher.png" type="Texture" id=1]
 [ext_resource path="res://image/brik.png" type="Texture" id=2]
@@ -11,6 +11,7 @@
 [ext_resource path="res://scripts/ScoreCounterLabel.gd" type="Script" id=9]
 [ext_resource path="res://scripts/LifeContainer.gd" type="Script" id=10]
 [ext_resource path="res://scripts/DeadLine.gd" type="Script" id=11]
+[ext_resource path="res://scripts/TargetLine.gd" type="Script" id=12]
 
 [sub_resource type="PhysicsMaterial" id=1]
 friction = 0.0
@@ -99,8 +100,12 @@ script = ExtResource( 6 )
 [node name="LifeContainer" type="Node2D" parent="Launcher/Grid"]
 script = ExtResource( 10 )
 
-[node name="Target" type="Sprite" parent="Launcher"]
+[node name="TargetLine" type="RayCast2D" parent="Launcher"]
 position = Vector2( 200, 400 )
+collision_mask = 6
+script = ExtResource( 12 )
+
+[node name="TargetVisor" type="Sprite" parent="Launcher/TargetLine"]
 texture = ExtResource( 5 )
 
 [node name="Source" type="Sprite" parent="Launcher"]
@@ -210,6 +215,7 @@ __meta__ = {
 [connection signal="gain_new_bullet" from="Launcher" to="UI/BallCounterPanel/BallCounterLabel" method="_on_Launcher_gain_new_bullet"]
 [connection signal="get_firing" from="Launcher" to="Launcher" method="_on_Launcher_get_firing"]
 [connection signal="is_full" from="Launcher" to="Launcher/Grid" method="_on_Launcher_is_full"]
+[connection signal="move_visor" from="Launcher" to="Launcher/TargetLine" method="_on_Launcher_move_visor"]
 [connection signal="has_moved" from="Launcher/Grid" to="Launcher" method="_on_Grid_has_moved"]
 [connection signal="has_moved" from="Launcher/Grid" to="Launcher/DeadLine" method="_on_Grid_has_moved"]
 [connection signal="kace_broken" from="Launcher/Grid" to="Launcher" method="_on_Grid_kace_broken"]

+ 7 - 6
godot/scripts/Launcher.gd

@@ -6,6 +6,7 @@ signal get_firing
 signal is_full
 signal gain_new_bullet
 signal interrupt_wave
+signal move_visor
 
 enum State {
 	READY,
@@ -22,7 +23,8 @@ var _ballScene = preload("res://scenes/Ball.tscn")
 var nbAmmo = 0
 var nbStorage = 1
 
-onready var target = $Target
+onready var target = $TargetLine
+onready var visor = $TargetLine/TargetVisor
 onready var source = $Source
 onready var shootDelay = $ShootDelay
 onready var grid = $Grid
@@ -42,14 +44,13 @@ func _input(event):
 
 
 func aim_at(cursor:Vector2):
-	target.position = source.position
-	target.position += (cursor - source.position) * visor_ratio
+	visor.position = (cursor - source.position) * visor_ratio
+	emit_signal("move_visor")
 
 
 func trigger(aim:Vector2):
 	if (current_state == State.READY):
 		current_state = State.FIRING
-		target.position = aim
 		emit_signal("get_firing")
 	elif (current_state == State.WAITING):
 		current_state = State.INTERRUPTING
@@ -59,7 +60,7 @@ func trigger(aim:Vector2):
 func shoot():
 		var bullet = _ballScene.instance()
 		bullet.position = source.position
-		bullet.linear_velocity = (target.position - source.position).normalized()
+		bullet.linear_velocity = (target.get_visor_pos() - source.position).normalized()
 		bullet.linear_velocity *= BULLET_SPEED
 		add_child(bullet)
 		bullet.connect("tree_exited", self, "_on_Ball_tree_exited")
@@ -83,7 +84,7 @@ func end_of_round():
 
 func get_ready():
 	current_state = State.READY
-	target.position = source.position
+	visor.position = source.position
 	nbAmmo = nbStorage
 	nbStorage = 0
 

+ 19 - 0
godot/scripts/TargetLine.gd

@@ -0,0 +1,19 @@
+extends RayCast2D
+
+
+onready var visor = $TargetVisor
+
+
+func get_visor_pos():
+	return visor.position + position
+
+
+func focus_visor():
+	cast_to = visor.position
+	force_raycast_update()
+	if is_colliding():
+		visor.position = get_collision_point() - position
+
+
+func _on_Launcher_move_visor():
+	focus_visor()