Browse Source

use sprites for foreground

Arnaud Vergnet 4 months ago
parent
commit
225a55bacd

+ 3
- 4
mini-game/MiniGame.gd View File

@@ -110,9 +110,8 @@ func update_score():
110 110
 
111 111
 
112 112
 func _on_player_score() -> void:
113
-	if game_mode == "score":
114
-		update_score()
113
+	update_score()
114
+
115 115
 
116 116
 func _on_Timer_timeout() -> void:
117
-	if game_mode == "time":
118
-		update_score()
117
+	update_score()

+ 35
- 0
mini-game/scenes/decor/ForegroundSprite.gd View File

@@ -0,0 +1,35 @@
1
+extends Sprite
2
+
3
+export var scroll_speed = 9
4
+
5
+var moving = false
6
+
7
+signal viewport_entered(object)
8
+signal viewport_exited(object)
9
+
10
+
11
+func start():
12
+	moving = true
13
+
14
+
15
+func stop():
16
+	moving = false
17
+
18
+
19
+func _physics_process(delta):
20
+	if moving:
21
+		move()
22
+
23
+
24
+func move():
25
+	position.x -= scroll_speed
26
+
27
+
28
+func _on_VisibilityNotifier2D_viewport_entered(viewport: Viewport) -> void:
29
+	print("viewport_entered")
30
+	emit_signal("viewport_entered", self)
31
+
32
+
33
+func _on_VisibilityNotifier2D_viewport_exited(viewport: Viewport) -> void:
34
+	print("viewport_exited")
35
+	emit_signal("viewport_exited", self)

+ 36
- 0
mini-game/scenes/decor/ForegroundSprite.tscn View File

@@ -0,0 +1,36 @@
1
+[gd_scene load_steps=5 format=2]
2
+
3
+[ext_resource path="res://mini-game/ressources/backgrounds/road3.png" type="Texture" id=1]
4
+[ext_resource path="res://mini-game/scenes/decor/ForegroundSprite.gd" type="Script" id=2]
5
+
6
+[sub_resource type="Shader" id=1]
7
+code = "shader_type canvas_item;
8
+
9
+uniform float scroll_speed;
10
+
11
+void fragment(){
12
+	vec2 u = UV;
13
+	u.x += scroll_speed*TIME;
14
+	vec4 color = texture(TEXTURE,u);
15
+	COLOR = color;
16
+}
17
+"
18
+
19
+[sub_resource type="ShaderMaterial" id=2]
20
+shader = SubResource( 1 )
21
+shader_param/scroll_speed = 0.0
22
+
23
+[node name="ForegroundSprite" type="Sprite"]
24
+material = SubResource( 2 )
25
+texture = ExtResource( 1 )
26
+offset = Vector2( 960, 540 )
27
+script = ExtResource( 2 )
28
+__meta__ = {
29
+"_editor_description_": ""
30
+}
31
+
32
+[node name="VisibilityNotifier2D" type="VisibilityNotifier2D" parent="."]
33
+position = Vector2( 1930, 540 )
34
+
35
+[connection signal="viewport_entered" from="VisibilityNotifier2D" to="." method="_on_VisibilityNotifier2D_viewport_entered"]
36
+[connection signal="viewport_exited" from="VisibilityNotifier2D" to="." method="_on_VisibilityNotifier2D_viewport_exited"]

+ 26
- 16
mini-game/scenes/decor/foreground.gd View File

@@ -1,28 +1,38 @@
1 1
 extends Node2D
2 2
 
3
-onready var texture : TextureRect = $TextureRect
3
+var sprite_scene = preload("res://mini-game/scenes/decor/ForegroundSprite.tscn")
4 4
 
5
-var force_stop = false
6
-var moving = false
5
+var textures := []
6
+var moving := false
7 7
 
8 8
 func _ready():
9
+	textures.append($ForegroundSprite)
9 10
 	stop()
10 11
 
12
+
11 13
 func stop():
12 14
 	moving = false
13
-	force_stop = true
14
-	texture.material.set_shader_param("scroll_speed", 0)
15
+	for t in textures:
16
+		t.stop()
17
+
15 18
 
16 19
 func start():
17 20
 	moving = true
18
-	force_stop = false
19
-	texture.material.set_shader_param("scroll_speed", 0.282)
20
-	#texture.material.set_shader_param("scroll_speed", 0.2)
21
-
22
-func _process(delta):
23
-	if not force_stop:
24
-		if get_tree().paused and moving:
25
-			stop()
26
-			force_stop = false
27
-		elif not get_tree().paused and not moving:
28
-			start()
21
+	for t in textures:
22
+		t.start()
23
+
24
+
25
+func _on_ForegroundSprite_viewport_entered(object: Sprite) -> void:
26
+	var new_sprite = sprite_scene.instance()
27
+	new_sprite.position = Vector2(1920, 0)
28
+	new_sprite.connect("viewport_entered", self, "_on_ForegroundSprite_viewport_entered")
29
+	new_sprite.connect("viewport_exited", self, "_on_ForegroundSprite_viewport_exited")
30
+	if moving:
31
+		new_sprite.start()
32
+	add_child(new_sprite)
33
+	textures.append(new_sprite)
34
+
35
+
36
+func _on_ForegroundSprite_viewport_exited(object: Sprite) -> void:
37
+	textures.remove(textures.find(object))
38
+	object.queue_free()

