Make Enemy Class and Make Trash Functional

main
Luna Brovchuk 2023-09-30 23:31:24 +02:00
parent e895edd5e6
commit 0d30fb29be
8 changed files with 71 additions and 18 deletions

View File

@ -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]

View File

@ -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="."]

View File

@ -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)

View File

@ -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="."]

View File

@ -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"]

View File

@ -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)

View File

@ -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 )

View File

@ -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()