improve minigame

This commit is contained in:
Arnaud Vergnet 2021-04-08 12:17:48 +02:00
parent 7e00d5a054
commit 0a61adf7b0
10 changed files with 163 additions and 62 deletions

45
mini-game/MiniGame.gd Normal file
View file

@ -0,0 +1,45 @@
extends Node2D
signal game_over
onready var background := $background
onready var foreground := $foreground
onready var spawner := $spawner
onready var player := $player2
onready var scoreUI := $scoreUI
var next_timeline := ""
func _ready():
Signals.connect("die", self, "on_game_over")
func setup(mode: String, next: String):
print("minigame: " + mode + " " + next)
set_mode(mode)
next_timeline = next
func set_mode(mode: String):
match mode:
_:
print("unkonwn mini-game mode")
func start():
print("starting minigame")
foreground.start()
player.start()
spawner.start()
func stop():
foreground.stop()
player.stop()
spawner.stop()
func on_game_over():
stop()
emit_signal("game_over", next_timeline)

View file

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

View file

@ -0,0 +1,9 @@
extends Node2D
onready var texture : TextureRect = $TextureRect
func stop():
texture.material.set_shader_param("scroll_speed", 0)
func start():
texture.material.set_shader_param("scroll_speed", 0.2)

View file

@ -1,23 +1,46 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=6 format=2]
[ext_resource path="res://mini-game/scenes/effets/ScrollingBG.tscn" type="PackedScene" id=1]
[ext_resource path="res://mini-game/scenes/decor/foreground.gd" type="Script" id=1]
[ext_resource path="res://mini-game/ressources/backgrounds/road3.png" type="Texture" id=2]
[sub_resource type="RectangleShape2D" id=1]
[sub_resource type="Shader" id=1]
code = "shader_type canvas_item;
uniform float scroll_speed;
void fragment(){
vec2 u = UV;
u.x += scroll_speed*TIME;
vec4 color = texture(TEXTURE,u);
COLOR = color;
}
"
[sub_resource type="ShaderMaterial" id=2]
shader = SubResource( 1 )
shader_param/scroll_speed = 0.0
[sub_resource type="RectangleShape2D" id=3]
extents = Vector2( 552.889, 78.6175 )
[node name="foreground" type="Node2D"]
script = ExtResource( 1 )
[node name="ScrollingBG" parent="." instance=ExtResource( 1 )]
margin_left = 0.734742
margin_top = -356.907
margin_right = 1920.73
margin_bottom = 723.093
rect_scale = Vector2( 0.533041, 0.912837 )
[node name="TextureRect" type="TextureRect" parent="."]
material = SubResource( 2 )
margin_top = -356.0
margin_right = 1920.0
margin_bottom = 723.0
rect_scale = Vector2( 0.533, 0.913 )
texture = ExtResource( 2 )
stretch_mode = 2
__meta__ = {
"_edit_use_anchors_": false,
"_editor_description_": ""
}
[node name="StaticBody2D" type="StaticBody2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2( 497.671, 612.885 )
shape = SubResource( 1 )
shape = SubResource( 3 )

View file

@ -0,0 +1,11 @@
extends Control
onready var label : RichTextLabel = $RichTextLabel
func _ready():
Signals.connect("update_score",self,"update_score")
func update_score(score: int):
label.text = String(score)

View file

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

View file

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

View file

@ -13,6 +13,8 @@ enum {
IDLE
}
var player_enabled = false;
var state = RUN
var au_sol = true
@ -23,54 +25,56 @@ onready var animation = $AnimatedSprite
# Called when the node enters the scene tree for the first time.
func _ready():
stop()
Signals.connect("gain",self,"increase_score")
Signals.connect("die",self,"player_die")
func _physics_process(delta):
match state:
RUN:
animation.play("run")
JUMP:
vitesse = Vector2.ZERO
vitesse.y -= jump_vitesse
animation.play("jump")
state = IDLE
print("jump")
IDLE:
pass
vitesse.y += gravite
move_and_collide(vitesse*delta)
if player_enabled:
match state:
RUN:
animation.play("run")
JUMP:
vitesse = Vector2.ZERO
vitesse.y -= jump_vitesse
animation.play("jump")
state = IDLE
IDLE:
pass
vitesse.y += gravite
move_and_collide(vitesse*delta)
func _input(event):
if state == RUN and event.is_action_pressed("ui_accept"):
if player_enabled and state == RUN and event.is_action_pressed("ui_accept"):
state = JUMP
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func _on_Area2D_body_entered(body):
print("landing")
if body is StaticBody2D:
print("Yep a dino is landing")
state = RUN
func _on_Area2D_body_exited(body):
print("jumping")
if body is StaticBody2D:
print("Yep a dino is jumping")
state = JUMP
func increase_score(scoretoadd):
score+=scoretoadd
Signals.emit_signal("update_score",score)
print(score)
func player_die():
queue_free()
stop()
func start():
show()
player_enabled = true
func stop():
hide()
player_enabled = false

View file

@ -7,20 +7,35 @@ var scene_index = 0;
var last_object
var end_of_game = false
var spawner_enabled = false
func _ready():
Signals.connect("die",self,"game_over")
func _on_Timer_timeout():
random_scene.randomize()
scene_index = random_scene.randi_range(0,scenes.size()-1)
print(scene_index)
var tmp = scenes[scene_index].instance()
add_child_below_node(self,tmp)
last_object = tmp
self.get_node("Timer").wait_time *= 0.99
self.get_node("Timer").start()
if spawner_enabled:
random_scene.randomize()
scene_index = random_scene.randi_range(0,scenes.size()-1)
var tmp = scenes[scene_index].instance()
$items.add_child(tmp)
last_object = tmp
self.get_node("Timer").wait_time *= 0.99
self.get_node("Timer").start()
func game_over():
self.get_node("Timer").set_paused(true)
last_object.queue_free()
func start():
spawner_enabled = true
func stop():
spawner_enabled = false
for obj in $items.get_children():
remove_child(obj)
obj.queue_free()

View file

@ -1,10 +0,0 @@
extends RichTextLabel
func _ready():
Signals.connect("update_score",self,"update_score")
func update_score(score):
self.text = String(score)