From d31456fdc7cd55576523bc32ff8a7c2c18d66710 Mon Sep 17 00:00:00 2001 From: Zoryn Aaron Date: Tue, 22 Mar 2016 20:36:04 -0400 Subject: okay. things. --- StardewModdingAPI/Inheritance/SGame.cs | 199 +++++++++++---------------------- 1 file changed, 64 insertions(+), 135 deletions(-) (limited to 'StardewModdingAPI/Inheritance/SGame.cs') diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index 53d9df59..8ba76ed3 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -2,24 +2,17 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Xml.Serialization; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using StardewModdingAPI.Events; using StardewValley; -using StardewValley.Characters; using StardewValley.Menus; -using StardewValley.Monsters; -using StardewValley.Quests; -using StardewValley.TerrainFeatures; -using StardewModdingAPI.Events; namespace StardewModdingAPI.Inheritance { public class SGame : Game1 { - public static List ModLocations = new List(); - public static SGameLocation CurrentLocation { get; internal set; } public static Dictionary ModItems { get; private set; } public const Int32 LowestModItemID = 1000; @@ -70,9 +63,12 @@ namespace StardewModdingAPI.Inheritance return WasButtonJustReleased(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex); } + public bool PreviouslyLoadedGame { get; private set; } + private bool FireLoadedGameEvent; + public Buttons[] GetButtonsDown(PlayerIndex index) { - GamePadState state = GamePad.GetState((PlayerIndex)index); + GamePadState state = GamePad.GetState(index); List buttons = new List(); if (state.IsConnected) { @@ -99,7 +95,7 @@ namespace StardewModdingAPI.Inheritance public Buttons[] GetFramePressedButtons(PlayerIndex index) { - GamePadState state = GamePad.GetState((PlayerIndex)index); + GamePadState state = GamePad.GetState(index); List buttons = new List(); if (state.IsConnected) { @@ -126,7 +122,7 @@ namespace StardewModdingAPI.Inheritance public Buttons[] GetFrameReleasedButtons(PlayerIndex index) { - GamePadState state = GamePad.GetState((PlayerIndex)index); + GamePadState state = GamePad.GetState(index); List buttons = new List(); if (state.IsConnected) { @@ -153,7 +149,6 @@ namespace StardewModdingAPI.Inheritance public int PreviousGameLocations { get; private set; } public int PreviousLocationObjects { get; private set; } - public int PreviousItems_ { get; private set; } public Dictionary PreviousItems { get; private set; } public int PreviousCombatLevel { get; private set; } @@ -179,54 +174,18 @@ namespace StardewModdingAPI.Inheritance public RenderTarget2D Screen { get { return typeof (Game1).GetBaseFieldValue(Program.gamePtr, "screen"); } - set { typeof (Game1).SetBaseFieldValue("screen", value); } + set { typeof (Game1).SetBaseFieldValue(this, "screen", value); } } private static SGame instance; - public static SGame Instance { get { return instance; } } + public static SGame Instance => instance; - public Farmer CurrentFarmer { get { return player; } } + public Farmer CurrentFarmer => player; public SGame() { instance = this; FirstUpdate = true; - - /* -#if DEBUG - SaveGame.serializer = new XmlSerializer(typeof (SaveGame), new Type[28] - { - typeof (Tool), - typeof (GameLocation), - typeof (Crow), - typeof (Duggy), - typeof (Bug), - typeof (BigSlime), - typeof (Fireball), - typeof (Ghost), - typeof (Child), - typeof (Pet), - typeof (Dog), - typeof (StardewValley.Characters.Cat), - typeof (Horse), - typeof (GreenSlime), - typeof (LavaCrab), - typeof (RockCrab), - typeof (ShadowGuy), - typeof (SkeletonMage), - typeof (SquidKid), - typeof (Grub), - typeof (Fly), - typeof (DustSpirit), - typeof (Quest), - typeof (MetalHead), - typeof (ShadowGirl), - typeof (Monster), - typeof (TerrainFeature), - typeof (SObject) - }); -#endif - */ } protected override void Initialize() @@ -238,14 +197,14 @@ namespace StardewModdingAPI.Inheritance for (int i = 0; i < 4; ++i) PreviouslyPressedButtons[i] = new Buttons[0]; base.Initialize(); - Events.GameEvents.InvokeInitialize(); + GameEvents.InvokeInitialize(); } protected override void LoadContent() { Log.Verbose("XNA LoadContent"); base.LoadContent(); - Events.GameEvents.InvokeLoadContent(); + GameEvents.InvokeLoadContent(); } protected override void Update(GameTime gameTime) @@ -262,7 +221,7 @@ namespace StardewModdingAPI.Inheritance Console.ReadKey(); } - Events.GameEvents.InvokeUpdateTick(); + GameEvents.InvokeUpdateTick(); if (FirstUpdate) { GameEvents.InvokeFirstUpdateTick(); @@ -270,22 +229,22 @@ namespace StardewModdingAPI.Inheritance } if (CurrentUpdateTick % 2 == 0) - Events.GameEvents.InvokeSecondUpdateTick(); + GameEvents.InvokeSecondUpdateTick(); if (CurrentUpdateTick % 4 == 0) - Events.GameEvents.InvokeFourthUpdateTick(); + GameEvents.InvokeFourthUpdateTick(); if (CurrentUpdateTick % 8 == 0) - Events.GameEvents.InvokeEighthUpdateTick(); + GameEvents.InvokeEighthUpdateTick(); if (CurrentUpdateTick % 15 == 0) - Events.GameEvents.InvokeQuarterSecondTick(); + GameEvents.InvokeQuarterSecondTick(); if (CurrentUpdateTick % 30 == 0) - Events.GameEvents.InvokeHalfSecondTick(); + GameEvents.InvokeHalfSecondTick(); if (CurrentUpdateTick % 60 == 0) - Events.GameEvents.InvokeOneSecondTick(); + GameEvents.InvokeOneSecondTick(); CurrentUpdateTick += 1; if (CurrentUpdateTick >= 60) @@ -301,20 +260,7 @@ namespace StardewModdingAPI.Inheritance protected override void Draw(GameTime gameTime) { base.Draw(gameTime); - Events.GraphicsEvents.InvokeDrawTick(); - - if (false) - { - spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); - - if (CurrentLocation != null) - CurrentLocation.draw(spriteBatch); - - if (player != null && player.position != null) - spriteBatch.DrawString(dialogueFont, player.position.ToString(), new Vector2(0, 180), Color.Orange); - - spriteBatch.End(); - } + GraphicsEvents.InvokeDrawTick(); } public static Int32 RegisterModItem(SObject modItem) @@ -341,44 +287,14 @@ namespace StardewModdingAPI.Inheritance { return ModItems.ElementAt(id).Value.Clone(); } - Log.Error("ModItem Dictionary does not contain index: " + id.ToString()); + Log.Error("ModItem Dictionary does not contain index: " + id); return null; } if (ModItems.ContainsKey(id)) { return ModItems[id].Clone(); } - Log.Error("ModItem Dictionary does not contain ID: " + id.ToString()); - return null; - } - - public static SGameLocation GetLocationFromName(String name) - { - return ModLocations.FirstOrDefault(n => n.name == name); - } - - public static SGameLocation LoadOrCreateSGameLocationFromName(String name) - { - if (GetLocationFromName(name) != null) - return GetLocationFromName(name); - GameLocation gl = locations.FirstOrDefault(x => x.name == name); - if (gl != null) - { - Log.Debug("A custom location was created for the new name: " + name); - SGameLocation s = SGameLocation.ConstructFromBaseClass(gl); - ModLocations.Add(s); - return s; - } - if (currentLocation != null && currentLocation.name == name) - { - gl = currentLocation; - Log.Debug("A custom location was created from the current location for the new name: " + name); - SGameLocation s = SGameLocation.ConstructFromBaseClass(gl); - ModLocations.Add(s); - return s; - } - - Log.Debug("A custom location could not be created for: " + name); + Log.Error("ModItem Dictionary does not contain ID: " + id); return null; } @@ -390,10 +306,10 @@ namespace StardewModdingAPI.Inheritance MStateNow = Mouse.GetState(); foreach (Keys k in FramePressedKeys) - Events.ControlEvents.InvokeKeyPressed(k); + ControlEvents.InvokeKeyPressed(k); foreach (Keys k in FrameReleasedKeys) - Events.ControlEvents.InvokeKeyReleased(k); + ControlEvents.InvokeKeyReleased(k); for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++) { @@ -402,11 +318,11 @@ namespace StardewModdingAPI.Inheritance { if(b == Buttons.LeftTrigger || b == Buttons.RightTrigger) { - Events.ControlEvents.InvokeTriggerPressed(i, b, b == Buttons.LeftTrigger ? GamePad.GetState(i).Triggers.Left : GamePad.GetState(i).Triggers.Right); + ControlEvents.InvokeTriggerPressed(i, b, b == Buttons.LeftTrigger ? GamePad.GetState(i).Triggers.Left : GamePad.GetState(i).Triggers.Right); } else { - Events.ControlEvents.InvokeButtonPressed(i, b); + ControlEvents.InvokeButtonPressed(i, b); } } } @@ -417,11 +333,11 @@ namespace StardewModdingAPI.Inheritance { if (b == Buttons.LeftTrigger || b == Buttons.RightTrigger) { - Events.ControlEvents.InvokeTriggerReleased(i, b, b == Buttons.LeftTrigger ? GamePad.GetState(i).Triggers.Left : GamePad.GetState(i).Triggers.Right); + ControlEvents.InvokeTriggerReleased(i, b, b == Buttons.LeftTrigger ? GamePad.GetState(i).Triggers.Left : GamePad.GetState(i).Triggers.Right); } else { - Events.ControlEvents.InvokeButtonReleased(i, b); + ControlEvents.InvokeButtonReleased(i, b); } } } @@ -429,138 +345,151 @@ namespace StardewModdingAPI.Inheritance if (KStateNow != KStatePrior) { - Events.ControlEvents.InvokeKeyboardChanged(KStatePrior, KStateNow); + ControlEvents.InvokeKeyboardChanged(KStatePrior, KStateNow); KStatePrior = KStateNow; } if (MStateNow != MStatePrior) { - Events.ControlEvents.InvokeMouseChanged(MStatePrior, MStateNow); + ControlEvents.InvokeMouseChanged(MStatePrior, MStateNow); MStatePrior = MStateNow; } if (activeClickableMenu != null && activeClickableMenu != PreviousActiveMenu) { - Events.MenuEvents.InvokeMenuChanged(PreviousActiveMenu, activeClickableMenu); + MenuEvents.InvokeMenuChanged(PreviousActiveMenu, activeClickableMenu); PreviousActiveMenu = activeClickableMenu; } if (locations.GetHash() != PreviousGameLocations) { - Events.LocationEvents.InvokeLocationsChanged(locations); + LocationEvents.InvokeLocationsChanged(locations); PreviousGameLocations = locations.GetHash(); } if (currentLocation != PreviousGameLocation) { - Events.LocationEvents.InvokeCurrentLocationChanged(PreviousGameLocation, currentLocation); + LocationEvents.InvokeCurrentLocationChanged(PreviousGameLocation, currentLocation); PreviousGameLocation = currentLocation; } if (player != null && player != PreviousFarmer) { - Events.PlayerEvents.InvokeFarmerChanged(PreviousFarmer, player); + PlayerEvents.InvokeFarmerChanged(PreviousFarmer, player); PreviousFarmer = player; } if (player != null && player.combatLevel != PreviousCombatLevel) { - Events.PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Combat, player.combatLevel); + PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Combat, player.combatLevel); PreviousCombatLevel = player.combatLevel; } if (player != null && player.farmingLevel != PreviousFarmingLevel) { - Events.PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Farming, player.farmingLevel); + PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Farming, player.farmingLevel); PreviousFarmingLevel = player.farmingLevel; } if (player != null && player.fishingLevel != PreviousFishingLevel) { - Events.PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Fishing, player.fishingLevel); + PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Fishing, player.fishingLevel); PreviousFishingLevel = player.fishingLevel; } if (player != null && player.foragingLevel != PreviousForagingLevel) { - Events.PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Foraging, player.foragingLevel); + PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Foraging, player.foragingLevel); PreviousForagingLevel = player.foragingLevel; } if (player != null && player.miningLevel != PreviousMiningLevel) { - Events.PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Mining, player.miningLevel); + PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Mining, player.miningLevel); PreviousMiningLevel = player.miningLevel; } if (player != null && player.luckLevel != PreviousLuckLevel) { - Events.PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Luck, player.luckLevel); + PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Luck, player.luckLevel); PreviousLuckLevel = player.luckLevel; } List changedItems; if (player != null && HasInventoryChanged(player.items, out changedItems)) { - Events.PlayerEvents.InvokeInventoryChanged(player.items, changedItems); + PlayerEvents.InvokeInventoryChanged(player.items, changedItems); PreviousItems = player.items.Where(n => n != null).ToDictionary(n => n, n => n.Stack); } var objectHash = currentLocation?.objects?.GetHash(); if(objectHash != null && PreviousLocationObjects != objectHash) { - Events.LocationEvents.InvokeOnNewLocationObject(currentLocation.objects); + LocationEvents.InvokeOnNewLocationObject(currentLocation.objects); PreviousLocationObjects = objectHash ?? -1; } if (timeOfDay != PreviousTimeOfDay) { - Events.TimeEvents.InvokeTimeOfDayChanged(PreviousTimeOfDay, timeOfDay); + TimeEvents.InvokeTimeOfDayChanged(PreviousTimeOfDay, timeOfDay); PreviousTimeOfDay = timeOfDay; } if (dayOfMonth != PreviousDayOfMonth) { - Events.TimeEvents.InvokeDayOfMonthChanged(PreviousDayOfMonth, dayOfMonth); + TimeEvents.InvokeDayOfMonthChanged(PreviousDayOfMonth, dayOfMonth); PreviousDayOfMonth = dayOfMonth; } if (currentSeason != PreviousSeasonOfYear) { - Events.TimeEvents.InvokeSeasonOfYearChanged(PreviousSeasonOfYear, currentSeason); + TimeEvents.InvokeSeasonOfYearChanged(PreviousSeasonOfYear, currentSeason); PreviousSeasonOfYear = currentSeason; } if (year != PreviousYearOfGame) { - Events.TimeEvents.InvokeYearOfGameChanged(PreviousYearOfGame, year); + TimeEvents.InvokeYearOfGameChanged(PreviousYearOfGame, year); PreviousYearOfGame = year; } + + //NOTE THAT THIS MUST CHECK BEFORE SETTING IT TO TRUE BECAUSE OF SOME SILLY ISSUES + if (FireLoadedGameEvent) + { + PlayerEvents.InvokeLoadedGame(new EventArgsLoadedGameChanged(hasLoadedGame)); + FireLoadedGameEvent = false; + } + + if (hasLoadedGame != PreviouslyLoadedGame) + { + FireLoadedGameEvent = true; + PreviouslyLoadedGame = hasLoadedGame; + } } private bool HasInventoryChanged(List items, out List changedItems) { changedItems = new List(); - IEnumerable actualItems = items.Where(n => n != null); + IEnumerable actualItems = items.Where(n => n != null)?.ToArray(); foreach (var item in actualItems) { if (PreviousItems != null && PreviousItems.ContainsKey(item)) { if(PreviousItems[item] != item.Stack) { - changedItems.Add(new ItemStackChange() { Item = item, StackChange = item.Stack - PreviousItems[item], ChangeType = ChangeType.StackChange }); + changedItems.Add(new ItemStackChange { Item = item, StackChange = item.Stack - PreviousItems[item], ChangeType = ChangeType.StackChange }); } } else { - changedItems.Add(new ItemStackChange() { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added }); + changedItems.Add(new ItemStackChange { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added }); } } if (PreviousItems != null) { - changedItems.AddRange(PreviousItems.Where(n => !actualItems.Any(i => i == n.Key)).Select(n => - new ItemStackChange() { Item = n.Key, StackChange = -n.Key.Stack, ChangeType = ChangeType.Removed })); + changedItems.AddRange(PreviousItems.Where(n => actualItems.All(i => i != n.Key)).Select(n => + new ItemStackChange { Item = n.Key, StackChange = -n.Key.Stack, ChangeType = ChangeType.Removed })); } return (changedItems.Any()); -- cgit