Wrote netcode to support Animations, AnimationManagers, and CoreObjects.

This commit is contained in:
2018-08-06 20:01:59 -07:00
parent 3af2da68fa
commit 2276faa610
12 changed files with 290 additions and 30 deletions

View File

@ -1,9 +1,11 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Netcode;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Serialization;
namespace StardustCore.Animations namespace StardustCore.Animations
{ {
@ -19,12 +21,15 @@ namespace StardustCore.Animations
/// <summary> /// <summary>
/// The duration of the frame in length. /// The duration of the frame in length.
/// </summary> /// </summary>
public readonly int frameDuration; public int frameDuration;
/// <summary> /// <summary>
/// The duration until the next frame. /// The duration until the next frame.
/// </summary> /// </summary>
public int frameCountUntilNextAnimation; public int frameCountUntilNextAnimation;
[XmlIgnore]
public NetFields NetFields { get; } = new NetFields();
/// <summary> /// <summary>

View File

@ -16,15 +16,17 @@ namespace StardustCore.Animations
/// </summary> /// </summary>
public class AnimationManager public class AnimationManager
{ {
public Dictionary<string, List<Animation>> animations = new Dictionary<string, List<Animation>>(); public Dictionary<string, List<Animation>> animations = new SerializableDictionary<string, List<Animation>>();
public string currentAnimationName; public string currentAnimationName;
public int currentAnimationListIndex; public int currentAnimationListIndex;
public List<Animation> currentAnimationList = new List<Animation>(); public List<Animation> currentAnimationList = new List<Animation>();
private Texture2DExtended objectTexture; ///Might not be necessary if I use the CoreObject texture sheet. private Texture2DExtended objectTexture; ///Might not be necessary if I use the CoreObject texture sheet.
public Animation defaultDrawFrame; public Animation defaultDrawFrame;
public Animation currentAnimation; public Animation currentAnimation;
bool enabled; public bool enabled;
public string animationDataString;
/// <summary> /// <summary>
/// Constructor for Animation Manager class. /// Constructor for Animation Manager class.
/// </summary> /// </summary>
@ -38,16 +40,19 @@ namespace StardustCore.Animations
this.defaultDrawFrame = DefaultFrame; this.defaultDrawFrame = DefaultFrame;
this.enabled = EnabledByDefault; this.enabled = EnabledByDefault;
currentAnimation = this.defaultDrawFrame; currentAnimation = this.defaultDrawFrame;
this.currentAnimationName = "";
this.animationDataString = "";
} }
public AnimationManager(Texture2DExtended ObjectTexture,Animation DefaultFrame ,Dictionary<string, List<Animation>> animationsToPlay, string startingAnimationKey, int startingAnimationFrame=0,bool EnabledByDefault=true) public AnimationManager(Texture2DExtended ObjectTexture,Animation DefaultFrame ,string animationString, string startingAnimationKey, int startingAnimationFrame=0,bool EnabledByDefault=true)
{ {
currentAnimationListIndex = 0; currentAnimationListIndex = 0;
this.objectTexture = ObjectTexture; this.objectTexture = ObjectTexture;
this.defaultDrawFrame = DefaultFrame; this.defaultDrawFrame = DefaultFrame;
this.enabled = EnabledByDefault; this.enabled = EnabledByDefault;
this.animations = animationsToPlay; this.animationDataString = animationString;
this.animations = parseAnimationsFromXNB(animationString);
bool f = animations.TryGetValue(startingAnimationKey, out currentAnimationList); bool f = animations.TryGetValue(startingAnimationKey, out currentAnimationList);
if (f == true) { if (f == true) {
setAnimation(startingAnimationKey, startingAnimationFrame); setAnimation(startingAnimationKey, startingAnimationFrame);
@ -55,6 +60,22 @@ namespace StardustCore.Animations
else currentAnimation = this.defaultDrawFrame; else currentAnimation = this.defaultDrawFrame;
} }
public AnimationManager(Texture2DExtended ObjectTexture, Animation DefaultFrame, Dictionary<string,List<Animations.Animation>> animationString, string startingAnimationKey, int startingAnimationFrame = 0, bool EnabledByDefault = true)
{
currentAnimationListIndex = 0;
this.objectTexture = ObjectTexture;
this.defaultDrawFrame = DefaultFrame;
this.enabled = EnabledByDefault;
this.animations = animationString;
bool f = animations.TryGetValue(startingAnimationKey, out currentAnimationList);
if (f == true)
{
setAnimation(startingAnimationKey, startingAnimationFrame);
}
else currentAnimation = this.defaultDrawFrame;
}
/// <summary> /// <summary>
/// Update the animation frame once after drawing the object. /// Update the animation frame once after drawing the object.
/// </summary> /// </summary>
@ -223,6 +244,16 @@ namespace StardustCore.Animations
return this.objectTexture; return this.objectTexture;
} }
public void setExtendedTexture(Texture2DExtended texture)
{
this.objectTexture = texture;
}
public void setEnabled(bool enabled)
{
this.enabled = enabled;
}
public Texture2D getTexture() public Texture2D getTexture()
{ {
return this.objectTexture.getTexture(); return this.objectTexture.getTexture();

View File

@ -78,6 +78,8 @@ namespace StardustCore
tile1.Name = "test"; tile1.Name = "test";
tile1.displayName = "test"; tile1.displayName = "test";
Game1.player.addItemToInventory(tile1); Game1.player.addItemToInventory(tile1);
} }
private void SaveEvents_AfterSave(object sender, EventArgs e) private void SaveEvents_AfterSave(object sender, EventArgs e)

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Netcode;
namespace StardustCore.NetCode
{
public class NetAnimation : Netcode.NetField<Animations.Animation, NetAnimation>
{
public NetRectangle sourceRect;
public NetInt frameDuration;
public NetInt frameDurationUntilNextAnimation;
public NetAnimation()
{
}
public NetAnimation(Animations.Animation animation) : base(animation)
{
}
protected override void ReadDelta(BinaryReader reader, NetVersion version)
{
sourceRect = new NetRectangle();
sourceRect.Read(reader, version);
Value.sourceRectangle = sourceRect.Value;
frameDuration = new NetInt();
frameDuration.Read(reader, version);
Value.frameDuration = frameDuration.Value;
frameDurationUntilNextAnimation = new NetInt();
frameDurationUntilNextAnimation.Read(reader, version);
Value.frameDuration = frameDuration.Value;
}
protected override void WriteDelta(BinaryWriter writer)
{
sourceRect = new NetRectangle(Value.sourceRectangle);
sourceRect.Write(writer);
frameDuration = new NetInt(Value.frameDuration);
frameDuration.Write(writer);
frameDurationUntilNextAnimation = new NetInt(Value.frameCountUntilNextAnimation);
frameDurationUntilNextAnimation.Write(writer);
}
}
}

View File

@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Netcode;
using StardewValley;
using StardewValley.Network;
namespace StardustCore.NetCode
{
public class NetAnimationManager : Netcode.NetField<Animations.AnimationManager,NetAnimationManager>
{
public NetAnimationManager()
{
}
public NetAnimationManager(Animations.AnimationManager manager): base(manager)
{
}
public NetString currentAnimationName;
public NetInt currentAnimationListIndex;
public NetTexture2DExtended objectTexture;
public NetAnimation defaultDrawFrame;
public NetBool enabled;
public NetString animationDataString;
protected override void ReadDelta(BinaryReader reader, NetVersion version)
{
NetString currentAnimationName = new NetString();
currentAnimationName.Read(reader, version);
NetInt currentIndex = new NetInt();
currentIndex.Read(reader, version);
NetTexture2DExtended text = new NetTexture2DExtended();
text.Read(reader, version);
NetAnimation defaultAnimation = new NetAnimation();
defaultAnimation.Read(reader, version);
NetBool enabled = new NetBool();
enabled.Read(reader, version);
NetString data = new NetString();
data.Read(reader, version);
Value.setExtendedTexture(text.Value);
Value.defaultDrawFrame = defaultAnimation.Value;
Value.enabled = enabled.Value;
//Try and prevent unnecessary parsing.
if (Value.animations == null && !String.IsNullOrEmpty(Value.animationDataString))
{
Value.animations = Animations.AnimationManager.parseAnimationsFromXNB(data.Value);
}
if (!String.IsNullOrEmpty(data.Value))
{
Value.setAnimation(currentAnimationName.Value, currentIndex.Value);
}
else
{
Value.currentAnimation = defaultDrawFrame.Value;
}
}
protected override void WriteDelta(BinaryWriter writer)
{
NetString curentAnimationName = new NetString(Value.currentAnimationName);
currentAnimationName.Write(writer);
NetInt currentAnimationListIndex = new NetInt(Value.currentAnimationListIndex);
currentAnimationListIndex.Write(writer);
NetTexture2DExtended texture = new NetTexture2DExtended(Value.getExtendedTexture());
texture.Write(writer);
NetAnimation defaultDrawFrame = new NetAnimation(Value.defaultDrawFrame);
defaultDrawFrame.Write(writer);
NetBool enabled = new NetBool(Value.enabled);
enabled.Write(writer);
NetString animationData = new NetString(Value.animationDataString);
animationData.Write(writer);
}
}
}

View File

@ -6,20 +6,54 @@ using System.Runtime.Serialization.Formatters.Binary;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Netcode; using Netcode;
using StardewValley;
using StardustCore.UIUtilities; using StardustCore.UIUtilities;
namespace StardustCore.NetCode namespace StardustCore.NetCode
{ {
class NetCoreObject : Netcode.NetField<CoreObject,NetCoreObject> public class NetCoreObject : Netcode.NetField<CoreObject,NetCoreObject>
{ {
public NetTexture2DExtended texture;
public NetInt which; public NetInt which;
public NetVector2 tilePos; public NetVector2 tilePos;
public NetInt InventoryMaxSize;
public NetRectangle sourceRect;
public NetRectangle boundingBox; public NetRectangle boundingBox;
public NetVector2 position;
public NetInt Decoration_type;
public NetInt rotations;
public NetInt currentRotation;
public NetInt sourceIndexOffset;
public NetVector2 drawPosition;
public NetRectangle sourceRect;
public NetRectangle defaultSourceRect;
public NetRectangle defaultBoundingBox;
public NetString description;
public NetTexture2DExtended texture;
public NetBool flipped;
public NetBool flaggedForPickup;
public NetBool lightGlowAdded;
public NetObjectList<Item> inventory;
public NetInt InventoryMaxSize;
public NetBool itemReadyForHarvest;
public NetBool lightsOn;
public NetString locationName;
public NetColor lightColor;
public NetBool removable;
public NetColor drawColor;
public NetBool useXML;
public NetString serializationName;
//Animation Manager.....
public NetAnimationManager animationManager;
public NetCoreObject() public NetCoreObject()
{ {
@ -55,6 +89,14 @@ namespace StardustCore.NetCode
boundingBox = new NetRectangle(); boundingBox = new NetRectangle();
boundingBox.Read(reader, version); boundingBox.Read(reader, version);
Value.boundingBox.Value = boundingBox.Value; Value.boundingBox.Value = boundingBox.Value;
drawPosition = new NetVector2();
drawPosition.Read(reader, version);
Value.drawPosition = drawPosition.Value;
animationManager = new NetAnimationManager();
animationManager.Read(reader, version);
Value.animationManager = animationManager.Value;
} }
protected override void WriteDelta(BinaryWriter writer) protected override void WriteDelta(BinaryWriter writer)
@ -76,6 +118,15 @@ namespace StardustCore.NetCode
boundingBox = new NetRectangle(Value.boundingBox.Value); boundingBox = new NetRectangle(Value.boundingBox.Value);
sourceRect.Write(writer); sourceRect.Write(writer);
drawPosition = new NetVector2(Value.drawPosition);
drawPosition.Write(writer);
if (Value.animationManager != null)
{
animationManager = new NetAnimationManager(Value.animationManager);
animationManager.Write(writer);
}
} }
} }
} }

