Skip to content

Instantly share code, notes, and snippets.

@LuizMoratelli
Last active March 28, 2024 19:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuizMoratelli/62dd2ea0d0a52103d8d762c7aaec6f7a to your computer and use it in GitHub Desktop.
Save LuizMoratelli/62dd2ea0d0a52103d8d762c7aaec6f7a to your computer and use it in GitHub Desktop.
[TCG Engine] Enhance Text at Preview

1. Add TextEnhanceData.cs to your project

2. Change CardUI

public string EnhancedText(Card pcard = null)
{
  foreach (var enhancer in TextEnhanceData.enchance_list)
    text = enhancer.Enhance(GameClient.Get().GetGameData(), pcard, text);
  }
  
  return text;
}

3. Add the Load in DataLoader.cs TextEnhanceData.Load();

4. Create a TextEnchanceData to test it

image

1. Add EnhancedText method to CardUI.cs

public string EnhancedText(Card pcard = null)
{
    string text = card.GetText();
    var spellDamageRegex = new Regex(@"%spell_damage%\+([0-9]*)");
    string finalText = text;

    var spellDamageMatches = spellDamageRegex.Matches(text);
    if (spellDamageMatches.Count > 0)
    {
        var playerSpellDamage = pcard == null ? 0 : GameClient.Get().GetGameData().GetPlayer(pcard.player_id).GetTraitValue("spell_damage");

        foreach (Match match in spellDamageMatches)
        {
            var cardSpellDamage = int.Parse(match.Groups[1].Value);
            var spellDamageText = playerSpellDamage != 0 ? $"*{(playerSpellDamage + cardSpellDamage)}*" : cardSpellDamage.ToString();

            finalText = spellDamageRegex.Replace(finalText, spellDamageText, 1);
        }
    }


    return finalText;
}

2. Add SetText to CardUI.cs

public void SetText(string text)
{
    if (card_text != null)
        card_text.text = text;
}

3. Call the new method in Update in CardPreviewUI.cs

void Update()
{
    //...
    if (show_preview)
    {
        //...
        card_ui.SetCard(icard, pcard.VariantData);
        card_ui.SetText(card_ui.EnhancedText(pcard)); // <-- Added this line
        //...
    }
    //...
}

4. Call the new method in OnPlayCard in GameBoardFX.cs

void OnPlayCard(Card card, Slot slot)
{
    //...
    ui.SetCard(icard, card.VariantData);
    ui.SetText(ui.EnhancedText(card)); // <-- Added this line
    //...
}

5. Update text on cards to be replaced


image

6. See in play :D

image

Notes:

  • You MUST have TCG Engine to add the changes showned above;
  • This gist was created using 1.09, so is possible that in the future you will need to change minor details to make it work;
  • The CardEditor and Inspector are edited by me, so if you just installed TCG Engine, please consider that are differences in the UI, but are just visual.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using System.Text.RegularExpressions;
namespace TcgEngine
{
[InlineButton("@CardEditor.CloneAction($root, $property, $value)", SdfIconType.FileEarmarkPlusFill, "", ShowIf = "@CardEditor.ShowNotNull($value)")]
[CreateAssetMenu(fileName = "textEnhancer", menuName = "TcgEngine/TextEnhancer", order = 99)]
public class TextEnhanceData : ScriptableObject
{
public string id;
public string regex_string = "";
public bool add_value_from_player = false;
public bool add_value_from_card = false;
public bool add_value_from_text = false;
public TraitData trait;
public string prefix = "";
public string suffix = "";
public bool always_add_prefix_sufix = false;
public ValueOperatorInt valueOperator = ValueOperatorInt.Add;
public static List<TextEnhanceData> enchance_list = new List<TextEnhanceData>(); //Faster access in loops
public static Dictionary<string, TextEnhanceData> enchance_dict = new Dictionary<string, TextEnhanceData>(); //Faster access in Get(id)
public virtual string Enhance(Game data, Card card, string text)
{
var regex = new Regex($@"{regex_string}");
var matches = regex.Matches(text);
if (matches.Count > 0)
{
int playerValue = GetPlayerValue(data, card);
int cardValue = GetCardValue(data, card);
Debug.Log(cardValue);
foreach (Match match in matches)
{
int textValue = add_value_from_text ? int.Parse(match.Groups[1].Value) : 0;
int calculatedValue = CalculateValue(playerValue, cardValue, textValue);
bool add_prefix_suffix = always_add_prefix_sufix || textValue == 0 || calculatedValue != textValue;
var replacedText = add_prefix_suffix
? $"{prefix}{(calculatedValue)}{suffix}"
: $"{(calculatedValue)}";
text = regex.Replace(text, replacedText, 1);
}
}
return text;
}
public static void Load(string folder = "")
{
if (enchance_list.Count == 0)
{
enchance_list.AddRange(Resources.LoadAll<TextEnhanceData>(folder));
foreach (TextEnhanceData enchance in enchance_list)
enchance_dict.Add(enchance.id, enchance);
}
}
private int GetPlayerValue(Game data, Card card)
{
if (!add_value_from_player)
return 0;
if (card == null)
return 0;
Player player = data.GetPlayer(card.player_id);
if (trait)
return player.GetTraitValue(trait);
return 0;
}
private int GetCardValue(Game data, Card card)
{
if (!add_value_from_card)
return 0;
if (card == null)
return 0;
if (trait)
return card.GetTraitValue(trait);
return 0;
}
private int CalculateValue(int playerValue, int cardValue, int textValue)
{
switch (valueOperator)
{
case (ValueOperatorInt.Add):
return textValue + playerValue + cardValue;
case (ValueOperatorInt.Sub):
return textValue - playerValue - cardValue;
}
return 0;
}
public enum ValueOperatorInt
{
Add,
Sub
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment