improved pathfinding
This commit is contained in:
17
src/scene-scripts/enemies/slime.cs
Normal file
17
src/scene-scripts/enemies/slime.cs
Normal 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();
|
||||
}
|
||||
}
|
@@ -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());
|
||||
|
||||
|
Reference in New Issue
Block a user