diff --git a/scenes/enemy.tscn b/scenes/enemy.tscn new file mode 100644 index 0000000..8e2a1db --- /dev/null +++ b/scenes/enemy.tscn @@ -0,0 +1,39 @@ +[gd_scene load_steps=4 format=3 uid="uid://d2w5qydbu4alc"] + +[ext_resource type="Script" path="res://scripts/enemy.gd" id="1_1svwr"] +[ext_resource type="Texture2D" uid="uid://cqjv6gj3aanf5" path="res://sprites/antred.png" id="2_75flp"] + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_w4m25"] +radius = 30.0 +height = 278.0 + +[node name="enemy" type="CharacterBody2D" node_paths=PackedStringArray("nav", "patrolpoints")] +script = ExtResource("1_1svwr") +nav = NodePath("NavigationAgent2D") +patrolpoints = [null] + +[node name="Sprite2D" type="Sprite2D" parent="."] +scale = Vector2(0.3, 0.3) +texture = ExtResource("2_75flp") + +[node name="colltion" type="Area2D" parent="."] +scale = Vector2(0.3, 0.3) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="colltion"] +position = Vector2(-34, 11) +rotation = 1.5708 +shape = SubResource("CapsuleShape2D_w4m25") + +[node name="dictection" type="Area2D" parent="."] +position = Vector2(24, 3) +scale = Vector2(0.5, 0.5) +collision_layer = 2 +collision_mask = 2 + +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="dictection"] +polygon = PackedVector2Array(9, 2, 2883, -934, 2919, 1070) + +[node name="NavigationAgent2D" type="NavigationAgent2D" parent="."] + +[connection signal="body_entered" from="dictection" to="." method="_on_dictection_body_entered"] +[connection signal="body_exited" from="dictection" to="." method="_on_dictection_body_exited"] diff --git a/scripts/enemy.gd b/scripts/enemy.gd new file mode 100644 index 0000000..ba3c3c3 --- /dev/null +++ b/scripts/enemy.gd @@ -0,0 +1,46 @@ +extends CharacterBody2D; + +@export var speed = 300 +var ac = 7; +var SpriteTurningSpeed = 5 +@export var FrictionMult = 0 +var player_chase = false; +var patrolpoint_cur = 0; +@export var nav: NavigationAgent2D +var player = null; +@onready var Player = get_tree().get_first_node_in_group("player") +@export var patrolpoints: Array[Node2D]; +func _physics_process(delta: float) -> void: + var direction = Vector2(0,0) + + if (player_chase): + nav.target_position = player.position; + else: + if(patrolpoints[patrolpoint_cur] != null): + nav.target_position = patrolpoints[patrolpoint_cur].position; + else: + nav.target_position = Vector2(0,0) + + direction = nav.get_next_path_position() - global_position; + direction = direction.normalized(); + velocity = velocity.lerp(direction * speed, ac * delta) + if velocity.length() > 0.1: + rotation = rotate_toward(rotation,velocity.angle(),delta*SpriteTurningSpeed) + if(patrolpoints[patrolpoint_cur] != null): + if(position.distance_to(patrolpoints[patrolpoint_cur].position) < 10): + print(patrolpoint_cur) + patrolpoint_cur += 1 + if(patrolpoint_cur == patrolpoints.size()): + patrolpoint_cur = 0; + + + + move_and_slide(); +func _on_dictection_body_entered(body: Node2D) -> void: + player = body; + player_chase = true; + + +func _on_dictection_body_exited(body: Node2D) -> void: + player = null; + player_chase = false;