Aetheria/Assets/Scripts/ServerShared/Behaviors/MiningTool.cs
2026-06-16 15:08:00 +01:00

72 lines
2.9 KiB
C#

/* 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 System.Linq;
using Unity.Mathematics;
using static Unity.Mathematics.math;
public class MiningTool : Behavior
{
public string AsteroidBeltBodyKey = "";
public int Asteroid;
private readonly PerformanceStat _damagePerSecond;
private readonly PerformanceStat _efficiency;
private readonly PerformanceStat _penetration;
private readonly PerformanceStat _range;
public float Range { get; private set; }
public MiningTool(RuntimeBehaviorDefinition definition, EquippedItem item) : base(definition, item)
{
_damagePerSecond = definition.PerformanceStat(1, new PerformanceStat());
_efficiency = definition.PerformanceStat(2, new PerformanceStat());
_penetration = definition.PerformanceStat(3, new PerformanceStat());
_range = definition.PerformanceStat(4, new PerformanceStat());
RegisterPerformanceStat("DamagePerSecond", _damagePerSecond);
RegisterPerformanceStat("Efficiency", _efficiency);
RegisterPerformanceStat("Penetration", _penetration);
RegisterPerformanceStat(nameof(Range), _range);
}
public MiningTool(RuntimeBehaviorDefinition definition, ConsumableItemEffect item) : base(definition, item)
{
_damagePerSecond = definition.PerformanceStat(1, new PerformanceStat());
_efficiency = definition.PerformanceStat(2, new PerformanceStat());
_penetration = definition.PerformanceStat(3, new PerformanceStat());
_range = definition.PerformanceStat(4, new PerformanceStat());
RegisterPerformanceStat("DamagePerSecond", _damagePerSecond);
RegisterPerformanceStat("Efficiency", _efficiency);
RegisterPerformanceStat("Penetration", _penetration);
RegisterPerformanceStat(nameof(Range), _range);
}
public override bool Execute(float dt)
{
Range = Evaluate(_range);
if (Entity.Zone.TryGetAsteroidBelt(AsteroidBeltBodyKey, out var belt) &&
!string.IsNullOrWhiteSpace(AsteroidBeltBodyKey) &&
Entity.Zone.AsteroidExists(AsteroidBeltBodyKey, Asteroid) &&
length(Entity.Position.xz - belt.Transforms[Asteroid].xy) - belt.Transforms[Asteroid].w < Range)
{
Entity.Zone.MineAsteroid(
Entity,
AsteroidBeltBodyKey,
Asteroid,
Evaluate(_damagePerSecond) * dt,
Evaluate(_efficiency),
Evaluate(_penetration));
return true;
}
return false;
}
public void RestoreRuntimeState(string asteroidBeltBodyKey, int asteroid, float range)
{
AsteroidBeltBodyKey = asteroidBeltBodyKey ?? "";
Asteroid = asteroid;
Range = range;
}
}