Browse Source

improve minigame

Arnaud Vergnet 6 months ago
parent
commit
0a61adf7b0

+ 45
- 0
mini-game/MiniGame.gd View File

@@ -0,0 +1,45 @@
1
+extends Node2D
2
+
3
+signal game_over
4
+
5
+onready var background := $background
6
+onready var foreground := $foreground
7
+onready var spawner := $spawner
8
+onready var player := $player2
9
+onready var scoreUI := $scoreUI
10
+
11
+var next_timeline := ""
12
+
13
+func _ready():
14
+	Signals.connect("die", self, "on_game_over")
15
+
16
+
17
+func setup(mode: String, next: String):
18
+	print("minigame: " + mode + " " + next)
19
+	set_mode(mode)
20
+	next_timeline = next
21
+
22
+
23
+func set_mode(mode: String):
24
+	match mode:
25
+		_:
26
+			print("unkonwn mini-game mode")
27
+	
28
+
29
+func start():
30
+	print("starting minigame")
31
+	foreground.start()
32
+	player.start()
33
+	spawner.start()
34
+
35
+
36
+func stop():
37
+	foreground.stop()
38
+	player.stop()
39
+	spawner.stop()
40
+	
41
+
42
+
43
+func on_game_over():
44
+	stop()
45
+	emit_signal("game_over", next_timeline)

mini-game/scenes/main.tscn → mini-game/MiniGame.tscn View File

@@ -1,12 +1,14 @@
1
-[gd_scene load_steps=6 format=2]
1
+[gd_scene load_steps=7 format=2]
2 2
 
3 3
 [ext_resource path="res://mini-game/scenes/decor/background.tscn" type="PackedScene" id=1]
4 4
 [ext_resource path="res://mini-game/scenes/decor/foreground.tscn" type="PackedScene" id=2]
5 5
 [ext_resource path="res://mini-game/scenes/players/player2.tscn" type="PackedScene" id=3]
6
+[ext_resource path="res://mini-game/MiniGame.gd" type="Script" id=4]
6 7
 [ext_resource path="res://mini-game/scenes/spawner/spawner.tscn" type="PackedScene" id=6]
7 8
 [ext_resource path="res://mini-game/scenes/decor/scoreUI.tscn" type="PackedScene" id=7]
8 9
 
9
-[node name="main" type="Node2D"]
10
+[node name="MiniGame" type="Node2D"]
11
+script = ExtResource( 4 )
10 12
 
11 13
 [node name="background" parent="." instance=ExtResource( 1 )]
12 14
 position = Vector2( 1, -125 )

+ 9
- 0
mini-game/scenes/decor/foreground.gd View File

@@ -0,0 +1,9 @@
1
+extends Node2D
2
+
3
+onready var texture : TextureRect = $TextureRect
4
+
5
+func stop():
6
+	texture.material.set_shader_param("scroll_speed", 0)
7
+
8
+func start():
9
+	texture.material.set_shader_param("scroll_speed", 0.2)

+ 33
- 10
mini-game/scenes/decor/foreground.tscn View File

@@ -1,23 +1,46 @@
1
-[gd_scene load_steps=4 format=2]
1
+[gd_scene load_steps=6 format=2]
2 2
 
3
-[ext_resource path="res://mini-game/scenes/effets/ScrollingBG.tscn" type="PackedScene" id=1]
3
+[ext_resource path="res://mini-game/scenes/decor/foreground.gd" type="Script" id=1]
4 4
 [ext_resource path="res://mini-game/ressources/backgrounds/road3.png" type="Texture" id=2]
5 5
 
6
-[sub_resource type="RectangleShape2D" id=1]
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
+[sub_resource type="RectangleShape2D" id=3]
7 24
 extents = Vector2( 552.889, 78.6175 )
8 25
 
9 26
 [node name="foreground" type="Node2D"]
27
+script = ExtResource( 1 )
10 28
 
