Skip to content

Commit

Permalink
PortraitLayer working
Browse files Browse the repository at this point in the history
  • Loading branch information
RedDude committed Jun 7, 2023
1 parent 2808e88 commit dc1f2f2
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Source/ArcherCustomData/ArcherCustomData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ArcherCustomData

public HairInfo HairInfo;

public readonly List<ParticlesInfo> ParticlesInfos = new();
public List<ParticlesInfo> ParticlesInfos = new();

public string originalName;
public ArcherData original;
Expand Down Expand Up @@ -353,10 +353,10 @@ public ArcherData ToArcherData()
if (TFGame.Players.Length > 4)
{
ad.Portraits.NotJoined.Rect.Y += EightPlayersNotJoinedPortraitTopOffset;
ad.Portraits.NotJoined.Rect.Height = 60;
// ad.Portraits.NotJoined.Rect.Height = 60;

ad.Portraits.Joined.Rect.Y += EightPlayersJoinedPortraitTopOffset;
ad.Portraits.Joined.Rect.Height = 60;
// ad.Portraits.Joined.Rect.Height = 60;
}
ad.Statue = Statue;
ad.Statue.Image = Statue.Image;
Expand Down
6 changes: 4 additions & 2 deletions Source/Layers/LayerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ public static void Parse(ArcherCustomData data, XmlElement xml)
if (xml.HasChild("Layer"))
{
var info = HandleLayer(xml["Layer"]);
data.LayerInfos ??= new(1);
data.LayerInfos.Add(info);
}

if (!xml.HasChild("Layers")) return;

foreach (var o in xml["Layers"])
{
if (o is not XmlElement {Name: "Layer"}) continue;
var info = HandleLayer(xml["Layer"]);
if (o is not XmlElement {Name: "Layer"} layerXml) continue;
var info = HandleLayer(layerXml);
data.LayerInfos ??= new();
data.LayerInfos.Add(info);
}
}
Expand Down
14 changes: 9 additions & 5 deletions Source/Layers/PortraitLayers/PortraitLayerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ public static void Parse(ArcherCustomData data, XmlElement xml)
if (xml.HasChild("PortraitLayer"))
{
var info = ParseLayer(xml["PortraitLayer"]);
data.PortraitLayerInfos ??= new(1);
data.PortraitLayerInfos.Add(info);
}

if (!xml.HasChild("PortraitLayers")) return;

foreach (var o in xml["PortraitLayers"])
var portraitLayersXml = xml["PortraitLayers"];
foreach (var o in portraitLayersXml)
{
if (o is not XmlElement {Name: "Layer"}) continue;
var info = ParseLayer(xml["Layer"]);
if (o is not XmlElement {Name: "Layer"} layerXml) continue;
var info = ParseLayer(layerXml);
data.PortraitLayerInfos ??= new();
data.PortraitLayerInfos.Add(info);
}
}
Expand All @@ -30,12 +33,13 @@ private static PortraitLayerInfo ParseLayer(XmlElement xml)
if (FortEntrance.Settings.DisableLayers)
return null;

var attachToText = xml.ChildText("AttachTo");
var attachToText = xml.ChildText("AttachTo", null);

