From bb9682cd166b1b6e72861d3c40b560d8eec00f0c Mon Sep 17 00:00:00 2001 From: sstone Date: Fri, 2 Oct 2026 21:10:47 -0600 Subject: [PATCH] Enemy made a basic enemy that can harm/kill the player. --- scenes/enemy.tscn | 32 ++++++++++++++++++ scenes/player.tscn | 44 ++++++++++++++++++++++++- scenes/test_scene.tscn | 6 ++-- scripts/UltimatePlatformerController.gd | 12 ++++++- scripts/enemy.gd | 26 +++++++++++++++ scripts/enemy.gd.uid | 1 + 6 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 scenes/enemy.tscn create mode 100644 scripts/enemy.gd create mode 100644 scripts/enemy.gd.uid diff --git a/scenes/enemy.tscn b/scenes/enemy.tscn new file mode 100644 index 0000000..2b37f57 --- /dev/null +++ b/scenes/enemy.tscn @@ -0,0 +1,32 @@ +[gd_scene format=3 uid="uid://bjk1cmuogswyd"] + +[ext_resource type="Script" uid="uid://2jmspsmp5wpm" path="res://scripts/enemy.gd" id="1_7p1mj"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_aqe1g"] +size = Vector2(168, 167) + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_yg5ta"] +size = Vector2(178, 168) + +[node name="enemy" type="CharacterBody2D" unique_id=1471932521] +collision_layer = 4 +collision_mask = 3 +script = ExtResource("1_7p1mj") + +[node name="ColorRect" type="ColorRect" parent="." unique_id=519837051] +offset_right = 169.0 +offset_bottom = 166.0 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1567529793] +position = Vector2(85, 82) +shape = SubResource("RectangleShape2D_aqe1g") + +[node name="Timer" type="Timer" parent="." unique_id=356300230] + +[node name="Area2D" type="Area2D" parent="." unique_id=461696644] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D" unique_id=72940614] +position = Vector2(84, 79) +shape = SubResource("RectangleShape2D_yg5ta") + +[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"] diff --git a/scenes/player.tscn b/scenes/player.tscn index e80e713..5ec1842 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -5,8 +5,11 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_d2by0"] size = Vector2(111, 105) +[sub_resource type="RectangleShape2D" id="RectangleShape2D_aqe1g"] +size = Vector2(114.5, 109) + [node name="player" type="CharacterBody2D" unique_id=1596338883] -collision_mask = 2 +collision_mask = 6 script = ExtResource("1_3vyb7") [node name="ColorRect" type="ColorRect" parent="." unique_id=42689460] @@ -18,3 +21,42 @@ offset_bottom = 105.0 [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1496788104] position = Vector2(55.5, 52.5) shape = SubResource("RectangleShape2D_d2by0") + +[node name="Area2D" type="Area2D" parent="." unique_id=680224441] +collision_mask = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D" unique_id=736937610] +position = Vector2(55.25, 52.5) +shape = SubResource("RectangleShape2D_aqe1g") + +[node name="health" type="Control" parent="." unique_id=1976293596] +layout_mode = 3 +anchors_preset = 0 +offset_right = 40.0 +offset_bottom = 40.0 + +[node name="ColorRect" type="ColorRect" parent="health" unique_id=110340136] +self_modulate = Color(1, 0, 0, 1) +layout_mode = 0 +offset_left = 3.0 +offset_top = -42.0 +offset_right = 26.0 +offset_bottom = -20.0 + +[node name="ColorRect2" type="ColorRect" parent="health" unique_id=1043497392] +self_modulate = Color(1, 0, 0, 1) +layout_mode = 0 +offset_left = 37.0 +offset_top = -42.0 +offset_right = 60.0 +offset_bottom = -20.0 + +[node name="ColorRect3" type="ColorRect" parent="health" unique_id=175994440] +self_modulate = Color(1, 0, 0, 1) +layout_mode = 0 +offset_left = 70.0 +offset_top = -42.0 +offset_right = 93.0 +offset_bottom = -20.0 + +[connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"] diff --git a/scenes/test_scene.tscn b/scenes/test_scene.tscn index fba1ac0..e639568 100644 --- a/scenes/test_scene.tscn +++ b/scenes/test_scene.tscn @@ -1,7 +1,7 @@ [gd_scene format=3 uid="uid://d1xirc54wud0s"] [ext_resource type="PackedScene" uid="uid://cmtdq6tedv1mm" path="res://scenes/player.tscn" id="1_g7g4h"] -[ext_resource type="Script" uid="uid://87oh2y0i2muf" path="res://scenes/rigid_body_2d.gd" id="2_nd71p"] +[ext_resource type="PackedScene" uid="uid://bjk1cmuogswyd" path="res://scenes/enemy.tscn" id="2_j8ivh"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_aqe1g"] size = Vector2(1116.25, 73) @@ -19,8 +19,10 @@ position = Vector2(64, 386) [node name="floor" type="StaticBody2D" parent="." unique_id=866269158] collision_layer = 2 -script = ExtResource("2_nd71p") [node name="CollisionShape2D" type="CollisionShape2D" parent="floor" unique_id=1146724072] position = Vector2(561.125, 569) shape = SubResource("RectangleShape2D_aqe1g") + +[node name="enemy" parent="floor" unique_id=1471932521 instance=ExtResource("2_j8ivh")] +position = Vector2(527, 311) diff --git a/scripts/UltimatePlatformerController.gd b/scripts/UltimatePlatformerController.gd index 3fff838..7aba111 100644 --- a/scripts/UltimatePlatformerController.gd +++ b/scripts/UltimatePlatformerController.gd @@ -138,7 +138,7 @@ var gravityActive: bool = true var dashing: bool = false var dashCount: int var rolling: bool = false - +var death_counter = 3 var twoWayDashHorizontal var twoWayDashVertical var eightWayDash @@ -254,6 +254,8 @@ func _updateData(): func _process(_delta): #INFO animations #directions + if death_counter == 0: + get_tree().quit() if is_on_wall() and !is_on_floor() and latch and wallLatching and ((wallLatchingModifer and latchHold) or !wallLatchingModifer): latched = true else: @@ -325,6 +327,10 @@ func _process(_delta): func _physics_process(delta): + if death_counter == 2: + $health/ColorRect3.hide() + elif death_counter == 1: + $health/ColorRect2.hide() if !dset: gdelta = delta dset = true @@ -674,3 +680,7 @@ func _endGroundPound(): func _placeHolder(): print("") + + +func _on_area_2d_body_entered(body: Node2D) -> void: + death_counter -= 1 diff --git a/scripts/enemy.gd b/scripts/enemy.gd new file mode 100644 index 0000000..827fab6 --- /dev/null +++ b/scripts/enemy.gd @@ -0,0 +1,26 @@ +extends CharacterBody2D + + +var SPEED = Vector2(5, 0) +const JUMP_VELOCITY = -400.0 + + + + +func _ready() -> void: + $Timer.start() + + +func _physics_process(delta: float) -> void: + velocity += get_gravity() * delta + move_and_slide() + patrol() + + +func patrol(): + position += SPEED + + +func _on_timer_timeout() -> void: + SPEED = -SPEED + $Timer.start() diff --git a/scripts/enemy.gd.uid b/scripts/enemy.gd.uid new file mode 100644 index 0000000..61e0721 --- /dev/null +++ b/scripts/enemy.gd.uid @@ -0,0 +1 @@ +uid://2jmspsmp5wpm -- 2.54.0