This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
forked from estacaoespacialpirata/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Makes Card Hand sprite change dynamically
- Loading branch information
Showing
4 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
Content.Client/_EstacaoPirata/Cards/Hand/CardHandSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters