diff --git a/hit_box.gd b/hit_box.gd new file mode 100644 index 0000000..72d9b42 --- /dev/null +++ b/hit_box.gd @@ -0,0 +1,9 @@ +extends Area2D + +@onready var hit_sound: AudioStreamPlayer2D = $HitSound +signal killed() + +func _on_body_entered(_body: Node2D) -> void: + hit_sound.play() + killed.emit() + diff --git a/hit_box.gd.uid b/hit_box.gd.uid new file mode 100644 index 0000000..a8998b9 --- /dev/null +++ b/hit_box.gd.uid @@ -0,0 +1 @@ +uid://b3mc3t5k3yr7y diff --git a/scenes/game.tscn b/scenes/game.tscn index f8b75d4..a893770 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -498,3 +498,6 @@ horizontal_alignment = 1 [node name="Slime" parent="Enemies" instance=ExtResource("6_p57ef")] position = Vector2(569, 97) + +[node name="Slime2" parent="Enemies" instance=ExtResource("6_p57ef")] +position = Vector2(633, 97) diff --git a/scenes/hit_box.tscn b/scenes/hit_box.tscn new file mode 100644 index 0000000..b37e86f --- /dev/null +++ b/scenes/hit_box.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://bmv6eq8rt7fsb"] + +[ext_resource type="Script" uid="uid://b3mc3t5k3yr7y" path="res://hit_box.gd" id="1_o631b"] +[ext_resource type="AudioStream" uid="uid://bhayvfhydadqy" path="res://assets/sounds/tap.wav" id="2_d7xod"] + +[node name="HitBox" type="Area2D"] +collision_mask = 2 +script = ExtResource("1_o631b") + +[node name="HitSound" type="AudioStreamPlayer2D" parent="."] +stream = ExtResource("2_d7xod") +bus = &"SFX" + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/scenes/slime.tscn b/scenes/slime.tscn index c15fbec..97f46d7 100644 --- a/scenes/slime.tscn +++ b/scenes/slime.tscn @@ -1,8 +1,12 @@ -[gd_scene load_steps=10 format=3 uid="uid://bvw0afyajdmht"] +[gd_scene load_steps=15 format=3 uid="uid://bvw0afyajdmht"] [ext_resource type="Script" uid="uid://cxpc8xcf3r6rg" path="res://scripts/slime.gd" id="1_n6pvg"] [ext_resource type="Texture2D" uid="uid://do7enjpf3pftb" path="res://assets/sprites/slime_green.png" id="1_p2gj0"] [ext_resource type="PackedScene" uid="uid://dpox5fa2pojpo" path="res://scenes/killzone.tscn" id="2_n6pvg"] +[ext_resource type="PackedScene" uid="uid://bmv6eq8rt7fsb" path="res://scenes/hit_box.tscn" id="4_pjw23"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_pjw23"] +radius = 5.0 [sub_resource type="AtlasTexture" id="AtlasTexture_pjw23"] atlas = ExtResource("1_p2gj0") @@ -41,11 +45,28 @@ animations = [{ }] [sub_resource type="RectangleShape2D" id="RectangleShape2D_0l8pv"] -size = Vector2(10, 13) +size = Vector2(10, 9) -[node name="Slime" type="Node2D"] +[sub_resource type="RectangleShape2D" id="RectangleShape2D_2npkn"] +size = Vector2(8, 3) + +[sub_resource type="Animation" id="Animation_pjw23"] +resource_name = "die" + +[sub_resource type="AnimationLibrary" id="AnimationLibrary_2npkn"] +_data = { +&"die": SubResource("Animation_pjw23") +} + +[node name="Slime" type="CharacterBody2D"] +floor_stop_on_slope = false +floor_snap_length = 5.0 script = ExtResource("1_n6pvg") +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(0, -6) +shape = SubResource("CircleShape2D_pjw23") + [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] position = Vector2(0, -12) sprite_frames = SubResource("SpriteFrames_v5wyi") @@ -55,9 +76,15 @@ frame_progress = 0.45857617 [node name="Killzone" parent="." instance=ExtResource("2_n6pvg")] [node name="CollisionShape2D" type="CollisionShape2D" parent="Killzone"] -position = Vector2(0, -6.5) +position = Vector2(0, -4.5) shape = SubResource("RectangleShape2D_0l8pv") +[node name="HitBox" parent="." instance=ExtResource("4_pjw23")] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="HitBox"] +position = Vector2(0, -11.5) +shape = SubResource("RectangleShape2D_2npkn") + [node name="RayCastRight" type="RayCast2D" parent="."] position = Vector2(0, -6) target_position = Vector2(8, 0) @@ -65,3 +92,10 @@ target_position = Vector2(8, 0) [node name="RayCastLeft" type="RayCast2D" parent="."] position = Vector2(0, -6) target_position = Vector2(-9, 0) + +[node name="AnimationPlayer" type="AnimationPlayer" parent="."] +libraries = { +&"": SubResource("AnimationLibrary_2npkn") +} + +[connection signal="killed" from="HitBox" to="." method="_on_hit_box_killed"] diff --git a/scripts/NodeTypes.gd b/scripts/NodeTypes.gd index 3d575b3..bdc6408 100644 --- a/scripts/NodeTypes.gd +++ b/scripts/NodeTypes.gd @@ -2,3 +2,4 @@ extends Node2D const COLLISION_SHAPE_2D = "CollisionShape2D" const CHARACTER_BODY_2D = "CharacterBody2D" +const ANIMATED_SPRITE_2D = "AnimatedSprite2D" diff --git a/scripts/slime.gd b/scripts/slime.gd index 66ac133..90e85ba 100644 --- a/scripts/slime.gd +++ b/scripts/slime.gd @@ -1,4 +1,4 @@ -extends Node2D +extends CharacterBody2D @export var speed = 60 @@ -9,7 +9,11 @@ var direction = 1 @onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D # Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: +func _physics_process(delta: float) -> void: + # Apply gravity + if not is_on_floor(): + velocity += get_gravity() * delta + if ray_cast_right.is_colliding(): direction = -1 animated_sprite.flip_h = true @@ -19,3 +23,9 @@ func _process(delta: float) -> void: animated_sprite.flip_h = false position.x += direction * speed * delta + + move_and_slide() + + +func _on_hit_box_killed() -> void: + queue_free()