SMAPI/StardewModdingAPI/Inheritance/SGame.cs

745 lines
31 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
{
/// <summary>
/// The 'SGame' class.
/// This summary, and many others, only exists because XML doc tags.
/// </summary>
2016-03-05 03:10:39 +08:00
public class SGame : Game1
{
/// <summary>
/// Useless right now.
/// </summary>
public const int LowestModItemID = 1000;
2016-03-05 03:10:39 +08:00
private bool FireLoadedGameEvent;
/// <summary>
/// Gets a jagged array of all buttons pressed on the gamepad the prior frame.
/// </summary>
public Buttons[][] PreviouslyPressedButtons;
2016-03-05 03:10:39 +08:00
internal SGame()
2016-03-05 03:10:39 +08:00
{
Instance = this;
FirstUpdate = true;
2016-03-05 03:10:39 +08:00
}
/// <summary>
/// Useless at this time.
/// </summary>
[Obsolete]
public static Dictionary<int, SObject> ModItems { get; private set; }
/// <summary>
/// The current KeyboardState
/// </summary>
2016-03-05 03:10:39 +08:00
public KeyboardState KStateNow { get; private set; }
/// <summary>
/// The prior KeyboardState
/// </summary>
2016-03-05 03:10:39 +08:00
public KeyboardState KStatePrior { get; private set; }
/// <summary>
/// The current MouseState
/// </summary>
2016-03-05 03:10:39 +08:00
public MouseState MStateNow { get; private set; }
/// <summary>
/// The prior MouseState
/// </summary>
2016-03-05 03:10:39 +08:00
public MouseState MStatePrior { get; private set; }
/// <summary>
/// All keys pressed on the current frame
/// </summary>
2016-03-23 13:11:13 +08:00
public Keys[] CurrentlyPressedKeys => KStateNow.GetPressedKeys();
/// <summary>
/// All keys pressed on the prior frame
/// </summary>
2016-03-23 13:11:13 +08:00
public Keys[] PreviouslyPressedKeys => KStatePrior.GetPressedKeys();
2016-03-05 03:10:39 +08:00
/// <summary>
/// All keys pressed on this frame except for the ones pressed on the prior frame
/// </summary>
public Keys[] FramePressedKeys => CurrentlyPressedKeys.Except(PreviouslyPressedKeys).ToArray();
/// <summary>
/// All keys pressed on the prior frame except for the ones pressed on the current frame
/// </summary>
public Keys[] FrameReleasedKeys => PreviouslyPressedKeys.Except(CurrentlyPressedKeys).ToArray();
/// <summary>
/// Whether or not a save was tagged as 'Loaded' the prior frame.
/// </summary>
public bool PreviouslyLoadedGame { get; private set; }
/// <summary>
/// The list of GameLocations on the prior frame
/// </summary>
public int PreviousGameLocations { get; private set; }
/// <summary>
/// The list of GameObjects on the prior frame
/// </summary>
public int PreviousLocationObjects { get; private set; }
/// <summary>
/// The list of Items in the player's inventory on the prior frame
/// </summary>
public Dictionary<Item, int> PreviousItems { get; private set; }
/// <summary>
/// The player's Combat level on the prior frame
/// </summary>
public int PreviousCombatLevel { get; private set; }
/// <summary>
/// The player's Farming level on the prior frame
/// </summary>
public int PreviousFarmingLevel { get; private set; }
/// <summary>
/// The player's Fishing level on the prior frame
/// </summary>
public int PreviousFishingLevel { get; private set; }
/// <summary>
/// The player's Foraging level on the prior frame
/// </summary>
public int PreviousForagingLevel { get; private set; }
/// <summary>
/// The player's Mining level on the prior frame
/// </summary>
public int PreviousMiningLevel { get; private set; }
/// <summary>
/// The player's Luck level on the prior frame
/// </summary>
public int PreviousLuckLevel { get; private set; }
//Kill me now comments are so boring
/// <summary>
/// The player's previous game location
/// </summary>
public GameLocation PreviousGameLocation { get; private set; }
/// <summary>
/// The previous ActiveGameMenu in Game1
/// </summary>
public IClickableMenu PreviousActiveMenu { get; private set; }
/// <summary>
/// The previous mine level
/// </summary>
public int PreviousMineLevel { get; private set; }
/// <summary>
/// The previous TimeOfDay (Int32 between 600 and 2400?)
/// </summary>
public int PreviousTimeOfDay { get; private set; }
/// <summary>
/// The previous DayOfMonth (Int32 between 1 and 28?)
/// </summary>
public int PreviousDayOfMonth { get; private set; }
/// <summary>
/// The previous Season (String as follows: "winter", "spring", "summer", "fall")
/// </summary>
public string PreviousSeasonOfYear { get; private set; }
/// <summary>
/// The previous Year
/// </summary>
public int PreviousYearOfGame { get; private set; }
/// <summary>
/// The previous 'Farmer' (Player)
/// </summary>
public Farmer PreviousFarmer { get; private set; }
/// <summary>
/// The current index of the update tick. Recycles every 60th tick to 0. (Int32 between 0 and 59)
/// </summary>
public int CurrentUpdateTick { get; private set; }
/// <summary>
/// Whether or not this update frame is the very first of the entire game
/// </summary>
public bool FirstUpdate { get; private set; }
/// <summary>
/// The current RenderTarget in Game1 (Private field, uses reflection)
/// </summary>
public RenderTarget2D Screen
{
get { return typeof (Game1).GetBaseFieldValue<RenderTarget2D>(Program.gamePtr, "screen"); }
set { typeof (Game1).SetBaseFieldValue<RenderTarget2D>(this, "screen", value); }
}
/// <summary>
/// Static accessor for an Instance of the class SGame
/// </summary>
public static SGame Instance { get; private set; }
/// <summary>
/// The game's FPS. Re-determined every Draw update.
/// </summary>
public static float FramesPerSecond { get; private set; }
/// <summary>
/// Whether or not we're in a pseudo 'debug' mode. Mostly for displaying information like FPS.
/// </summary>
public static bool Debug { get; private set; }
/// <summary>
/// The current player (equal to Farmer.Player)
/// </summary>
[Obsolete("Use Farmer.Player instead")]
public Farmer CurrentFarmer => player;
/// <summary>
/// Gets ALL static fields that belong to 'Game1'
/// </summary>
public static FieldInfo[] GetStaticFields => typeof (Game1).GetFields();
/// <summary>
/// Whether or not a button was just pressed on the controller
/// </summary>
/// <param name="button"></param>
/// <param name="buttonState"></param>
/// <param name="stateIndex"></param>
/// <returns></returns>
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
/// <summary>
/// Whether or not a button was just released on the controller
/// </summary>
/// <param name="button"></param>
/// <param name="buttonState"></param>
/// <param name="stateIndex"></param>
/// <returns></returns>
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);
2016-03-08 01:25:51 +08:00
}
/// <summary>
/// Whether or not an analog button was just pressed on the controller
/// </summary>
/// <param name="button"></param>
/// <param name="value"></param>
/// <param name="stateIndex"></param>
/// <returns></returns>
2016-03-08 01:25:51 +08:00
private bool WasButtonJustPressed(Buttons button, float value, PlayerIndex stateIndex)
{
return WasButtonJustPressed(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex);
}
/// <summary>
/// Whether or not an analog button was just released on the controller
/// </summary>
/// <param name="button"></param>
/// <param name="value"></param>
/// <param name="stateIndex"></param>
/// <returns></returns>
2016-03-08 01:25:51 +08:00
private bool WasButtonJustReleased(Buttons button, float value, PlayerIndex stateIndex)
{
return WasButtonJustReleased(button, value > 0.2f ? ButtonState.Pressed : ButtonState.Released, stateIndex);
}
/// <summary>
/// Gets an array of all Buttons pressed on a joystick
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
2016-03-08 01:25:51 +08:00
public Buttons[] GetButtonsDown(PlayerIndex index)
{
var state = GamePad.GetState(index);
var buttons = new List<Buttons>();
2016-03-08 01:25:51 +08:00
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();
}
/// <summary>
/// Gets all buttons that were pressed on the current frame of a joystick
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public Buttons[] GetFramePressedButtons(PlayerIndex index)
{
var state = GamePad.GetState(index);
var 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();
}
/// <summary>
/// Gets all buttons that were released on the current frame of a joystick
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
2016-03-08 01:25:51 +08:00
public Buttons[] GetFrameReleasedButtons(PlayerIndex index)
{
var state = GamePad.GetState(index);
var buttons = new List<Buttons>();
2016-03-08 01:25:51 +08:00
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();
}
/// <summary>
/// XNA Init Method
/// </summary>
2016-03-05 03:10:39 +08:00
protected override void Initialize()
{
Log.AsyncY("XNA Initialize");
//ModItems = new Dictionary<int, SObject>();
2016-03-08 01:25:51 +08:00
PreviouslyPressedButtons = new Buttons[4][];
for (var i = 0; i < 4; ++i) PreviouslyPressedButtons[i] = new Buttons[0];
2016-03-08 01:25:51 +08:00
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
}
/// <summary>
/// XNA LC Method
/// </summary>
2016-03-05 03:10:39 +08:00
protected override void LoadContent()
{
Log.AsyncY("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
}
/// <summary>
/// XNA Update Method
/// </summary>
/// <param name="gameTime"></param>
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.AsyncR("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 (var i = PlayerIndex.One; i <= PlayerIndex.Four; i++)
2016-03-08 01:25:51 +08:00
{
PreviouslyPressedButtons[(int) i] = GetButtonsDown(i);
2016-03-08 01:25:51 +08:00
}
2016-03-05 03:10:39 +08:00
}
/// <summary>
/// XNA Draw Method
/// </summary>
/// <param name="gameTime"></param>
2016-03-05 03:10:39 +08:00
protected override void Draw(GameTime gameTime)
{
FramesPerSecond = 1 / (float) gameTime.ElapsedGameTime.TotalSeconds;
2016-03-23 13:11:13 +08:00
try
{
base.Draw(gameTime);
}
catch (Exception ex)
{
Log.AsyncR("An error occured in the base draw loop: " + ex);
2016-03-23 13:11:13 +08:00
Console.ReadKey();
}
2016-03-23 08:36:04 +08:00
GraphicsEvents.InvokeDrawTick();
2016-03-23 13:11:13 +08:00
if (Constants.EnableDrawingIntoRenderTarget)
{
if (!options.zoomLevel.Equals(1.0f))
{
if (Screen.RenderTargetUsage == RenderTargetUsage.DiscardContents)
{
Screen = new RenderTarget2D(graphics.GraphicsDevice, Math.Min(4096, (int) (Window.ClientBounds.Width * (1.0 / options.zoomLevel))),
Math.Min(4096, (int) (Window.ClientBounds.Height * (1.0 / options.zoomLevel))),
false, SurfaceFormat.Color, DepthFormat.Depth16, 1, RenderTargetUsage.PreserveContents);
}
GraphicsDevice.SetRenderTarget(Screen);
}
// Not beginning the batch due to inconsistancies with the standard draw tick...
//spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null);
GraphicsEvents.InvokeDrawInRenderTargetTick();
//spriteBatch.End();
//Re-draw the HUD
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null);
if ((displayHUD || eventUp) && currentBillboard == 0 && gameMode == 3 && !freezeControls && !panMode)
typeof (Game1).GetMethod("drawHUD", BindingFlags.NonPublic | BindingFlags.Instance)?.Invoke(Program.gamePtr, null);
spriteBatch.End();
if (!options.zoomLevel.Equals(1.0f))
{
GraphicsDevice.SetRenderTarget(null);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone);
spriteBatch.Draw(Screen, Vector2.Zero, Screen.Bounds, Color.White, 0.0f, Vector2.Zero, options.zoomLevel, SpriteEffects.None, 1f);
spriteBatch.End();
}
}
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
}
[Obsolete("Do not use at this time.")]
private static int RegisterModItem(SObject modItem)
2016-03-05 03:10:39 +08:00
{
if (modItem.HasBeenRegistered)
{
Log.AsyncR($"The item {modItem.Name} has already been registered with ID {modItem.RegisteredId}");
2016-03-05 03:10:39 +08:00
return modItem.RegisteredId;
}
var newId = LowestModItemID;
2016-03-05 03:10:39 +08:00
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;
}
[Obsolete("Do not use at this time.")]
private static SObject PullModItemFromDict(int id, bool isIndex)
2016-03-05 03:10:39 +08:00
{
if (isIndex)
{
2016-03-05 03:10:39 +08:00
if (ModItems.ElementAtOrDefault(id).Value != null)
{
return ModItems.ElementAt(id).Value.Clone();
}
Log.AsyncR("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();
}
Log.AsyncR("ModItem Dictionary does not contain ID: " + id);
2016-03-05 03:10:39 +08:00
return null;
}
private void UpdateEventCalls()
2016-03-05 03:10:39 +08:00
{
KStateNow = Keyboard.GetState();
2016-03-07 03:46:47 +08:00
2016-03-05 03:10:39 +08:00
MStateNow = Mouse.GetState();
foreach (var k in FramePressedKeys)
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeKeyPressed(k);
2016-03-07 03:46:47 +08:00
foreach (var k in FrameReleasedKeys)
2016-03-23 08:36:04 +08:00
ControlEvents.InvokeKeyReleased(k);
2016-03-07 03:46:47 +08:00
for (var i = PlayerIndex.One; i <= PlayerIndex.Four; i++)
{
var buttons = GetFramePressedButtons(i);
foreach (var b in buttons)
{
if (b == Buttons.LeftTrigger || b == Buttons.RightTrigger)
2016-03-08 01:25:51 +08:00
{
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 (var i = PlayerIndex.One; i <= PlayerIndex.Four; i++)
{
foreach (var 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;
}
if (mine != null && PreviousMineLevel != mine.mineLevel)
{
MineEvents.InvokeMineLevelChanged(PreviousMineLevel, mine.mineLevel);
PreviousMineLevel = mine.mineLevel;
}
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)
{
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});
}
}
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
}