Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Makes Card Hand sprite change dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
Day-OS authored and rbertoche committed Jul 8, 2024
1 parent e8ef47d commit 8cddc4c
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 6 deletions.
1 change: 0 additions & 1 deletion Content.Client/Content.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Spawners\" />
<Folder Include="_EstacaoPirata\Cards\Hand\" />
</ItemGroup>
<Import Project="..\RobustToolbox\MSBuild\Robust.Properties.targets" />
<Import Project="..\RobustToolbox\MSBuild\XamlIL.targets" />
Expand Down
12 changes: 7 additions & 5 deletions Content.Client/_EstacaoPirata/Cards/Deck/CardDeckSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ public override void Update(float frameTime)

}

private void OnComponentStartupEvent(EntityUid uid, CardDeckComponent comp, ComponentStartup args)
{

UpdateSprite(uid, comp);
}

// This is executed only if there are no available layers to work with
private static void SetupSpriteLayers(EntityUid _, CardDeckComponent comp, SpriteComponent sprite, int layersQuantity)
Expand Down Expand Up @@ -169,6 +164,12 @@ private void OnAppearanceChanged(EntityUid uid, CardDeckComponent comp, Appearan
{
UpdateSprite(uid, comp);
}
private void OnComponentStartupEvent(EntityUid uid, CardDeckComponent comp, ComponentStartup args)
{

UpdateSprite(uid, comp);
}


private void OnStackStart(CardStackInitiatedEvent args)
{
Expand All @@ -178,4 +179,5 @@ private void OnStackStart(CardStackInitiatedEvent args)

UpdateSprite(entity, comp);
}

}
110 changes: 110 additions & 0 deletions Content.Client/_EstacaoPirata/Cards/Hand/CardHandSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System.Linq;
using System.Numerics;
using Content.Shared._EstacaoPirata.Cards.Deck;
using Content.Shared._EstacaoPirata.Cards.Hand;
using Content.Shared._EstacaoPirata.Cards.Stack;
using Robust.Client.GameObjects;

namespace Content.Client._EstacaoPirata.Cards.Hand;

/// <summary>
/// This handles...
/// </summary>
public sealed class CardHandSystem : EntitySystem
{
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<CardHandComponent, ComponentStartup>(OnComponentStartupEvent);
SubscribeNetworkEvent<CardStackInitiatedEvent>(OnStackStart);
SubscribeNetworkEvent<CardStackQuantityChangeEvent>(OnStackUpdate);
}

private void UpdateSprite(EntityUid uid, CardHandComponent comp)
{
if (!TryComp(uid, out SpriteComponent? sprite))
return;

if (!TryComp(uid, out CardStackComponent? cardStack))
return;

var cardCount = Math.Min(cardStack.Cards.Count, comp.CardLimit);

//inserts Missing Layers
if (sprite.AllLayers.Count() < cardCount)
{
if (!sprite.TryGetLayer(0, out var firstLayer))
return;
for (var i = sprite.AllLayers.Count(); i < cardCount; i++)
{
sprite.AddLayer(firstLayer.State, i);
}
}
//Removes extra layers
else if (sprite.AllLayers.Count() > cardCount)
{
for (var i = cardCount; i < sprite.AllLayers.Count(); i++)
{
sprite.RemoveLayer(i);
}
}

var j = 0;
var intervalAngle = comp.Angle / (cardCount-1);
var intervalSize = comp.XOffset / (cardCount - 1);
foreach (var card in cardStack.Cards)
{
if (!TryGetCardLayer(card, out var layer) || layer == null)
return;

var angle = (-(comp.Angle/2)) + j * intervalAngle;
var x = (-(comp.XOffset / 2)) + j * intervalSize;
var y = -(x * x) + 0.10f;

sprite.LayerSetVisible(0, true);
sprite.LayerSetTexture(j, layer.Texture);
sprite.LayerSetState(j, layer.State);
sprite.LayerSetRotation(j, Angle.FromDegrees(-angle));
sprite.LayerSetOffset(j, new Vector2(x, y));
sprite.LayerSetScale(j, new Vector2(comp.Scale, comp.Scale));
j++;
}
}

private bool TryGetCardLayer(EntityUid card, out SpriteComponent.Layer? layer)
{
layer = null;
if (!TryComp(card, out SpriteComponent? cardSprite))
return false;

if (!cardSprite.TryGetLayer(0, out var l))
return false;

layer = l;
return true;
}


private void OnStackUpdate(CardStackQuantityChangeEvent args)
{
if (!TryComp(GetEntity(args.Stack), out CardHandComponent? comp))
return;
UpdateSprite(GetEntity(args.Stack), comp);
}

private void OnStackStart(CardStackInitiatedEvent args)
{
var entity = GetEntity(args.CardStack);
if (!TryComp(entity, out CardHandComponent? comp))
return;

UpdateSprite(entity, comp);
}
private void OnComponentStartupEvent(EntityUid uid, CardHandComponent comp, ComponentStartup args)
{

UpdateSprite(uid, comp);
}


}
11 changes: 11 additions & 0 deletions Content.Shared/_EstacaoPirata/Cards/Hand/CardHandComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ namespace Content.Shared._EstacaoPirata.Cards.Hand;
[RegisterComponent]
public sealed partial class CardHandComponent : Component
{
[DataField("angle")]
public float Angle = 120f;

[DataField("xOffset")]
public float XOffset = 0.5f;

[DataField("scale")]
public float Scale = 1;

[DataField("limit")]
public int CardLimit = 10;

}

Expand Down

0 comments on commit 8cddc4c

Please sign in to comment.