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;
|
2026-06-21 09:37:54 +01:00
|
|
|
using static CultMath.math;
|
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
|
|
|
{
|
2026-06-15 18:27:29 +01:00
|
|
|
private readonly float _ammoIntervalDuration;
|
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-14 14:19:21 +01:00
|
|
|
public float AmmoIntervalProgress => _ammoInterval;
|
|
|
|
|
public float ReloadProgress => _reload;
|
|
|
|
|
public bool Reloading => _reloading;
|
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)
|
|
|
|
|
{
|
2026-06-21 09:37:54 +01:00
|
|
|
return EvaluateRangeDamage(range);
|
2021-02-26 19:01:47 +00:00
|
|
|
}
|
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
|
|
|
|
2026-06-15 20:12:34 +01:00
|
|
|
public ConstantWeapon(RuntimeBehaviorDefinition definition, EquippedItem item) : base(definition, item)
|
2021-01-25 05:53:36 +00:00
|
|
|
{
|
2026-06-15 20:12:34 +01:00
|
|
|
_ammoIntervalDuration = definition.Float(17, 1);
|
2021-01-25 05:53:36 +00:00
|
|
|
}
|
2021-01-05 02:56:17 +00:00
|
|
|
|
2026-06-15 20:12:34 +01:00
|
|
|
public ConstantWeapon(RuntimeBehaviorDefinition definition, ConsumableItemEffect item) : base(definition, item)
|
2021-04-18 05:13:20 +01:00
|
|
|
{
|
2026-06-15 20:12:34 +01:00
|
|
|
_ammoIntervalDuration = definition.Float(17, 1);
|
2021-04-18 05:13:20 +01:00
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2026-06-15 18:27:29 +01:00
|
|
|
if (!Entity.TryConsumeEnergy(Energy * dt))
|
2021-03-02 23:02:19 +00:00
|
|
|
{
|
|
|
|
|
_firing = false;
|
|
|
|
|
OnStopFiring?.Invoke();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-06-15 18:27:29 +01:00
|
|
|
if (!string.IsNullOrWhiteSpace(AmmoItemKey))
|
2021-01-25 05:53:36 +00:00
|
|
|
{
|
|
|
|
|
if (_reloading)
|
|
|
|
|
{
|
2026-06-15 18:27:29 +01:00
|
|
|
_reload -= dt / 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
|
|
|
|
2026-06-15 18:27:29 +01:00
|
|
|
_ammoInterval -= dt / _ammoIntervalDuration;
|
2021-01-25 05:53:36 +00:00
|
|
|
if (_ammoInterval < 0)
|
|
|
|
|
{
|
|
|
|
|
_ammoInterval = 1;
|
2026-06-15 18:27:29 +01:00
|
|
|
if (MagazineSize > 1 && _ammo > 0) _ammo--;
|
2021-01-25 05:53:36 +00:00
|
|
|
else
|
|
|
|
|
{
|
2026-06-15 18:27:29 +01:00
|
|
|
var cargo = Entity.FindItemInCargo(AmmoItemKey);
|
2021-01-25 05:53:36 +00:00
|
|
|
if (cargo != null)
|
|
|
|
|
{
|
2026-06-15 18:27:29 +01:00
|
|
|
var item = cargo.GetFirstItem(AmmoItemKey);
|
2021-01-25 05:53:36 +00:00
|
|
|
if (item is SimpleCommodity simpleCommodity)
|
|
|
|
|
cargo.Remove(simpleCommodity, 1);
|
2026-06-13 04:56:50 +01:00
|
|
|
|
2026-06-15 18:27:29 +01:00
|
|
|
if(MagazineSize > 1)
|
2021-01-25 05:53:36 +00:00
|
|
|
{
|
|
|
|
|
_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);
|
2026-06-15 18:27:29 +01:00
|
|
|
AddHeat(Heat * dt);
|
|
|
|
|
Entity.VisibilitySources[this] = 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
|
|
|
{
|
2026-06-14 17:43:42 +01:00
|
|
|
UpdateStats();
|
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
|
|
|
}
|
|
|
|
|
}
|
2026-06-16 09:01:05 +01:00
|
|
|
|
|
|
|
|
public void RestoreRuntimeState(
|
|
|
|
|
bool firing,
|
|
|
|
|
int ammo,
|
|
|
|
|
float ammoIntervalProgress,
|
|
|
|
|
float reloadProgress,
|
|
|
|
|
bool reloading)
|
|
|
|
|
{
|
|
|
|
|
base.RestoreRuntimeState(firing);
|
|
|
|
|
_ammo = ammo;
|
|
|
|
|
_ammoInterval = ammoIntervalProgress;
|
|
|
|
|
_reload = reloadProgress;
|
|
|
|
|
_reloading = reloading;
|
|
|
|
|
}
|
2026-06-14 14:19:21 +01:00
|
|
|
}
|