Ranged weapon firing does not depend on DefaultGrid anymore.

This commit is contained in:
Acruid
2020-08-11 16:44:15 -07:00
parent 32e4c24342
commit cdc6ec3bfc
5 changed files with 78 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
using Content.Shared.GameObjects.Components.Weapons.Ranged; using Content.Shared.GameObjects.Components.Weapons.Ranged;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Client.GameObjects.Components.Weapons.Ranged namespace Content.Client.GameObjects.Components.Weapons.Ranged
{ {
@@ -30,9 +31,9 @@ namespace Content.Client.GameObjects.Components.Weapons.Ranged
FireRateSelector = rangedState.FireRateSelector; FireRateSelector = rangedState.FireRateSelector;
} }
public void SyncFirePos(GridCoordinates worldPos) public void SyncFirePos(GridId targetGrid, Vector2 targetPosition)
{ {
SendNetworkMessage(new FirePosComponentMessage(worldPos)); SendNetworkMessage(new FirePosComponentMessage(targetGrid, targetPosition));
} }
} }
} }

View File

@@ -11,6 +11,7 @@ using Robust.Shared.Input;
using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Timing; using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.Client.GameObjects.EntitySystems namespace Content.Client.GameObjects.EntitySystems
{ {
@@ -96,9 +97,13 @@ namespace Content.Client.GameObjects.EntitySystems
var worldPos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition); var worldPos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition);
if (!_mapManager.TryFindGridAt(worldPos, out var grid)) if (!_mapManager.TryFindGridAt(worldPos, out var grid))
grid = _mapManager.GetDefaultGrid(worldPos.MapId); {
weapon.SyncFirePos(GridId.Invalid, worldPos.Position);
weapon.SyncFirePos(grid.MapToGrid(worldPos)); }
else
{
weapon.SyncFirePos(grid.Index, grid.MapToGrid(worldPos).Position);
}
} }
} }
} }

View File

@@ -159,10 +159,10 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
public override void OnAdd() public override void OnAdd()
{ {
base.OnAdd(); base.OnAdd();
var rangedWeapon = Owner.GetComponent<ServerRangedWeaponComponent>(); var rangedWeaponComponent = Owner.GetComponent<ServerRangedWeaponComponent>();
rangedWeapon.Barrel = this; rangedWeaponComponent.Barrel = this;
rangedWeapon.FireHandler += Fire; rangedWeaponComponent.FireHandler += Fire;
rangedWeapon.WeaponCanFireHandler += WeaponCanFire; rangedWeaponComponent.WeaponCanFireHandler += WeaponCanFire;
} }
public override void OnRemove() public override void OnRemove()
@@ -208,7 +208,12 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
return true; return true;
} }
private void Fire(IEntity shooter, GridCoordinates target) /// <summary>
/// Fires a round of ammo out of the weapon.
/// </summary>
/// <param name="shooter">Entity that is operating the weapon, usually the player.</param>
/// <param name="targetPos">Target position on the map to shoot at.</param>
private void Fire(IEntity shooter, Vector2 targetPos)
{ {
var soundSystem = EntitySystem.Get<AudioSystem>(); var soundSystem = EntitySystem.Get<AudioSystem>();
if (ShotsLeft == 0) if (ShotsLeft == 0)
@@ -229,8 +234,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Barrels
} }
// At this point firing is confirmed // At this point firing is confirmed
var worldPosition = IoCManager.Resolve<IMapManager>().GetGrid(target.GridID).LocalToWorld(target).Position; var direction = (targetPos - shooter.Transform.WorldPosition).ToAngle();
var direction = (worldPosition - shooter.Transform.WorldPosition).ToAngle();
var angle = GetRecoilAngle(direction); var angle = GetRecoilAngle(direction);
// This should really be client-side but for now we'll just leave it here // This should really be client-side but for now we'll just leave it here
if (shooter.TryGetComponent(out CameraRecoilComponent recoilComponent)) if (shooter.TryGetComponent(out CameraRecoilComponent recoilComponent))

View File

