2021-01-05 02:56:17 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* 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;
|
|
|
|
|
using MessagePack;
|
2021-01-25 05:53:36 +00:00
|
|
|
using Unity.Mathematics;
|
|
|
|
|
using static Unity.Mathematics.math;
|
2021-01-05 02:56:17 +00:00
|
|
|
|
2026-06-13 04:56:50 +01:00
|
|
|
[Inspectable, MessagePackObject]
|
2021-01-05 02:56:17 +00:00
|
|
|
public class ConstantWeaponData : WeaponData
|
|
|
|
|
{
|
2026-06-13 04:56:50 +01:00
|
|
|
[InspectablePrefab, Key(17)]
|
2021-01-25 05:53:36 +00:00
|
|
|
public float AmmoInterval = 1;
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public override Behavior CreateInstance(EquippedItem item)
|
|
|
|
|
{
|
|
|
|
|
return new ConstantWeapon(this, item);
|
|
|
|
|
}
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public override Behavior CreateInstance(ConsumableItemEffect item)
|
2021-01-05 02:56:17 +00:00
|
|
|
{
|
2021-04-12 04:20:52 +01:00
|
|
|
return new ConstantWeapon(this, item);
|
2021-01-05 02:56:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-21 01:55:25 +00:00
|
|
|
public class ConstantWeapon : Weapon, IProgressBehavior, IEventBehavior
|
2021-01-05 02:56:17 +00:00
|
|
|
{
|
|
|
|
|
private ConstantWeaponData _data;
|
2021-01-25 05:53:36 +00:00
|
|
|
private int _ammo = 1;
|
|
|
|
|
private float _ammoInterval;
|
|
|
|
|
private float _reload;
|
|
|
|
|
private bool _reloading;
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-01-25 05:53:36 +00:00
|
|
|
public override int Ammo
|
|
|
|
|
{
|
|
|
|
|
get => _ammo;
|
|
|
|
|
}
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-01-25 05:53:36 +00:00
|
|
|
public float Progress
|
|
|
|
|
{
|
|
|
|
|
get { return saturate(_reload); }
|
|
|
|
|
}
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-02-26 19:01:47 +00:00
|
|
|
public override float DamagePerSecond => Damage;
|
|
|
|
|
public override float RangeDamagePerSecond(float range)
|
|
|
|
|
{
|
|
|
|
|
return Damage * _data.DamageCurve.Evaluate(saturate(unlerp(MinRange, Range, range)));
|
|
|
|
|
}
|
2021-01-05 02:56:17 +00:00
|
|
|
|
2021-01-25 05:53:36 +00:00
|
|
|
public event Action OnReloadBegin;
|
|
|
|
|
public event Action OnReloadComplete;
|
2021-01-05 02:56:17 +00:00
|
|
|
public event Action OnStartFiring;
|
|
|
|
|
public event Action OnStopFiring;
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-03-21 01:55:25 +00:00
|
|
|
public void ResetEvents()
|
2021-01-31 03:52:32 +00:00
|
|
|
{
|
|
|
|
|
OnReloadBegin = null;
|
|
|
|
|
OnReloadComplete = null;
|
|
|
|
|
OnStartFiring = null;
|
|
|
|
|
OnStopFiring = null;
|
|
|
|
|
}
|
2021-01-05 02:56:17 +00:00
|
|
|
|
2021-04-12 04:20:52 +01:00
|
|
|
public ConstantWeapon(ConstantWeaponData data, EquippedItem item) : base(data, item)
|
2021-01-25 05:53:36 +00:00
|
|
|
{
|
|
|
|
|
_data = data;
|
|
|
|
|
}
|
2021-01-05 02:56:17 +00:00
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
public ConstantWeapon(ConstantWeaponData data, ConsumableItemEffect item) : base(data, item)
|
|
|
|
|
{
|
|
|
|
|
_data = data;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 03:29:14 +01:00
|
|
|
public override bool Execute(float dt)
|
2021-01-05 02:56:17 +00:00
|
|
|
{
|
2021-04-14 03:29:14 +01:00
|
|
|
base.Execute(dt);
|
2021-01-25 05:53:36 +00:00
|
|
|
if (_firing)
|
|
|
|
|
{
|
2021-04-18 05:13:20 +01:00
|
|
|
if (!Entity.TryConsumeEnergy(Evaluate(_data.Energy) * dt))
|
2021-03-02 23:02:19 +00:00
|
|
|
{
|
|
|
|
|
_firing = false;
|
|
|
|
|
OnStopFiring?.Invoke();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-01-25 05:53:36 +00:00
|
|
|
if (_data.AmmoType != Guid.Empty)
|
|
|
|
|
{
|
|
|
|
|
if (_reloading)
|
|
|
|
|
{
|
2021-04-14 03:29:14 +01:00
|
|
|
_reload -= dt / _data.ReloadTime;
|
2021-01-25 05:53:36 +00:00
|
|
|
if (_reload < 0)
|
|
|
|
|
{
|
|
|
|
|
_reloading = false;
|
|
|
|
|
OnReloadComplete?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-04-14 03:29:14 +01:00
|
|
|
_ammoInterval -= dt / _data.AmmoInterval;
|
2021-01-25 05:53:36 +00:00
|
|
|
if (_ammoInterval < 0)
|
|
|
|
|
{
|
|
|
|
|
_ammoInterval = 1;
|
|
|
|
|
if (_data.MagazineSize > 1 && _ammo > 0) _ammo--;
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-18 05:13:20 +01:00
|
|
|
var cargo = Entity.FindItemInCargo(_data.AmmoType);
|
2021-01-25 05:53:36 +00:00
|
|
|
if (cargo != null)
|
|
|
|
|
{
|
|
|
|
|
var item = cargo.ItemsOfType[_data.AmmoType][0];
|
|
|
|
|
if (item is SimpleCommodity simpleCommodity)
|
|
|
|
|
cargo.Remove(simpleCommodity, 1);
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2021-01-25 05:53:36 +00:00
|
|
|
if(_data.MagazineSize > 1)
|
|
|
|
|
{
|
|
|
|
|
_reloading = true;
|
|
|
|
|
_reload = 1;
|
|
|
|
|
OnReloadBegin?.Invoke();
|
|
|
|
|
|
|
|
|
|
_firing = false;
|
|
|
|
|
OnStopFiring?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-04 03:58:46 +00:00
|
|
|
|
2021-04-18 05:13:20 +01:00
|
|
|
CauseWearDamage(dt);
|
|
|
|
|
AddHeat(Evaluate(_data.Heat) * dt);
|
|
|
|
|
Entity.VisibilitySources[this] = Evaluate(_data.Visibility);
|
2021-01-25 05:53:36 +00:00
|
|
|
}
|
|
|
|
|
return true;
|
2021-01-05 02:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-25 05:53:36 +00:00
|
|
|
public override void Activate()
|
2021-01-05 02:56:17 +00:00
|
|
|
{
|
2021-02-11 03:14:20 +00:00
|
|
|
if(!_firing && !_reloading)
|
2021-01-05 02:56:17 +00:00
|
|
|
{
|
|
|
|
|
_firing = true;
|
|
|
|
|
OnStartFiring?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 05:53:36 +00:00
|
|
|
public override void Deactivate()
|
2021-01-05 02:56:17 +00:00
|
|
|
{
|
|
|
|
|
if (_firing)
|
|
|
|
|
{
|
2021-01-25 05:53:36 +00:00
|
|
|
_firing = false;
|
|
|
|
|
OnStopFiring?.Invoke();
|
2021-01-05 02:56:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|