Skip to content

Commit

Permalink
~39 add a wizard enemy
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cho committed Jun 2, 2024
1 parent 12c2b3e commit f8c34d5
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 16 deletions.
32 changes: 32 additions & 0 deletions scenes/component/velocity_component.gd
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions scenes/component/velocity_component.tscn
Original file line number Diff line number Diff line change
@@ -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")
5 changes: 3 additions & 2 deletions scenes/component/vial_drop_component.gd
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
16 changes: 3 additions & 13 deletions scenes/game_object/basic_enemy/basic_enemy.gd
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion scenes/game_object/basic_enemy/basic_enemy.tscn
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down Expand Up @@ -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")
Expand Down
13 changes: 13 additions & 0 deletions scenes/game_object/wizard_enemy/wizard_enemy.gd
Original file line number Diff line number Diff line change
@@ -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)
Binary file added scenes/game_object/wizard_enemy/wizard_enemy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions scenes/game_object/wizard_enemy/wizard_enemy.png.import
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions scenes/game_object/wizard_enemy/wizard_enemy.tscn
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit f8c34d5

Please sign in to comment.