SupaLidlGame/Characters/NPC.cs

209 lines
6.6 KiB
C#
Raw Normal View History

2022-11-10 20:29:53 -08:00
using Godot;
2022-11-19 21:21:12 -08:00
using SupaLidlGame.Items;
2022-11-10 20:29:53 -08:00
using System;
namespace SupaLidlGame.Characters
{
public partial class NPC : Character
{
2022-11-12 16:45:04 -08:00
/// <summary>
/// Time in seconds it takes for the NPC to think FeelsDankCube
/// </summary>
2022-11-19 21:21:12 -08:00
public const float ThinkTime = 0.125f;
2022-11-10 20:29:53 -08:00
public float[] Weights => _weights;
2022-11-13 19:52:09 -08:00
protected float[] _weights = new float[16];
protected Vector2[] _weightDirs = new Vector2[16];
protected int _bestWeightIdx;
2022-11-10 20:29:53 -08:00
protected double _thinkTimeElapsed = 0;
public override void _Ready()
{
base._Ready();
Array.Fill(_weights, 0);
2022-11-12 16:45:04 -08:00
for (int i = 0; i < 16; i++)
{
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
}
2022-11-13 15:42:04 -08:00
/*
2022-11-10 20:29:53 -08:00
public override void _Process(double delta)
{
if ((_thinkTimeElapsed += delta) > ThinkTime)
{
_thinkTimeElapsed = 0;
Think();
}
2022-11-12 16:45:04 -08:00
Direction = _weightDirs[_bestWeightIdx];
2022-11-10 20:29:53 -08:00
//Direction = (Target.GlobalPosition - GlobalPosition).Normalized();
base._Process(delta);
}
2022-11-13 15:42:04 -08:00
*/
2022-11-10 20:29:53 -08:00
public override void _Draw()
{
2022-11-12 16:45:04 -08:00
for (int i = 0; i < 16; i++)
2022-11-10 20:29:53 -08:00
{
2022-11-12 16:45:04 -08:00
Vector2 vec = _weightDirs[i] * _weights[i] * 128;
Color c = Colors.Green;
if (_bestWeightIdx == i)
{
c = Colors.Blue;
}
else if (_weights[i] < 0)
{
c = Colors.Red;
vec = -vec;
}
DrawLine(Vector2.Zero, vec, c);
2022-11-10 20:29:53 -08:00
}
base._Draw();
}
2022-11-12 16:45:04 -08:00
protected virtual Character FindBestTarget()
2022-11-10 20:29:53 -08:00
{
float bestDist = float.MaxValue;
Character bestChar = null;
foreach (Node node in GetParent().GetChildren())
{
2022-11-19 21:21:12 -08:00
if (node is Character character && character.Faction != Faction)
2022-11-10 20:29:53 -08:00
{
float dist = Position.DistanceTo(character.Position);
if (dist < bestDist)
{
bestDist = dist;
bestChar = character;
}
}
}
return bestChar;
}
2022-11-13 15:42:04 -08:00
public void ThinkProcess(double delta)
{
if ((_thinkTimeElapsed += delta) > ThinkTime)
{
_thinkTimeElapsed = 0;
Think();
}
Direction = _weightDirs[_bestWeightIdx];
}
2022-11-19 21:21:12 -08:00
protected virtual void Think()
2022-11-10 20:29:53 -08:00
{
2022-11-12 16:45:04 -08:00
Vector2 pos = FindBestTarget().GlobalPosition;
2022-11-19 21:21:12 -08:00
Target = pos - GlobalPosition;//GlobalPosition.DirectionTo(pos);
Vector2 dir = Target.Normalized();
2022-11-12 16:45:04 -08:00
float dist = GlobalPosition.DistanceSquaredTo(pos);
2022-11-10 20:29:53 -08:00
2022-11-19 21:21:12 -08:00
if (Target.LengthSquared() < 1024)
{
GD.Print("lol");
if (Inventory.SelectedItem is Weapon weapon)
{
GD.Print("use");
weapon.Use();
}
}
2022-11-12 16:45:04 -08:00
for (int i = 0; i < 16; i++)
2022-11-10 20:29:53 -08:00
{
2022-11-12 16:45:04 -08:00
float directDot = _weightDirs[i].Dot(dir);
directDot = (directDot + 1) / 2;
// this dot product resembles values of sine rather than cosine
// use it to weigh direction horizontally
Vector2 rotatedDir = new Vector2(-dir.y, dir.x);
float horizDot = Math.Abs(_weightDirs[i].Dot(rotatedDir));
2022-11-13 15:42:04 -08:00
// this is a smaller weight so they are more likely to pick the
// direction they are currently heading when choosing between two
// horizontal weights
2022-11-12 16:45:04 -08:00
float currDirDot = (_weightDirs[i].Dot(Direction) + 1) / 16;
// square so lower values are even lower
horizDot = Mathf.Pow((horizDot + 1) / 2, 2) + currDirDot;
// "When will I use math in the real world" Clueful
2022-11-19 21:21:12 -08:00
if (dist > 4096)
2022-11-12 16:45:04 -08:00
{
_weights[i] = directDot;
}
else if (dist > 64)
2022-11-10 20:29:53 -08:00
{
2022-11-19 21:21:12 -08:00
//float directDotWeighting = dist / 4096;
//float directDotWeighting = Mathf.Log(dist) / _log1024;
float directDotWeighting = Mathf.Sqrt(dist / 4096);
2022-11-12 16:45:04 -08:00
float horizDotWeighting = 1 - directDotWeighting;
_weights[i] = (directDot * directDotWeighting) +
2022-11-19 21:21:12 -08:00
(horizDot * horizDotWeighting * 0.5f);
2022-11-10 20:29:53 -08:00
}
else
{
2022-11-12 16:45:04 -08:00
// shorter than 64
_weights[i] = horizDot;
2022-11-10 20:29:53 -08:00
}
2022-11-12 16:45:04 -08:00
// now we shall subtract weights whose rays collide
// with something
2022-11-10 20:29:53 -08:00
{
var spaceState = GetWorld2d().DirectSpaceState;
2022-11-12 16:45:04 -08:00
var exclude = new Godot.Collections.Array<RID>();
exclude.Add(this.GetRid());
var rayParams = new PhysicsRayQueryParameters2D
{
Exclude = exclude,
CollideWithBodies = true,
From = GlobalPosition,
To = GlobalPosition + (_weightDirs[i] * 16)
};
var result = spaceState.IntersectRay(rayParams);
// if our ray cast hits something
2022-11-10 20:29:53 -08:00
if (result.Count > 0)
{
2022-11-12 16:45:04 -08:00
// then we subtract the dot product of other directions
for (int j = 0; j < 16; j++)
{
if (i == j)
{
_weights[i] = 0;
}
else
{
2022-11-19 21:21:12 -08:00
float dot = _weightDirs[i].Dot(_weightDirs[j]) / 2;
2022-11-13 15:42:04 -08:00
_weights[j] -= (dot + 1) / 2;
2022-11-12 16:45:04 -08:00
}
}
2022-11-10 20:29:53 -08:00
}
}
2022-11-12 16:45:04 -08:00
}
float bestWeight = 0;
2022-11-10 20:29:53 -08:00
2022-11-12 16:45:04 -08:00
for (int i = 0; i < 16; i++)
{
2022-11-10 20:29:53 -08:00
if (_weights[i] > bestWeight)
{
bestWeight = _weights[i];
2022-11-12 16:45:04 -08:00
_bestWeightIdx = i;
2022-11-10 20:29:53 -08:00
}
}
2022-11-12 16:45:04 -08:00
2022-11-10 20:29:53 -08:00
QueueRedraw();
}
}
}