enemy is done maybe just a bit

This commit is contained in:
asenicmammal 2024-10-06 23:05:37 -07:00
parent f6d91820c6
commit e98e9088fd
4 changed files with 119 additions and 0 deletions

39
scenes/enemy.tscn Normal file
View file

@ -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://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"]

46
scripts/enemy.gd Normal file
View file

@ -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;

BIN
sprites/antred.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

34
sprites/antred.png.import Normal file
View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cqjv6gj3aanf5"
path="res://.godot/imported/antred.png-1b7a90b2b7461edbecb421b3444d9e87.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://sprites/antred.png"
dest_files=["res://.godot/imported/antred.png-1b7a90b2b7461edbecb421b3444d9e87.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=512
detect_3d/compress_to=1