improved pathfinding

This commit is contained in:
2023-02-27 19:14:41 +01:00
parent a94ee84718
commit 86d6563975
5 changed files with 42 additions and 48 deletions

View File

@@ -92,6 +92,7 @@ height = 54.0
process_mode = 1
collision_layer = 5
collision_mask = 5
collision_priority = 5.0
motion_mode = 1
script = ExtResource("1_qehox")
@@ -133,3 +134,7 @@ visible = false
position = Vector2(0, 36)
rotation = 1.5708
shape = SubResource("CapsuleShape2D_38v5o")
[node name="globalposition_timer" type="Timer" parent="."]
wait_time = 1.5
autostart = true

View File

@@ -1,32 +1,7 @@
[gd_scene load_steps=9 format=3 uid="uid://cpu7t7csffoxg"]
[gd_scene load_steps=8 format=3 uid="uid://cpu7t7csffoxg"]
[ext_resource type="Texture2D" uid="uid://ca1es3hoj53wg" path="res://assets/textures/enemies/slime_green_atlas.png" id="1_bw6us"]
[sub_resource type="GDScript" id="GDScript_78jn1"]
resource_name = "debug"
script/source = "extends CharacterBody2D
var speed = 35
var motion = Vector2.ZERO
var player = null
func _ready():
$animated_sprite_2d.play()
func _physics_process(_delta):
motion = Vector2.ZERO
if player:
motion = position.direction_to(player.position) * speed
velocity = motion
move_and_slide()
func OnArea2dBodyEntered(body):
player = body
func OnArea2dBodyExited(_body):
player = null
"
[ext_resource type="Script" path="res://src/scene-scripts/enemies/slime.cs" id="1_l1742"]
[sub_resource type="AtlasTexture" id="AtlasTexture_eghup"]
atlas = ExtResource("1_bw6us")
@@ -61,34 +36,24 @@ animations = [{
}]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_e6mi3"]
radius = 6.0
height = 56.0
[sub_resource type="CircleShape2D" id="CircleShape2D_3gptu"]
radius = 440.41
radius = 9.0
height = 46.0
[node name="slime" type="CharacterBody2D"]
collision_layer = 9
collision_mask = 8
script = SubResource("GDScript_78jn1")
collision_layer = 8
collision_mask = 9
collision_priority = 2.0
script = ExtResource("1_l1742")
[node name="animated_sprite_2d" type="AnimatedSprite2D" parent="."]
position = Vector2(0, 33)
position = Vector2(0, 19)
sprite_frames = SubResource("SpriteFrames_bwr2v")
frame_progress = 0.348187
[node name="collision_shape_2d" type="CollisionShape2D" parent="."]
position = Vector2(1, 43)
position = Vector2(0, 28)
rotation = 1.5708
shape = SubResource("CapsuleShape2D_e6mi3")
[node name="area_2d" type="Area2D" parent="."]
position = Vector2(0, 32)
collision_layer = 4
collision_mask = 4
[node name="collision_shape_2d" type="CollisionShape2D" parent="area_2d"]
shape = SubResource("CircleShape2D_3gptu")
[connection signal="body_entered" from="area_2d" to="." method="OnArea2dBodyEntered"]
[connection signal="body_exited" from="area_2d" to="." method="OnArea2dBodyExited"]
[node name="visible_notifier_2d" type="VisibleOnScreenNotifier2D" parent="."]
rect = Rect2(-25, 0, 50, 40)

View File

@@ -7,7 +7,7 @@ radius = 33.1361
[node name="rigid_body_2d" type="RigidBody2D"]
collision_layer = 2
collision_mask = 3
collision_mask = 11
collision_priority = 20.0
gravity_scale = 0.0

View File

@@ -0,0 +1,17 @@
using Godot;
using System;
public partial class slime : CharacterBody2D
{
[Export] int speed = 70;
Vector2 motion = Vector2.Zero;
public override void _Ready() => GetNode<AnimatedSprite2D>("animated_sprite_2d").Play();
public override void _PhysicsProcess(double delta)
{
if (GetNode<VisibleOnScreenNotifier2D>("visible_notifier_2d").IsOnScreen())
motion = Position.DirectionTo(player.globalPlayerPosition) * speed;
else motion = Vector2.Zero;
Velocity = motion;
MoveAndSlide();
}
}

View File

@@ -10,6 +10,7 @@ public partial class player : CharacterBody2D
public AnimatedSprite2D animatedSprite;
public Marker2D rotCenter;
public RayCast2D dialogRayCast;
public static Vector2 globalPlayerPosition; //for enemy path finding with delay for less bugs
//console cheats:
private static Camera2D cheatCam;
private static Camera2D mainCam;
@@ -32,8 +33,14 @@ public partial class player : CharacterBody2D
if (Math.Round(movement.Length(), 0) != 0) rotCenter.Rotation = new Vector2((float)Math.Round(movement.X, 0), (float)Math.Round(movement.Y, 0)).Angle();
MoveAndCollide(movement * speed * 200 * (float)delta);
}
void GlobalPlayerPosition()
{
if (Mathf.Round(GetNode<Timer>("globalposition_timer").TimeLeft) == 0)
globalPlayerPosition = Position;
}
public override void _Process(double delta)
{
GlobalPlayerPosition();
if (Input.IsActionJustPressed("ui_accept") && dialogRayCast.IsColliding() && allowMovement)
GetNode<dialog_bubble>("dialog_bubble").GetDialog(dialogRayCast.GetCollider().Get("file").AsString(), (Area2D)dialogRayCast.GetCollider());