11
-[node name="ScrollingBG" parent="." instance=ExtResource( 1 )]
12
-margin_left = 0.734742
13
-margin_top = -356.907
14
-margin_right = 1920.73
15
-margin_bottom = 723.093
16
-rect_scale = Vector2( 0.533041, 0.912837 )
29
+[node name="TextureRect" type="TextureRect" parent="."]
30
+material = SubResource( 2 )
31
+margin_top = -356.0
32
+margin_right = 1920.0
33
+margin_bottom = 723.0
34
+rect_scale = Vector2( 0.533, 0.913 )
17 35
 texture = ExtResource( 2 )
36
+stretch_mode = 2
37
+__meta__ = {
38
+"_edit_use_anchors_": false,
39
+"_editor_description_": ""
40
+}
18 41
 
19 42
 [node name="StaticBody2D" type="StaticBody2D" parent="."]
20 43
 
21 44
 [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
22 45
 position = Vector2( 497.671, 612.885 )
23
-shape = SubResource( 1 )
46
+shape = SubResource( 3 )

+ 11
- 0
mini-game/scenes/decor/scoreUI.gd View File

@@ -0,0 +1,11 @@
1
+extends Control
2
+
3
+onready var label : RichTextLabel = $RichTextLabel
4
+
5
+func _ready():
6
+	Signals.connect("update_score",self,"update_score")
7
+	
8
+
9
+func update_score(score: int):
10
+	label.text = String(score)
11
+	

+ 2
- 2
mini-game/scenes/decor/scoreUI.tscn View File

@@ -1,6 +1,6 @@
1 1
 [gd_scene load_steps=4 format=2]
2 2
 
3
-[ext_resource path="res://mini-game/scripts/update_score.gd" type="Script" id=1]
3
+[ext_resource path="res://mini-game/scenes/decor/scoreUI.gd" type="Script" id=2]
4 4
 
5 5
 [sub_resource type="DynamicFontData" id=1]
6 6
 antialiased = false
@@ -12,6 +12,7 @@ outline_color = Color( 0, 0, 0, 1 )
12 12
 font_data = SubResource( 1 )
13 13
 
14 14
 [node name="scoreUI" type="Control"]
15
+script = ExtResource( 2 )
15 16
 __meta__ = {
16 17
 "_edit_use_anchors_": false
17 18
 }
@@ -32,7 +33,6 @@ tab_size = 1
32 33
 text = "0"
33 34
 fit_content_height = true
34 35
 scroll_active = false
35
-script = ExtResource( 1 )
36 36
 __meta__ = {
37 37
 "_edit_use_anchors_": false
38 38
 }

+ 2
- 0
mini-game/scenes/spawner/spawner.tscn View File

@@ -11,4 +11,6 @@ scenes = [ ExtResource( 3 ), ExtResource( 2 ) ]
11 11
 [node name="Timer" type="Timer" parent="."]
12 12
 wait_time = 2.094
13 13
 autostart = true
14
+
15
+[node name="items" type="Node2D" parent="."]
14 16
 [connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]

+ 33
- 29
mini-game/scripts/player2.gd View File

@@ -13,6 +13,8 @@ enum {
13 13
 	IDLE
14 14
 }
15 15
 
16
+var player_enabled = false;
17
+
16 18
 var state = RUN
17 19
 var au_sol = true
18 20
 
@@ -23,54 +25,56 @@ onready var animation = $AnimatedSprite
23 25
 
24 26
 # Called when the node enters the scene tree for the first time.
25 27
 func _ready():
28
+	stop()
26 29
 	Signals.connect("gain",self,"increase_score")
27 30
 	Signals.connect("die",self,"player_die")
28 31
 
29 32
 func _physics_process(delta):
30
-	match state:
31
-		RUN:
32
-			animation.play("run")
33
-		JUMP:
34
-			vitesse = Vector2.ZERO
35
-			vitesse.y -= jump_vitesse
36
-			animation.play("jump")
37
-			state = IDLE
38
-			print("jump")
39
-		IDLE:
40
-			pass
41
-	vitesse.y += gravite
42
-	move_and_collide(vitesse*delta)
43
-	
33
+	if player_enabled:
34
+		match state:
35
+			RUN:
36
+				animation.play("run")
37
+			JUMP:
38
+				vitesse = Vector2.ZERO
39
+				vitesse.y -= jump_vitesse
40
+				animation.play("jump")
41
+				state = IDLE
42
+			IDLE:
43
+				pass
44
+		vitesse.y += gravite
45
+		move_and_collide(vitesse*delta)
46
+
47
+
44 48
 func _input(event):
45
-	if state == RUN and event.is_action_pressed("ui_accept"):
49
+	if player_enabled and state == RUN and event.is_action_pressed("ui_accept"):
46 50
 			state = JUMP
47
-			
48
-			
49
-		
50
-
51
-# Called every frame. 'delta' is the elapsed time since the previous frame.
52
-#func _process(delta):
53
-#	pass
54 51
 
55 52
 
56 53
 func _on_Area2D_body_entered(body):
57
-	print("landing")
58 54
 	if body is StaticBody2D:
59
-		print("Yep a dino is landing")
60 55
 		state = RUN
61 56
 		
62 57
 
63 58
 
64 59
 func _on_Area2D_body_exited(body):
65
-	print("jumping")
66 60
 	if body is StaticBody2D:
67
-		print("Yep a dino is jumping")
68 61
 		state = JUMP
69 62
 
63
+
70 64
 func increase_score(scoretoadd):
71 65
 	score+=scoretoadd
72 66
 	Signals.emit_signal("update_score",score)
73
-	print(score)
74
-		
67
+
68
+
75 69
 func player_die():
76
-	queue_free()
70
+	stop()
71
+
72
+
73
+func start():
74
+	show()
75
+	player_enabled = true
76
+
77
+
78
+func stop():
79
+	hide()
80
+	player_enabled = false

+ 23
- 8
mini-game/scripts/spawner.gd View File

@@ -7,20 +7,35 @@ var scene_index = 0;
7 7
 var last_object
8 8
 var end_of_game = false
9 9
 
10
+var spawner_enabled = false
11
+
12
+
10 13
 func _ready():
11 14
 	Signals.connect("die",self,"game_over")
12 15
 
16
+
13 17
 func _on_Timer_timeout():
14
-	random_scene.randomize()
15
-	scene_index = random_scene.randi_range(0,scenes.size()-1)
16
-	print(scene_index)
17
-	var tmp = scenes[scene_index].instance()
18
-	add_child_below_node(self,tmp)
19
-	last_object = tmp
20
-	self.get_node("Timer").wait_time *= 0.99
21
-	self.get_node("Timer").start()
18
+	if spawner_enabled:
19
+		random_scene.randomize()
20
+		scene_index = random_scene.randi_range(0,scenes.size()-1)
21
+		var tmp = scenes[scene_index].instance()
22
+		$items.add_child(tmp)
23
+		last_object = tmp
24
+		self.get_node("Timer").wait_time *= 0.99
25
+		self.get_node("Timer").start()
26
+
22 27
 
23 28
 func game_over():
24 29
 	self.get_node("Timer").set_paused(true)
25 30
 	last_object.queue_free()
26 31
 
32
+
33
+func start():
34
+	spawner_enabled = true
35
+
36
+
37
+func stop():
38
+	spawner_enabled = false
39
+	for obj in $items.get_children():
40
+		remove_child(obj)
41
+		obj.queue_free()

+ 0
- 10
mini-game/scripts/update_score.gd View File

@@ -1,10 +0,0 @@
1
-extends RichTextLabel
2
-
3
-
4
-func _ready():
5
-	Signals.connect("update_score",self,"update_score")
6
-	
7
-
8
-func update_score(score):
9
-	self.text = String(score)
10
-	

Loading…
Cancel
Save