Made a fancier lasergun (#174)

Laserguns now have an internal capacitor that can be recharged by using it with a power cell

Makes the final fix for #138
This commit is contained in:
ScumbagDog
2019-04-01 20:06:43 +02:00
committed by Pieter-Jan Briers
parent b94e015314
commit 1af1ee2ad4
8 changed files with 148 additions and 38 deletions

View File

@@ -0,0 +1,50 @@
using System;
using Content.Server.GameObjects.Components.Power;
using SS14.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Hitscan
{
public class HitscanWeaponCapacitorComponent : PowerCellComponent
{
public override string Name => "HitscanWeaponCapacitor";
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
}
public override void Initialize()
{
base.Initialize();
Charge = Capacity;
}
public float GetChargeFrom(float toDeduct)
{
//Use this function when you want to shoot even though you don't have enough energy for basecost
ChargeChanged();
var chargeChangedBy = Math.Min(this.Charge, toDeduct);
this.DeductCharge(chargeChangedBy);
return chargeChangedBy;
}
public void FillFrom(PowerStorageComponent battery)
{
var capacitorPowerDeficit = this.Capacity - this.Charge;
if (battery.CanDeductCharge(capacitorPowerDeficit))
{
battery.DeductCharge(capacitorPowerDeficit);
this.AddCharge(capacitorPowerDeficit);
}
else
{
this.AddCharge(battery.Charge);
battery.DeductCharge(battery.Charge);
}
}
}
}

View File

@@ -14,35 +14,74 @@ using SS14.Shared.Serialization;
using System;
using Content.Server.GameObjects.Components.Sound;
using SS14.Shared.GameObjects;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.GameObjects.Components.Power;
using Content.Shared.Interfaces;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Hitscan
{
public class HitscanWeaponComponent : Component
public class HitscanWeaponComponent : Component, IAttackby
{
private const float MaxLength = 20;
public override string Name => "HitscanWeapon";
string Spritename = "Objects/laser.png";
int Damage = 10;
string _spritename;
private int _damage;
private int _baseFireCost;
private float _lowerChargeLimit;
//As this is a component that sits on the weapon rather than a static value
//we just declare the field and then use GetComponent later to actually get it.
//Do remember to add it in both the .yaml prototype and the factory in EntryPoint.cs
//Otherwise you will get errors
private HitscanWeaponCapacitorComponent capacitorComponent;
public int Damage => _damage;
public int BaseFireCost => _baseFireCost;
public HitscanWeaponCapacitorComponent CapacitorComponent => capacitorComponent;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref Spritename, "sprite", "Objects/laser.png");
serializer.DataField(ref Damage, "damage", 10);
serializer.DataField(ref _spritename, "sprite", "Objects/laser.png");
serializer.DataField(ref _damage, "damage", 10);
serializer.DataField(ref _baseFireCost, "baseFireCost", 300);
serializer.DataField(ref _lowerChargeLimit, "lowerChargeLimit", 10);
}
public override void Initialize()
{
base.Initialize();
var rangedWeapon = Owner.GetComponent<RangedWeaponComponent>();
capacitorComponent = Owner.GetComponent<HitscanWeaponCapacitorComponent>();
rangedWeapon.FireHandler = Fire;
}
public bool Attackby(IEntity user, IEntity attackwith)
{
if (!attackwith.TryGetComponent(out PowerStorageComponent component))
{
return false;
}
if (capacitorComponent.Full)
{
Owner.PopupMessage(user, "Capacitor at max charge");
return false;
}
capacitorComponent.FillFrom(component);
return true;
}
private void Fire(IEntity user, GridCoordinates clickLocation)
{
if (capacitorComponent.Charge < _lowerChargeLimit)
{//If capacitor has less energy than the lower limit, do nothing
return;
}
float energyModifier = capacitorComponent.GetChargeFrom(_baseFireCost) / _baseFireCost;
var userPosition = user.Transform.WorldPosition; //Remember world positions are ephemeral and can only be used instantaneously
var angle = new Angle(clickLocation.Position - userPosition);
@@ -50,26 +89,28 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Hitscan
var rayCastResults = IoCManager.Resolve<IPhysicsManager>().IntersectRay(ray, MaxLength,
Owner.Transform.GetMapTransform().Owner);
Hit(rayCastResults);
AfterEffects(user, rayCastResults, angle);
Hit(rayCastResults, energyModifier);
AfterEffects(user, rayCastResults, angle, energyModifier);
}
protected virtual void Hit(RayCastResults ray)
protected virtual void Hit(RayCastResults ray, float damageModifier)
{
if (ray.HitEntity != null && ray.HitEntity.TryGetComponent(out DamageableComponent damage))
{
damage.TakeDamage(DamageType.Heat, Damage);
damage.TakeDamage(DamageType.Heat, (int)Math.Round(_damage * damageModifier, MidpointRounding.AwayFromZero));
//I used Math.Round over Convert.toInt32, as toInt32 always rounds to
//even numbers if halfway between two numbers, rather than rounding to nearest
}
}
protected virtual void AfterEffects(IEntity user, RayCastResults ray, Angle angle)
protected virtual void AfterEffects(IEntity user, RayCastResults ray, Angle angle, float energyModifier)
{
var time = IoCManager.Resolve<IGameTiming>().CurTime;
var dist = ray.DidHitObject ? ray.Distance : MaxLength;
var offset = angle.ToVec() * dist / 2;
var message = new EffectSystemMessage
{
EffectSprite = Spritename,
EffectSprite = _spritename,
Born = time,
DeathTime = time + TimeSpan.FromSeconds(1),
Size = new Vector2(dist, 1f),
@@ -77,7 +118,8 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Hitscan
//Rotated from east facing
Rotation = (float) angle.Theta,
ColorDelta = new Vector4(0, 0, 0, -1500f),
Color = new Vector4(255, 255, 255, 750),
Color = Vector4.Multiply(new Vector4(255, 255, 255, 750), energyModifier),
Shaded = false
};
var mgr = IoCManager.Resolve<IEntitySystemManager>();