From 3931ba81b83d6b99374aea3526e5a5323fa6e576 Mon Sep 17 00:00:00 2001 From: Joshua Navarro Date: Sun, 13 Jan 2019 14:46:31 -0800 Subject: [PATCH] Experimenting with benches and updated serializer to serialize to strings. --- .../Framework/Objects/Furniture/Bench.cs | 110 ++++++++++++++++++ .../Objects/Furniture/ChairTileComponent.cs | 4 + .../Framework/Objects/MultiTiledComponent.cs | 4 + .../Framework/Objects/MultiTiledObject.cs | 32 ++++- .../Framework/Player/Managers/SittingInfo.cs | 15 ++- .../Utilities/Serialization/Serialization.cs | 40 +++++++ GeneralMods/Revitalize/Revitalize.csproj | 1 + 7 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 GeneralMods/Revitalize/Framework/Objects/Furniture/Bench.cs diff --git a/GeneralMods/Revitalize/Framework/Objects/Furniture/Bench.cs b/GeneralMods/Revitalize/Framework/Objects/Furniture/Bench.cs new file mode 100644 index 00000000..16e7357d --- /dev/null +++ b/GeneralMods/Revitalize/Framework/Objects/Furniture/Bench.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Xna.Framework; +using PyTK.CustomElementHandler; +using StardewValley; + +namespace Revitalize.Framework.Objects.Furniture +{ + public class Bench:ChairMultiTiledObject + { + public Bench() : base() + { + + } + + public List playersSittingHere = new List(); + + public Bench(BasicItemInformation info, Vector2 TilePosition) : base(info, TilePosition) + { + + } + + + public Bench(BasicItemInformation info,Vector2 TilePosition, Dictionary Objects): base(info, TilePosition, Objects) + { + + } + + /// + /// Rotate all chair components associated with this chair object. + /// + public override void rotate() + { + + if (Revitalize.ModCore.playerInfo.sittingInfo.SittingObject == this) return; + if (this.playersSittingHere.Count > 0) + { + Game1.showRedMessage("Can't rotate furniture when people are siting on it."); + return; + } + + foreach (KeyValuePair pair in this.objects) + { + (pair.Value as ChairTileComponent).rotate(); + } + foreach (KeyValuePair pair in this.objects) + { + (pair.Value as ChairTileComponent).checkForSpecialUpSittingAnimation(); + } + + base.rotate(); + } + + public override Item getOne() + { + Dictionary objs = new Dictionary(); + foreach (var pair in this.objects) + { + objs.Add(pair.Key, (MultiTiledComponent)pair.Value); + } + + return new Bench(this.info, this.TileLocation, objs); + } + + + public override ICustomObject recreate(Dictionary additionalSaveData, object replacement) + { + Bench obj = (Bench)Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["GUID"]); + if (obj == null) + { + return null; + } + + Dictionary guids = new Dictionary(); + + foreach (KeyValuePair pair in obj.childrenGuids) + { + guids.Add(pair.Key, pair.Value); + } + + foreach (KeyValuePair pair in guids) + { + obj.childrenGuids.Remove(pair.Key); + //Revitalize.ModCore.log("DESERIALIZE: " + pair.Value.ToString()); + ChairTileComponent component = Revitalize.ModCore.Serializer.DeserializeGUID(pair.Value.ToString()); + component.InitNetFields(); + + obj.addComponent(pair.Key, component); + + + } + obj.InitNetFields(); + + if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["GUID"])) + { + Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["GUID"], obj); + return obj; + } + else + { + return Revitalize.ModCore.ObjectGroups[additionalSaveData["GUID"]]; + } + } + + + } +} diff --git a/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairTileComponent.cs b/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairTileComponent.cs index d7db3f34..1b72aa2c 100644 --- a/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairTileComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/Furniture/ChairTileComponent.cs @@ -71,6 +71,10 @@ namespace Revitalize.Framework.Objects.Furniture if (this.CanSitHere) { Revitalize.ModCore.playerInfo.sittingInfo.sit(this.containerObject, this.TileLocation*Game1.tileSize); + if(this.containerObject is Bench) + { + (this.containerObject as Bench).playersSittingHere.Add(Game1.player.uniqueMultiplayerID); + } foreach(KeyValuePair pair in this.containerObject.objects) { (pair.Value as ChairTileComponent).checkForSpecialUpSittingAnimation(); diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs index aa5cc108..335b121c 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs @@ -218,6 +218,10 @@ namespace Revitalize.Framework.Objects addedDepth += (this.containerObject.Height - 1) - ((int)(this.offsetKey.Y)); if (this.info.ignoreBoundingBox) addedDepth+=1.5f; } + else if (this.info.ignoreBoundingBox) + { + addedDepth += 1.0f; + } this.animationManager.draw(spriteBatch, this.displayTexture, Game1.GlobalToLocal(Game1.viewport, new Vector2((float)(x * Game1.tileSize), y * Game1.tileSize)), new Rectangle?(this.animationManager.currentAnimation.sourceRectangle), this.info.drawColor * alpha, 0f, Vector2.Zero, (float)Game1.pixelZoom, this.flipped ? SpriteEffects.FlipHorizontally : SpriteEffects.None, Math.Max(0f, (float)((this.TileLocation.Y + addedDepth) * Game1.tileSize) / 10000f)); try { diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs index eba0c40b..8cc01385 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledObject.cs @@ -7,6 +7,7 @@ using Microsoft.Xna.Framework.Graphics; using Newtonsoft.Json; using PyTK.CustomElementHandler; using StardewValley; +using StardewValley.Objects; namespace Revitalize.Framework.Objects { @@ -88,6 +89,8 @@ namespace Revitalize.Framework.Objects public bool removeComponent(Vector2 key) { + + if (!this.objects.ContainsKey(key)) return false; @@ -123,7 +126,33 @@ namespace Revitalize.Framework.Objects //base.drawWhenHeld(spriteBatch, objectPosition, f); } - //IMPLEMENT THESE! + + public override void drawPlacementBounds(SpriteBatch spriteBatch, GameLocation location) + { + foreach (KeyValuePair pair in this.objects) + { + if (!this.isPlaceable()) + return; + int x = Game1.getOldMouseX() + Game1.viewport.X+ (int)((pair.Value as MultiTiledComponent).offsetKey.X*Game1.tileSize); + int y = Game1.getOldMouseY() + Game1.viewport.Y+ (int)((pair.Value as MultiTiledComponent).offsetKey.Y * Game1.tileSize); + if ((double)Game1.mouseCursorTransparency == 0.0) + { + x = ((int)Game1.player.GetGrabTile().X+ (int)((pair.Value as MultiTiledComponent).offsetKey.X)) * 64; + y = ((int)Game1.player.GetGrabTile().Y + (int)((pair.Value as MultiTiledComponent).offsetKey.Y)) * 64; + } + if (Game1.player.GetGrabTile().Equals(Game1.player.getTileLocation()) && (double)Game1.mouseCursorTransparency == 0.0) + { + Vector2 translatedVector2 = Utility.getTranslatedVector2(Game1.player.GetGrabTile(), Game1.player.FacingDirection, 1f); + translatedVector2 += (pair.Value as MultiTiledComponent).offsetKey; + x = (int)translatedVector2.X * 64; + y = (int)translatedVector2.Y * 64; + } + bool flag = Utility.playerCanPlaceItemHere(location, (Item)pair.Value, x, y, Game1.player); + spriteBatch.Draw(Game1.mouseCursors, new Vector2((float)(x / 64 * 64 - Game1.viewport.X), (float)(y / 64 * 64 - Game1.viewport.Y)), new Microsoft.Xna.Framework.Rectangle?(new Microsoft.Xna.Framework.Rectangle(flag ? 194 : 210, 388, 16, 16)), Color.White, 0.0f, Vector2.Zero, 4f, SpriteEffects.None, 0.01f); + this.draw(spriteBatch, x / 64, y / 64, 0.5f); + } + } + public virtual void pickUp() @@ -270,6 +299,7 @@ namespace Revitalize.Framework.Objects } } } + public override bool canStackWith(Item other) { return false; diff --git a/GeneralMods/Revitalize/Framework/Player/Managers/SittingInfo.cs b/GeneralMods/Revitalize/Framework/Player/Managers/SittingInfo.cs index 6ca981d6..ff818e54 100644 --- a/GeneralMods/Revitalize/Framework/Player/Managers/SittingInfo.cs +++ b/GeneralMods/Revitalize/Framework/Player/Managers/SittingInfo.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Revitalize.Framework.Objects; +using Revitalize.Framework.Objects.Furniture; using StardewValley; namespace Revitalize.Framework.Player.Managers @@ -50,12 +51,22 @@ namespace Revitalize.Framework.Player.Managers { this.isSitting = false; this.elapsedTime = 0; - if(this.sittingObject is MultiTiledObject) + if(this.sittingObject is MultiTiledObject && (this.sittingObject.GetType()!=typeof(Bench))) { (this.sittingObject as MultiTiledObject).setAllAnimationsToDefault(); + this.sittingObject = null; + } + else if(this.sittingObject is Bench) + { + (this.sittingObject as Bench).playersSittingHere.Remove(Game1.player.uniqueMultiplayerID); + if((this.sittingObject as Bench).playersSittingHere.Count == 0) + { + (this.sittingObject as MultiTiledObject).setAllAnimationsToDefault(); + } } - this.sittingObject = null; + + } if (this.isSitting && Game1.player.CanMove) { diff --git a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs index dffd06ea..dd99a092 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs @@ -27,6 +27,7 @@ namespace Revitalize.Framework.Utilities public List itemsToRemove = new List(); + private JsonSerializerSettings settings; /// /// Constructor. @@ -48,6 +49,15 @@ namespace Revitalize.Framework.Utilities gatherAllFilesForCleanup(); + this.settings = new JsonSerializerSettings(); + foreach(JsonConverter converter in this.serializer.Converters) + { + this.settings.Converters.Add(converter); + } + this.settings.Formatting = Formatting.Indented; + this.settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + this.settings.NullValueHandling = NullValueHandling.Include; + this.settings.ContractResolver = new NetFieldContract(); } /// @@ -257,7 +267,37 @@ namespace Revitalize.Framework.Utilities } } + /// + /// Converts objects to json form. + /// + /// + /// + public string ToJSONString(object o) + { + return JsonConvert.SerializeObject(o, this.settings); + } + /// + /// Converts from json form to objects. + /// + /// + /// + /// + public T DeserializeFromJSONString(string info) + { + return JsonConvert.DeserializeObject(info,this.settings); + } + + /// + /// Converts from json form to objects. + /// + /// + /// + /// + public object DeserializeFromJSONString(string info, Type T) + { + return JsonConvert.DeserializeObject(info, T, this.settings); + } } } diff --git a/GeneralMods/Revitalize/Revitalize.csproj b/GeneralMods/Revitalize/Revitalize.csproj index a947f7e4..e1fa1111 100644 --- a/GeneralMods/Revitalize/Revitalize.csproj +++ b/GeneralMods/Revitalize/Revitalize.csproj @@ -62,6 +62,7 @@ +