Adds IThrown, ILand interfaces. Adds dice. (#273)

* Dice, IThrown, ILand

* Adds sounds to the dice using a sound collection.

* Seed random instance better.

* Missed a ")", should compile now
This commit is contained in:
Víctor Aguilera Puerto
2019-07-18 23:33:02 +02:00
committed by Pieter-Jan Briers
parent 92668432a7
commit d9077bde74
92 changed files with 447 additions and 5 deletions

View File

@@ -430,6 +430,18 @@ namespace Content.Server.GameObjects
}
}
public bool ThrowItem()
{
var item = GetActiveHand?.Owner;
if (item != null)
{
var interactionSystem = _entitySystemManager.GetEntitySystem<InteractionSystem>();
return interactionSystem.TryThrowInteraction(Owner, item);
}
return false;
}
public override void HandleMessage(ComponentMessage message, INetChannel netChannel = null,
IComponent component = null)
{

View File

@@ -0,0 +1,101 @@
using System;
using Content.Server.GameObjects.Components.Sound;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Audio;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Items
{
public class DiceComponent : Component, IActivate, IUse, ILand, IExamine
{
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
#pragma warning restore 649
public override string Name => "Dice";
private Random _random;
private int _step = 1;
private int _sides = 20;
private int _currentSide = 20;
[ViewVariables]
public string _soundCollectionName = "dice";
[ViewVariables]
public int Step => _step;
[ViewVariables]
public int Sides => _sides;
[ViewVariables]
public int CurrentSide => _currentSide;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _step, "step", 1);
serializer.DataField(ref _sides, "sides", 20);
serializer.DataField(ref _soundCollectionName, "diceSoundCollection", "dice");
_currentSide = _sides;
}
public override void OnAdd()
{
base.OnAdd();
_random = new Random(Owner.Uid.GetHashCode() ^ DateTime.Now.GetHashCode());
}
public void Roll()
{
_currentSide = _random.Next(1, (_sides/_step)+1) * _step;
if (!Owner.TryGetComponent(out SpriteComponent sprite)) return;
sprite.LayerSetState(0, $"d{_sides}{_currentSide}");
PlayDiceEffect();
}
public void PlayDiceEffect()
{
if (!string.IsNullOrWhiteSpace(_soundCollectionName))
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(_soundCollectionName);
var file = _random.Pick(soundCollection.PickFiles);
Owner.GetComponent<SoundComponent>().Play(file, AudioParams.Default);
}
}
public void Activate(ActivateEventArgs eventArgs)
{
Roll();
}
public bool UseEntity(UseEntityEventArgs eventArgs)
{
Roll();
return false;
}
public void Land(LandEventArgs eventArgs)
{
Roll();
}
public void Examine(FormattedMessage message)
{
message.AddText("A dice with ");
message.PushColor(new Color(1F, 0.75F, 0.75F));
message.AddText(_sides.ToString());
message.Pop();
message.AddText(" sides.\nIt has landed on a ");
message.PushColor(new Color(1F, 1F, 1F));
message.AddText(_currentSide.ToString());
message.Pop();
message.AddText(".");
}
}
}

View File

@@ -46,6 +46,11 @@ namespace Content.Server.GameObjects
{
return true;
}
bool IActionBlocker.CanThrow()
{
return true;
}
}
/// <summary>
@@ -77,6 +82,11 @@ namespace Content.Server.GameObjects
{
return false;
}
bool IActionBlocker.CanThrow()
{
return false;
}
}
/// <summary>
@@ -118,5 +128,10 @@ namespace Content.Server.GameObjects
{
return false;
}
bool IActionBlocker.CanThrow()
{
return false;
}
}
}

View File

@@ -76,6 +76,11 @@ namespace Content.Server.GameObjects
return CurrentDamageState.CanUse();
}
bool IActionBlocker.CanThrow()
{
return CurrentDamageState.CanThrow();
}
List<DamageThreshold> IOnDamageBehavior.GetAllDamageThresholds()
{
var thresholdlist = DamageTemplate.DamageThresholds;

View File

@@ -1,17 +1,29 @@
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Projectiles;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects;
using Content.Shared.Physics;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.IoC;
namespace Content.Server.GameObjects.Components
{
class ThrownItemComponent : ProjectileComponent, ICollideBehavior
{
#pragma warning disable 649
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
#pragma warning restore 649
public override string Name => "ThrownItem";
/// <summary>
/// User who threw the item.
/// </summary>
public IEntity User;
void ICollideBehavior.CollideWith(List<IEntity> collidedwith)
{
foreach (var entity in collidedwith)
@@ -31,9 +43,13 @@ namespace Content.Server.GameObjects.Components
body.CollisionMask &= (int)~CollisionGroup.Mob;
body.IsScrapingFloor = true;
// KYS, your job is finished.
// KYS, your job is finished. Trigger ILand as well.
Owner.RemoveComponent<ThrownItemComponent>();
_entitySystemManager.GetEntitySystem<InteractionSystem>().LandInteraction(User, Owner, Owner.Transform.GridPosition);
}
}
}
}