public string EnhancedText(Card pcard = null)
{
foreach (var enhancer in TextEnhanceData.enchance_list)
text = enhancer.Enhance(GameClient.Get().GetGameData(), pcard, text);
}
return text;
}
Last active
March 28, 2024 19:55
-
-
Save LuizMoratelli/62dd2ea0d0a52103d8d762c7aaec6f7a to your computer and use it in GitHub Desktop.
[TCG Engine] Enhance Text at Preview
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;
}
public void SetText(string text)
{
if (card_text != null)
card_text.text = text;
}
void Update()
{
//...
if (show_preview)
{
//...
card_ui.SetCard(icard, pcard.VariantData);
card_ui.SetText(card_ui.EnhancedText(pcard)); // <-- Added this line
//...
}
//...
}
void OnPlayCard(Card card, Slot slot)
{
//...
ui.SetCard(icard, card.VariantData);
ui.SetText(ui.EnhancedText(card)); // <-- Added this line
//...
}
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.
This file contains hidden or 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
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