Misc. Improvements

Turrets lead their projectiles
Double click to equip/unequip/move
Minimum weapon convergence distance
This commit is contained in:
Reinier Vens 2021-01-27 16:14:41 +00:00
commit 922c8ea460
18 changed files with 240 additions and 69 deletions

View file

@ -0,0 +1,67 @@
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using static Unity.Mathematics.math;
public class AetheriaMath
{
//first-order intercept using absolute target position
public static float3 FirstOrderIntercept
(
float3 shooterPosition,
float3 shooterVelocity,
float shotSpeed,
float3 targetPosition,
float3 targetVelocity
) {
float3 targetRelativePosition = targetPosition - shooterPosition;
float3 targetRelativeVelocity = targetVelocity - shooterVelocity;
float t = FirstOrderInterceptTime
(
shotSpeed,
targetRelativePosition,
targetRelativeVelocity
);
return targetPosition + t*targetRelativeVelocity;
}
//first-order intercept using relative target position
public static float FirstOrderInterceptTime
(
float shotSpeed,
float3 targetRelativePosition,
float3 targetRelativeVelocity
) {
float velocitySquared = lengthsq(targetRelativeVelocity);
if(velocitySquared < 0.001f)
return 0f;
float a = velocitySquared - shotSpeed*shotSpeed;
//handle similar velocities
if (abs(a) < 0.001f)
{
float t = -lengthsq(targetRelativePosition)/ (2f*dot(targetRelativeVelocity, targetRelativePosition));
return max(t, 0f); //don't shoot back in time
}
float b = 2f*dot(targetRelativeVelocity, targetRelativePosition);
float c = lengthsq(targetRelativePosition);
float determinant = b*b - 4f*a*c;
if (determinant > 0f) { //determinant > 0; two intercept paths (most common)
float t1 = (-b + sqrt(determinant))/(2f*a),
t2 = (-b - sqrt(determinant))/(2f*a);
if (t1 > 0f) {
if (t2 > 0f)
return min(t1, t2); //both are positive
else
return t1; //only t1 is positive
} else
return max(t2, 0f); //don't shoot back in time
} else if (determinant < 0f) //determinant < 0; no intercept path
return 0f;
else //determinant = 0; one intercept path, pretty much never happens
return max(-b/(2f*a), 0f); //don't shoot back in time
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 70c5f24f81c803248a4f682e6eb12ef2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -9,6 +9,7 @@ using Newtonsoft.Json;
using UniRx;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using float3 = Unity.Mathematics.float3;
[MessagePackObject, JsonObject(MemberSerialization.OptIn), RuntimeInspectable]
public class TurretControllerData : BehaviorData
@ -23,6 +24,8 @@ public class TurretController : IBehavior
{
private TurretControllerData _data;
private List<Weapon> _weapons = new List<Weapon>();
private float _shotSpeed;
private bool _predictShots;
public Entity EquippedEntity { get; }
public EquippedItem Item { get; }
@ -55,7 +58,14 @@ public class TurretController : IBehavior
{
foreach (var b in item.Behaviors)
if (b is Weapon weapon)
{
_weapons.Add(weapon);
if (weapon.Data is ProjectileWeaponData projectileWeaponData)
{
_predictShots = true;
_shotSpeed = EquippedEntity.ItemManager.Evaluate(projectileWeaponData.Velocity, weapon.Item.EquippableItem, EquippedEntity);
}
}
}
public bool Execute(float delta)
@ -63,7 +73,19 @@ public class TurretController : IBehavior
if (EquippedEntity.Target.Value != null)
{
var diff = EquippedEntity.Target.Value.Position - EquippedEntity.Position;
EquippedEntity.LookDirection = normalize(diff);
if (_predictShots)
{
var targetHullData = EquippedEntity.ItemManager.GetData(EquippedEntity.Target.Value.Hull) as HullData;
var targetVelocity = float3(EquippedEntity.Target.Value.Velocity.x, 0, EquippedEntity.Target.Value.Velocity.y);
var predictedPosition = AetheriaMath.FirstOrderIntercept(
EquippedEntity.Position, float3.zero, _shotSpeed,
EquippedEntity.Target.Value.Position, targetVelocity
);
predictedPosition.y = EquippedEntity.Zone.GetHeight(predictedPosition.xz) + targetHullData.GridOffset;
EquippedEntity.LookDirection = normalize(predictedPosition - EquippedEntity.Position);
}
else
EquippedEntity.LookDirection = normalize(diff);
var dist = length(diff);
foreach (var x in _weapons)

View file

@ -859,6 +859,15 @@ public class EquippedCargoBay : EquippedItem
return false;
}
public bool TryStore(ItemInstance item)
{
if (item is SimpleCommodity simpleCommodity)
return TryStore(simpleCommodity);
if (item is CraftedItemInstance craftedItem)
return TryStore(craftedItem);
return false;
}
public bool TryStore(ItemInstance item, int2 cargoCoord)
{

View file

@ -111,4 +111,5 @@ public class GameplaySettings
public float TorqueFloor;
public float TorqueMultiplier;
public float VisibilityDecay;
public float ConvergenceMinimumDistance;
}