diff --git a/scenes/component/velocity_component.gd b/scenes/component/velocity_component.gd new file mode 100644 index 0000000..6d5a096 --- /dev/null +++ b/scenes/component/velocity_component.gd @@ -0,0 +1,32 @@ +extends Node +class_name VelocityComponent + +@export var max_speed: int = 40 +@export var acceleration: float = 5 + +var velocity := Vector2.ZERO + + +func accelerate_to_player(): + var owner_node2d = owner as Node2D + if owner_node2d == null: + return + + var player = get_tree().get_first_node_in_group("player") as Node2D + if player == null: + return + + var direction = (player.global_position - owner_node2d.global_position).normalized() + accelerate_in_direction(direction) + + +func accelerate_in_direction(direction: Vector2): + var desired_velocity = direction * max_speed + velocity = velocity.lerp(desired_velocity, 1 - exp(-acceleration * get_process_delta_time())) + + +func move(character_body: CharacterBody2D): + character_body.velocity = velocity + character_body.move_and_slide() + + velocity = character_body.velocity diff --git a/scenes/component/velocity_component.tscn b/scenes/component/velocity_component.tscn new file mode 100644 index 0000000..fef1b9d --- /dev/null +++ b/scenes/component/velocity_component.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://cn4jjvvblm8lp"] + +[ext_resource type="Script" path="res://scenes/component/velocity_component.gd" id="1_pusvs"] + +[node name="VelocityComponent" type="Node"] +script = ExtResource("1_pusvs") diff --git a/scenes/component/vial_drop_component.gd b/scenes/component/vial_drop_component.gd index 19e3d4c..9d5f449 100644 --- a/scenes/component/vial_drop_component.gd +++ b/scenes/component/vial_drop_component.gd @@ -1,12 +1,13 @@ extends Node +class_name VialDropComponent @export_range(0, 1) var drop_rate: float = .5 -@export var health_component: Node +@export var health_component: HealthComponent @export var vial_scene: PackedScene func _ready(): - (health_component as HealthComponent).died.connect(on_died) + health_component.died.connect(on_died) func on_died(): diff --git a/scenes/game_object/basic_enemy/basic_enemy.gd b/scenes/game_object/basic_enemy/basic_enemy.gd index 647284c..9cfa1bc 100644 --- a/scenes/game_object/basic_enemy/basic_enemy.gd +++ b/scenes/game_object/basic_enemy/basic_enemy.gd @@ -1,23 +1,13 @@ extends CharacterBody2D -const MAX_SPEED = 40 - -@onready var health_component: HealthComponent = $HealthComponent @onready var visuals := $Visuals +@onready var velocity_component: VelocityComponent = $VelocityComponent func _process(delta): - var direction = get_direction_to_player() - velocity = direction * MAX_SPEED - move_and_slide() + velocity_component.accelerate_to_player() + velocity_component.move(self) var move_sign = sign(velocity.x) if move_sign != 0: visuals.scale = Vector2(-move_sign, 1) - - -func get_direction_to_player(): - var player_node = get_tree().get_first_node_in_group("player") as Node2D - if player_node != null: - return (player_node.global_position - global_position).normalized() - return Vector2.ZERO diff --git a/scenes/game_object/basic_enemy/basic_enemy.tscn b/scenes/game_object/basic_enemy/basic_enemy.tscn index c31b728..5e2a196 100644 --- a/scenes/game_object/basic_enemy/basic_enemy.tscn +++ b/scenes/game_object/basic_enemy/basic_enemy.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=12 format=3 uid="uid://bses7vk27cfvt"] +[gd_scene load_steps=13 format=3 uid="uid://bses7vk27cfvt"] [ext_resource type="Script" path="res://scenes/game_object/basic_enemy/basic_enemy.gd" id="1_gpcli"] [ext_resource type="Texture2D" uid="uid://cc6p0wg3ww2jf" path="res://scenes/game_object/basic_enemy/basic_enemy.png" id="1_m80w1"] [ext_resource type="PackedScene" uid="uid://d32bhk07dm6w8" path="res://scenes/component/health_component.tscn" id="2_en8ir"] [ext_resource type="PackedScene" uid="uid://ci3popyfx4vbv" path="res://scenes/component/vial_drop_component.tscn" id="3_v8t1l"] +[ext_resource type="PackedScene" uid="uid://cn4jjvvblm8lp" path="res://scenes/component/velocity_component.tscn" id="4_7n42o"] [ext_resource type="PackedScene" uid="uid://gsyk0iqo1m50" path="res://scenes/component/hurtbox_component.tscn" id="4_e2gan"] [ext_resource type="PackedScene" uid="uid://cjxiijis5jocn" path="res://scenes/component/death_component.tscn" id="4_j8jii"] @@ -90,8 +91,11 @@ autoplay = "walk" [node name="HealthComponent" parent="." instance=ExtResource("2_en8ir")] [node name="VialDropComponent" parent="." node_paths=PackedStringArray("health_component") instance=ExtResource("3_v8t1l")] +drop_rate = 0.3 health_component = NodePath("../HealthComponent") +[node name="VelocityComponent" parent="." instance=ExtResource("4_7n42o")] + [node name="DeathComponent" parent="." node_paths=PackedStringArray("health_component", "sprite") instance=ExtResource("4_j8jii")] health_component = NodePath("../HealthComponent") sprite = NodePath("../Visuals/Sprite2D") diff --git a/scenes/game_object/wizard_enemy/wizard_enemy.gd b/scenes/game_object/wizard_enemy/wizard_enemy.gd new file mode 100644 index 0000000..6a98620 --- /dev/null +++ b/scenes/game_object/wizard_enemy/wizard_enemy.gd @@ -0,0 +1,13 @@ +extends CharacterBody2D + +@onready var visuals := $Visuals +@onready var velocity_component: VelocityComponent = $VelocityComponent + + +func _process(delta): + velocity_component.accelerate_to_player() + velocity_component.move(self) + + var move_sign = sign(velocity.x) + if move_sign != 0: + visuals.scale = Vector2(move_sign, 1) diff --git a/scenes/game_object/wizard_enemy/wizard_enemy.png b/scenes/game_object/wizard_enemy/wizard_enemy.png new file mode 100644 index 0000000..564b39c Binary files /dev/null and b/scenes/game_object/wizard_enemy/wizard_enemy.png differ diff --git a/scenes/game_object/wizard_enemy/wizard_enemy.png.import b/scenes/game_object/wizard_enemy/wizard_enemy.png.import new file mode 100644 index 0000000..4183e8d --- /dev/null +++ b/scenes/game_object/wizard_enemy/wizard_enemy.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://bj74fspmmt8fp" +path="res://.godot/imported/wizard_enemy.png-ea4dcbc1cd21f78193df73188d66741a.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://scenes/game_object/wizard_enemy/wizard_enemy.png" +dest_files=["res://.godot/imported/wizard_enemy.png-ea4dcbc1cd21f78193df73188d66741a.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/scenes/game_object/wizard_enemy/wizard_enemy.tscn b/scenes/game_object/wizard_enemy/wizard_enemy.tscn new file mode 100644 index 0000000..3a1d13a --- /dev/null +++ b/scenes/game_object/wizard_enemy/wizard_enemy.tscn @@ -0,0 +1,51 @@ +[gd_scene load_steps=10 format=3 uid="uid://biw4lngphcynu"] + +[ext_resource type="PackedScene" uid="uid://d32bhk07dm6w8" path="res://scenes/component/health_component.tscn" id="1_5bkfq"] +[ext_resource type="Texture2D" uid="uid://bj74fspmmt8fp" path="res://scenes/game_object/wizard_enemy/wizard_enemy.png" id="1_beb02"] +[ext_resource type="Script" path="res://scenes/game_object/wizard_enemy/wizard_enemy.gd" id="1_ta3ds"] +[ext_resource type="PackedScene" uid="uid://cn4jjvvblm8lp" path="res://scenes/component/velocity_component.tscn" id="2_2sxlo"] +[ext_resource type="PackedScene" uid="uid://cjxiijis5jocn" path="res://scenes/component/death_component.tscn" id="2_aml71"] +[ext_resource type="PackedScene" uid="uid://gsyk0iqo1m50" path="res://scenes/component/hurtbox_component.tscn" id="3_2ak7l"] +[ext_resource type="PackedScene" uid="uid://ci3popyfx4vbv" path="res://scenes/component/vial_drop_component.tscn" id="4_blfg5"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_40784"] +radius = 8.0 + +[sub_resource type="CircleShape2D" id="CircleShape2D_6q0gl"] +radius = 5.0 + +[node name="WizardEnemy" type="CharacterBody2D" groups=["enemy"]] +position = Vector2(0, -8) +collision_layer = 8 +collision_mask = 9 +script = ExtResource("1_ta3ds") + +[node name="HealthComponent" parent="." instance=ExtResource("1_5bkfq")] +max_health = 30.0 + +[node name="VelocityComponent" parent="." instance=ExtResource("2_2sxlo")] +max_speed = 60 +acceleration = 2.0 + +[node name="VialDropComponent" parent="." node_paths=PackedStringArray("health_component") instance=ExtResource("4_blfg5")] +health_component = NodePath("../HealthComponent") + +[node name="DeathComponent" parent="." node_paths=PackedStringArray("health_component", "sprite") instance=ExtResource("2_aml71")] +health_component = NodePath("../HealthComponent") +sprite = NodePath("../Visuals/Sprite2D") + +[node name="HurtboxComponent" parent="." node_paths=PackedStringArray("health_component") instance=ExtResource("3_2ak7l")] +health_component = NodePath("../HealthComponent") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="HurtboxComponent"] +position = Vector2(0, 1) +shape = SubResource("CircleShape2D_40784") + +[node name="Visuals" type="Node2D" parent="."] + +[node name="Sprite2D" type="Sprite2D" parent="Visuals"] +texture = ExtResource("1_beb02") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(0, 3) +shape = SubResource("CircleShape2D_6q0gl")