Skip to content

Commit 6ef05c7

Browse files
authored
Merge pull request #20 from IngoHHacks/TalkingCards
Merge PRs
2 parents a28691b + 1581d18 commit 6ef05c7

6 files changed

Lines changed: 166 additions & 4 deletions

File tree

Models/NewCard.cs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Reflection;
34
using DiskCardGame;
45
using UnityEngine;
6+
using static DiskCardGame.CharacterFace;
7+
using static UnityEngine.Object;
58

69
namespace APIPlugin
710
{
@@ -127,6 +130,11 @@ public static void Add(string name, string displayedName, int baseAttack, int ba
127130
// TODO Provide a function to create animated card textures
128131
card.animatedPortrait = animatedPortrait;
129132
}
133+
else if (specialAbilities.Contains(SpecialTriggeredAbility.TalkingCardChooser))
134+
{
135+
// TODO: Make talking cards not depend on animated portraits
136+
CreateAnimatedPortrait(card);
137+
}
130138

131139
if (decals is not null)
132140
{
@@ -165,7 +173,7 @@ private static void HandleIdentifiers(
165173
foreach (var id in abilityIdsParam.Where(id => id.id != 0))
166174
{
167175
card.abilities.Add(id.id);
168-
abilitiesToRemove.Add(id);
176+
abilitiesToRemove.Add(id);
169177
}
170178

171179
foreach (AbilityIdentifier id in abilitiesToRemove)
@@ -180,16 +188,16 @@ private static void HandleIdentifiers(
180188
}
181189

182190
// Handle SpecialAbilityIds
183-
List<SpecialAbilityIdentifier> specialAbilitiesToRemove = new List<SpecialAbilityIdentifier>();
191+
List<SpecialAbilityIdentifier> specialAbilitiesToRemove = new List<SpecialAbilityIdentifier>();
184192
if (specialAbilitiesIdsParam is not null)
185193
{
186194
foreach (var id in specialAbilitiesIdsParam.Where(id => id.id != 0))
187195
{
188196
card.specialAbilities.Add(id.id);
189-
specialAbilitiesToRemove.Add(id);
197+
specialAbilitiesToRemove.Add(id);
190198
}
191199

192-
foreach (SpecialAbilityIdentifier id in specialAbilitiesToRemove)
200+
foreach (SpecialAbilityIdentifier id in specialAbilitiesToRemove)
193201
{
194202
specialAbilitiesIdsParam.Remove(id);
195203
}
@@ -260,5 +268,38 @@ private static void DetermineAndSetCardArt(
260268
card.pixelPortrait.name = newName;
261269
}
262270
}
271+
272+
private static void CreateAnimatedPortrait(CardInfo card)
273+
{
274+
if (!card.appearanceBehaviour.Contains(CardAppearanceBehaviour.Appearance.AnimatedPortrait))
275+
{
276+
card.appearanceBehaviour.Add(CardAppearanceBehaviour.Appearance.AnimatedPortrait);
277+
}
278+
Sprite mainSprite = Sprite.Create(card.portraitTex.texture, card.portraitTex.rect, new Vector2(0.5f, 0f));
279+
280+
Sprite empty = Sprite.Create(mainSprite.texture, new Rect(0, 0, 0, 0), new Vector2(0.5f, 0.5f));
281+
282+
GameObject obj = Instantiate(Resources.Load<GameObject>("Prefabs/Cards/AnimatedPortraits/TalkingCardPortrait"));
283+
obj.transform.localScale = new Vector3(obj.transform.localScale.x * 0.9f, obj.transform.localScale.y * 0.9f, obj.transform.localScale.z * 0.9f);
284+
DontDestroyOnLoad(obj);
285+
286+
CharacterFace face = obj.GetComponentInChildren<CharacterFace>();
287+
List<EmotionSprites> emotes = (List<EmotionSprites>) face.GetType().GetField("emotionSprites", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(face);
288+
289+
emotes.Clear();
290+
emotes.Add(new EmotionSprites()
291+
{
292+
emotion = Emotion.Neutral,
293+
face = mainSprite,
294+
eyesClosed = empty,
295+
eyesClosedEmission = empty,
296+
eyesOpen = empty,
297+
eyesOpenEmission = empty,
298+
mouthClosed = empty,
299+
mouthOpen = empty
300+
});
301+
302+
card.animatedPortrait = obj;
303+
}
263304
}
264305
}

Models/NewDialogue.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using DiskCardGame;
3+
4+
namespace APIPlugin
5+
{
6+
public class NewDialogue
7+
{
8+
public static Dictionary<string, DialogueEvent> dialogueEvents = new();
9+
10+
public static void AddAll(Dictionary<string, DialogueEvent> dialogueEvents)
11+
{
12+
foreach (KeyValuePair<string, DialogueEvent> pair in dialogueEvents)
13+
{
14+
NewDialogue.dialogueEvents.Add(pair.Key, pair.Value);
15+
}
16+
if (dialogueEvents.Count > 0)
17+
{
18+
Plugin.Log.LogInfo($"Loaded {dialogueEvents.Count} custom dialogues!");
19+
}
20+
}
21+
22+
public static void Add(string id, DialogueEvent dialogueEvent) {
23+
NewDialogue.dialogueEvents.Add(id, dialogueEvent);
24+
Plugin.Log.LogInfo($"Loaded custom dialogue {id}!");
25+
}
26+
}
27+
}

Models/NewTalkingCard.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using DiskCardGame;
4+
5+
namespace APIPlugin
6+
{
7+
public class NewTalkingCard
8+
{
9+
public static Dictionary<string,Type> talkingCards = new();
10+
public static List<Type> types = new(); // For easy access by CardTriggerHandler.GetType
11+
12+
public static void Add<T>(string name, Dictionary<string, DialogueEvent> dialogueEvents)
13+
{
14+
NewTalkingCard.talkingCards.Add(name.Replace(" ", "_"), typeof(T));
15+
NewTalkingCard.types.Add(typeof(T));
16+
NewDialogue.AddAll(dialogueEvents);
17+
Plugin.Log.LogInfo($"Loaded talking card {name}!");
18+
}
19+
20+
public static void Add<T>(string name) {
21+
Add<T>(name, new Dictionary<string, DialogueEvent>());
22+
}
23+
}
24+
}

Patches/CardTriggerHandler.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,22 @@ public static bool Prefix(SpecialTriggeredAbility ability, CardTriggerHandler __
7575
return false;
7676
}
7777
}
78+
79+
[HarmonyPatch(typeof(CardTriggerHandler), "GetType", typeof(string))]
80+
public class TriggerTypePatch
81+
{
82+
public static bool Prefix(ref Type __result, string typeName)
83+
{
84+
Type t = NewTalkingCard.types.Find(type => type.Name == typeName);
85+
if (t != null)
86+
{
87+
__result = t;
88+
return false;
89+
}
90+
else
91+
{
92+
return true;
93+
}
94+
}
95+
}
7896
}

