SupaLidlGame/Characters/NPC.cs

256 lines
6.8 KiB
C#
Raw Normal View History

2023-07-31 01:12:47 -07:00
#define DEBUG_NPC
2023-07-21 02:54:13 -07:00
2022-11-10 20:29:53 -08:00
using Godot;
2022-11-27 19:37:16 -08:00
using SupaLidlGame.Extensions;
2022-11-19 21:21:12 -08:00
using SupaLidlGame.Items;
2022-11-10 20:29:53 -08:00
using System;
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.Characters;
public partial class NPC : Character
2022-11-10 20:29:53 -08:00
{
2023-06-03 18:21:46 -07:00
/// <summary>
/// Time in seconds it takes for the NPC to think FeelsDankCube
/// </summary>
public const float ThinkTime = 0.125f;
public float[] Weights => _weights;
protected float _preferredWeightDistance = 64.0f;
protected float _maxWeightDistance = 8.0f;
protected float _preferredWeightDistanceSq = 4096.0f;
protected float _maxWeightDistanceSq = 64.0f;
[Export]
public float PreferredWeightDistance
{
get => _preferredWeightDistance;
protected set
{
_preferredWeightDistance = value;
_preferredWeightDistanceSq = value * value;
2023-04-01 16:25:00 -07:00
}
2023-06-03 18:21:46 -07:00
}
2023-04-01 16:25:00 -07:00
2023-06-03 18:21:46 -07:00
[Export]
public float MaxWeightDistance
{
get => _maxWeightDistance;
protected set
{
_maxWeightDistance = value;
_maxWeightDistanceSq = value * value;
2023-04-01 16:25:00 -07:00
}
2023-06-03 18:21:46 -07:00
}
2022-11-27 19:37:16 -08:00
2023-07-31 01:12:47 -07:00
[Export]
public Items.Item DefaultSelectedItem { get; set; }
2023-07-21 02:54:13 -07:00
public bool ShouldMove { get; set; } = true;
2023-07-23 11:05:01 -07:00
public bool CanAttack { get; set; } = true;
2023-06-03 18:21:46 -07:00
protected float[] _weights = new float[16];
protected int _bestWeightIdx;
protected double _thinkTimeElapsed = 0;
protected Vector2 _blockingDir;
protected static readonly Vector2[] _weightDirs = new Vector2[16];
2022-11-10 20:29:53 -08:00
2023-06-03 18:21:46 -07:00
static NPC()
{
for (int i = 0; i < 16; i++)
2022-11-10 20:29:53 -08:00
{
2023-06-03 18:21:46 -07:00
float y = Mathf.Sin(Mathf.Pi * i * 2 / 16);
float x = Mathf.Cos(Mathf.Pi * i * 2 / 16);
_weightDirs[i] = new Vector2(x, y);
2022-11-10 20:29:53 -08:00
}
2023-06-03 18:21:46 -07:00
}
2022-11-10 20:29:53 -08:00
2023-06-03 18:21:46 -07:00
public override void _Ready()
{
base._Ready();
Array.Fill(_weights, 0);
2023-07-31 01:12:47 -07:00
if (DefaultSelectedItem is not null)
{
Inventory.SelectedItem = DefaultSelectedItem;
}
2023-06-03 18:21:46 -07:00
}
2022-11-27 19:37:16 -08:00
2023-06-03 18:21:46 -07:00
public override void _Draw()
{
2023-07-31 01:12:47 -07:00
#if DEBUG
2023-06-03 18:21:46 -07:00
for (int i = 0; i < 16; i++)
{
Vector2 vec = _weightDirs[i] * _weights[i] * 32;
Color c = Colors.Green;
if (_bestWeightIdx == i)
2022-11-10 20:29:53 -08:00
{
2023-06-03 18:21:46 -07:00
c = Colors.Blue;
}
else if (_weights[i] < 0)
{
c = Colors.Red;
vec = -vec;
2022-11-10 20:29:53 -08:00
}
2023-06-03 18:21:46 -07:00
DrawLine(Vector2.Zero, vec, c);
}
2022-11-27 19:37:16 -08:00
#endif
2022-11-10 20:29:53 -08:00
2023-06-03 18:21:46 -07:00
base._Draw();
}
2022-11-10 20:29:53 -08:00
2023-07-31 01:12:47 -07:00
public virtual Character FindBestTarget()
2023-06-03 18:21:46 -07:00
{
float bestDist = float.MaxValue;
Character bestChar = null;
foreach (Node node in GetParent().GetChildren())
2022-11-10 20:29:53 -08:00
{
2023-06-13 02:55:30 -07:00
if (node is Character character)
2022-11-10 20:29:53 -08:00
{
2023-07-31 01:12:47 -07:00
bool isFriendly = character.Faction == Faction;
if (isFriendly || character.Health <= 0)
2023-06-13 02:55:30 -07:00
{
continue;
}
2023-06-03 18:21:46 -07:00
float dist = Position.DistanceTo(character.Position);
if (dist < bestDist)
2022-11-10 20:29:53 -08:00
{
2023-06-03 18:21:46 -07:00
bestDist = dist;
bestChar = character;
2022-11-10 20:29:53 -08:00
}
}
}
2023-06-03 18:21:46 -07:00
return bestChar;
}
2022-11-10 20:29:53 -08:00
2023-06-03 18:21:46 -07:00
public void ThinkProcess(double delta)
{
if ((_thinkTimeElapsed += delta) > ThinkTime)
2022-11-13 15:42:04 -08:00
{
2023-06-03 18:21:46 -07:00
_thinkTimeElapsed = 0;
Think();
2023-07-21 02:54:13 -07:00
#if DEBUG_NPC
2023-06-03 18:21:46 -07:00
QueueRedraw();
2023-04-01 16:25:00 -07:00
#endif
2022-11-13 15:42:04 -08:00
}
2023-07-21 02:54:13 -07:00
if (ShouldMove)
{
Direction = _weightDirs[_bestWeightIdx];
}
else
{
Direction = Vector2.Zero;
}
2023-06-03 18:21:46 -07:00
}
2022-11-10 20:29:53 -08:00
2023-06-03 18:21:46 -07:00
public void UpdateWeights(Vector2 pos)
{
// FIXME: TODO: remove all the spaghetti
Vector2 dir = Target.Normalized();
float distSq = GlobalPosition.DistanceSquaredTo(pos);
2022-11-19 21:21:12 -08:00
2023-06-03 18:21:46 -07:00
var spaceState = GetWorld2D().DirectSpaceState;
var exclude = new Godot.Collections.Array<Godot.Rid>();
exclude.Add(this.GetRid());
2022-11-12 16:45:04 -08:00
2023-06-03 18:21:46 -07:00
// calculate weights based on distance
for (int i = 0; i < 16; i++)
{
float directDot = _weightDirs[i].Dot(dir);
// clamp dot from [-1, 1] to [0, 1]
directDot = (directDot + 1) / 2;
2022-11-12 16:45:04 -08:00
2023-06-03 18:21:46 -07:00
float strafeDot = Math.Abs(_weightDirs[i].Dot(dir.Clockwise90()));
float currDirDot = (_weightDirs[i].Dot(Direction) + 1) / 16;
strafeDot = Mathf.Pow((strafeDot + 1) / 2, 2) + currDirDot;
// favor strafing when getting closer
if (distSq > _preferredWeightDistanceSq)
{
_weights[i] = directDot;
}
else if (distSq > _maxWeightDistanceSq)
{
float dDotWeight = Mathf.Sqrt(distSq / 4096);
float sDotWeight = 1 - dDotWeight;
_weights[i] = (dDotWeight * directDot) +
(sDotWeight * strafeDot);
2022-11-27 19:37:16 -08:00
}
2023-06-03 18:21:46 -07:00
else
{
_weights[i] = strafeDot;
}
}
2022-11-12 16:45:04 -08:00
2023-06-03 18:21:46 -07:00
// subtract weights that collide
for (int i = 0; i < 16; i++)
{
var rayParams = new PhysicsRayQueryParameters2D
2022-11-27 19:37:16 -08:00
{
2023-06-03 18:21:46 -07:00
Exclude = exclude,
CollideWithBodies = true,
From = GlobalPosition,
To = GlobalPosition + (_weightDirs[i] * 24),
CollisionMask = 1 + 2 + 16
};
2022-11-12 16:45:04 -08:00
2023-06-03 18:21:46 -07:00
var result = spaceState.IntersectRay(rayParams);
2022-11-12 16:45:04 -08:00
2023-06-03 18:21:46 -07:00
// if we hit something
if (result.Count > 0)
{
// then we subtract the value of this from the other weights
float oldWeight = _weights[i];
for (int j = 0; j < 16; j++)
2022-11-27 19:37:16 -08:00
{
2023-06-03 18:21:46 -07:00
if (i == j)
2022-11-10 20:29:53 -08:00
{
2023-06-03 18:21:46 -07:00
_weights[i] = 0;
}
else
{
float dot = _weightDirs[i].Dot(_weightDirs[j]);
_weights[j] -= _weights[j] * dot;
2022-11-10 20:29:53 -08:00
}
}
2022-11-12 16:45:04 -08:00
}
2023-06-03 18:21:46 -07:00
}
2022-11-12 16:45:04 -08:00
2022-11-10 20:29:53 -08:00
2023-06-03 18:21:46 -07:00
float bestWeight = 0;
for (int i = 0; i < 16; i++)
{
if (_weights[i] > bestWeight)
2022-11-12 16:45:04 -08:00
{
2023-06-03 18:21:46 -07:00
_bestWeightIdx = i;
bestWeight = _weights[i];
2022-11-27 19:37:16 -08:00
}
}
2023-06-03 18:21:46 -07:00
}
2022-11-27 19:37:16 -08:00
2023-06-03 18:21:46 -07:00
protected virtual void Think()
{
// TODO: the entity should wander if it doesn't find a best target
Character bestTarget = FindBestTarget();
if (bestTarget is not null)
2022-11-27 19:37:16 -08:00
{
2023-06-03 18:21:46 -07:00
Vector2 pos = FindBestTarget().GlobalPosition;
Target = pos - GlobalPosition;
Vector2 dir = Target;
float dist = GlobalPosition.DistanceSquaredTo(pos);
UpdateWeights(pos);
2023-03-19 23:36:36 -07:00
2023-07-23 11:05:01 -07:00
if (dist < 1600 && CanAttack)
2023-06-03 18:21:46 -07:00
{
if (Inventory.SelectedItem is Weapon weapon)
2022-11-27 19:37:16 -08:00
{
2023-06-03 18:21:46 -07:00
UseCurrentItem();
2022-11-27 19:37:16 -08:00
}
}
2022-11-10 20:29:53 -08:00
}
}
}