feat: 添加敌人并且追踪player,同时避免敌人重叠
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
extends Unit
|
||||
class_name Enemy
|
||||
|
||||
@export var flock_push := 20.0
|
||||
|
||||
@onready var vision_area: Area2D = $VisionArea
|
||||
|
||||
var can_move := true
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
if not can_move:
|
||||
return
|
||||
|
||||
if not can_move_forwards_player():
|
||||
return
|
||||
|
||||
position += get_move_direction() * stats.speed * delta
|
||||
|
||||
update_rotate()
|
||||
|
||||
|
||||
func get_move_direction() -> Vector2:
|
||||
|
||||
if not is_instance_valid(Global.player):
|
||||
return Vector2.ZERO
|
||||
|
||||
var direction := global_position.direction_to(Global.player.position)
|
||||
|
||||
for area: Node2D in vision_area.get_overlapping_areas():
|
||||
if area != self and area.is_inside_tree():
|
||||
var vector := global_position - area.global_position
|
||||
direction += flock_push * vector.normalized() / vector.length()
|
||||
|
||||
return direction
|
||||
|
||||
func update_rotate() -> void:
|
||||
if is_instance_valid(Global.player):
|
||||
return
|
||||
|
||||
var player_pos := Global.player.position
|
||||
|
||||
var moving_right := global_position.x < player_pos.x
|
||||
visuals.scale = Vector2(-0.5,0.5) if moving_right else Vector2(0.5,0.5)
|
||||
|
||||
func can_move_forwards_player() -> bool:
|
||||
return is_instance_valid(Global.player) and\
|
||||
global_position.distance_to(Global.player.position) > 60
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://b5j2awtdd1a4a
|
||||
@@ -0,0 +1,11 @@
|
||||
[gd_scene format=3 uid="uid://bot1mnbk3iu11"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://bfuqhhe8r8asg" path="res://scenes/unit/enemy/enemy_chaser_slow.tscn" id="1_ksdef"]
|
||||
[ext_resource type="Resource" uid="uid://delxlqjos3br" path="res://resources/units/enemies/stats_enemy_charger.tres" id="2_4i178"]
|
||||
[ext_resource type="Texture2D" uid="uid://emsi6ifyt0vf" path="res://assets/sprites/Enemies/Enemy_5.png" id="2_knpng"]
|
||||
|
||||
[node name="EnemyCharger" unique_id=2092979720 instance=ExtResource("1_ksdef")]
|
||||
stats = ExtResource("2_4i178")
|
||||
|
||||
[node name="Sprite" parent="Visuals" parent_id_path=PackedInt32Array(395976388) index="1" unique_id=1925870318]
|
||||
texture = ExtResource("2_knpng")
|
||||
@@ -0,0 +1,11 @@
|
||||
[gd_scene format=3 uid="uid://s6tc5m2wg178"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://bfuqhhe8r8asg" path="res://scenes/unit/enemy/enemy_chaser_slow.tscn" id="1_yb8ih"]
|
||||
[ext_resource type="Texture2D" uid="uid://c6vplmpl4q4jr" path="res://assets/sprites/Enemies/Enemy_3.png" id="2_fw3op"]
|
||||
[ext_resource type="Resource" uid="uid://cv3187t7wsfxh" path="res://resources/units/enemies/stats_enemy_chaser_fast.tres" id="2_p0tt2"]
|
||||
|
||||
[node name="EnemyChaserFast" unique_id=2092979720 instance=ExtResource("1_yb8ih")]
|
||||
stats = ExtResource("2_p0tt2")
|
||||
|
||||
[node name="Sprite" parent="Visuals" parent_id_path=PackedInt32Array(395976388) index="1" unique_id=1925870318]
|
||||
texture = ExtResource("2_fw3op")
|
||||
@@ -0,0 +1,11 @@
|
||||
[gd_scene format=3 uid="uid://c151jxi18d6uo"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://bfuqhhe8r8asg" path="res://scenes/unit/enemy/enemy_chaser_slow.tscn" id="1_qulsm"]
|
||||
[ext_resource type="Texture2D" uid="uid://bf4ls8ho3cuqa" path="res://assets/sprites/Enemies/Enemy_2.png" id="2_aqv7o"]
|
||||
[ext_resource type="Resource" uid="uid://db0igkyfvumk7" path="res://resources/units/enemies/stats_enemy_chaser_mid.tres" id="2_w0b3r"]
|
||||
|
||||
[node name="EnemyChaserMid" unique_id=2092979720 instance=ExtResource("1_qulsm")]
|
||||
stats = ExtResource("2_w0b3r")
|
||||
|
||||
[node name="Sprite" parent="Visuals" parent_id_path=PackedInt32Array(395976388) index="1" unique_id=1925870318]
|
||||
texture = ExtResource("2_aqv7o")
|
||||
@@ -0,0 +1,34 @@
|
||||
[gd_scene format=3 uid="uid://bfuqhhe8r8asg"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://d2l68snnemcet" path="res://scenes/unit/unit.tscn" id="1_dd0vi"]
|
||||
[ext_resource type="Script" uid="uid://b5j2awtdd1a4a" path="res://scenes/unit/enemy/enemy.gd" id="2_lmqbh"]
|
||||
[ext_resource type="Texture2D" uid="uid://d1ou504pylric" path="res://assets/sprites/Enemies/Enemy_1.png" id="2_oo7ju"]
|
||||
[ext_resource type="Resource" uid="uid://bn4dskgwnahaq" path="res://resources/units/enemies/stats_enemy_chaser_slow.tres" id="3_lmqbh"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_e15k4"]
|
||||
radius = 31.016125
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_lmqbh"]
|
||||
radius = 150.0
|
||||
|
||||
[node name="EnemyChaserSlow" unique_id=2092979720 instance=ExtResource("1_dd0vi")]
|
||||
collision_layer = 2
|
||||
script = ExtResource("2_lmqbh")
|
||||
flock_push = 20.0
|
||||
stats = ExtResource("3_lmqbh")
|
||||
|
||||
[node name="Sprite" parent="Visuals" parent_id_path=PackedInt32Array(395976388) index="1" unique_id=1925870318]
|
||||
texture = ExtResource("2_oo7ju")
|
||||
|
||||
[node name="CollisionShape2D" parent="." index="1" unique_id=139786467]
|
||||
position = Vector2(0, -30)
|
||||
shape = SubResource("CircleShape2D_e15k4")
|
||||
debug_color = Color(0.97292477, 0.10649621, 0.32710177, 0.41960785)
|
||||
|
||||
[node name="VisionArea" type="Area2D" parent="." index="3" unique_id=466967301]
|
||||
collision_layer = 0
|
||||
collision_mask = 2
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="VisionArea" index="0" unique_id=715489053]
|
||||
position = Vector2(0, -25)
|
||||
shape = SubResource("CircleShape2D_lmqbh")
|
||||
@@ -0,0 +1,11 @@
|
||||
[gd_scene format=3 uid="uid://cb24l2xfnn7fy"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://bfuqhhe8r8asg" path="res://scenes/unit/enemy/enemy_chaser_slow.tscn" id="1_tty1r"]
|
||||
[ext_resource type="Resource" uid="uid://bnknrm368mpwb" path="res://resources/units/enemies/stats_enemy_shooter.tres" id="2_6k67w"]
|
||||
[ext_resource type="Texture2D" uid="uid://4mllmwdfhmce" path="res://assets/sprites/Enemies/Enemy_4.png" id="2_tev67"]
|
||||
|
||||
[node name="EnemyShooter" unique_id=2092979720 instance=ExtResource("1_tty1r")]
|
||||
stats = ExtResource("2_6k67w")
|
||||
|
||||
[node name="Sprite" parent="Visuals" parent_id_path=PackedInt32Array(395976388) index="1" unique_id=1925870318]
|
||||
texture = ExtResource("2_tev67")
|
||||
Reference in New Issue
Block a user