From 0d30fb29be851eb3549f35c3f7b335464571ee48 Mon Sep 17 00:00:00 2001 From: Luna Brovchuk Date: Sat, 30 Sep 2023 23:31:24 +0200 Subject: [PATCH] Make Enemy Class and Make Trash Functional --- project.godot | 1 + src/models/enemies/bat/Bat.tscn | 2 +- src/models/enemies/bat/bat.gd | 31 ++++++++++++------- .../enemies/trash_can/drop/TrashDrop.tscn | 3 +- src/models/player/Player.tscn | 5 ++- src/models/player/player.gd | 16 +++++++++- src/scenes/game/Game.tscn | 11 +++++-- src/scripts/enemy_class.gd | 20 ++++++++++++ 8 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 src/scripts/enemy_class.gd diff --git a/project.godot b/project.godot index 61c5cb7..1807714 100644 --- a/project.godot +++ b/project.godot @@ -30,6 +30,7 @@ config/icon="res://icon.png" Global="*res://src/global.gd" Window="*res://src/window.gd" +EnemyClass="*res://src/scripts/enemy_class.gd" [display] diff --git a/src/models/enemies/bat/Bat.tscn b/src/models/enemies/bat/Bat.tscn index 8d8df04..52955db 100644 --- a/src/models/enemies/bat/Bat.tscn +++ b/src/models/enemies/bat/Bat.tscn @@ -6,7 +6,7 @@ [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 4, 3 ) -[node name="Bat" type="KinematicBody2D"] +[node name="Bat" type="KinematicBody2D" groups=["enemy"]] script = ExtResource( 2 ) [node name="Sprite" type="Sprite" parent="."] diff --git a/src/models/enemies/bat/bat.gd b/src/models/enemies/bat/bat.gd index 48d14f2..2b471d0 100644 --- a/src/models/enemies/bat/bat.gd +++ b/src/models/enemies/bat/bat.gd @@ -1,25 +1,32 @@ -extends KinematicBody2D +extends EnemyClass var velocity = Vector2.ZERO var speed = 64 +onready var player = $"../../Player" + +func _ready(): + hp = 15 + func _process(_delta): - get_node("Sprite").flip_h = true if $"../Player".global_position.x > self.position.x else false + get_node("Sprite").flip_h = true if player.global_position.x > self.position.x else false func _physics_process(_delta): - if position.distance_to(Vector2($"../Player".global_position.x, $"../Player".global_position.y)) > 64: - velocity.x = speed * cos(get_angle_to(Vector2($"../Player".global_position.x + 7, $"../Player".global_position.y + 8))) - velocity.y = speed * sin(get_angle_to(Vector2($"../Player".global_position.x + 7, $"../Player".global_position.y + 8))) + var player_position = player.global_position + Vector2(8, 8) + var angle = get_angle_to(player_position) + if position.distance_to(player_position) > 64: + velocity.x = cos(angle) + velocity.y = sin(angle) else: velocity = Vector2.ZERO - if abs($"../Player".velocity.x) > 0 and position.distance_to(Vector2($"../Player".global_position.x, $"../Player".global_position.y)) < 64: - velocity.x = speed * cos(get_angle_to(Vector2($"../Player".global_position.x + 8, $"../Player".global_position.y + 8)) + 180) - velocity.y = speed * sin(get_angle_to(Vector2($"../Player".global_position.x + 8, $"../Player".global_position.y + 8)) + 180) - - if position.distance_to(Vector2($"../Player".global_position.x, $"../Player".global_position.y)) < 48: - velocity.x = speed * cos(get_angle_to(Vector2($"../Player".global_position.x + 8, $"../Player".global_position.y + 8)) + 180) - velocity.y = speed * sin(get_angle_to(Vector2($"../Player".global_position.x + 8, $"../Player".global_position.y + 8)) + 180) + if abs(player.velocity.x) > 0 and position.distance_to(player_position) < 64: + velocity.x = cos(angle + 180) + velocity.y = sin(angle + 180) + if position.distance_to(player_position) < 48: + velocity.x = cos(angle + 180) + velocity.y = sin(angle + 180) + velocity *= speed * speed_multiplier move_and_slide(velocity) diff --git a/src/models/enemies/trash_can/drop/TrashDrop.tscn b/src/models/enemies/trash_can/drop/TrashDrop.tscn index 3a5b905..7bfc603 100644 --- a/src/models/enemies/trash_can/drop/TrashDrop.tscn +++ b/src/models/enemies/trash_can/drop/TrashDrop.tscn @@ -6,7 +6,8 @@ [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 4, 4 ) -[node name="TrashDrop" type="StaticBody2D"] +[node name="TrashDrop" type="StaticBody2D" groups=["trash_drop"]] +z_index = -1 script = ExtResource( 2 ) [node name="Sprite" type="Sprite" parent="."] diff --git a/src/models/player/Player.tscn b/src/models/player/Player.tscn index 14e9b52..ab17ddf 100644 --- a/src/models/player/Player.tscn +++ b/src/models/player/Player.tscn @@ -9,7 +9,7 @@ extents = Vector2( 4, 5.5 ) [sub_resource type="RectangleShape2D" id=2] extents = Vector2( 7, 11 ) -[node name="Player" type="KinematicBody2D"] +[node name="Player" type="KinematicBody2D" groups=["player"]] script = ExtResource( 2 ) [node name="Sprite" type="Sprite" parent="."] @@ -25,3 +25,6 @@ shape = SubResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="EnemyCollision"] position = Vector2( 12, 12 ) shape = SubResource( 2 ) + +[connection signal="area_entered" from="EnemyCollision" to="." method="area_entered"] +[connection signal="area_exited" from="EnemyCollision" to="." method="area_exited"] diff --git a/src/models/player/player.gd b/src/models/player/player.gd index 368bd6e..b11928c 100644 --- a/src/models/player/player.gd +++ b/src/models/player/player.gd @@ -1,12 +1,26 @@ extends KinematicBody2D var velocity = Vector2.ZERO +var speed_multiplier = 1 var speed = 100 func get_input(): var input_direction = Input.get_vector("left", "right", "up", "down") velocity = input_direction * speed +func area_entered(area): + if area.get_parent().is_in_group("trash_drop"): + speed_multiplier = 0.5 + +func area_exited(area): + # Check if __all__ the drops exited + var exited = true + for area in $EnemyCollision.get_overlapping_areas(): + if area.get_parent().is_in_group("trash_drop"): + exited = false + if exited: + speed_multiplier = 1 + func _physics_process(_delta): get_input() - velocity = move_and_slide(velocity) + velocity = move_and_slide(velocity * speed_multiplier) diff --git a/src/scenes/game/Game.tscn b/src/scenes/game/Game.tscn index 2065621..9dcac09 100644 --- a/src/scenes/game/Game.tscn +++ b/src/scenes/game/Game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=18 format=2] +[gd_scene load_steps=19 format=2] [ext_resource path="res://src/scenes/menu/background/background.png" type="Texture" id=1] [ext_resource path="res://src/scenes/game/sprites/background_houses.png" type="Texture" id=2] @@ -13,6 +13,7 @@ [ext_resource path="res://src/scenes/game/game.gd" type="Script" id=11] [ext_resource path="res://src/scenes/game/sprites/ui_background.png" type="Texture" id=12] [ext_resource path="res://src/models/enemies/trash_can/TrashCan.tscn" type="PackedScene" id=13] +[ext_resource path="res://src/models/enemies/bat/Bat.tscn" type="PackedScene" id=14] [sub_resource type="RectangleShape2D" id=2] extents = Vector2( 10, 60 ) @@ -40,6 +41,7 @@ script = ExtResource( 11 ) [node name="Background" type="Node2D" parent="View"] [node name="Stones" type="Sprite" parent="View/Background"] +z_index = -2 texture = ExtResource( 1 ) centered = false region_enabled = true @@ -68,7 +70,12 @@ shape = SubResource( 3 ) position = Vector2( 160, 109.875 ) shape = SubResource( 3 ) -[node name="TrashCan" parent="View" instance=ExtResource( 13 )] +[node name="Enemies" type="Node2D" parent="View"] + +[node name="TrashCan" parent="View/Enemies" instance=ExtResource( 13 )] + +[node name="Bat" parent="View/Enemies" instance=ExtResource( 14 )] +z_index = 1 [node name="Player" parent="View" instance=ExtResource( 5 )] position = Vector2( 46, 42 ) diff --git a/src/scripts/enemy_class.gd b/src/scripts/enemy_class.gd new file mode 100644 index 0000000..32aa010 --- /dev/null +++ b/src/scripts/enemy_class.gd @@ -0,0 +1,20 @@ +extends KinematicBody2D + +var hp +var poisoned = false +var poisoned_timer = .0 +var speed_multiplier = 1 +var frozen = false + +func check_frozen(): + speed_multiplier = 1 + if frozen: + speed_multiplier = 0.5 + +func check_poisoned(): + if poisoned_timer >= 1: + hp -= 5 + +func _physics_process(delta): + check_frozen() + check_poisoned()