From e3d0dfb6740fa418346dea531ba053ca5575f501 Mon Sep 17 00:00:00 2001 From: Ruslan Ignatov Date: Thu, 8 Jan 2026 00:04:41 +0300 Subject: [PATCH] Added door closing. Fixed death in the air --- game/characters/player.gd | 36 +++++++++++++++++--------- game/doors/book_door.tscn | 29 +++++++++++++++++++-- game/doors/circle_door.tscn | 28 +++++++++++++++++++- game/doors/closed_door.gd | 9 +++++++ game/doors/closed_door.tscn | 29 +++++++++++++++++++-- game/doors/door.gd | 48 ++++++++++++++++++++++++++++++++--- game/doors/door.tscn | 31 ++++++++++++++++++++-- game/doors/square_door.tscn | 28 +++++++++++++++++++- game/doors/triangle_door.tscn | 28 +++++++++++++++++++- project.godot | 2 ++ 10 files changed, 244 insertions(+), 24 deletions(-) diff --git a/game/characters/player.gd b/game/characters/player.gd index ed86d00..c5f2702 100644 --- a/game/characters/player.gd +++ b/game/characters/player.gd @@ -28,6 +28,9 @@ const PICKUP_OFFSET = 16.0 @export_range(0.0, 10.0) var switch_time := 1.0 +var _is_alive := true + + @onready var sprite : AnimatedSprite2D = $AnimatedSprite2D @onready var collision_switcher : CollisionSwitcher = $CollisionSwitcher @onready var pickups : Node2D = $Pickups @@ -40,25 +43,30 @@ func _ready() -> void: func _physics_process(delta: float) -> void: if not is_on_floor(): velocity += get_gravity() * delta - - if Input.is_action_just_pressed("jump") and is_on_floor(): - velocity.y = -jump_velocity - - var direction := Input.get_axis("move_left", "move_right") - if direction: - velocity.x = move_toward(velocity.x, direction * max_speed, acceleration * delta) - else: - velocity.x = move_toward(velocity.x, 0, acceleration * delta) - _update_animation() + if not _is_alive: + _slow_down(delta) + else: + if Input.is_action_just_pressed("jump") and is_on_floor(): + velocity.y = -jump_velocity + + var direction := Input.get_axis("move_left", "move_right") + if direction: + velocity.x = move_toward(velocity.x, direction * max_speed, acceleration * delta) + else: + _slow_down(delta) + + _update_animation() var was_collided := move_and_slide() - if was_collided: + if was_collided and _is_alive: for i in range(get_slide_collision_count()): var collision := get_slide_collision(i) if collision.get_collider() is AbstractEnemy: + _is_alive = false + collision_mask = 1 get_tree().paused = true - sprite.process_mode = Node.PROCESS_MODE_ALWAYS + process_mode = Node.PROCESS_MODE_ALWAYS sprite.play(ANIMATION_DEATH) @@ -79,6 +87,10 @@ func remove_pickup(pickup: AbstractPickup) -> void: _rearrange_pickups() +func _slow_down(delta: float) -> void: + velocity.x = move_toward(velocity.x, 0, acceleration * delta) + + func _update_animation() -> void: var animation := _get_animation() if sprite.animation != animation: diff --git a/game/doors/book_door.tscn b/game/doors/book_door.tscn index 4294b07..74fd6f0 100644 --- a/game/doors/book_door.tscn +++ b/game/doors/book_door.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://dioc4r03dfleq"] +[gd_scene load_steps=13 format=3 uid="uid://dioc4r03dfleq"] [ext_resource type="PackedScene" uid="uid://cojxqx5al7kyv" path="res://game/doors/closed_door.tscn" id="1_p87ci"] [ext_resource type="Script" uid="uid://befuova1g4tth" path="res://game/doors/book_door.gd" id="2_atb6l"] @@ -8,6 +8,18 @@ atlas = ExtResource("3_gbgmk") region = Rect2(0, 240, 32, 48) +[sub_resource type="AtlasTexture" id="AtlasTexture_gbgmk"] +atlas = ExtResource("3_gbgmk") +region = Rect2(64, 240, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_3d7ts"] +atlas = ExtResource("3_gbgmk") +region = Rect2(32, 240, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_u0yk6"] +atlas = ExtResource("3_gbgmk") +region = Rect2(0, 240, 32, 48) + [sub_resource type="AtlasTexture" id="AtlasTexture_g21nq"] atlas = ExtResource("3_gbgmk") region = Rect2(64, 240, 32, 48) @@ -36,6 +48,20 @@ animations = [{ }, { "frames": [{ "duration": 1.0, +"texture": SubResource("AtlasTexture_gbgmk") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_3d7ts") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_u0yk6") +}], +"loop": false, +"name": &"closing", +"speed": 10.0 +}, { +"frames": [{ +"duration": 1.0, "texture": SubResource("AtlasTexture_g21nq") }], "loop": true, @@ -62,4 +88,3 @@ script = ExtResource("2_atb6l") [node name="AnimatedSprite2D" parent="." index="2"] sprite_frames = SubResource("SpriteFrames_3d7ts") -animation = &"opening" diff --git a/game/doors/circle_door.tscn b/game/doors/circle_door.tscn index 088c39c..14f618a 100644 --- a/game/doors/circle_door.tscn +++ b/game/doors/circle_door.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://g0n5npbt3hb2"] +[gd_scene load_steps=13 format=3 uid="uid://g0n5npbt3hb2"] [ext_resource type="PackedScene" uid="uid://cojxqx5al7kyv" path="res://game/doors/closed_door.tscn" id="1_amonf"] [ext_resource type="Texture2D" uid="uid://sfjjweantpja" path="res://images/level/doors.png" id="2_5d3q0"] @@ -8,6 +8,18 @@ atlas = ExtResource("2_5d3q0") region = Rect2(0, 96, 32, 48) +[sub_resource type="AtlasTexture" id="AtlasTexture_18a0u"] +atlas = ExtResource("2_5d3q0") +region = Rect2(64, 96, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_jx0aw"] +atlas = ExtResource("2_5d3q0") +region = Rect2(32, 96, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_pefjn"] +atlas = ExtResource("2_5d3q0") +region = Rect2(0, 96, 32, 48) + [sub_resource type="AtlasTexture" id="AtlasTexture_bcn3i"] atlas = ExtResource("2_5d3q0") region = Rect2(64, 96, 32, 48) @@ -36,6 +48,20 @@ animations = [{ }, { "frames": [{ "duration": 1.0, +"texture": SubResource("AtlasTexture_18a0u") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_jx0aw") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_pefjn") +}], +"loop": false, +"name": &"closing", +"speed": 10.0 +}, { +"frames": [{ +"duration": 1.0, "texture": SubResource("AtlasTexture_bcn3i") }], "loop": true, diff --git a/game/doors/closed_door.gd b/game/doors/closed_door.gd index a538297..67be18a 100644 --- a/game/doors/closed_door.gd +++ b/game/doors/closed_door.gd @@ -2,17 +2,26 @@ class_name ClosedDoor extends Door +var _unlocked := false + + func _is_key(_node: Node) -> bool: return false func _can_open(body: Node2D) -> bool: + if _unlocked: return true if not body.has_node("Pickups"): return false if not body.has_method("remove_pickup"): return false for pickup in body.get_node("Pickups").get_children(): if _is_key(pickup): body.remove_pickup(pickup) + _unlocked = true return true return false + + +func _can_close(_body: Node2D) -> bool: + return _unlocked diff --git a/game/doors/closed_door.tscn b/game/doors/closed_door.tscn index ab8a660..11076f1 100644 --- a/game/doors/closed_door.tscn +++ b/game/doors/closed_door.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://cojxqx5al7kyv"] +[gd_scene load_steps=13 format=3 uid="uid://cojxqx5al7kyv"] [ext_resource type="PackedScene" uid="uid://b7mhdrj3b6loq" path="res://game/doors/door.tscn" id="1_rxn60"] [ext_resource type="Script" uid="uid://qd7aihqwgddj" path="res://game/doors/closed_door.gd" id="2_dntl7"] @@ -8,6 +8,18 @@ atlas = ExtResource("3_dntl7") region = Rect2(0, 48, 32, 48) +[sub_resource type="AtlasTexture" id="AtlasTexture_dntl7"] +atlas = ExtResource("3_dntl7") +region = Rect2(64, 48, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_fgn1o"] +atlas = ExtResource("3_dntl7") +region = Rect2(32, 48, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_qpt8q"] +atlas = ExtResource("3_dntl7") +region = Rect2(0, 48, 32, 48) + [sub_resource type="AtlasTexture" id="AtlasTexture_gu75k"] atlas = ExtResource("3_dntl7") region = Rect2(64, 48, 32, 48) @@ -36,6 +48,20 @@ animations = [{ }, { "frames": [{ "duration": 1.0, +"texture": SubResource("AtlasTexture_dntl7") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_fgn1o") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_qpt8q") +}], +"loop": false, +"name": &"closing", +"speed": 10.0 +}, { +"frames": [{ +"duration": 1.0, "texture": SubResource("AtlasTexture_gu75k") }], "loop": true, @@ -62,4 +88,3 @@ script = ExtResource("2_dntl7") [node name="AnimatedSprite2D" parent="." index="2"] sprite_frames = SubResource("SpriteFrames_fgn1o") -animation = &"closed" diff --git a/game/doors/door.gd b/game/doors/door.gd index 0c3993f..d7656cf 100644 --- a/game/doors/door.gd +++ b/game/doors/door.gd @@ -5,6 +5,11 @@ extends Node2D const ANIMATION_CLOSED = "closed" const ANIMATION_OPEN = "open" const ANIMATION_OPENING = "opening" +const ANIMATION_CLOSING = "closing" + + +var _needed_to_be_open := false +var _needed_to_be_closed := false @onready var sprite : AnimatedSprite2D = $AnimatedSprite2D @@ -17,6 +22,10 @@ func _ready() -> void: sprite.play(ANIMATION_CLOSED) +func _is_open() -> bool: + return sprite.animation == ANIMATION_OPEN + + func _is_closed() -> bool: return sprite.animation == ANIMATION_CLOSED @@ -25,15 +34,48 @@ func _can_open(_body: Node2D) -> bool: return true +func _can_close(_body: Node2D) -> bool: + return true + + func _open() -> void: sprite.play(ANIMATION_OPENING) static_body.process_mode = Node.PROCESS_MODE_DISABLED +func _close() -> void: + sprite.play(ANIMATION_CLOSING) + static_body.process_mode = Node.PROCESS_MODE_DISABLED + + func _on_animation_finished() -> void: - sprite.play(ANIMATION_OPEN) + match sprite.animation: + ANIMATION_OPENING: + _needed_to_be_open = false + if _needed_to_be_closed: + sprite.play(ANIMATION_CLOSING) + else: + sprite.play(ANIMATION_OPEN) + ANIMATION_CLOSING: + _needed_to_be_closed = false + if _needed_to_be_open: + sprite.play(ANIMATION_OPENING) + else: + sprite.play(ANIMATION_CLOSED) + pass func _on_area_entered(body: Node2D) -> void: - if _is_closed() and _can_open(body): - _open() + if _can_open(body): + if _is_closed(): + _open() + else: + _needed_to_be_open = true + + +func _on_area_exited(body: Node2D) -> void: + if _can_close(body): + if _is_open(): + _close() + else: + _needed_to_be_closed = true diff --git a/game/doors/door.tscn b/game/doors/door.tscn index 05f3e3c..accbd26 100644 --- a/game/doors/door.tscn +++ b/game/doors/door.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=12 format=3 uid="uid://b7mhdrj3b6loq"] +[gd_scene load_steps=15 format=3 uid="uid://b7mhdrj3b6loq"] [ext_resource type="Script" uid="uid://5uqub2a6701b" path="res://game/doors/door.gd" id="1_vx1od"] [ext_resource type="Material" uid="uid://dojhoc6ljpt1a" path="res://game/materials/gray_walls.tres" id="1_xelsh"] @@ -14,6 +14,18 @@ size = Vector2(48, 48) atlas = ExtResource("3_ymlcn") region = Rect2(0, 0, 32, 48) +[sub_resource type="AtlasTexture" id="AtlasTexture_8lhfw"] +atlas = ExtResource("3_ymlcn") +region = Rect2(64, 0, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_qci2x"] +atlas = ExtResource("3_ymlcn") +region = Rect2(32, 0, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_4vug2"] +atlas = ExtResource("3_ymlcn") +region = Rect2(0, 0, 32, 48) + [sub_resource type="AtlasTexture" id="AtlasTexture_ymlcn"] atlas = ExtResource("3_ymlcn") region = Rect2(64, 0, 32, 48) @@ -42,6 +54,20 @@ animations = [{ }, { "frames": [{ "duration": 1.0, +"texture": SubResource("AtlasTexture_8lhfw") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_qci2x") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_4vug2") +}], +"loop": false, +"name": &"closing", +"speed": 10.0 +}, { +"frames": [{ +"duration": 1.0, "texture": SubResource("AtlasTexture_ymlcn") }], "loop": true, @@ -85,7 +111,8 @@ shape = SubResource("RectangleShape2D_u7b5a") [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] material = ExtResource("1_xelsh") sprite_frames = SubResource("SpriteFrames_r8rhb") -animation = &"opening" +animation = &"closing" [connection signal="body_entered" from="Area2D" to="." method="_on_area_entered"] +[connection signal="body_exited" from="Area2D" to="." method="_on_area_exited"] [connection signal="animation_finished" from="AnimatedSprite2D" to="." method="_on_animation_finished"] diff --git a/game/doors/square_door.tscn b/game/doors/square_door.tscn index a5e93cb..949cd84 100644 --- a/game/doors/square_door.tscn +++ b/game/doors/square_door.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://b7imlh4sl036i"] +[gd_scene load_steps=13 format=3 uid="uid://b7imlh4sl036i"] [ext_resource type="PackedScene" uid="uid://cojxqx5al7kyv" path="res://game/doors/closed_door.tscn" id="1_jg0qv"] [ext_resource type="Script" uid="uid://mp2dfsd1uk5p" path="res://game/doors/square_door.gd" id="2_wyn1t"] @@ -8,6 +8,18 @@ atlas = ExtResource("3_txk00") region = Rect2(0, 144, 32, 48) +[sub_resource type="AtlasTexture" id="AtlasTexture_txk00"] +atlas = ExtResource("3_txk00") +region = Rect2(64, 144, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_wk632"] +atlas = ExtResource("3_txk00") +region = Rect2(32, 144, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_av2fj"] +atlas = ExtResource("3_txk00") +region = Rect2(0, 144, 32, 48) + [sub_resource type="AtlasTexture" id="AtlasTexture_rwkr8"] atlas = ExtResource("3_txk00") region = Rect2(64, 144, 32, 48) @@ -36,6 +48,20 @@ animations = [{ }, { "frames": [{ "duration": 1.0, +"texture": SubResource("AtlasTexture_txk00") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_wk632") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_av2fj") +}], +"loop": false, +"name": &"closing", +"speed": 10.0 +}, { +"frames": [{ +"duration": 1.0, "texture": SubResource("AtlasTexture_rwkr8") }], "loop": true, diff --git a/game/doors/triangle_door.tscn b/game/doors/triangle_door.tscn index 4568254..2bb746b 100644 --- a/game/doors/triangle_door.tscn +++ b/game/doors/triangle_door.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://dwvv5dj8lqif2"] +[gd_scene load_steps=13 format=3 uid="uid://dwvv5dj8lqif2"] [ext_resource type="PackedScene" uid="uid://cojxqx5al7kyv" path="res://game/doors/closed_door.tscn" id="1_l21ff"] [ext_resource type="Script" uid="uid://d4lrt6yw0knf7" path="res://game/doors/triangle_door.gd" id="2_ifpbb"] @@ -8,6 +8,18 @@ atlas = ExtResource("3_eqipl") region = Rect2(0, 192, 32, 48) +[sub_resource type="AtlasTexture" id="AtlasTexture_eqipl"] +atlas = ExtResource("3_eqipl") +region = Rect2(64, 192, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_o203p"] +atlas = ExtResource("3_eqipl") +region = Rect2(32, 192, 32, 48) + +[sub_resource type="AtlasTexture" id="AtlasTexture_pvb1j"] +atlas = ExtResource("3_eqipl") +region = Rect2(0, 192, 32, 48) + [sub_resource type="AtlasTexture" id="AtlasTexture_1a8ca"] atlas = ExtResource("3_eqipl") region = Rect2(64, 192, 32, 48) @@ -36,6 +48,20 @@ animations = [{ }, { "frames": [{ "duration": 1.0, +"texture": SubResource("AtlasTexture_eqipl") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_o203p") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_pvb1j") +}], +"loop": false, +"name": &"closing", +"speed": 10.0 +}, { +"frames": [{ +"duration": 1.0, "texture": SubResource("AtlasTexture_1a8ca") }], "loop": true, diff --git a/project.godot b/project.godot index df451fe..d111041 100644 --- a/project.godot +++ b/project.godot @@ -29,6 +29,8 @@ gdscript/warnings/untyped_declaration=1 window/size/viewport_width=640 window/size/viewport_height=360 +window/size/resizable=false +window/size/maximize_disabled=true window/stretch/mode="viewport" window/stretch/scale_mode="integer"