private static double CalculateFireDistance(WeaponFireTracePreset weaponTracePreset, Vector2D deltaPos) { var fireDistance = deltaPos.Length; fireDistance = Math.Max(0, fireDistance); // extend fire distance by start offset (yes, we're using `-` here as TraceStartWorldOffset expected to be negative) fireDistance -= weaponTracePreset.TraceStartWorldOffset; return(fireDistance); }
private void Setup( WeaponFireTracePreset weaponTracePreset, IComponentSpriteRenderer componentSpriteRender, Vector2D worldPositionSource, Vector2D normalizedRay, double fireDistance, double totalDuration, bool hasHit, WeaponHitData hitData) { this.weaponTracePreset = weaponTracePreset; this.componentSpriteRender = componentSpriteRender; this.worldPositionSource = worldPositionSource; this.normalizedRay = normalizedRay; this.fireDistance = fireDistance; this.totalDuration = totalDuration; this.hasHit = hasHit; this.hitData = hitData; this.LateUpdate(0); }
public static void Create( WeaponFireTracePreset weaponTracePreset, Vector2D worldPositionSource, Vector2D endPosition, WeaponHitData lastHitData, bool hasHit) { if (weaponTracePreset is null) { // no weapon trace for this weapon return; } var deltaPos = endPosition - worldPositionSource; var fireDistance = CalculateFireDistance(weaponTracePreset, deltaPos); if (fireDistance <= weaponTracePreset.TraceMinDistance) { return; } CalculateAngleAndDirection(deltaPos, out var angleRad, out var normalizedRay); // offset start position of the ray worldPositionSource += normalizedRay * weaponTracePreset.TraceStartWorldOffset; // actual trace life duration is larger when has a hit // (to provide a contact fade-out animation for the sprite length) if (!hasHit) { // otherwise it's shorter on the sprite length fireDistance -= weaponTracePreset.TraceWorldLength; } var totalDuration = WeaponSystemClientDisplay.SharedCalculateTimeToHit(fireDistance, weaponTracePreset); var sceneObject = Api.Client.Scene.CreateSceneObject("Temp_WeaponTrace"); var componentSpriteRender = Api.Client.Rendering.CreateSpriteRenderer( sceneObject, weaponTracePreset.TraceTexture, positionOffset: Vector2D.Zero, spritePivotPoint: (0, 0.5), // yes, it's actually making the weapon trace to draw in the light layer! drawOrder: DrawOrder.Light); componentSpriteRender.RotationAngleRad = (float)angleRad; componentSpriteRender.BlendMode = weaponTracePreset.UseScreenBlending ? BlendMode.Screen // it's important to use premultiplied mode here for correct rendering : BlendMode.AlphaBlendPremultiplied; sceneObject.AddComponent <ComponentWeaponTrace>() .Setup(weaponTracePreset, componentSpriteRender, worldPositionSource, normalizedRay, fireDistance, totalDuration, hasHit, lastHitData); sceneObject.Destroy(totalDuration); }