var portraitLayerInfo = new PortraitLayerInfo
{

AttachTo = attachToText == "join" ? PortraitLayersAttachType.Join : PortraitLayersAttachType.NotJoin,
AttachTo = attachToText == "join" || attachToText == "Join"
|| attachToText == "joined" || attachToText == "Joined"? PortraitLayersAttachType.Join : PortraitLayersAttachType.NotJoin,

Sprite = xml.ChildText("Sprite"),
Position = xml.ChildPosition("Position", Vector2.Zero),
Expand Down
61 changes: 42 additions & 19 deletions Source/Layers/PortraitLayers/PortraitLayerPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,67 +38,90 @@ public static void Unload()

private static void OnArcherPortraitOnUpdate(ArcherPortrait.orig_Update orig, TowerFall.ArcherPortrait self)
{

orig(self);
}

private static void OnArcherPortraitOnLeave(ArcherPortrait.orig_Leave orig, TowerFall.ArcherPortrait self)
{
orig(self);
if (!portraitLayers.ContainsKey(self)) return;
var layers = portraitLayers[self];
if (layers == null) return;
foreach (var portraitLayerInfo in layers)
{
if(portraitLayerInfo.layerInfo.AttachTo == PortraitLayersAttachType.Join)
portraitLayerInfo.Visible = false;
portraitLayerInfo.Visible = portraitLayerInfo.layerInfo.AttachTo == PortraitLayersAttachType.NotJoin;
}
}

private static void OnArcherPortraitOnStartJoined(ArcherPortrait.orig_StartJoined orig, TowerFall.ArcherPortrait self)
{
orig(self);
if (!portraitLayers.ContainsKey(self)) return;
var layers = portraitLayers[self];
if (layers == null) return;
foreach (var portraitLayerInfo in layers)
{
if(portraitLayerInfo.layerInfo.AttachTo == PortraitLayersAttachType.NotJoin)
portraitLayerInfo.Visible = false;
portraitLayerInfo.Visible = portraitLayerInfo.layerInfo.AttachTo == PortraitLayersAttachType.Join;
}
}

private static void OnSetCharacter(ArcherPortrait.orig_SetCharacter origSetCharacter, TowerFall.ArcherPortrait archerPortrait, int characterIndex, ArcherData.ArcherTypes altSelect, int moveDir)
{
if (portraitLayers.ContainsKey(archerPortrait))
{
var currentLayers = portraitLayers[archerPortrait];
if (currentLayers == null) return;
foreach (var portraitLayerInfo in currentLayers)
{
portraitLayerInfo.Visible = false;
}
}

origSetCharacter(archerPortrait, characterIndex, altSelect, moveDir);

var data = ArcherData.Get(characterIndex, altSelect);

var exist = Mod.ArcherCustomDataDict.TryGetValue(data, out var archerCustomData);
if (!exist) return;

var layerInfos = archerCustomData.PortraitLayerInfos;
if (layerInfos == null) return;

if (!portraitLayers.ContainsKey(archerPortrait)) return;
var layers = portraitLayers[archerPortrait];
if (layers == null)

if (!portraitLayers.ContainsKey(archerPortrait))
{
layers = new List<PortraitLayerSpriteComponent>(layerInfos.Count);
portraitLayers[archerPortrait] = layers;
var flashSprite = DynamicData.For(archerPortrait).Get<Sprite<string>>("flash");
var flashIndex = -1;
for (var i = 0; i < archerPortrait.Components.Count; i++)
{
if (archerPortrait.Components[i] == flashSprite)
{
flashIndex = i;
}
}

var newLayers = new List<PortraitLayerSpriteComponent>(layerInfos.Count);
foreach (var portraitLayerInfo in layerInfos)
{
var layer = new PortraitLayerSpriteComponent(portraitLayerInfo,true, false);
archerPortrait.Add(layer);
layers.Add(layer);
newLayers.Add(layer);
archerPortrait.Components.Remove(layer);
archerPortrait.Components.Insert(flashIndex, layer);
}
portraitLayers[archerPortrait] = newLayers;
}


var layers = portraitLayers[archerPortrait];
foreach (var portraitLayerInfo in layers)
{
portraitLayerInfo.Visible = portraitLayerInfo.layerInfo.AttachTo switch
if (portraitLayerInfo.layerInfo.AttachTo == PortraitLayersAttachType.Join)
portraitLayerInfo.Visible = false;
else if (portraitLayerInfo.layerInfo.AttachTo == PortraitLayersAttachType.NotJoin)
{
PortraitLayersAttachType.Join => false,
PortraitLayersAttachType.NotJoin => true,
_ => portraitLayerInfo.Visible
};
portraitLayerInfo.Visible = true;
}

else
portraitLayerInfo.Visible = portraitLayerInfo.Visible;
}
}
}
Expand Down
43 changes: 38 additions & 5 deletions Source/Layers/PortraitLayers/PortraitLayerSpriteComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Xml;
using ArcherLoaderMod.Layers;
using ArcherLoaderMod.Rainbow;
using Microsoft.Xna.Framework;
using Monocle;
using MonoMod.Utils;
using TowerFall;
Expand All @@ -14,7 +15,7 @@ public class PortraitLayerSpriteComponent : Component
private ArcherPortrait attachedSprite;

public PortraitLayerInfo layerInfo;
private Sprite<string> layerSprite;
public Sprite<string> layerSprite;

