From c026702f0923c117364a218964dfd12d911f074b Mon Sep 17 00:00:00 2001 From: JoshuaNavarro Date: Fri, 17 May 2019 11:04:16 -0700 Subject: [PATCH] Got lamps working with a furniture factory. --- .../Objects/Furniture/FurnitureFactory.cs | 93 ++++++++++++++++++- .../Framework/Illuminate/FakeLightSource.cs | 32 +++++++ .../Framework/Illuminate/LightManager.cs | 37 ++++++++ .../Objects/Furniture/LampTileComponent.cs | 19 ---- .../Framework/Objects/MultiTiledComponent.cs | 1 - GeneralMods/Revitalize/ModCore.cs | 12 ++- GeneralMods/Revitalize/Revitalize.csproj | 1 + 7 files changed, 166 insertions(+), 29 deletions(-) create mode 100644 GeneralMods/Revitalize/Framework/Illuminate/FakeLightSource.cs diff --git a/GeneralMods/Revitalize/Framework/Factories/Objects/Furniture/FurnitureFactory.cs b/GeneralMods/Revitalize/Framework/Factories/Objects/Furniture/FurnitureFactory.cs index 6ebef3bb..de4410b8 100644 --- a/GeneralMods/Revitalize/Framework/Factories/Objects/Furniture/FurnitureFactory.cs +++ b/GeneralMods/Revitalize/Framework/Factories/Objects/Furniture/FurnitureFactory.cs @@ -11,6 +11,7 @@ using Revitalize.Framework.Graphics.Animations; using Revitalize.Framework.Illuminate; using Revitalize.Framework.Objects; using Revitalize.Framework.Objects.Furniture; +using StardewValley; namespace Revitalize.Framework.Factories.Objects { @@ -18,11 +19,13 @@ namespace Revitalize.Framework.Factories.Objects { public static string ChairFolder = Path.Combine("Objects", "Furniture", "Chairs"); public static string TablesFolder = Path.Combine("Objects", "Furniture", "Tables"); + public static string LampsFolder = Path.Combine("Objects", "Furniture", "Lamps"); public static void LoadFurnitureFiles() { LoadChairFiles(); LoadTableFiles(); + LoadLampFiles(); } private static void LoadChairFiles() @@ -37,11 +40,91 @@ namespace Revitalize.Framework.Factories.Objects DeserializeTableFiles(); } + private static void LoadLampFiles() + { + SerializeLamps(); + DeserializeLamps(); + } + private static void SerializeLamps() { - LampTileComponent lampTop = new LampTileComponent(new BasicItemInformation("Oak Chair", "A basic wooden chair", "Chairs", Color.Brown, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Chairs.OakChair", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Chair").texture, Color.White, 0, true, typeof(Framework.Objects.Furniture.ChairTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Chair"), new Animation(new Rectangle(0, 0, 16, 16))), Color.White, true, new Framework.Utilities.InventoryManager(), new LightManager())); + LampTileComponent lampTop = new LampTileComponent(new BasicItemInformation("Oak Lamp", "A basic wooden light", "Lamps", Color.Brown, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Lamps.OakLamp", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Lamp").texture, Color.White, 0, true, typeof(Framework.Objects.Furniture.LampTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Lamp"), new Animation(new Rectangle(0, 0, 16, 16))), Color.White, true, new Framework.Utilities.InventoryManager(), new LightManager())); + LampTileComponent lampMiddle = new LampTileComponent(new BasicItemInformation("Oak Lamp", "A basic wooden light", "Lamps", Color.Brown, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Lamps.OakLamp", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Lamp").texture, Color.White, 0, true, typeof(Framework.Objects.Furniture.LampTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Lamp"), new Animation(new Rectangle(0, 16, 16, 16))), Color.White, false, new Framework.Utilities.InventoryManager(), new LightManager())); + LampTileComponent lampBottom = new LampTileComponent(new BasicItemInformation("Oak Lamp", "A basic wooden light", "Lamps", Color.Brown, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Lamps.OakLamp", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Lamp").texture, Color.White, 0, true, typeof(Framework.Objects.Furniture.LampTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Lamp"), new Animation(new Rectangle(0, 32, 16, 16))), Color.White, false, new Framework.Utilities.InventoryManager(), new LightManager())); + + lampMiddle.lights.addLight(new Vector2(Game1.tileSize), new LightSource(4, new Vector2(0, 0), 2.5f, Color.Orange.Invert()), lampMiddle); + + LampMultiTiledObject lamp = new LampMultiTiledObject(new BasicItemInformation("Oak Lamp", "A basic wooden light", "Lamps", Color.Brown, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Lamps.OakLamp", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Lamp").texture, Color.White, 0, true, typeof(Framework.Objects.Furniture.LampTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Lamp"), new Animation(new Rectangle(0, 0, 16, 16))), Color.White, true, new Framework.Utilities.InventoryManager(), new LightManager())); + lamp.addComponent(new Vector2(0, -2), lampTop); + lamp.addComponent(new Vector2(0, -1), lampMiddle); + lamp.addComponent(new Vector2(0, 0), lampBottom); + + FactoryInfo lT = new FactoryInfo(lampTop); + FactoryInfo lM = new FactoryInfo(lampMiddle); + FactoryInfo lB = new FactoryInfo(lampBottom); + + FactoryInfo lO = new FactoryInfo(lamp); + + ModCore.Serializer.SerializeContentFile("OakLamp_0_-2", lT, LampsFolder); + ModCore.Serializer.SerializeContentFile("OakLamp_0_-1", lM, LampsFolder); + ModCore.Serializer.SerializeContentFile("OakLamp_0_0", lB, LampsFolder); + ModCore.Serializer.SerializeContentFile("OakLamp", lO, LampsFolder); + + //ModCore.customObjects.Add(lamp.info.id, lamp); } + private static void DeserializeLamps() + { + if (!Directory.Exists(Path.Combine(ModCore.ModHelper.DirectoryPath, "Content", LampsFolder))) Directory.CreateDirectory(Path.Combine(ModCore.ModHelper.DirectoryPath, "Content", LampsFolder)); + string[] files = Directory.GetFiles(Path.Combine(ModCore.ModHelper.DirectoryPath, "Content", LampsFolder)); + + Dictionary objs = new Dictionary(); + + //Deserialize container. + foreach (string file in files) + { + if ((Path.GetFileName(file)).Contains("_") == true) continue; + else + { + objs.Add(Path.GetFileNameWithoutExtension(file), new LampMultiTiledObject(ModCore.Serializer.DeserializeContentFile(file).info)); + } + } + //Deseralize components + foreach (string file in files) + { + if ((Path.GetFileName(file)).Contains("_") == false) continue; + else + { + + string[] splits = Path.GetFileNameWithoutExtension(file).Split('_'); + string name = splits[0]; + Vector2 offset = new Vector2(Convert.ToInt32(splits[1]), Convert.ToInt32(splits[2])); + FactoryInfo info = ModCore.Serializer.DeserializeContentFile(file); + + LampTileComponent lampPiece = new LampTileComponent(info.info); + //Recreate the lights info. + if (lampPiece.lights != null) + { + //ModCore.log("Info for file"+Path.GetFileNameWithoutExtension(file)+" has this many lights: " + info.info.lightManager.fakeLights.Count); + lampPiece.lights.lights.Clear(); + foreach (KeyValuePair light in info.info.lightManager.fakeLights) + { + lampPiece.lights.addLight(new Vector2(Game1.tileSize), new LightSource(light.Value.id, new Vector2(0, 0), light.Value.radius,light.Value.color.Invert()), lampPiece); + } + } + + + objs[name].addComponent(offset,lampPiece ); + } + } + foreach (var v in objs) + { + ModCore.customObjects.Add(v.Value.info.id, v.Value); + } + } + + + private static void SerializeChairs() { @@ -150,7 +233,7 @@ namespace Revitalize.Framework.Factories.Objects //Deserialize container. foreach (string file in files) { - if ((Path.GetFileName(file)).Contains("_")==true) continue; + if ((Path.GetFileName(file)).Contains("_") == true) continue; else { chairObjects.Add(Path.GetFileNameWithoutExtension(file), new ChairMultiTiledObject(ModCore.Serializer.DeserializeContentFile(file).info)); @@ -159,10 +242,10 @@ namespace Revitalize.Framework.Factories.Objects //Deseralize components foreach (string file in files) { - if ((Path.GetFileName(file)).Contains("_")==false) continue; + if ((Path.GetFileName(file)).Contains("_") == false) continue; else { - + string[] splits = Path.GetFileNameWithoutExtension(file).Split('_'); string name = splits[0]; Vector2 offset = new Vector2(Convert.ToInt32(splits[1]), Convert.ToInt32(splits[2])); @@ -178,7 +261,7 @@ namespace Revitalize.Framework.Factories.Objects private static void SerializeTableFiles() { - TableTileComponent upperLeft = new TableTileComponent(new BasicItemInformation("Oak Table", "A simple oak table to hold things.", "Tables", Color.White, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Tables.OakTable", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Table").texture, Color.White, 0, true, typeof(TableTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Table"),new Animation(new Rectangle(0,0,16,16),-1)),Color.White,true, new Utilities.InventoryManager(), new LightManager()), new Framework.Objects.InformationFiles.Furniture.TableInformation(true)); + TableTileComponent upperLeft = new TableTileComponent(new BasicItemInformation("Oak Table", "A simple oak table to hold things.", "Tables", Color.White, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Tables.OakTable", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Table").texture, Color.White, 0, true, typeof(TableTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Table"), new Animation(new Rectangle(0, 0, 16, 16), -1)), Color.White, true, new Utilities.InventoryManager(), new LightManager()), new Framework.Objects.InformationFiles.Furniture.TableInformation(true)); TableTileComponent upperRight = new TableTileComponent(new BasicItemInformation("Oak Table", "A simple oak table to hold things.", "Tables", Color.White, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Tables.OakTable", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Table").texture, Color.White, 0, true, typeof(TableTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Table"), new Animation(new Rectangle(16, 0, 16, 16), -1)), Color.White, true, new Utilities.InventoryManager(), new LightManager()), new Framework.Objects.InformationFiles.Furniture.TableInformation(true)); TableTileComponent centerLeft = new TableTileComponent(new BasicItemInformation("Oak Table", "A simple oak table to hold things.", "Tables", Color.White, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Tables.OakTable", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Table").texture, Color.White, 0, true, typeof(TableTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Table"), new Animation(new Rectangle(0, 16, 16, 16), -1)), Color.White, false, new Utilities.InventoryManager(), new LightManager()), new Framework.Objects.InformationFiles.Furniture.TableInformation(true)); TableTileComponent centerRight = new TableTileComponent(new BasicItemInformation("Oak Table", "A simple oak table to hold things.", "Tables", Color.White, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.Revitalize.Furniture.Tables.OakTable", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Table").texture, Color.White, 0, true, typeof(TableTileComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Table"), new Animation(new Rectangle(16, 16, 16, 16), -1)), Color.White, false, new Utilities.InventoryManager(), new LightManager()), new Framework.Objects.InformationFiles.Furniture.TableInformation(true)); diff --git a/GeneralMods/Revitalize/Framework/Illuminate/FakeLightSource.cs b/GeneralMods/Revitalize/Framework/Illuminate/FakeLightSource.cs new file mode 100644 index 00000000..dcca4394 --- /dev/null +++ b/GeneralMods/Revitalize/Framework/Illuminate/FakeLightSource.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Xna.Framework; + +namespace Revitalize.Framework.Illuminate +{ + public class FakeLightSource + { + public int id; + public Vector2 positionOffset; + public Color color; + public float radius; + + + public FakeLightSource() + { + + } + + public FakeLightSource(int ID, Vector2 Position, Color Color, float Raidus) + { + this.id = ID; + this.positionOffset = Position; + this.color = Color; + this.radius = Raidus; + } + + } +} diff --git a/GeneralMods/Revitalize/Framework/Illuminate/LightManager.cs b/GeneralMods/Revitalize/Framework/Illuminate/LightManager.cs index ff9f82fd..7844690f 100644 --- a/GeneralMods/Revitalize/Framework/Illuminate/LightManager.cs +++ b/GeneralMods/Revitalize/Framework/Illuminate/LightManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; using StardewValley; namespace Revitalize.Framework.Illuminate @@ -8,6 +9,7 @@ namespace Revitalize.Framework.Illuminate public class LightManager { public Dictionary lights; + public Dictionary fakeLights; public bool lightsOn; public const int lightBigNumber= 1000000; @@ -15,12 +17,16 @@ namespace Revitalize.Framework.Illuminate public LightManager() { this.lights = new Dictionary(); + this.fakeLights = new Dictionary(); this.lightsOn = false; } /// Add a light to the list of tracked lights. public bool addLight(Vector2 IdKey, LightSource light, StardewValley.Object gameObject) { + if (gameObject.TileLocation.X < 0) gameObject.TileLocation = new Vector2(gameObject.TileLocation.X * -1, gameObject.TileLocation.Y); + if (gameObject.TileLocation.Y < 0) gameObject.TileLocation = new Vector2(gameObject.TileLocation.X, gameObject.TileLocation.Y*-1); + Vector2 initialPosition = gameObject.TileLocation * Game1.tileSize; initialPosition += IdKey; @@ -29,6 +35,8 @@ namespace Revitalize.Framework.Illuminate light.position.Value = initialPosition; this.lights.Add(IdKey, light); + if (this.fakeLights.ContainsKey(IdKey)) return true; + this.fakeLights.Add(IdKey, new FakeLightSource(light.Identifier, light.position.Value, light.color.Value.Invert(), light.radius.Value)); return true; } @@ -62,6 +70,12 @@ namespace Revitalize.Framework.Illuminate throw new Exception("Locational lights is null!"); Game1.showRedMessage("TURN ON!"); + + if (light.lightTexture == null) + { + light.lightTexture = this.loadTextureFromConstantValue(light.Identifier); + } + Game1.currentLightSources.Add(light); location.sharedLights.Add((int)IdKey.X*lightBigNumber+(int)IdKey.Y,light); this.repositionLight(light, IdKey, gameObject); @@ -117,5 +131,28 @@ namespace Revitalize.Framework.Illuminate { this.turnOffLights(loc); } + + private Texture2D loadTextureFromConstantValue(int value) + { + switch (value) + { + case 1: + return Game1.lantern; + break; + case 2: + return Game1.windowLight; + break; + case 4: + return Game1.sconceLight; + break; + case 5: + return Game1.cauldronLight; + break; + case 6: + return Game1.indoorWindowLight; + break; + } + return Game1.sconceLight; + } } } diff --git a/GeneralMods/Revitalize/Framework/Objects/Furniture/LampTileComponent.cs b/GeneralMods/Revitalize/Framework/Objects/Furniture/LampTileComponent.cs index 968e13f0..6dfd3d8a 100644 --- a/GeneralMods/Revitalize/Framework/Objects/Furniture/LampTileComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/Furniture/LampTileComponent.cs @@ -47,25 +47,6 @@ namespace Revitalize.Framework.Objects.Furniture } - /// - /// Forcefully clears the held object without much fuss. - /// - public void clearHeldObject() - { - if (this.heldObject.Value != null) - { - if (Game1.player.isInventoryFull() == false) - { - Game1.player.addItemToInventoryBool(this.heldObject.Value, false); - this.heldObject.Value = null; - return; - } - else - { - Game1.createItemDebris(this.heldObject.Value, Vector2.Zero, 0); - } - } - } public override bool performObjectDropInAction(Item dropInItem, bool probe, Farmer who) { diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs index fe414e91..bc66ff15 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs @@ -52,7 +52,6 @@ namespace Revitalize.Framework.Objects { if (this.location == null) this.location = Game1.player.currentLocation; - if(this.info.lightManager!=null)this.info.lightManager.toggleLights(this.location, this); //ModCore.playerInfo.sittingInfo.sit(this, Vector2.Zero); diff --git a/GeneralMods/Revitalize/ModCore.cs b/GeneralMods/Revitalize/ModCore.cs index 8e619cb3..c07594f8 100644 --- a/GeneralMods/Revitalize/ModCore.cs +++ b/GeneralMods/Revitalize/ModCore.cs @@ -27,7 +27,9 @@ namespace Revitalize // -load content MUST be enabled for the table to be placed?????? WTF // TODO: // - // + // -Get way to read in textures at runtime without having to load them in... + // -Make this mod able to load content packs for easier future modding + // -Make deserialize/serialize look through sub directories instead of just one directory. // // -Multiple Lights On Object // -Illumination Colors @@ -139,13 +141,14 @@ namespace Revitalize Framework.Graphics.TextureManager.TextureManagers.Add("Furniture", new TextureManager()); TextureManager.addTexture("Furniture","Oak Chair", new Texture2DExtended(this.Helper, this.ModManifest, Path.Combine("Content","Graphics","Furniture", "Chairs", "OakChair.png"))); TextureManager.addTexture("Furniture", "Oak Table", new Texture2DExtended(this.Helper, this.ModManifest, Path.Combine("Content", "Graphics", "Furniture", "Tables", "OakTable.png"))); + TextureManager.addTexture("Furniture", "Oak Lamp", new Texture2DExtended(this.Helper, this.ModManifest, Path.Combine("Content", "Graphics", "Furniture", "Lamps", "OakLamp.png"))); customObjects = new Dictionary(); ObjectGroups = new Dictionary(); Serializer = new Serializer(); ObjectsToDraw = new Dictionary(); - this.loadContent(); + } @@ -165,7 +168,7 @@ namespace Revitalize MultiTiledComponent obj3 = new MultiTiledComponent(new BasicItemInformation("CoreObjectTest3", "NoFun", "Omegasis.Revitalize.MultiTiledComponent", Color.White, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.TEST1", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Chair").texture, Color.White, 0, true, typeof(MultiTiledComponent), null, new AnimationManager(TextureManager.TextureManagers["Furniture"].getTexture("Oak Chair"), new Animation(new Rectangle(0, 32, 16, 16))), Color.Red, false, null, null)); - obj3.info.lightManager.addLight(new Vector2(Game1.tileSize), new LightSource(4, new Vector2(0, 0), 2.5f, Color.Orange.Invert()), obj); + obj3.info.lightManager.addLight(new Vector2(Game1.tileSize), new LightSource(4, new Vector2(0, 0), 2.5f, Color.Orange.Invert()), obj3); MultiTiledObject bigObject = new MultiTiledObject(new BasicItemInformation("MultiTest", "A really big object", "Omegasis.Revitalize.MultiTiledObject", Color.Blue, -300, 0, false, 100, Vector2.Zero, true, true, "Omegasis.BigTiledTest", "2048/0/-300/Crafting -9/Play '2048 by Platonymous' at home!/true/true/0/2048", TextureManager.TextureManagers["Furniture"].getTexture("Oak Chair").texture, Color.White, 0, true, typeof(MultiTiledObject), null, new AnimationManager(), Color.White, false, null, null)); bigObject.addComponent(new Vector2(0, 0), obj); @@ -219,7 +222,7 @@ namespace Revitalize private void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e) { - + this.loadContent(); Serializer.afterLoad(); @@ -232,6 +235,7 @@ namespace Revitalize //Game1.player.addItemToInventory(GetObjectFromPool("Omegasis.Revitalize.Furniture.Chairs.OakChair")); //Game1.player.addItemToInventory(GetObjectFromPool("Omegasis.Revitalize.Furniture.Rugs.RugTest")); Game1.player.addItemToInventory(GetObjectFromPool("Omegasis.Revitalize.Furniture.Tables.OakTable")); + Game1.player.addItemToInventory(GetObjectFromPool("Omegasis.Revitalize.Furniture.Lamps.OakLamp")); /* StardewValley.Tools.Axe axe = new StardewValley.Tools.Axe(); Serializer.Serialize(Path.Combine(this.Helper.DirectoryPath, "AXE.json"), axe); diff --git a/GeneralMods/Revitalize/Revitalize.csproj b/GeneralMods/Revitalize/Revitalize.csproj index eeeb9e06..a3bf9bb1 100644 --- a/GeneralMods/Revitalize/Revitalize.csproj +++ b/GeneralMods/Revitalize/Revitalize.csproj @@ -63,6 +63,7 @@ +