From a15966a36150f21f64f3b5da4db94aa9b499512a Mon Sep 17 00:00:00 2001 From: Kilokem Date: Mon, 28 Oct 2024 10:25:07 +0100 Subject: [PATCH] Health and killing zones --- scenes/Game/Background/parallax_background.gd | 12 ------------ scenes/Game/Background/static_body_2d.gd | 11 ----------- scenes/Game/Enemy/enemy.gd | 3 ++- scenes/Game/Enemy/enemy.tscn | 2 +- scenes/Game/Player/player.gd | 14 +++++++++++++- scenes/Game/Player/player.tscn | 18 +++++++++++++++++- scenes/Game/floor.gd | 11 ----------- scenes/Game/world.tscn | 16 +++++++++------- scenes/Main/main.gd | 2 -- 9 files changed, 42 insertions(+), 47 deletions(-) delete mode 100644 scenes/Game/Background/parallax_background.gd delete mode 100644 scenes/Game/Background/static_body_2d.gd delete mode 100644 scenes/Game/floor.gd diff --git a/scenes/Game/Background/parallax_background.gd b/scenes/Game/Background/parallax_background.gd deleted file mode 100644 index f8075d0..0000000 --- a/scenes/Game/Background/parallax_background.gd +++ /dev/null @@ -1,12 +0,0 @@ -extends ParallaxBackground - -const scrolling_seed = 100 - -# Called when the node enters the scene tree for the first time. -func _ready() -> void: - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta: float) -> void: - #scroll_offset.x -= scrolling_seed * delta diff --git a/scenes/Game/Background/static_body_2d.gd b/scenes/Game/Background/static_body_2d.gd deleted file mode 100644 index 05c52a7..0000000 --- a/scenes/Game/Background/static_body_2d.gd +++ /dev/null @@ -1,11 +0,0 @@ -extends StaticBody2D - - -# Called when the node enters the scene tree for the first time. -func _ready() -> void: - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: - pass diff --git a/scenes/Game/Enemy/enemy.gd b/scenes/Game/Enemy/enemy.gd index 4c857ee..7cba5b1 100644 --- a/scenes/Game/Enemy/enemy.gd +++ b/scenes/Game/Enemy/enemy.gd @@ -35,9 +35,10 @@ func _on_player_detection_body_exited(body: Node2D) -> void: func _on_killingzone_body_entered(body: Node2D) -> void: if body.name == "Player": - if player.global_position.y < self.global_position.y - 10 : + if player.global_position.y < self.global_position.y - 20 : get_node("AnimatedSprite2D").play("death") alive = false + self.collision_mask = 2 timer.start() elif alive: print("HURT") diff --git a/scenes/Game/Enemy/enemy.tscn b/scenes/Game/Enemy/enemy.tscn index 8e32d0e..eaff605 100644 --- a/scenes/Game/Enemy/enemy.tscn +++ b/scenes/Game/Enemy/enemy.tscn @@ -139,7 +139,7 @@ shape = SubResource("CapsuleShape2D_o6lk6") debug_color = Color(0.987816, 0.0454308, 0.255515, 0.42) [node name="Deathcounter" type="Timer" parent="."] -wait_time = 0.6 +wait_time = 2.0 [connection signal="body_entered" from="PlayerDetection" to="." method="_on_player_detection_body_entered"] [connection signal="body_exited" from="PlayerDetection" to="." method="_on_player_detection_body_exited"] diff --git a/scenes/Game/Player/player.gd b/scenes/Game/Player/player.gd index 1417d2a..5cead65 100644 --- a/scenes/Game/Player/player.gd +++ b/scenes/Game/Player/player.gd @@ -1,13 +1,15 @@ extends CharacterBody2D - const SPEED = 400.0 const JUMP_VELOCITY = -400.0 var jumped = false @onready var animation = get_node("AnimatedSprite2D") +@onready var healthBar: ProgressBar = $ProgressBar +var health = 100 + func _ready() -> void: animation.play("idle") @@ -47,3 +49,13 @@ func _physics_process(delta: float) -> void: #print("DEAD") move_and_slide() + + + + +func _on_enemy_hurt() -> void: + if health - 50 == 0: + get_tree().reload_current_scene() + else: + health -= 50 + healthBar.value -= 50 diff --git a/scenes/Game/Player/player.tscn b/scenes/Game/Player/player.tscn index 07d4f3c..3f53f68 100644 --- a/scenes/Game/Player/player.tscn +++ b/scenes/Game/Player/player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=28 format=3 uid="uid://cwhk7ysxws0s0"] +[gd_scene load_steps=30 format=3 uid="uid://cwhk7ysxws0s0"] [ext_resource type="Script" path="res://scenes/Game/Player/player.gd" id="1_f437e"] [ext_resource type="Texture2D" uid="uid://uobvqa86mtra" path="res://assets/Sunny Land Collection Files/Sunny Land Collection Files/Assets/Characters/Players/Squirrel/idle/spritesheet.png" id="2_r3s32"] @@ -173,6 +173,12 @@ animations = [{ radius = 8.0 height = 24.0 +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_nv1uy"] +bg_color = Color(1, 0, 0, 1) + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_qoc3s"] +bg_color = Color(0, 1, 0, 1) + [node name="Player" type="CharacterBody2D"] script = ExtResource("1_f437e") @@ -185,3 +191,13 @@ autoplay = "idle" [node name="CollisionShape2D" type="CollisionShape2D" parent="."] position = Vector2(1, -11) shape = SubResource("CapsuleShape2D_gqjua") + +[node name="ProgressBar" type="ProgressBar" parent="."] +offset_left = -14.0 +offset_top = -33.0 +offset_right = 12.0 +offset_bottom = -27.0 +theme_override_styles/background = SubResource("StyleBoxFlat_nv1uy") +theme_override_styles/fill = SubResource("StyleBoxFlat_qoc3s") +value = 100.0 +show_percentage = false diff --git a/scenes/Game/floor.gd b/scenes/Game/floor.gd deleted file mode 100644 index eebeca3..0000000 --- a/scenes/Game/floor.gd +++ /dev/null @@ -1,11 +0,0 @@ -extends CollisionShape2D - - -# Called when the node enters the scene tree for the first time. -func _ready() -> void: - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: - pass diff --git a/scenes/Game/world.tscn b/scenes/Game/world.tscn index 65bff9c..f4200a8 100644 --- a/scenes/Game/world.tscn +++ b/scenes/Game/world.tscn @@ -1,10 +1,9 @@ -[gd_scene load_steps=12 format=3 uid="uid://cxni03aowykty"] +[gd_scene load_steps=11 format=3 uid="uid://cxni03aowykty"] [ext_resource type="PackedScene" uid="uid://cwhk7ysxws0s0" path="res://scenes/Game/Player/player.tscn" id="1_ol65m"] [ext_resource type="Texture2D" uid="uid://cyn664h8v4yjq" path="res://assets/Sunny Land Collection Files/Sunny Land Collection Files/Assets/Environments/Day-Platformer/PNG/tileset.png" id="2_2shen"] [ext_resource type="PackedScene" uid="uid://5bcs7peqaotp" path="res://scenes/Game/Background/parallax_background.tscn" id="2_7th7e"] -[ext_resource type="Script" path="res://scenes/Game/Background/static_body_2d.gd" id="4_4hn3y"] -[ext_resource type="Script" path="res://scenes/Game/floor.gd" id="5_ehkb0"] +[ext_resource type="Script" path="res://scenes/Game/killinzone.gd" id="4_rly4c"] [ext_resource type="PackedScene" uid="uid://cwwrv2mwnu2q3" path="res://scenes/Game/Enemy/enemy.tscn" id="6_0idbk"] [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_650cm"] @@ -150,7 +149,6 @@ layer_0/tile_data = PackedInt32Array(1507360, 196608, 4, 1507361, 196608, 4, 150 position = Vector2(131, 104) [node name="StaticBody2D" type="StaticBody2D" parent="."] -script = ExtResource("4_4hn3y") [node name="roof" type="CollisionShape2D" parent="StaticBody2D"] position = Vector2(575, -9) @@ -166,13 +164,17 @@ position = Vector2(-9, 322.5) rotation = 1.5708 shape = SubResource("RectangleShape2D_5wtle") -[node name="floor" type="CollisionShape2D" parent="StaticBody2D"] -unique_name_in_owner = true +[node name="Area2D" type="Area2D" parent="StaticBody2D"] position = Vector2(602, 751) +script = ExtResource("4_rly4c") + +[node name="floor" type="CollisionShape2D" parent="StaticBody2D/Area2D"] shape = SubResource("RectangleShape2D_igtsr") -script = ExtResource("5_ehkb0") [node name="mobs" type="Node" parent="."] [node name="enemy" parent="mobs" instance=ExtResource("6_0idbk")] position = Vector2(439, 456) + +[connection signal="body_entered" from="StaticBody2D/Area2D" to="StaticBody2D/Area2D" method="_on_body_entered"] +[connection signal="hurt" from="mobs/enemy" to="Player" method="_on_enemy_hurt"] diff --git a/scenes/Main/main.gd b/scenes/Main/main.gd index d263d50..98f795e 100644 --- a/scenes/Main/main.gd +++ b/scenes/Main/main.gd @@ -5,8 +5,6 @@ extends Node2D func _on_quit_pressed() -> void: get_tree().quit() - - func _on_play_pressed() -> void: get_tree().change_scene_to_file("res://scenes/Game/world.tscn")