diff options
| -rw-r--r-- | HUD.tscn | 7 | ||||
| -rw-r--r-- | HealthBar.gd | 18 | ||||
| -rw-r--r-- | KinematicBody2D.gd | 15 | ||||
| -rw-r--r-- | Main.gd | 1 | ||||
| -rw-r--r-- | Node2D.tscn | 6 | ||||
| -rw-r--r-- | Player.tscn | 5 |
6 files changed, 41 insertions, 11 deletions
@@ -1,4 +1,6 @@ -[gd_scene format=2] +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://HealthBar.gd" type="Script" id=1] [node name="HUD" type="CanvasLayer"] @@ -16,13 +18,14 @@ margin_bottom = 14.0 text = "Health:" align = 2 -[node name="ProgressBar" type="ProgressBar" parent="HBoxContainer"] +[node name="HealthBar" type="ProgressBar" parent="HBoxContainer"] margin_left = 56.0 margin_right = 1205.0 margin_bottom = 14.0 size_flags_horizontal = 3 size_flags_vertical = 3 value = 100.0 +script = ExtResource( 1 ) [node name="Label2" type="Label" parent="HBoxContainer"] margin_left = 1215.0 diff --git a/HealthBar.gd b/HealthBar.gd new file mode 100644 index 0000000..ae4ce42 --- /dev/null +++ b/HealthBar.gd @@ -0,0 +1,18 @@ +extends ProgressBar + + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass +func _on_player_hit(delta): + value -= delta * 20 diff --git a/KinematicBody2D.gd b/KinematicBody2D.gd index 3be3afc..4f2949c 100644 --- a/KinematicBody2D.gd +++ b/KinematicBody2D.gd @@ -1,5 +1,7 @@ -extends KinematicBody2D +extends Area2D +signal hit +var screen_size export (int) var speed = 200 var spawn_object = load("res://Bullet.tscn") var velocity = Vector2() @@ -17,11 +19,20 @@ func get_input(): velocity.y -= 1 velocity = velocity.normalized() * speed +func _ready(): + screen_size = get_viewport_rect().size + func _physics_process(delta): get_input() globals.playerPos = global_position - velocity = move_and_slide(velocity) + position += velocity * delta + position.x = clamp(position.x, 0, screen_size.x) + position.y = clamp(position.y, 0, screen_size.y) if Input.is_action_just_pressed("click"): var obj = spawn_object.instance() obj.position = get_position() get_owner().add_child(obj) + var bodies = get_overlapping_bodies() + for body in bodies: + if body.get_name() != "Bullet": + emit_signal("hit", delta) @@ -9,6 +9,7 @@ onready var globals = get_node("/root/Global") # Called when the node enters the scene tree for the first time. func _ready(): randomize() + get_node("KinematicBody2D").connect( "hit", get_node("HUD/HBoxContainer/HealthBar"), "_on_player_hit") $EnemySpawnTimer.start() diff --git a/Node2D.tscn b/Node2D.tscn index a471e7e..d6a1149 100644 --- a/Node2D.tscn +++ b/Node2D.tscn @@ -1,9 +1,8 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://HUD.tscn" type="PackedScene" id=1] [ext_resource path="res://Player.tscn" type="PackedScene" id=2] [ext_resource path="res://Main.gd" type="Script" id=3] -[ext_resource path="res://Bullet.tscn" type="PackedScene" id=4] [ext_resource path="res://Enemy0.tscn" type="PackedScene" id=5] [sub_resource type="Curve2D" id=1] @@ -15,9 +14,6 @@ _data = { script = ExtResource( 3 ) enemy_scene = ExtResource( 5 ) -[node name="Bullet" parent="." instance=ExtResource( 4 )] -position = Vector2( 548, 225 ) - [node name="KinematicBody2D" parent="." instance=ExtResource( 2 )] position = Vector2( 544, 216 ) diff --git a/Player.tscn b/Player.tscn index e8cef48..38589cc 100644 --- a/Player.tscn +++ b/Player.tscn @@ -5,8 +5,7 @@ [sub_resource type="CircleShape2D" id=1] -[node name="KinematicBody2D" type="KinematicBody2D"] -input_pickable = true +[node name="Area2D" type="Area2D"] script = ExtResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] @@ -16,3 +15,5 @@ shape = SubResource( 1 ) position = Vector2( -1, -4 ) scale = Vector2( 0.515625, 0.53125 ) texture = ExtResource( 2 ) + +[connection signal="body_entered" from="." to="." method="_on_Player_body_entered"] |