+ 7
- 31
mini-game/scenes/decor/foreground.tscn View File

@@ -1,45 +1,21 @@
1
-[gd_scene load_steps=6 format=2]
1
+[gd_scene load_steps=4 format=2]
2 2
 
3 3
 [ext_resource path="res://mini-game/scenes/decor/foreground.gd" type="Script" id=1]
4
-[ext_resource path="res://mini-game/ressources/backgrounds/road3.png" type="Texture" id=2]
5
-
6
-[sub_resource type="Shader" id=1]
7
-code = "shader_type canvas_item;
8
-
9
-uniform float scroll_speed;
10
-
11
-void fragment(){
12
-	vec2 u = UV;
13
-	u.x += scroll_speed*TIME;
14
-	vec4 color = texture(TEXTURE,u);
15
-	COLOR = color;
16
-}
17
-"
18
-
19
-[sub_resource type="ShaderMaterial" id=2]
20
-shader = SubResource( 1 )
21
-shader_param/scroll_speed = 0.0
4
+[ext_resource path="res://mini-game/scenes/decor/ForegroundSprite.tscn" type="PackedScene" id=2]
22 5
 
23 6
 [sub_resource type="RectangleShape2D" id=3]
24 7
 extents = Vector2( 958.398, 78.6175 )
25 8
 
26 9
 [node name="foreground" type="Node2D"]
27
-pause_mode = 2
28 10
 script = ExtResource( 1 )
29 11
 
30
-[node name="TextureRect" type="TextureRect" parent="."]
31
-material = SubResource( 2 )
32
-margin_right = 1920.0
33
-margin_bottom = 1080.0
34
-texture = ExtResource( 2 )
35
-stretch_mode = 2
36
-__meta__ = {
37
-"_edit_use_anchors_": false,
38
-"_editor_description_": ""
39
-}
40
-
41 12
 [node name="StaticBody2D" type="StaticBody2D" parent="."]
42 13
 
43 14
 [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
44 15
 position = Vector2( 960, 1080 )
45 16
 shape = SubResource( 3 )
17
+
18
+[node name="ForegroundSprite" parent="." instance=ExtResource( 2 )]
19
+
20
+[connection signal="viewport_entered" from="ForegroundSprite" to="." method="_on_ForegroundSprite_viewport_entered"]
21
+[connection signal="viewport_exited" from="ForegroundSprite" to="." method="_on_ForegroundSprite_viewport_exited"]

+ 2
- 1
mini-game/scenes/players/player.gd View File

@@ -27,7 +27,7 @@ var au_sol = true
27 27
 var current_lives = max_lives
28 28
 
29 29
 onready var animation = $AnimatedSprite
30
-onready var bonus_texture = $BonusControl/TextureRect
30
+onready var bonus_texture = $BonusControl/Sprite
31 31
 onready var bonus_tween = $BonusControl/Tween
32 32
 onready var bonus_timer = $BonusControl/Timer
33 33
 
@@ -95,6 +95,7 @@ func play_bonus_anim():
95 95
 	bonus_tween.stop_all()
96 96
 	bonus_timer.stop()
97 97
 	bonus_tween.interpolate_property(bonus_texture, "modulate", null, Color(1, 1, 1, 1), 0.3, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
98
+	bonus_tween.interpolate_property(bonus_texture, "position", Vector2(bonus_texture.position.x, 0), Vector2(bonus_texture.position.x, bonus_texture.position.y), 0.3, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
98 99
 	bonus_tween.start()
99 100
 	bonus_timer.start()
100 101
 

+ 4
- 16
mini-game/scenes/players/player.tscn View File

@@ -73,24 +73,12 @@ position = Vector2( -0.0400658, 9.23466 )
73 73
 scale = Vector2( 0.609429, 0.281311 )
74 74
 shape = SubResource( 3 )
75 75
 
76
-[node name="BonusControl" type="Control" parent="."]
77
-margin_right = 40.0
78
-margin_bottom = 40.0
79
-__meta__ = {
80
-"_edit_use_anchors_": false
81
-}
76
+[node name="BonusControl" type="Node2D" parent="."]
82 77
 
83
-[node name="TextureRect" type="TextureRect" parent="BonusControl"]
84
-margin_left = -10.7864
85
-margin_top = -26.3001
86
-margin_right = 10.2136
87
-margin_bottom = -11.3001
78
+[node name="Sprite" type="Sprite" parent="BonusControl"]
79
+position = Vector2( 0, -15 )
80
+scale = Vector2( 0.02, 0.02 )
88 81
 texture = ExtResource( 22 )
89
-expand = true
90
-stretch_mode = 6
91
-__meta__ = {
92
-"_edit_use_anchors_": false
93
-}
94 82
 
95 83
 [node name="Tween" type="Tween" parent="BonusControl"]
96 84
 

Loading…
Cancel
Save