Weapon Scripting Overhaul
Weapons are now monolithic behaviors Various new weapon behavior types implemented All weapons have optional magazine and reload mechanic Visibility now decays globally
This commit is contained in:
parent
48abee4847
commit
357f9db329
95 changed files with 2571 additions and 754 deletions
|
|
@ -5,67 +5,117 @@
|
|||
using System;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[InspectableField, MessagePackObject, JsonObject(MemberSerialization.OptIn)]
|
||||
public class ConstantWeaponData : WeaponData
|
||||
{
|
||||
[InspectablePrefab, JsonProperty("ammoInterval"), Key(13)]
|
||||
public float AmmoInterval = 1;
|
||||
|
||||
public override IBehavior CreateInstance(ItemManager context, Entity entity, EquippedItem item)
|
||||
{
|
||||
return new ConstantWeapon(context, this, entity, item);
|
||||
}
|
||||
}
|
||||
|
||||
public class ConstantWeapon : IBehavior, IAlwaysUpdatedBehavior
|
||||
public class ConstantWeapon : Weapon, IProgressBehavior
|
||||
{
|
||||
private ConstantWeaponData _data;
|
||||
private int _ammo = 1;
|
||||
private float _ammoInterval;
|
||||
private float _reload;
|
||||
private bool _reloading;
|
||||
|
||||
private Entity Entity { get; }
|
||||
private EquippedItem Item { get; }
|
||||
private ItemManager Context { get; }
|
||||
public override int Ammo
|
||||
{
|
||||
get => _ammo;
|
||||
}
|
||||
|
||||
public BehaviorData Data => _data;
|
||||
public float Progress
|
||||
{
|
||||
get { return saturate(_reload); }
|
||||
}
|
||||
|
||||
public event Action OnReloadBegin;
|
||||
public event Action OnReloadComplete;
|
||||
public event Action OnStartFiring;
|
||||
public event Action OnStopFiring;
|
||||
|
||||
private bool _firing;
|
||||
private bool _stoppedFiring;
|
||||
|
||||
public ConstantWeapon(ItemManager context, ConstantWeaponData c, Entity entity, EquippedItem item)
|
||||
public ConstantWeapon(ItemManager context, ConstantWeaponData data, Entity entity, EquippedItem item) : base(context, data, entity, item)
|
||||
{
|
||||
Context = context;
|
||||
_data = c;
|
||||
Entity = entity;
|
||||
Item = item;
|
||||
_data = data;
|
||||
}
|
||||
|
||||
public bool Execute(float delta)
|
||||
public override bool Execute(float delta)
|
||||
{
|
||||
if (!_firing)
|
||||
if (_firing)
|
||||
{
|
||||
if (_data.AmmoType != Guid.Empty)
|
||||
{
|
||||
if (_reloading)
|
||||
{
|
||||
_reload -= delta / _data.ReloadTime;
|
||||
if (_reload < 0)
|
||||
{
|
||||
_reloading = false;
|
||||
OnReloadComplete?.Invoke();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
_ammoInterval -= delta / _data.AmmoInterval;
|
||||
if (_ammoInterval < 0)
|
||||
{
|
||||
_ammoInterval = 1;
|
||||
if (_data.MagazineSize > 1 && _ammo > 0) _ammo--;
|
||||
else
|
||||
{
|
||||
var cargo = Entity.FindItemInCargo(_data.AmmoType);
|
||||
if (cargo != null)
|
||||
{
|
||||
var item = cargo.ItemsOfType[_data.AmmoType][0];
|
||||
if (item is SimpleCommodity simpleCommodity)
|
||||
cargo.Remove(simpleCommodity, 1);
|
||||
|
||||
if(_data.MagazineSize > 1)
|
||||
{
|
||||
_reloading = true;
|
||||
_reload = 1;
|
||||
OnReloadBegin?.Invoke();
|
||||
|
||||
_firing = false;
|
||||
OnStopFiring?.Invoke();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Item.AddHeat(Context.Evaluate(_data.Heat, Item.EquippableItem, Entity) * delta);
|
||||
Entity.VisibilitySources[this] = Context.Evaluate(_data.Visibility, Item.EquippableItem, Entity);
|
||||
Entity.Energy -= Context.Evaluate(_data.Energy, Item.EquippableItem, Entity) * delta;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Activate()
|
||||
{
|
||||
if(!_reloading)
|
||||
{
|
||||
_firing = true;
|
||||
OnStartFiring?.Invoke();
|
||||
}
|
||||
|
||||
_stoppedFiring = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
public override void Deactivate()
|
||||
{
|
||||
// Update executes after AlwaysUpdate,
|
||||
// so if the stop flag didn't get disabled previous frame,
|
||||
// we know the weapon has stopped firing
|
||||
if (_firing)
|
||||
{
|
||||
if (_stoppedFiring)
|
||||
{
|
||||
_firing = false;
|
||||
_stoppedFiring = false;
|
||||
OnStopFiring?.Invoke();
|
||||
}
|
||||
else
|
||||
_stoppedFiring = true;
|
||||
_firing = false;
|
||||
OnStopFiring?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue