SMAPI/StardewModdingAPI/Inheritance/SGame.cs

523 lines
23 KiB
C#
Raw Normal View History

2016-03-05 03:10:39 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
2016-03-23 08:36:04 +08:00
using StardewModdingAPI.Events;
2016-03-05 03:10:39 +08:00
using StardewValley;
using StardewValley.Menus;
namespace StardewModdingAPI.Inheritance
{
public class SGame : Game1
{
public static Dictionary<Int32, SObject> ModItems { get; private set; }
public const Int32 LowestModItemID = 1000;
public static FieldInfo[] StaticFields { get { return GetStaticFields(); } }
public static FieldInfo[] GetStaticFields()
{
return typeof(Game1).GetFields();
}
public KeyboardState KStateNow { get; private set; }
public KeyboardState KStatePrior { get; private set; }
public MouseState MStateNow { get; private set; }
public MouseState MStatePrior { get; private set; }
2016-03-23 13:11:13 +08:00
public Keys[] CurrentlyPressedKeys => KStateNow.GetPressedKeys();
public Keys[] PreviouslyPressedKeys => KStatePrior.GetPressedKeys();
2016-03-05 03:10:39 +08:00
public Keys[] FramePressedKeys
{
2016-03-23 13:11:13 +08:00
get { return CurrentlyPressedKeys.Except(PreviouslyPressedKeys).ToArray(); }
2016-03-05 03:10:39 +08:00
}
2016-03-07 03:46:47 +08:00
public Keys[] FrameReleasedKeys
{
2016-03-23 13:11:13 +08:00
get { return PreviouslyPressedKeys.Except(CurrentlyPressedKeys).ToArray(); }
2016-03-07 03:46:47 +08:00
}
2016-03-08 01:25:51 +08:00
public Buttons[][] PreviouslyPressedButtons;
private bool WasButtonJustPressed(Buttons button, ButtonState buttonState, PlayerIndex stateIndex)
{
return buttonState == ButtonState.Pressed && !PreviouslyPressedButtons[(int)stateIndex].Contains(button);
}
2016-03-08 01:25:51 +08:00
private bool WasButtonJustReleased(Buttons button, ButtonState buttonState, PlayerIndex stateIndex)
{
return buttonState == ButtonState.Released && PreviouslyPressedButtons[(int)stateIndex].Contains(button);
}
private bool WasButtonJustPressed(Buttons button, float value, PlayerIndex stateIndex)
{
return WasButtonJustPressed(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex);
}
private bool WasButtonJustReleased(Buttons button, float value, PlayerIndex stateIndex)
{
return WasButtonJustReleased(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex);
}
2016-03-23 08:36:04 +08:00
public bool PreviouslyLoadedGame { get; private set; }
private bool FireLoadedGameEvent;
2016-03-08 01:25:51 +08:00
public Buttons[] GetButtonsDown(PlayerIndex index)
{
2016-03-23 08:36:04 +08:00
GamePadState state = GamePad.GetState(index);
2016-03-08 01:25:51 +08:00
List<Buttons> buttons = new List<Buttons>();
if (state.IsConnected)
{
if (state.Buttons.A == ButtonState.Pressed) buttons.Add(Buttons.A);
if (state.Buttons.B == ButtonState.Pressed) buttons.Add(Buttons.B);
if (state.Buttons.Back == ButtonState.Pressed) buttons.Add(Buttons.Back);
if (state.Buttons.BigButton == ButtonState.Pressed) buttons.Add(Buttons.BigButton);
if (state.Buttons.LeftShoulder == ButtonState.Pressed) buttons.Add(Buttons.LeftShoulder);
if (state.Buttons.LeftStick == ButtonState.Pressed) buttons.Add(Buttons.LeftStick);
if (state.Buttons.RightShoulder == ButtonState.Pressed) buttons.Add(Buttons.RightShoulder);
if (state.Buttons.RightStick == ButtonState.Pressed) buttons.Add(Buttons.RightStick);
if (state.Buttons.Start == ButtonState.Pressed) buttons.Add(Buttons.Start);
if (state.Buttons.X == ButtonState.Pressed) buttons.Add(Buttons.X);
if (state.Buttons.Y == ButtonState.Pressed) buttons.Add(Buttons.Y);
if (state.DPad.Up == ButtonState.Pressed) buttons.Add(Buttons.DPadUp);
if (state.DPad.Down == ButtonState.Pressed) buttons.Add(Buttons.DPadDown);
if (state.DPad.Left == ButtonState.Pressed) buttons.Add(Buttons.DPadLeft);
if (state.DPad.Right == ButtonState.Pressed) buttons.Add(Buttons.DPadRight);
if (state.Triggers.Left > 0.2f) buttons.Add(Buttons.LeftTrigger);
if (state.Triggers.Right > 0.2f) buttons.Add(Buttons.RightTrigger);
}
return buttons.ToArray();
}
public Buttons[] GetFramePressedButtons(PlayerIndex index)
{
2016-03-23 08:36:04 +08:00
GamePadState state = GamePad.GetState(index);
List<Buttons> buttons = new List<Buttons>();
if (state.IsConnected)
{
if (WasButtonJustPressed(Buttons.A, state.Buttons.A, index)) buttons.Add(Buttons.A);
if (WasButtonJustPressed(Buttons.B, state.Buttons.B, index)) buttons.Add(Buttons.B);
if (WasButtonJustPressed(Buttons.Back, state.Buttons.Back, index)) buttons.Add(Buttons.Back);
if (WasButtonJustPressed(Buttons.BigButton, state.Buttons.BigButton, index)) buttons.Add(Buttons.BigButton);
if (WasButtonJustPressed(Buttons.LeftShoulder, state.Buttons.LeftShoulder, index)) buttons.Add(Buttons.LeftShoulder);
if (WasButtonJustPressed(Buttons.LeftStick, state.Buttons.LeftStick, index)) buttons.Add(Buttons.LeftStick);
if (WasButtonJustPressed(Buttons.RightShoulder, state.Buttons.RightShoulder, index)) buttons.Add(Buttons.RightShoulder);
if (WasButtonJustPressed(Buttons.RightStick, state.Buttons.RightStick, index)) buttons.Add(Buttons.RightStick);
if (WasButtonJustPressed(Buttons.Start, state.Buttons.Start, index)) buttons.Add(Buttons.Start);
if (WasButtonJustPressed(Buttons.X, state.Buttons.X, index)) buttons.Add(Buttons.X);
if (WasButtonJustPressed(Buttons.Y, state.Buttons.Y, index)) buttons.Add(Buttons.Y);
if (WasButtonJustPressed(Buttons.DPadUp, state.DPad.Up, index)) buttons.Add(Buttons.DPadUp);
if (WasButtonJustPressed(Buttons.DPadDown, state.DPad.Down, index)) buttons.Add(Buttons.DPadDown);
if (WasButtonJustPressed(Buttons.DPadLeft, state.DPad.Left, index)) buttons.Add(Buttons.DPadLeft);
if (WasButtonJustPressed(Buttons.DPadRight, state.DPad.Right, index)) buttons.Add(Buttons.DPadRight);
2016-03-08 01:25:51 +08:00
if (WasButtonJustPressed(Buttons.LeftTrigger, state.Triggers.Left, index)) buttons.Add(Buttons.LeftTrigger);
if (WasButtonJustPressed(Buttons.RightTrigger, state.Triggers.Right, index)) buttons.Add(Buttons.RightTrigger);
}
return buttons.ToArray();
}
2016-03-08 01:25:51 +08:00
public Buttons[] GetFrameReleasedButtons(PlayerIndex index)
{
2016-03-23 08:36:04 +08:00
GamePadState state = GamePad.GetState(index);
2016-03-08 01:25:51 +08:00
List<Buttons> buttons = new List<Buttons>();
if (state.IsConnected)
{
if (WasButtonJustReleased(Buttons.A, state.Buttons.A, index)) buttons.Add(Buttons.A);
if (WasButtonJustReleased(Buttons.B, state.Buttons.B, index)) buttons.Add(Buttons.B);
if (WasButtonJustReleased(Buttons.Back, state.Buttons.Back, index)) buttons.Add(Buttons.Back);
if (WasButtonJustReleased(Buttons.BigButton, state.Buttons.BigButton, index)) buttons.Add(Buttons.BigButton);
if (WasButtonJustReleased(Buttons.LeftShoulder, state.Buttons.LeftShoulder, index)) buttons.Add(Buttons.LeftShoulder);
if (WasButtonJustReleased(Buttons.LeftStick, state.Buttons.LeftStick, index)) buttons.Add(Buttons.LeftStick);
if (WasButtonJustReleased(Buttons.RightShoulder, state.Buttons.RightShoulder, index)) buttons.Add(Buttons.RightShoulder);
if (WasButtonJustReleased(Buttons.RightStick, state.Buttons.RightStick, index)) buttons.Add(Buttons.RightStick);
if (WasButtonJustReleased(Buttons.Start, state.Buttons.Start, index)) buttons.Add(Buttons.Start);
if (WasButtonJustReleased(Buttons.X, state.Buttons.X, index)) buttons.Add(Buttons.X);
if (WasButtonJustReleased(Buttons.Y, state.Buttons.Y, index)) buttons.Add(Buttons.Y);
if (WasButtonJustReleased(Buttons.DPadUp, state.DPad.Up, index)) buttons.Add(Buttons.DPadUp);
if (WasButtonJustReleased(Buttons.DPadDown, state.DPad.Down, index)) buttons.Add(Buttons.DPadDown);
if (WasButtonJustReleased(Buttons.DPadLeft, state.DPad.Left, index)) buttons.Add(Buttons.DPadLeft);
if (WasButtonJustReleased(Buttons.DPadRight, state.DPad.Right, index)) buttons.Add(Buttons.DPadRight);
if (WasButtonJustReleased(Buttons.LeftTrigger, state.Triggers.Left, index)) buttons.Add(Buttons.LeftTrigger);
if (WasButtonJustReleased(Buttons.RightTrigger, state.Triggers.Right, index)) buttons.Add(Buttons.RightTrigger);
}
return buttons.ToArray();
}
2016-03-05 03:10:39 +08:00
public int PreviousGameLocations { get; private set; }
public int PreviousLocationObjects { get; private set; }
public Dictionary<Item, int> PreviousItems { get; private set; }
2016-03-05 03:10:39 +08:00
public int PreviousCombatLevel { get; private set; }
public int PreviousFarmingLevel { get; private set; }
public int PreviousFishingLevel { get; private set; }
public int PreviousForagingLevel { get; private set; }
public int PreviousMiningLevel { get; private set; }
public int PreviousLuckLevel { get; private set; }
2016-03-05 03:10:39 +08:00
public GameLocation PreviousGameLocation { get; private set; }
public IClickableMenu PreviousActiveMenu { get; private set; }
public Int32 PreviousTimeOfDay { get; private set; }
public Int32 PreviousDayOfMonth { get; private set; }
public String PreviousSeasonOfYear { get; private set; }
public Int32 PreviousYearOfGame { get; private set; }
public Farmer PreviousFarmer { get; private set; }
public Int32 CurrentUpdateTick { get; private set; }
2016-03-21 13:57:42 +08:00
public bool FirstUpdate { get; private set; }
public RenderTarget2D Screen
{
get { return typeof (Game1).GetBaseFieldValue<RenderTarget2D>(Program.gamePtr, "screen"); }
2016-03-23 08:36:04 +08:00
set { typeof (Game1).SetBaseFieldValue<RenderTarget2D>(this, "screen", value); }
2016-03-21 13:57:42 +08:00
}
private static SGame instance;
2016-03-23 08:36:04 +08:00
public static SGame Instance => instance;
2016-03-23 13:11:13 +08:00
public static float FramesPerSecond { get; private set; }
public static bool Debug { get; private set; }
2016-03-23 08:36:04 +08:00
public Farmer CurrentFarmer => player;
2016-03-05 03:10:39 +08:00
public SGame()
{
instance = this;
2016-03-21 13:57:42 +08:00
FirstUpdate = true;
2016-03-05 03:10:39 +08:00
}
protected override void Initialize()
{
Log.Verbose("XNA Initialize");
2016-03-05 03:10:39 +08:00
ModItems = new Dictionary<Int32, SObject>();
2016-03-08 01:25:51 +08:00
PreviouslyPressedButtons = new Buttons[4][];
for (int i = 0; i < 4; ++i) PreviouslyPressedButtons[i] = new Buttons[0];
2016-03-05 03:10:39 +08:00
base.Initialize();
2016-03-23 08:36:04 +08:00
GameEvents.InvokeInitialize();
2016-03-05 03:10:39 +08:00
}
protected override void LoadContent()
{
Log.Verbose("XNA LoadContent");
2016-03-05 03:10:39 +08:00
base.LoadContent();
2016-03-23 08:36:04 +08:00
GameEvents.InvokeLoadContent();
2016-03-05 03:10:39 +08:00
}
protected override void Update(GameTime gameTime)
{
UpdateEventCalls();
2016-03-23 13:11:13 +08:00
if (FramePressedKeys.Contains(Keys.F3))
{
Debug = !Debug;
}
2016-03-05 03:10:39 +08:00
try
{
2016-03-05 03:10:39 +08:00
base.Update(gameTime);
}
catch (Exception ex)
{
Log.Error("An error occured in the base update loop: " + ex);
2016-03-05 03:10:39 +08:00
Console.ReadKey();
}
2016-03-23 08:36:04 +08:00
GameEvents.InvokeUpdateTick();
2016-03-21 13:57:42 +08:00
if (FirstUpdate)
{
GameEvents.InvokeFirstUpdateTick();
FirstUpdate = false;
}
2016-03-05 03:10:39 +08:00
if (CurrentUpdateTick % 2 == 0)
2016-03-23 08:36:04 +08:00
GameEvents.InvokeSecondUpdateTick();
if (CurrentUpdateTick % 4 == 0)
2016-03-23 08:36:04 +08:00
GameEvents.InvokeFourthUpdateTick();
if (CurrentUpdateTick % 8 == 0)
2016-03-23 08:36:04 +08:00
GameEvents.InvokeEighthUpdateTick();
if (CurrentUpdateTick % 15 == 0)
2016-03-23 08:36:04 +08:00
GameEvents.InvokeQuarterSecondTick();
if (CurrentUpdateTick % 30 == 0)
2016-03-23 08:36:04 +08:00
GameEvents.InvokeHalfSecondTick();
if (CurrentUpdateTick % 60 == 0)
2016-03-23 08:36:04 +08:00
GameEvents.InvokeOneSecondTick();
CurrentUpdateTick += 1;
if (CurrentUpdateTick >= 60)
CurrentUpdateTick = 0;
2016-03-23 13:11:13 +08:00
if (KStatePrior != KStateNow)
KStatePrior = KStateNow;
for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++)
2016-03-08 01:25:51 +08:00
{
PreviouslyPressedButtons[(int)i] = GetButtonsDown(i);
}
2016-03-05 03:10:39 +08:00
}
protected override void Draw(GameTime gameTime)
{
2016-03-23 13:11:13 +08:00
FramesPerSecond = 1 / (float)gameTime.ElapsedGameTime.TotalSeconds;
try
{
base.Draw(gameTime);
}
catch (Exception ex)
{
Log.Error("An error occured in the base draw loop: " + ex);
Console.ReadKey();
}
2016-03-23 08:36:04 +08:00
GraphicsEvents.InvokeDrawTick();
2016-03-23 13:11:13 +08:00
if (Debug)
{
spriteBatch.Begin();
spriteBatch.DrawString(dialogueFont, "FPS: " + FramesPerSecond, Vector2.Zero, Color.CornflowerBlue);
spriteBatch.End();
}
2016-03-05 03:10:39 +08:00
}
public static Int32 RegisterModItem(SObject modItem)
{
if (modItem.HasBeenRegistered)
{
Log.Error("The item {0} has already been registered with ID {1}", modItem.Name, modItem.RegisteredId);
2016-03-05 03:10:39 +08:00
return modItem.RegisteredId;
}
Int32 newId = LowestModItemID;
if (ModItems.Count > 0)
newId = Math.Max(LowestModItemID, ModItems.OrderBy(x => x.Key).First().Key + 1);
ModItems.Add(newId, modItem);
modItem.HasBeenRegistered = true;
modItem.RegisteredId = newId;
return newId;
}
public static SObject PullModItemFromDict(Int32 id, bool isIndex)
{
if (isIndex)
{
2016-03-05 03:10:39 +08:00
if (ModItems.ElementAtOrDefault(id).Value != null)
{
return ModItems.ElementAt(id).Value.Clone();
}
2016-03-23 08:36:04 +08:00
Log.Error("ModItem Dictionary does not contain index: " + id);
2016-03-05 03:10:39 +08:00
return null;
}
if (ModItems.ContainsKey(id))
{
return ModItems[id].Clone();
}
2016-03-23 08:36:04 +08:00
Log.Error("ModItem Dictionary does not contain ID: " + id);
2016-03-05 03:10:39 +08:00
return null;
}
2016-03-05 03:10:39 +08:00
public void UpdateEventCalls()
{
KStateNow = Keyboard.GetState();
2016-03-07 03:46:47 +08:00
2016-03-05 03:10:39 +08:00
MStateNow = Mouse.GetState();
foreach (Keys k in FramePressedKeys)
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeKeyPressed(k);
2016-03-07 03:46:47 +08:00
foreach (Keys k in FrameReleasedKeys)
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeKeyReleased(k);
2016-03-07 03:46:47 +08:00
for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++)
{
2016-03-08 01:25:51 +08:00
Buttons[] buttons = GetFramePressedButtons(i);
foreach (Buttons b in buttons)
{
2016-03-08 01:25:51 +08:00
if(b == Buttons.LeftTrigger || b == Buttons.RightTrigger)
{
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeTriggerPressed(i, b, b == Buttons.LeftTrigger ? GamePad.GetState(i).Triggers.Left : GamePad.GetState(i).Triggers.Right);
2016-03-08 01:25:51 +08:00
}
else
{
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeButtonPressed(i, b);
2016-03-08 01:25:51 +08:00
}
}
}
for (PlayerIndex i = PlayerIndex.One; i <= PlayerIndex.Four; i++)
{
2016-03-08 01:25:51 +08:00
foreach (Buttons b in GetFrameReleasedButtons(i))
{
2016-03-08 01:25:51 +08:00
if (b == Buttons.LeftTrigger || b == Buttons.RightTrigger)
{
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeTriggerReleased(i, b, b == Buttons.LeftTrigger ? GamePad.GetState(i).Triggers.Left : GamePad.GetState(i).Triggers.Right);
2016-03-08 01:25:51 +08:00
}
else
{
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeButtonReleased(i, b);
2016-03-08 01:25:51 +08:00
}
}
}
2016-03-05 03:10:39 +08:00
if (KStateNow != KStatePrior)
{
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeKeyboardChanged(KStatePrior, KStateNow);
2016-03-05 03:10:39 +08:00
}
if (MStateNow != MStatePrior)
{
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeMouseChanged(MStatePrior, MStateNow);
2016-03-05 03:10:39 +08:00
MStatePrior = MStateNow;
}
if (activeClickableMenu != null && activeClickableMenu != PreviousActiveMenu)
{
2016-03-23 08:36:04 +08:00
MenuEvents.InvokeMenuChanged(PreviousActiveMenu, activeClickableMenu);
2016-03-05 03:10:39 +08:00
PreviousActiveMenu = activeClickableMenu;
}
if (locations.GetHash() != PreviousGameLocations)
{
2016-03-23 08:36:04 +08:00
LocationEvents.InvokeLocationsChanged(locations);
2016-03-05 03:10:39 +08:00
PreviousGameLocations = locations.GetHash();
}
if (currentLocation != PreviousGameLocation)
{
2016-03-23 08:36:04 +08:00
LocationEvents.InvokeCurrentLocationChanged(PreviousGameLocation, currentLocation);
2016-03-05 03:10:39 +08:00
PreviousGameLocation = currentLocation;
}
if (player != null && player != PreviousFarmer)
{
2016-03-23 08:36:04 +08:00
PlayerEvents.InvokeFarmerChanged(PreviousFarmer, player);
2016-03-05 03:10:39 +08:00
PreviousFarmer = player;
}
if (player != null && player.combatLevel != PreviousCombatLevel)
{
2016-03-23 08:36:04 +08:00
PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Combat, player.combatLevel);
PreviousCombatLevel = player.combatLevel;
}
if (player != null && player.farmingLevel != PreviousFarmingLevel)
{
2016-03-23 08:36:04 +08:00
PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Farming, player.farmingLevel);
PreviousFarmingLevel = player.farmingLevel;
}
if (player != null && player.fishingLevel != PreviousFishingLevel)
{
2016-03-23 08:36:04 +08:00
PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Fishing, player.fishingLevel);
PreviousFishingLevel = player.fishingLevel;
}
if (player != null && player.foragingLevel != PreviousForagingLevel)
{
2016-03-23 08:36:04 +08:00
PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Foraging, player.foragingLevel);
PreviousForagingLevel = player.foragingLevel;
}
if (player != null && player.miningLevel != PreviousMiningLevel)
{
2016-03-23 08:36:04 +08:00
PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Mining, player.miningLevel);
PreviousMiningLevel = player.miningLevel;
}
if (player != null && player.luckLevel != PreviousLuckLevel)
{
2016-03-23 08:36:04 +08:00
PlayerEvents.InvokeLeveledUp(EventArgsLevelUp.LevelType.Luck, player.luckLevel);
PreviousLuckLevel = player.luckLevel;
}
List<ItemStackChange> changedItems;
if (player != null && HasInventoryChanged(player.items, out changedItems))
2016-03-05 03:10:39 +08:00
{
2016-03-23 08:36:04 +08:00
PlayerEvents.InvokeInventoryChanged(player.items, changedItems);
PreviousItems = player.items.Where(n => n != null).ToDictionary(n => n, n => n.Stack);
}
2016-03-05 03:10:39 +08:00
var objectHash = currentLocation?.objects?.GetHash();
if(objectHash != null && PreviousLocationObjects != objectHash)
{
2016-03-23 08:36:04 +08:00
LocationEvents.InvokeOnNewLocationObject(currentLocation.objects);
PreviousLocationObjects = objectHash ?? -1;
2016-03-05 03:10:39 +08:00
}
if (timeOfDay != PreviousTimeOfDay)
{
2016-03-23 08:36:04 +08:00
TimeEvents.InvokeTimeOfDayChanged(PreviousTimeOfDay, timeOfDay);
2016-03-05 03:10:39 +08:00
PreviousTimeOfDay = timeOfDay;
}
if (dayOfMonth != PreviousDayOfMonth)
{
2016-03-23 08:36:04 +08:00
TimeEvents.InvokeDayOfMonthChanged(PreviousDayOfMonth, dayOfMonth);
2016-03-05 03:10:39 +08:00
PreviousDayOfMonth = dayOfMonth;
}
if (currentSeason != PreviousSeasonOfYear)
{
2016-03-23 08:36:04 +08:00
TimeEvents.InvokeSeasonOfYearChanged(PreviousSeasonOfYear, currentSeason);
2016-03-05 03:10:39 +08:00
PreviousSeasonOfYear = currentSeason;
}
if (year != PreviousYearOfGame)
{
2016-03-23 08:36:04 +08:00
TimeEvents.InvokeYearOfGameChanged(PreviousYearOfGame, year);
2016-03-05 03:10:39 +08:00
PreviousYearOfGame = year;
}
2016-03-23 08:36:04 +08:00
//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;
}
2016-03-05 03:10:39 +08:00
}
private bool HasInventoryChanged(List<Item> items, out List<ItemStackChange> changedItems)
{
changedItems = new List<ItemStackChange>();
2016-03-23 08:36:04 +08:00
IEnumerable<Item> actualItems = items.Where(n => n != null)?.ToArray();
foreach (var item in actualItems)
{
if (PreviousItems != null && PreviousItems.ContainsKey(item))
{
if(PreviousItems[item] != item.Stack)
{
2016-03-23 08:36:04 +08:00
changedItems.Add(new ItemStackChange { Item = item, StackChange = item.Stack - PreviousItems[item], ChangeType = ChangeType.StackChange });
}
}
else
{
2016-03-23 08:36:04 +08:00
changedItems.Add(new ItemStackChange { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added });
}
}
if (PreviousItems != null)
{
2016-03-23 08:36:04 +08:00
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());
}
2016-03-05 03:10:39 +08:00
}
2016-02-29 09:26:36 +08:00
}