Stupidly shoddy combat mode system.

Doesn't even work for guns, oh well.
This commit is contained in:
Pieter-Jan Briers
2019-06-30 00:01:41 +02:00
parent 338f456c50
commit c4523a956d
9 changed files with 126 additions and 6 deletions

View File

@@ -24,6 +24,7 @@ namespace Content.Client.Input
human.AddFunction(ContentKeyFunctions.ActivateItemInWorld); human.AddFunction(ContentKeyFunctions.ActivateItemInWorld);
human.AddFunction(ContentKeyFunctions.ThrowItemInHand); human.AddFunction(ContentKeyFunctions.ThrowItemInHand);
human.AddFunction(ContentKeyFunctions.OpenContextMenu); human.AddFunction(ContentKeyFunctions.OpenContextMenu);
human.AddFunction(ContentKeyFunctions.ToggleCombatMode);
var ghost = contexts.New("ghost", "common"); var ghost = contexts.New("ghost", "common");
ghost.AddFunction(EngineKeyFunctions.MoveUp); ghost.AddFunction(EngineKeyFunctions.MoveUp);

View File

@@ -186,6 +186,8 @@ namespace Content.Server
factory.Register<FootstepModifierComponent>(); factory.Register<FootstepModifierComponent>();
factory.Register<EmitSoundOnUseComponent>(); factory.Register<EmitSoundOnUseComponent>();
factory.Register<CombatModeComponent>();
IoCManager.Register<ISharedNotifyManager, ServerNotifyManager>(); IoCManager.Register<ISharedNotifyManager, ServerNotifyManager>();
IoCManager.Register<IServerNotifyManager, ServerNotifyManager>(); IoCManager.Register<IServerNotifyManager, ServerNotifyManager>();
IoCManager.Register<IGameTicker, GameTicker>(); IoCManager.Register<IGameTicker, GameTicker>();

View File

@@ -0,0 +1,18 @@
using Robust.Shared.GameObjects;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Mobs
{
/// <summary>
/// Stores whether an entity is in "combat mode"
/// This is used to differentiate between regular item interactions or
/// using *everything* as a weapon.
/// </summary>
public sealed class CombatModeComponent : Component
{
public override string Name => "CombatMode";
[ViewVariables(VVAccess.ReadWrite)]
public bool IsInCombatMode { get; set; }
}
}

View File

