From 3b0fcda34b3dddc47082165f8bc7f019fadaf8ae Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 8 Apr 2020 12:40:43 +0200 Subject: [PATCH] add ui --- Camera2D.gd | 12 +++++++++- Main.tscn | 6 +++-- Player.gd | 22 +++++++++++++---- Player.tscn | 14 +++++------ Tower.gd | 7 ++---- UI.gd | 36 ++++++++++++++++++++++++++++ UI.tscn | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ project.godot | 2 -- 8 files changed, 144 insertions(+), 21 deletions(-) create mode 100644 UI.gd create mode 100644 UI.tscn diff --git a/Camera2D.gd b/Camera2D.gd index 33513f3..0680fdb 100644 --- a/Camera2D.gd +++ b/Camera2D.gd @@ -5,6 +5,9 @@ export (PackedScene) var Tower = preload("res://Tower.tscn"); onready var screen_size = Vector2(ProjectSettings.get("display/window/size/width"), ProjectSettings.get("display/window/size/height")) export var tower_density = 250 + +signal spawn + var last_built = 500 func build_tower(): @@ -15,13 +18,20 @@ func build_tower(): tower.position = pos + emit_signal("spawn", tower.get_global_transform().get_origin().x) + add_child(tower) last_built += tower_density func _ready(): + var ui = get_node("UI") + var player = get_node("Player") + player.connect("start", ui, "_on_Player_start") + player.connect("hit", ui, "_on_Player_hit") + player.connect("passed", ui, "_on_Player_passed") + connect("spawn", player, "_on_Tower_spawn") randomize() - print(screen_size) while last_built < screen_size.x * 3: build_tower() diff --git a/Main.tscn b/Main.tscn index 9bd26c7..73686e5 100644 --- a/Main.tscn +++ b/Main.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://Camera2D.gd" type="Script" id=1] [ext_resource path="res://Player.tscn" type="PackedScene" id=2] [ext_resource path="res://res/sprites.png" type="Texture" id=3] +[ext_resource path="res://UI.tscn" type="PackedScene" id=4] [sub_resource type="AtlasTexture" id=1] atlas = ExtResource( 3 ) @@ -27,4 +28,5 @@ position = Vector2( 209.839, 320 ) scale = Vector2( 2.91891, 2.5 ) texture = SubResource( 1 ) region_rect = Rect2( 0, 0, 144, 256 ) -[connection signal="move" from="Player" to="." method="_on_Player_move"] + +[node name="UI" parent="." instance=ExtResource( 4 )] diff --git a/Player.gd b/Player.gd index d4f4b5c..5ed4ca5 100644 --- a/Player.gd +++ b/Player.gd @@ -4,24 +4,31 @@ export var speed = 3 # How fast the player will move (pixels/sec). export var jump_speed = 4 export var speed_inc = 0.3 -signal move -signal hit +signal start # The bird starts +signal passed # The bird passes a Tower +signal hit # The bird is hit/flies off screen + +var tower_locations = [] var screen_size var playing = false var speed_y = 0 +var x_offset + func start(): position = screen_size / 2 # Called when the node enters the scene tree for the first time. func _ready(): screen_size = get_viewport_rect().size + x_offset = get_global_transform().get_origin().x func _process(delta): if Input.is_action_just_pressed("ui_accept"): if not playing: + emit_signal("start") speed_y = 0 playing = true $CollisionShape2D.set_deferred("disabled", false) @@ -29,11 +36,15 @@ func _process(delta): if not playing: return - + speed_y += speed_inc position += Vector2(speed, speed_y * speed) + var global = get_global_transform().get_origin() + while global.x - x_offset > tower_locations[0]: + emit_signal("passed") + tower_locations.pop_front() rotation = atan(speed_y / speed) @@ -46,4 +57,7 @@ func _on_Player_body_entered(body): playing = false #hide() # Player disappears after being hit. emit_signal("hit") - $CollisionShape2D.set_deferred("disabled", true) \ No newline at end of file + $CollisionShape2D.set_deferred("disabled", true) + +func _on_Tower_spawn(x): + tower_locations.append(x) diff --git a/Player.tscn b/Player.tscn index de0f917..5d3df87 100644 --- a/Player.tscn +++ b/Player.tscn @@ -3,11 +3,11 @@ [ext_resource path="res://Player.gd" type="Script" id=1] [ext_resource path="res://res/sprites.png" type="Texture" id=2] -[sub_resource type="AtlasTexture" id=2] +[sub_resource type="AtlasTexture" id=1] atlas = ExtResource( 2 ) region = Rect2( -4, 488, 84, 16 ) -[sub_resource type="Animation" id=3] +[sub_resource type="Animation" id=2] length = 0.8 loop = true step = 0.2 @@ -24,12 +24,13 @@ tracks/0/keys = { "values": [ 0, 1, 2, 1 ] } -[sub_resource type="CapsuleShape2D" id=1] +[sub_resource type="CapsuleShape2D" id=3] radius = 12.8837 height = 4.07484 [node name="Area2D" type="Area2D"] position = Vector2( 9.25964, 6.09653 ) +scale = Vector2( 1, 1.02134 ) script = ExtResource( 1 ) __meta__ = { "_edit_group_": true @@ -40,18 +41,17 @@ __meta__ = { [node name="Sprite" type="Sprite" parent="Node2D"] position = Vector2( -3.46641, -2.14102 ) scale = Vector2( 2.14565, 2.14019 ) -texture = SubResource( 2 ) +texture = SubResource( 1 ) hframes = 3 -frame = 1 [node name="AnimationPlayer" type="AnimationPlayer" parent="Node2D"] root_node = NodePath("../..") autoplay = "fly" -anims/fly = SubResource( 3 ) +anims/fly = SubResource( 2 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] rotation = 1.5708 -shape = SubResource( 1 ) +shape = SubResource( 3 ) [node name="Camera2D" type="Camera2D" parent="."] current = true diff --git a/Tower.gd b/Tower.gd index e66a5d5..3f41ef7 100644 --- a/Tower.gd +++ b/Tower.gd @@ -1,17 +1,14 @@ extends Node2D signal exit -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" +signal passed # Called when the node enters the scene tree for the first time. func _ready(): pass # Replace with function body. func _on_Tower_body_entered(body): - print("HIT HERE") - pass # Replace with function body. + emit_signal("hit") func _on_Visibility_screen_exited(): queue_free() diff --git a/UI.gd b/UI.gd new file mode 100644 index 0000000..afd5813 --- /dev/null +++ b/UI.gd @@ -0,0 +1,36 @@ +extends CanvasLayer + +var best = 0 +var current = 0 + +var best_score; +var current_score; +var ready_text; + +# Called when the node enters the scene tree for the first time. +func _ready(): + best_score = get_node("MarginContainer/HBoxContainer/BestScore") + current_score = get_node("MarginContainer/HBoxContainer/CurrentScore") + ready_text = get_node("CenterContainer/ReadyText") + +func _on_Player_start(): + current = 0 + ready_text.hide() + update_ui() + +func _on_Player_hit(): + ready_text.show() + best = max(best, current) + update_ui() + +func _on_Player_passed(): + current += 1 + update_ui() + +func update_ui(): + best_score.text = "Best: "+str(best) + current_score.text = "Current: "+str(current) + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass diff --git a/UI.tscn b/UI.tscn new file mode 100644 index 0000000..9575154 --- /dev/null +++ b/UI.tscn @@ -0,0 +1,66 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://UI.gd" type="Script" id=1] + +[sub_resource type="StreamTexture" id=1] +load_path = "res://.import/sprites.png-7e797c86c60ebf1baad54990ba20f2d3.stex" + +[sub_resource type="AtlasTexture" id=2] +atlas = SubResource( 1 ) +region = Rect2( 329, 59, 58, 25 ) +margin = Rect2( -29, -12, 0, 0 ) + +[node name="UI" type="CanvasLayer"] +script = ExtResource( 1 ) + +[node name="MarginContainer" type="MarginContainer" parent="."] +margin_top = 600.0 +margin_right = 420.0 +margin_bottom = 640.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer"] +margin_right = 420.0 +margin_bottom = 40.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 +alignment = 1 + +[node name="BestScore" type="Label" parent="MarginContainer/HBoxContainer"] +margin_left = 28.0 +margin_top = 13.0 +margin_right = 208.0 +margin_bottom = 27.0 +rect_min_size = Vector2( 180, 0 ) +text = "Best: 0" + +[node name="CurrentScore" type="Label" parent="MarginContainer/HBoxContainer"] +margin_left = 212.0 +margin_top = 13.0 +margin_right = 392.0 +margin_bottom = 27.0 +rect_min_size = Vector2( 180, 0 ) +text = "Current: 0" +align = 2 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="CenterContainer" type="CenterContainer" parent="."] +margin_right = 420.0 +margin_bottom = 640.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="ReadyText" type="TextureRect" parent="CenterContainer"] +margin_left = 181.0 +margin_top = 307.0 +margin_right = 239.0 +margin_bottom = 332.0 +grow_horizontal = 2 +grow_vertical = 2 +rect_pivot_offset = Vector2( 0.5, 0.5 ) +texture = SubResource( 2 ) diff --git a/project.godot b/project.godot index 85d25c1..93d171f 100644 --- a/project.godot +++ b/project.godot @@ -23,8 +23,6 @@ config/icon="res://icon.png" window/size/width=420 window/size/height=640 -window/handheld/orientation="portrait" -window/stretch/mode="viewport" [physics]