From f8c34d55f51baa6ba5d34038f92b6437e8cd2dca Mon Sep 17 00:00:00 2001 From: Seonghyeon Cho Date: Sun, 2 Jun 2024 16:10:22 +0900 Subject: [PATCH] ~39 add a wizard enemy --- scenes/component/velocity_component.gd | 32 +++++++++++ scenes/component/velocity_component.tscn | 6 +++ scenes/component/vial_drop_component.gd | 5 +- scenes/game_object/basic_enemy/basic_enemy.gd | 16 ++---- .../game_object/basic_enemy/basic_enemy.tscn | 6 ++- .../game_object/wizard_enemy/wizard_enemy.gd | 13 +++++ .../game_object/wizard_enemy/wizard_enemy.png | Bin 0 -> 228 bytes .../wizard_enemy/wizard_enemy.png.import | 34 ++++++++++++ .../wizard_enemy/wizard_enemy.tscn | 51 ++++++++++++++++++ 9 files changed, 147 insertions(+), 16 deletions(-) create mode 100644 scenes/component/velocity_component.gd create mode 100644 scenes/component/velocity_component.tscn create mode 100644 scenes/game_object/wizard_enemy/wizard_enemy.gd create mode 100644 scenes/game_object/wizard_enemy/wizard_enemy.png create mode 100644 scenes/game_object/wizard_enemy/wizard_enemy.png.import create mode 100644 scenes/game_object/wizard_enemy/wizard_enemy.tscn 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 0000000000000000000000000000000000000000..564b39cc95f15a42779d916cf6761ea9424d302c GIT binary patch literal 228 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPFb95{VvZ;qF}n&GUTJ2!wT7)yfuf*Bm1-ADs+QaoK8 zLpZMcp4}+eV8Fq8VN1>;=@%S)6GZoH-lF8EP@-9M>VBAqRQ*>zzB(_KD