add boss enemy and refactor

This commit is contained in:
2025-10-22 16:42:26 +01:00
parent bfefcffb1f
commit c7933ca7b8
11 changed files with 501 additions and 24 deletions

354
scenes/boss.tscn Normal file
View File

@@ -0,0 +1,354 @@
[gd_scene load_steps=23 format=3 uid="uid://yawu757h8byd"]
[ext_resource type="Texture2D" uid="uid://olo0olybw7k2" path="res://assets/sprites/slime_purple.png" id="1_jncid"]
[ext_resource type="Script" uid="uid://bamgalx75xtf2" path="res://scripts/boss.gd" id="1_qnbj4"]
[ext_resource type="PackedScene" uid="uid://jnqii6cg0cy" path="res://scenes/hit_box.tscn" id="2_qnbj4"]
[ext_resource type="PackedScene" uid="uid://dpox5fa2pojpo" path="res://scenes/killzone.tscn" id="3_r3bxh"]
[ext_resource type="AudioStream" uid="uid://bhayvfhydadqy" path="res://assets/sounds/tap.wav" id="5_r3bxh"]
[ext_resource type="AudioStream" uid="uid://cm3cxtbk134ux" path="res://assets/sounds/explosion.wav" id="6_wqpjq"]
[sub_resource type="AtlasTexture" id="AtlasTexture_wqpjq"]
atlas = ExtResource("1_jncid")
region = Rect2(0, 24, 24, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_2cdgk"]
atlas = ExtResource("1_jncid")
region = Rect2(24, 24, 24, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_qsl3t"]
atlas = ExtResource("1_jncid")
region = Rect2(48, 24, 24, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_2avx2"]
atlas = ExtResource("1_jncid")
region = Rect2(72, 24, 24, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_qnbj4"]
atlas = ExtResource("1_jncid")
region = Rect2(72, 0, 24, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_r3bxh"]
atlas = ExtResource("1_jncid")
region = Rect2(48, 0, 24, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_5f346"]
atlas = ExtResource("1_jncid")
region = Rect2(24, 0, 24, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_0onds"]
atlas = ExtResource("1_jncid")
region = Rect2(0, 0, 24, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_lfs1x"]
atlas = ExtResource("1_jncid")
region = Rect2(48, 48, 24, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_8eiau"]
atlas = ExtResource("1_jncid")
region = Rect2(72, 48, 24, 24)
[sub_resource type="SpriteFrames" id="SpriteFrames_5f346"]
animations = [{
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_wqpjq")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_2cdgk")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_qsl3t")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_2avx2")
}],
"loop": true,
"name": &"default",
"speed": 10.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_qnbj4")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_r3bxh")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_5f346")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_0onds")
}],
"loop": false,
"name": &"die",
"speed": 10.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_lfs1x")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_8eiau")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_lfs1x")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_8eiau")
}],
"loop": false,
"name": &"hurt",
"speed": 10.0
}]
[sub_resource type="CircleShape2D" id="CircleShape2D_0onds"]
radius = 7.071068
[sub_resource type="RectangleShape2D" id="RectangleShape2D_lfs1x"]
size = Vector2(15, 2)
[sub_resource type="Animation" id="Animation_qnbj4"]
resource_name = "die"
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("CollisionShape2D:disabled")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("HitBox/CollisionShape2D:disabled")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Killzone/CollisionPolygon2D:disabled")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("RayCastRight:enabled")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
tracks/4/type = "value"
tracks/4/imported = false
tracks/4/enabled = true
tracks/4/path = NodePath("RayCastLeft:enabled")
tracks/4/interp = 1
tracks/4/loop_wrap = true
tracks/4/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
tracks/5/type = "bezier"
tracks/5/imported = false
tracks/5/enabled = true
tracks/5/path = NodePath("AnimatedSprite2D:rotation")
tracks/5/interp = 1
tracks/5/loop_wrap = true
tracks/5/keys = {
"handle_modes": PackedInt32Array(0, 2),
"points": PackedFloat32Array(0, -0.25, 0, 0.25, 0, -1.0192723, -0.083333336, 0.16987872, 0, 0),
"times": PackedFloat32Array(0, 0.5)
}
tracks/6/type = "method"
tracks/6/imported = false
tracks/6/enabled = true
tracks/6/path = NodePath(".")
tracks/6/interp = 1
tracks/6/loop_wrap = true
tracks/6/keys = {
"times": PackedFloat32Array(0.96666664),
"transitions": PackedFloat32Array(1),
"values": [{
"args": [],
"method": &"queue_free"
}]
}
tracks/7/type = "value"
tracks/7/imported = false
tracks/7/enabled = true
tracks/7/path = NodePath("Death:playing")
tracks/7/interp = 1
tracks/7/loop_wrap = true
tracks/7/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
[sub_resource type="Animation" id="Animation_r3bxh"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("CollisionShape2D:disabled")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("HitBox/CollisionShape2D:disabled")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Killzone/CollisionPolygon2D:disabled")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
tracks/3/type = "value"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("RayCastRight:enabled")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
tracks/4/type = "value"
tracks/4/imported = false
tracks/4/enabled = true
tracks/4/path = NodePath("RayCastLeft:enabled")
tracks/4/interp = 1
tracks/4/loop_wrap = true
tracks/4/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [true]
}
tracks/5/type = "bezier"
tracks/5/imported = false
tracks/5/enabled = true
tracks/5/path = NodePath("AnimatedSprite2D:rotation")
tracks/5/interp = 1
tracks/5/loop_wrap = true
tracks/5/keys = {
"handle_modes": PackedInt32Array(0),
"points": PackedFloat32Array(0, -0.25, 0, 0.25, 0),
"times": PackedFloat32Array(0)
}
tracks/6/type = "value"
tracks/6/imported = false
tracks/6/enabled = true
tracks/6/path = NodePath("Death:playing")
tracks/6/interp = 1
tracks/6/loop_wrap = true
tracks/6/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_wqpjq"]
_data = {
&"RESET": SubResource("Animation_r3bxh"),
&"die": SubResource("Animation_qnbj4")
}
[node name="Boss" type="CharacterBody2D"]
script = ExtResource("1_qnbj4")
speed = 100
hp = 4
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
position = Vector2(0, -8.258226)
scale = Vector2(1.4583334, 1.4080526)
sprite_frames = SubResource("SpriteFrames_5f346")
autoplay = "default"
offset = Vector2(0, -6.135)
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, -7)
shape = SubResource("CircleShape2D_0onds")
[node name="HitBox" parent="." instance=ExtResource("2_qnbj4")]
collision_mask = 2
player_bounce = -200
[node name="CollisionShape2D" type="CollisionShape2D" parent="HitBox"]
position = Vector2(0, -18)
shape = SubResource("RectangleShape2D_lfs1x")
[node name="Killzone" parent="." instance=ExtResource("3_r3bxh")]
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Killzone"]
position = Vector2(0, -10)
polygon = PackedVector2Array(-10, 10, 10, 10, 10, 4, 7, -3, -7, -3, -10, 5)
[node name="RayCastRight" type="RayCast2D" parent="."]
position = Vector2(0, -7)
target_position = Vector2(12, 0)
[node name="RayCastLeft" type="RayCast2D" parent="."]
position = Vector2(0, -7)
target_position = Vector2(-12, 0)
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
&"": SubResource("AnimationLibrary_wqpjq")
}
[node name="Hurt" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource("5_r3bxh")
bus = &"SFX"
[node name="Death" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource("6_wqpjq")
bus = &"SFX"
[connection signal="animation_finished" from="AnimatedSprite2D" to="." method="_on_animated_sprite_2d_animation_finished"]
[connection signal="hit" from="HitBox" to="." method="_on_hit_box_hit"]

File diff suppressed because one or more lines are too long

View File

@@ -1,13 +1,17 @@
[gd_scene load_steps=3 format=3 uid="uid://xacwq0rohthg"]
[gd_scene load_steps=4 format=3 uid="uid://xacwq0rohthg"]
[ext_resource type="Texture2D" uid="uid://b04teamu7o6jb" path="res://assets/sprites/platforms.png" id="1_ba0x2"]
[ext_resource type="Script" uid="uid://ymdnbe621mfv" path="res://scripts/platform.gd" id="1_nlgqw"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_nlgqw"]
size = Vector2(32, 8)
[node name="Platform" type="AnimatableBody2D"]
script = ExtResource("1_nlgqw")
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(0, -0.25)
scale = Vector2(1, 0.9444444)
texture = ExtResource("1_ba0x2")
region_enabled = true
region_rect = Rect2(16, 0, 32, 9)
@@ -15,3 +19,5 @@ region_rect = Rect2(16, 0, 32, 9)
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_nlgqw")
one_way_collision = true
[connection signal="ready" from="." to="." method="_on_ready"]

View File

@@ -265,22 +265,18 @@ animation = &"die"
autoplay = "default"
offset = Vector2(0, -3.285)
[node name="HitBox" parent="." node_paths=PackedStringArray("animation_player", "animated_sprite") instance=ExtResource("4_pjw23")]
[node name="HitBox" parent="." instance=ExtResource("4_pjw23")]
collision_mask = 2
animation_player = NodePath("../AnimationPlayer")
animation_name = "die"
animated_sprite = NodePath("../AnimatedSprite2D")
animated_sprite_name = "die"
player_bounce = -200
[node name="CollisionShape2D" type="CollisionShape2D" parent="HitBox"]
position = Vector2(0, -11)
position = Vector2(0, -12)
shape = SubResource("RectangleShape2D_pjw23")
[node name="Killzone" parent="." instance=ExtResource("2_n6pvg")]
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Killzone"]
polygon = PackedVector2Array(-7, 0, 7, 0, 7, -4, 5, -9, -5, -9, -7, -4)
polygon = PackedVector2Array(-7, 0, 7, 0, 7, -4, 5, -8, -5, -8, -7, -4)
[node name="RayCastRight" type="RayCast2D" parent="."]
position = Vector2(0, -6)
@@ -300,3 +296,5 @@ libraries = {
[node name="Death" type="AudioStreamPlayer2D" parent="Sounds"]
stream = ExtResource("5_2npkn")
bus = &"SFX"
[connection signal="hit" from="HitBox" to="." method="_on_hit_box_hit"]

45
scripts/boss.gd Normal file
View File

@@ -0,0 +1,45 @@
extends CharacterBody2D
@export var speed = 60
@export var hp = 2
var direction = 1
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var hurt_sound: AudioStreamPlayer2D = $Hurt
# Called every frame. 'delta' is the elapsed time since the previous frame.
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
if ray_cast_left.is_colliding():
direction = 1
animated_sprite.flip_h = false
position.x += direction * speed * delta
move_and_slide()
func _on_hit_box_hit() -> void:
hp -= 1
if hp <= 0:
animated_sprite.play("die")
animation_player.play("die")
else:
hurt_sound.play()
animated_sprite.play("hurt")
func _on_animated_sprite_2d_animation_finished() -> void:
animated_sprite.play("default")

1
scripts/boss.gd.uid Normal file
View File

@@ -0,0 +1 @@
uid://bamgalx75xtf2

View File

@@ -1,16 +1,11 @@
extends Area2D
@export var animation_player: AnimationPlayer
@export var animation_name: String
@export var animated_sprite: AnimatedSprite2D
@export var animated_sprite_name: String
@export var player_bounce: int
signal hit
func _on_body_entered(player_body: Node2D) -> void:
var player_character_body = player_body as CharacterBody2D
player_character_body.velocity.y = player_bounce
hit.emit()
animated_sprite.play(animated_sprite_name)
animation_player.play(animation_name)

View File

@@ -4,7 +4,6 @@ extends Area2D
@onready var death: AudioStreamPlayer2D = $Death
func _on_body_entered(player_body: Node2D) -> void:
print("You died.")
death.play()
Engine.time_scale = 0.5
player_body.get_node(NodeTypes.COLLISION_SHAPE_2D).queue_free()

13
scripts/platform.gd Normal file
View File

@@ -0,0 +1,13 @@
extends Node2D
@export_enum("Green", "Yellow") var colour: String
@onready var sprite_2d: Sprite2D = $Sprite2D
const REGION_DICT: Dictionary[String, Vector2] = {
"Green": Vector2(16, 0),
"Yellow": Vector2(16, 32)
}
func _on_ready() -> void:
print(colour)
sprite_2d.region_rect.position = REGION_DICT[colour]

1
scripts/platform.gd.uid Normal file
View File

@@ -0,0 +1 @@
uid://ymdnbe621mfv

View File

@@ -1,12 +1,14 @@
extends CharacterBody2D
@export var speed = 60
@export var hp = 1
var direction = 1
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var animation_player: AnimationPlayer = $AnimationPlayer
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
@@ -25,3 +27,10 @@ func _physics_process(delta: float) -> void:
position.x += direction * speed * delta
move_and_slide()
func _on_hit_box_hit() -> void:
hp -= 1
if hp <= 0:
animated_sprite.play("die")
animation_player.play("die")