2026-06-13 04:56:50 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2021-01-06 14:56:39 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using MessagePack;
|
|
|
|
|
using UniRx;
|
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
using static Unity.Mathematics.math;
|
2021-01-27 16:14:41 +00:00
|
|
|
using float3 = Unity.Mathematics.float3;
|
2021-01-06 14:56:39 +00:00
|
|
|
|
2026-06-13 04:56:50 +01:00
|
|
|
[MessagePackObject, RuntimeInspectable]
|
2021-01-06 14:56:39 +00:00
|
|
|
public class TurretControllerData : BehaviorData
|
|
|
|
|
{
|
2021-04-18 05:13:20 +01:00
|
|
|
public override Behavior CreateInstance(EquippedItem item)
|
|
|
|
|
{
|
|
|
|
|
return new TurretController(this, item);
|
|
|
|
|
}
|
|
|
|
|
public override Behavior CreateInstance(ConsumableItemEffect item)
|
2021-01-06 14:56:39 +00:00
|
|
|
{
|
2021-04-12 04:20:52 +01:00
|
|
|
return new TurretController(this, item);
|
2021-01-06 14:56:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public class TurretController : Behavior, IInitializableBehavior
|
2021-01-06 14:56:39 +00:00
|
|
|
{
|
|
|
|
|
private TurretControllerData _data;
|
2021-01-25 05:53:36 +00:00
|
|
|
private List<Weapon> _weapons = new List<Weapon>();
|
2021-01-27 16:14:41 +00:00
|
|
|
private float _shotSpeed;
|
|
|
|
|
private bool _predictShots;
|
2021-01-06 14:56:39 +00:00
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public TurretController(TurretControllerData data, EquippedItem item) : base(data, item)
|
2021-01-06 14:56:39 +00:00
|
|
|
{
|
|
|
|
|
_data = data;
|
2021-04-18 05:13:20 +01:00
|
|
|
}
|
2021-01-06 14:56:39 +00:00
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public TurretController(TurretControllerData data, ConsumableItemEffect item) : base(data, item)
|
|
|
|
|
{
|
|
|
|
|
_data = data;
|
2021-01-06 14:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public void Initialize()
|
2021-01-06 14:56:39 +00:00
|
|
|
{
|
2021-04-18 05:13:20 +01:00
|
|
|
foreach (var weapon in Entity.GetBehaviors<Weapon>())
|
|
|
|
|
{
|
|
|
|
|
_weapons.Add(weapon);
|
|
|
|
|
var vel = weapon.Evaluate(weapon.WeaponData.Velocity);
|
|
|
|
|
if (vel > .1f)
|
2021-01-27 16:14:41 +00:00
|
|
|
{
|
2021-04-18 05:13:20 +01:00
|
|
|
_predictShots = true;
|
|
|
|
|
_shotSpeed = vel;
|
2021-01-27 16:14:41 +00:00
|
|
|
}
|
2021-04-18 05:13:20 +01:00
|
|
|
}
|
2021-01-06 14:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public override bool Execute(float dt)
|
2021-01-06 14:56:39 +00:00
|
|
|
{
|
2021-04-18 05:13:20 +01:00
|
|
|
if (Entity.Target.Value != null)
|
2021-01-06 14:56:39 +00:00
|
|
|
{
|
2021-04-18 05:13:20 +01:00
|
|
|
var diff = Entity.Target.Value.Position - Entity.Position;
|
2021-01-27 16:14:41 +00:00
|
|
|
if (_predictShots)
|
|
|
|
|
{
|
2021-04-18 05:13:20 +01:00
|
|
|
var targetHullData = Entity.ItemManager.GetData(Entity.Target.Value.Hull) as HullData;
|
|
|
|
|
var targetVelocity = float3(Entity.Target.Value.Velocity.x, 0, Entity.Target.Value.Velocity.y);
|
2021-01-27 16:14:41 +00:00
|
|
|
var predictedPosition = AetheriaMath.FirstOrderIntercept(
|
2021-04-18 05:13:20 +01:00
|
|
|
Entity.Position, float3.zero, _shotSpeed,
|
|
|
|
|
Entity.Target.Value.Position, targetVelocity
|
2021-01-27 16:14:41 +00:00
|
|
|
);
|
2021-04-18 05:13:20 +01:00
|
|
|
predictedPosition.y = Entity.Zone.GetHeight(predictedPosition.xz) + targetHullData.GridOffset;
|
|
|
|
|
Entity.LookDirection = normalize(predictedPosition - Entity.Position);
|
2021-01-27 16:14:41 +00:00
|
|
|
}
|
|
|
|
|
else
|
2021-04-18 05:13:20 +01:00
|
|
|
Entity.LookDirection = normalize(diff);
|
2021-01-06 14:56:39 +00:00
|
|
|
var dist = length(diff);
|
|
|
|
|
|
2021-01-25 05:53:36 +00:00
|
|
|
foreach (var x in _weapons)
|
2021-01-06 14:56:39 +00:00
|
|
|
{
|
2021-01-25 05:53:36 +00:00
|
|
|
var data = x.Data as WeaponData;
|
|
|
|
|
var fire = dot(
|
2021-04-18 05:13:20 +01:00
|
|
|
x.Direction,
|
|
|
|
|
Entity.LookDirection) > .99f;
|
|
|
|
|
if (x.Evaluate(data.Range) > dist && fire)
|
2021-01-06 14:56:39 +00:00
|
|
|
{
|
2021-01-25 05:53:36 +00:00
|
|
|
x.Activate();
|
2021-01-06 14:56:39 +00:00
|
|
|
}
|
2021-01-25 05:53:36 +00:00
|
|
|
else if (x.Firing)
|
|
|
|
|
x.Deactivate();
|
2021-01-06 14:56:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-02-27 20:53:36 +00:00
|
|
|
foreach (var x in _weapons)
|
|
|
|
|
{
|
|
|
|
|
if (x.Firing)
|
|
|
|
|
x.Deactivate();
|
|
|
|
|
}
|
2021-05-11 04:45:42 +01:00
|
|
|
Entity.Target.Value = Entity.VisibleEnemies.FirstOrDefault(e => e is Ship);
|
2021-01-06 14:56:39 +00:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|