public PortraitLayerSpriteComponent(PortraitLayerInfo layerInfo, bool active, bool visible) : base(active, visible)
{
Expand Down Expand Up @@ -42,8 +43,26 @@ public override void Added()
// var attachedSpriteInfo = portrait;
// var xml = TFGame.SpriteData.GetXML(attachedSpriteInfo);

// foreach (var customSpriteDataInfo in Mod.customSpriteDataCategoryDict["layer"])
// {
// if (customSpriteDataInfo.id == layerInfo.Sprite)
// {
// layerSprite = customSpriteDataInfo.Element;
// break;
// }
// }

// var layer = TFGame.SpriteData.GetXML(layerInfo.Sprite);

// var xml = TFGame.SpriteData.GetXML(attachedSpriteInfo);

layerSprite = TFGame.SpriteData.GetSpriteString(layerInfo.Sprite);
layerSprite.Color = layerInfo.Color;


// layerSprite = TFGame.SpriteData.GetSpriteString(layerInfo.Sprite);


// var childText = layer.ChildText("Texture");
// var atlas = TFGame.Atlas[childText];

Expand All @@ -53,7 +72,7 @@ public override void Added()
//
// layerSprite.Visible = attachedSprite.Visible;
// DynamicData.For(layerSprite).Set("Entity", Entity);
// DynamicData.For(layerSprite).Set("Parent", Entity);
DynamicData.For(layerSprite).Set("Parent", Entity);

// sprite = new Sprite<string>(atlas, xML.ChildInt("FrameWidth"), xML.ChildInt("FrameHeight"))
// {
Expand Down Expand Up @@ -83,19 +102,19 @@ public override void Added()
//
// headSprite = DynamicData.For(player).Get<Sprite<string>>("headSprite");
// bowSprite = DynamicData.For(player).Get<Sprite<string>>("bowSprite");
base.Added();
}



public override void Update()
{
var player = ((Player) Parent);
// drawSelfPropertyInfo.SetValue(player, false);
// var childText = layer.ChildText("Texture");
// var atlas = TFGame.Atlas[childText];
// sprite.SwapSubtexture(atlas);

layerSprite.Visible = attachedSprite.Visible;
// layerSprite.Visible = attachedSprite.Visible;
// layerSprite.Effects = attachedSprite.Effects;
// if(layerInfo.AttachTo != LayerAttachType.Head)
// layerSprite.FlipX = player.Facing != Facing.Right;
Expand All @@ -119,7 +138,21 @@ public override void Update()

public override void Render()
{
var player = ((Player) Parent);
var portrait = ((ArcherPortrait) Parent);

var portraitImage = DynamicData.For(portrait).Get<Image>("portrait");
var offset = DynamicData.For(portrait).Get<Vector2>("offset");
// var Vector2 vector2_1 = this.Entity.Position + this.offset;
layerSprite.Position = ((RollcallElement)portrait.Parent).Position + offset;
layerSprite.Scale = portraitImage.Scale;
// layerSprite.Origin = portraitImage.Origin;
// layerSprite.Rotation = portraitImage.Rotation;
// layerSprite.X = portraitImage.X;
// layerSprite.Y = portraitImage.Y;

// = this.flash.Scale = new Vector2((float) (1.0 + (double) this.wiggler.Value * 0.0500000007450581), (float) (1.0 - (double) this.wiggler.Value * 0.0500000007450581));
// layerSprite.Position = this.portraitAlt.Position = this.flash.Position = this.offset + this.lastShake;

layerSprite.Render();
// layerSprite2.Render();
base.Render();
Expand Down
6 changes: 4 additions & 2 deletions Source/Particles/ParticleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ public static void ParseParticles(ArcherCustomData data, XmlElement xml)
if (xml.HasChild("Particle"))
{
var info = HandleParticle(xml["Particle"]);
data.ParticlesInfos ??= new(1);
data.ParticlesInfos.Add(info);
}

if (!xml.HasChild("Particles")) return;

foreach (var o in xml["Particles"])
{
if (o is not XmlElement {Name: "Particle"}) continue;
var info = HandleParticle(xml["Particle"]);
if (o is not XmlElement {Name: "Particle"} particleXml) continue;
var info = HandleParticle(particleXml);
data.ParticlesInfos ??= new();
data.ParticlesInfos.Add(info);
}
}
Expand Down

0 comments on commit dc1f2f2

Please sign in to comment.