Patches/DialogueUtils_GetEvent.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using HarmonyLib;
2+
using APIPlugin;
3+
4+
namespace API.Patches
5+
{
6+
[HarmonyPatch(typeof(DialogueDataUtil.DialogueData), "GetEvent", typeof(string))]
7+
public class DialogueUtilPatch
8+
{
9+
public static bool Prefix(ref DialogueEvent __result, string id)
10+
{
11+
DialogueEvent dialogueEvent;
12+
if (id != null && NewDialogue.dialogueEvents.TryGetValue(id, out dialogueEvent))
13+
{
14+
__result = dialogueEvent;
15+
return false;
16+
}
17+
else
18+
{
19+
return true;
20+
}
21+
}
22+
}
23+
24+
}

Patches/TalkingCards.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Reflection;
3+
using APIPlugin;
4+
using DiskCardGame;
5+
using HarmonyLib;
6+
7+
namespace API.Patches
8+
{
9+
[HarmonyPatch(typeof(TalkingCardChooser), "Awake")]
10+
public class TalkingCardPatch
11+
{
12+
public static bool Prefix(ref TalkingCardChooser __instance)
13+
{
14+
Card c = __instance.gameObject.GetComponentInChildren<Card>();
15+
Type t;
16+
if (NewTalkingCard.talkingCards.TryGetValue(c.Info.name.Replace(" ", "_"), out t))
17+
{
18+
MethodInfo info = c.GetType().GetMethod("AddPermanentBehaviour").MakeGenericMethod(new Type[] { t });
19+
info.Invoke(c, new object[] {});
20+
return false;
21+
}
22+
else
23+
{
24+
return true;
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)