@@ -9,7 +9,7 @@ using Robust.Shared.Interfaces.Map;
namespace Content.Server.GameObjects.Components.Weapon.Melee namespace Content.Server.GameObjects.Components.Weapon.Melee
{ {
public class MeleeWeaponComponent : Component, IAfterAttack public class MeleeWeaponComponent : Component, IAttack
{ {
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager; [Dependency] private readonly IMapManager _mapManager;
@@ -31,7 +31,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
serializer.DataField(ref ArcWidth, "arcwidth", 90); serializer.DataField(ref ArcWidth, "arcwidth", 90);
} }
void IAfterAttack.AfterAttack(AfterAttackEventArgs eventArgs) void IAttack.Attack(AttackEventArgs eventArgs)
{ {
var location = eventArgs.User.Transform.GridPosition; var location = eventArgs.User.Transform.GridPosition;
var angle = new Angle(eventArgs.ClickLocation.ToWorld(_mapManager).Position - location.ToWorld(_mapManager).Position); var angle = new Angle(eventArgs.ClickLocation.ToWorld(_mapManager).Position - location.ToWorld(_mapManager).Position);
@@ -42,9 +42,9 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
if (!entity.Transform.IsMapTransform || entity == eventArgs.User) if (!entity.Transform.IsMapTransform || entity == eventArgs.User)
continue; continue;
if (entity.TryGetComponent(out DamageableComponent damagecomponent)) if (entity.TryGetComponent(out DamageableComponent damageComponent))
{ {
damagecomponent.TakeDamage(DamageType.Brute, Damage); damageComponent.TakeDamage(DamageType.Brute, Damage);
} }
} }
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.Interfaces.GameObjects; using Content.Server.Interfaces.GameObjects;
using Content.Shared.Input; using Content.Shared.Input;
using JetBrains.Annotations; using JetBrains.Annotations;
@@ -124,6 +125,23 @@ namespace Content.Server.GameObjects.EntitySystems
public IEntity User { get; set; } public IEntity User { get; set; }
} }
public interface IAttack
{
void Attack(AttackEventArgs eventArgs);
}
public class AttackEventArgs : EventArgs
{
public AttackEventArgs(IEntity user, GridCoordinates clickLocation)
{
User = user;
ClickLocation = clickLocation;
}
public IEntity User { get; }
public GridCoordinates ClickLocation { get; }
}
/// <summary> /// <summary>
/// Governs interactions during clicking on entities /// Governs interactions during clicking on entities
/// </summary> /// </summary>
@@ -143,7 +161,7 @@ namespace Content.Server.GameObjects.EntitySystems
inputSys.BindMap.BindFunction(ContentKeyFunctions.UseItemInHand, inputSys.BindMap.BindFunction(ContentKeyFunctions.UseItemInHand,
new PointerInputCmdHandler(HandleUseItemInHand)); new PointerInputCmdHandler(HandleUseItemInHand));
inputSys.BindMap.BindFunction(ContentKeyFunctions.ActivateItemInWorld, inputSys.BindMap.BindFunction(ContentKeyFunctions.ActivateItemInWorld,
new PointerInputCmdHandler((HandleActivateItemInWorld))); new PointerInputCmdHandler(HandleActivateItemInWorld));
} }
public void HandleActivateItemInWorld(ICommonSession session, GridCoordinates coords, EntityUid uid) public void HandleActivateItemInWorld(ICommonSession session, GridCoordinates coords, EntityUid uid)
@@ -199,7 +217,16 @@ namespace Content.Server.GameObjects.EntitySystems
return; return;
} }
UserInteraction(((IPlayerSession) session).AttachedEntity, coords, uid); var userEntity = ((IPlayerSession) session).AttachedEntity;
if (userEntity.TryGetComponent(out CombatModeComponent combatMode) && combatMode.IsInCombatMode)
{
DoAttack(userEntity, coords, uid);
}
else
{
UserInteraction(userEntity, coords, uid);
}
} }
private void UserInteraction(IEntity player, GridCoordinates coordinates, EntityUid clickedUid) private void UserInteraction(IEntity player, GridCoordinates coordinates, EntityUid clickedUid)
@@ -479,6 +506,37 @@ namespace Content.Server.GameObjects.EntitySystems
afterAttack.AfterAttack(afterAttackEventArgs); afterAttack.AfterAttack(afterAttackEventArgs);
} }
} }
private void DoAttack(IEntity player, GridCoordinates coordinates, EntityUid uid)
{
// Verify player is on the same map as the entity he clicked on
if (_mapManager.GetGrid(coordinates.GridID).ParentMap.Index != player.Transform.MapID)
{
Logger.WarningS("system.interaction",
$"Player named {player.Name} clicked on a map he isn't located on");
return;
}
// Verify player has a hand, and find what object he is currently holding in his active hand
if (!player.TryGetComponent<IHandsComponent>(out var hands))
{
return;
}
var item = hands.GetActiveHand?.Owner;
// TODO: If item is null we need some kinda unarmed combat.
if (!ActionBlockerSystem.CanInteract(player) || item == null)
{
return;
}
var eventArgs = new AttackEventArgs(player, coordinates);
foreach (var attackComponent in item.GetAllComponents<IAttack>())
{
attackComponent.Attack(eventArgs);
}
}
} }
/// <summary> /// <summary>

View File

@@ -0,0 +1,35 @@
using Content.Server.GameObjects.Components.Mobs;
using Content.Shared.Input;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Input;
using Robust.Shared.Players;
namespace Content.Server.GameObjects.EntitySystems
{
public sealed class CombatModeSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
var inputSystem = EntitySystemManager.GetEntitySystem<InputSystem>();
inputSystem.BindMap.BindFunction(ContentKeyFunctions.ToggleCombatMode,
InputCmdHandler.FromDelegate(CombatModeToggled));
}
private void CombatModeToggled(ICommonSession session)
{
var playerSession = (IPlayerSession) session;
if (playerSession.AttachedEntity == null ||
!playerSession.AttachedEntity.TryGetComponent(out CombatModeComponent combatModeComponent))
{
return;
}
combatModeComponent.IsInCombatMode = !combatModeComponent.IsInCombatMode;
}
}
}

View File

@@ -15,5 +15,6 @@ namespace Content.Shared.Input
public static readonly BoundKeyFunction ThrowItemInHand = "ThrowItemInHand"; public static readonly BoundKeyFunction ThrowItemInHand = "ThrowItemInHand";
public static readonly BoundKeyFunction OpenContextMenu = "OpenContextMenu"; public static readonly BoundKeyFunction OpenContextMenu = "OpenContextMenu";
public static readonly BoundKeyFunction FocusChat = "FocusChatWindow"; public static readonly BoundKeyFunction FocusChat = "FocusChatWindow";
public static readonly BoundKeyFunction ToggleCombatMode = "ToggleCombatMode";
} }
} }

View File

@@ -64,6 +64,8 @@
visuals: visuals:
- type: SpeciesVisualizer2D - type: SpeciesVisualizer2D
- type: CombatMode
- type: entity - type: entity
id: MobObserver id: MobObserver
name: Observer name: Observer

View File

@@ -73,3 +73,6 @@ binds:
- function: OpenContextMenu - function: OpenContextMenu
key: MouseRight key: MouseRight
type: state type: state
- function: ToggleCombatMode
type: Toggle
key: Tab