View File

@ -0,0 +1,24 @@
using Netcode;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StardustCore.NetCode
{
public class NetKeyValuePair<K, V, KField, VField> : NetField<KeyValuePair<K, V>, NetKeyValuePair<K, V, KField, VField>> where KField : NetField<K, KField>, new() where VField : NetField<V, VField>, new()
{
protected override void ReadDelta(BinaryReader reader, NetVersion version)
{
throw new NotImplementedException();
}
protected override void WriteDelta(BinaryWriter writer)
{
throw new NotImplementedException();
}
}
}

View File

@ -51,8 +51,6 @@ namespace StardustCore
public bool lightGlowAdded; public bool lightGlowAdded;
public string texturePath;
public List<Item> inventory; public List<Item> inventory;
public int inventoryMaxSize; public int inventoryMaxSize;
@ -65,8 +63,6 @@ namespace StardustCore
public Color lightColor; public Color lightColor;
public string thisType;
public bool removable; public bool removable;
public string locationsName; public string locationsName;
@ -102,7 +98,6 @@ namespace StardustCore
lightsOn = false; lightsOn = false;
lightColor = Color.Black; lightColor = Color.Black;
thisType = this.GetType().ToString();
this.NetFields.AddField(new NetCode.NetCoreObject(this)); this.NetFields.AddField(new NetCode.NetCoreObject(this));
@ -126,7 +121,6 @@ namespace StardustCore
if (TextureSheet == null) if (TextureSheet == null)
{ {
TextureSheet = texture; TextureSheet = texture;
this.texturePath = texture.path;
} }
Dictionary<int, string> dictionary = Game1.content.Load<Dictionary<int, string>>("Data\\Furniture"); Dictionary<int, string> dictionary = Game1.content.Load<Dictionary<int, string>>("Data\\Furniture");
string[] array = dictionary[which].Split(new char[] string[] array = dictionary[which].Split(new char[]

View File

@ -26,7 +26,6 @@ namespace StardustCore.Objects
this.name = part.name; this.name = part.name;
this.description = part.description; this.description = part.description;
this.TextureSheet = part.getExtendedTexture(); this.TextureSheet = part.getExtendedTexture();
this.texturePath = this.TextureSheet.path;
if (part.animationManager != null) if (part.animationManager != null)
{ {
this.animationManager = part.animationManager; this.animationManager = part.animationManager;

View File

@ -27,7 +27,6 @@ namespace StardustCore.Objects
{ {
this.objects = Objects; this.objects = Objects;
this.TextureSheet = texture; this.TextureSheet = texture;
this.texturePath = texture.path;
this.categoryColor = CategoryColor; this.categoryColor = CategoryColor;
this.categoryName = CategoryName; this.categoryName = CategoryName;
this.name = Name; this.name = Name;
@ -51,7 +50,6 @@ namespace StardustCore.Objects
this.animationManager = animationManager; this.animationManager = animationManager;
this.objects = Objects; this.objects = Objects;
this.TextureSheet =animationManager.getExtendedTexture(); this.TextureSheet =animationManager.getExtendedTexture();
this.texturePath = animationManager.getExtendedTexture().path;
this.name = Name; this.name = Name;
this.displayName = Name; this.displayName = Name;
this.description = Description; this.description = Description;

View File

@ -390,7 +390,7 @@ namespace StardustCore.Serialization
{ {
//THE ERROR LIES HERE AS IT THINKS IT CAN TRY TO BE A CORE OBJECT WHEN IT IS NOT!!!! //THE ERROR LIES HERE AS IT THINKS IT CAN TRY TO BE A CORE OBJECT WHEN IT IS NOT!!!!
CoreObject core_obj = StardustCore.ModCore.ModHelper.ReadJsonFile<CoreObject>(path); //FIND A WAY TO FIX THIS!!!! CoreObject core_obj = StardustCore.ModCore.ModHelper.ReadJsonFile<CoreObject>(path); //FIND A WAY TO FIX THIS!!!!
type = (core_obj as CoreObject).thisType; type = (core_obj as CoreObject).serializationName;
//ModCore.ModMonitor.Log("UMM THIS CAN't BE RIGHT 1" + type); //ModCore.ModMonitor.Log("UMM THIS CAN't BE RIGHT 1" + type);
} }
@ -705,7 +705,7 @@ namespace StardustCore.Serialization
{ {
//THE ERROR LIES HERE AS IT THINKS IT CAN TRY TO BE A CORE OBJECT WHEN IT IS NOT!!!! //THE ERROR LIES HERE AS IT THINKS IT CAN TRY TO BE A CORE OBJECT WHEN IT IS NOT!!!!
CoreObject core_obj = StardustCore.ModCore.ModHelper.ReadJsonFile<CoreObject>(path); //FIND A WAY TO FIX THIS!!!! CoreObject core_obj = StardustCore.ModCore.ModHelper.ReadJsonFile<CoreObject>(path); //FIND A WAY TO FIX THIS!!!!
type = (core_obj as CoreObject).thisType; type = (core_obj as CoreObject).serializationName;
//ModCore.ModMonitor.Log("UMM THIS CAN't BE RIGHT 1" + type); //ModCore.ModMonitor.Log("UMM THIS CAN't BE RIGHT 1" + type);
} }

View File

@ -89,7 +89,10 @@
<Compile Include="Interfaces\IToolSerializer.cs" /> <Compile Include="Interfaces\IToolSerializer.cs" />
<Compile Include="Math\Hex.cs" /> <Compile Include="Math\Hex.cs" />
<Compile Include="Math\Hex32.cs" /> <Compile Include="Math\Hex32.cs" />
<Compile Include="NetCode\NetAnimation.cs" />
<Compile Include="NetCode\NetAnimationManager.cs" />
<Compile Include="NetCode\NetCoreObject.cs" /> <Compile Include="NetCode\NetCoreObject.cs" />
<Compile Include="NetCode\NetKeyValuePair.cs" />
<Compile Include="NetCode\NetTexure2DExtended.cs" /> <Compile Include="NetCode\NetTexure2DExtended.cs" />
<Compile Include="Objects\MultiTileComponent.cs" /> <Compile Include="Objects\MultiTileComponent.cs" />
<Compile Include="Objects\MultiTileObject.cs" /> <Compile Include="Objects\MultiTileObject.cs" />
@ -439,6 +442,10 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="NetCode\NetDictionaries\" />
<Folder Include="NetCode\NetLists\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\deploy.targets" /> <Import Project="$(SolutionDir)\deploy.targets" />
<Import Project="..\packages\Pathoschild.Stardew.ModBuildConfig.2.1.0-beta-20180428\build\Pathoschild.Stardew.ModBuildConfig.targets" Condition="Exists('..\packages\Pathoschild.Stardew.ModBuildConfig.2.1.0-beta-20180428\build\Pathoschild.Stardew.ModBuildConfig.targets')" /> <Import Project="..\packages\Pathoschild.Stardew.ModBuildConfig.2.1.0-beta-20180428\build\Pathoschild.Stardew.ModBuildConfig.targets" Condition="Exists('..\packages\Pathoschild.Stardew.ModBuildConfig.2.1.0-beta-20180428\build\Pathoschild.Stardew.ModBuildConfig.targets')" />