@@ -1,4 +1,4 @@
using System; using System;
using Content.Server.GameObjects.Components.GUI; using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Mobs; using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels; using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels;
@@ -12,6 +12,7 @@ using Robust.Shared.Audio;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Network;
using Robust.Shared.Interfaces.Random; using Robust.Shared.Interfaces.Random;
using Robust.Shared.Interfaces.Timing; using Robust.Shared.Interfaces.Timing;
@@ -19,6 +20,7 @@ using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Log; using Robust.Shared.Log;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Players; using Robust.Shared.Players;
using Robust.Shared.Random; using Robust.Shared.Random;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
@@ -38,7 +40,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged
public Func<bool> WeaponCanFireHandler; public Func<bool> WeaponCanFireHandler;
public Func<IEntity, bool> UserCanFireHandler; public Func<IEntity, bool> UserCanFireHandler;
public Action<IEntity, GridCoordinates> FireHandler; public Action<IEntity, Vector2> FireHandler;
public ServerRangedBarrelComponent Barrel public ServerRangedBarrelComponent Barrel
{ {
@@ -77,6 +79,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged
serializer.DataField(this, p => p.ClumsyExplodeChance, "clumsyExplodeChance", 0.5f); serializer.DataField(this, p => p.ClumsyExplodeChance, "clumsyExplodeChance", 0.5f);
} }
/// <inheritdoc />
public override void HandleNetworkMessage(ComponentMessage message, INetChannel channel, ICommonSession session = null) public override void HandleNetworkMessage(ComponentMessage message, INetChannel channel, ICommonSession session = null)
{ {
base.HandleNetworkMessage(message, channel, session); base.HandleNetworkMessage(message, channel, session);
@@ -95,7 +98,24 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged
return; return;
} }
_tryFire(user, msg.Target); if (msg.TargetGrid != GridId.Invalid)
{
// grid pos
if (!IoCManager.Resolve<IMapManager>().TryGetGrid(msg.TargetGrid, out var grid))
{
// Client sent us a message with an invalid grid.
break;
}
var targetPos = grid.LocalToWorld(msg.TargetPosition);
TryFire(user, targetPos);
}
else
{
// map pos
TryFire(user, msg.TargetPosition);
}
break; break;
} }
} }
@@ -105,7 +125,12 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged
return new RangedWeaponComponentState(FireRateSelector); return new RangedWeaponComponentState(FireRateSelector);
} }
private void _tryFire(IEntity user, GridCoordinates coordinates) /// <summary>
/// Tries to fire a round of ammo out of the weapon.
/// </summary>
/// <param name="user">Entity that is operating the weapon, usually the player.</param>
/// <param name="targetPos">Target position on the map to shoot at.</param>
private void TryFire(IEntity user, Vector2 targetPos)
{ {
if (!user.TryGetComponent(out HandsComponent hands) || hands.GetActiveHand?.Owner != Owner) if (!user.TryGetComponent(out HandsComponent hands) || hands.GetActiveHand?.Owner != Owner)
{ {
@@ -158,7 +183,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged
return; return;
} }
FireHandler?.Invoke(user, coordinates); FireHandler?.Invoke(user, targetPos);
} }
// Probably a better way to do this. // Probably a better way to do this.

View File

@@ -1,6 +1,7 @@
using System; using System;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
namespace Content.Shared.GameObjects.Components.Weapons.Ranged namespace Content.Shared.GameObjects.Components.Weapons.Ranged
@@ -26,14 +27,33 @@ namespace Content.Shared.GameObjects.Components.Weapons.Ranged
} }
} }
/// <summary>
/// A component message raised when the weapon is fired at a position on the map.
/// </summary>
[Serializable, NetSerializable] [Serializable, NetSerializable]
public sealed class FirePosComponentMessage : ComponentMessage public sealed class FirePosComponentMessage : ComponentMessage
{ {
public GridCoordinates Target { get; } /// <summary>
/// If this is not invalid, the target position is relative to the grid.
/// Otherwise, it is a map position.
/// </summary>
public GridId TargetGrid { get; }
public FirePosComponentMessage(GridCoordinates target) /// <summary>
/// If Target Grid is not invalid, this is relative to the grid, otherwise
/// it is a map position.
/// </summary>
public Vector2 TargetPosition { get; }
/// <summary>
/// Constructs a new instance of <see cref="FirePosComponentMessage"/>.
/// </summary>
/// <param name="targetGrid">The grid that the target position is on, if any.</param>
/// <param name="targetPosition">Target position relative to the grid, or a map position if the grid is invalid.</param>
public FirePosComponentMessage(GridId targetGrid, Vector2 targetPosition)
{ {
Target = target; TargetGrid = targetGrid;
TargetPosition = targetPosition;
} }
} }
} }