Stardew_Valley_Mods/GeneralMods/Revitalize/Framework/Objects/Extras/ArcadeCabinetTile.cs

315 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using PyTK.CustomElementHandler;
using Revitalize.Framework.Objects.Furniture;
using Revitalize.Framework.Objects.InformationFiles.Furniture;
using Revitalize.Framework.Utilities;
using Revitalize.Framework.Utilities.Serialization;
using StardewValley;
using StardewValley.Minigames;
namespace Revitalize.Framework.Objects.Extras
{
public class ArcadeCabinetTile : FurnitureTileComponent
{
public ArcadeCabinetInformation arcadeInfo;
public override string ItemInfo
{
get
{
string info = Revitalize.ModCore.Serializer.ToJSONString(this.info);
string guidStr = this.guid.ToString();
string pyTkData = ModCore.Serializer.ToJSONString(this.data);
string offsetKey = this.offsetKey != null ? ModCore.Serializer.ToJSONString(this.offsetKey) : "";
string container = this.containerObject != null ? this.containerObject.guid.ToString() : "";
string furnitureInfo = ModCore.Serializer.ToJSONString(this.arcadeInfo);
return info + "<" + guidStr + "<" + pyTkData + "<" + offsetKey + "<" + container + "<" + furnitureInfo;
}
set
{
if (string.IsNullOrEmpty(value)) return;
string[] data = value.Split('<');
string infoString = data[0];
string guidString = data[1];
string pyTKData = data[2];
string offsetVec = data[3];
string containerObject = data[4];
string furnitureInfo = data[5];
this.info = (BasicItemInformation)Revitalize.ModCore.Serializer.DeserializeFromJSONString(infoString, typeof(BasicItemInformation));
this.data = Revitalize.ModCore.Serializer.DeserializeFromJSONString<CustomObjectData>(pyTKData);
if (string.IsNullOrEmpty(offsetVec)) return;
if (string.IsNullOrEmpty(containerObject)) return;
this.offsetKey = ModCore.Serializer.DeserializeFromJSONString<Vector2>(offsetVec);
Guid oldGuid = this.guid;
this.guid = Guid.Parse(guidString);
if (ModCore.CustomObjects.ContainsKey(this.guid))
{
//ModCore.log("Update item with guid: " + this.guid);
ModCore.CustomObjects[this.guid] = this;
}
else
{
//ModCore.log("Add in new guid: " + this.guid);
ModCore.CustomObjects.Add(this.guid, this);
}
if (this.containerObject == null)
{
//ModCore.log(containerObject);
Guid containerGuid = Guid.Parse(containerObject);
if (ModCore.CustomObjects.ContainsKey(containerGuid))
{
this.containerObject = (MultiTiledObject)ModCore.CustomObjects[containerGuid];
this.containerObject.removeComponent(this.offsetKey);
this.containerObject.addComponent(this.offsetKey, this);
//ModCore.log("Set container object from existing object!");
}
else
{
//ModCore.log("Container hasn't been synced???");
MultiplayerUtilities.RequestGuidObject(containerGuid);
MultiplayerUtilities.RequestGuidObject_Tile(this.guid);
}
}
else
{
this.containerObject.updateInfo();
}
if (ModCore.CustomObjects.ContainsKey(oldGuid) && ModCore.CustomObjects.ContainsKey(this.guid))
{
if (ModCore.CustomObjects[oldGuid] == ModCore.CustomObjects[this.guid] && oldGuid != this.guid)
{
//ModCore.CustomObjects.Remove(oldGuid);
}
}
if (string.IsNullOrEmpty(furnitureInfo) == false)
{
this.arcadeInfo = ModCore.Serializer.DeserializeFromJSONString<ArcadeCabinetInformation>(furnitureInfo);
}
}
}
public ArcadeCabinetTile() : base()
{
}
public ArcadeCabinetTile(CustomObjectData PyTKData, BasicItemInformation Info, ArcadeCabinetInformation ArcadeInfo) : base(PyTKData, Info)
{
this.arcadeInfo = ArcadeInfo;
this.Price = Info.price;
}
public ArcadeCabinetTile(CustomObjectData PyTKData, BasicItemInformation Info, Vector2 TileLocation, ArcadeCabinetInformation ArcadeInfo) : base(PyTKData, Info, TileLocation)
{
this.arcadeInfo = ArcadeInfo;
this.Price = Info.price;
}
public override bool performObjectDropInAction(Item dropInItem, bool probe, Farmer who)
{
return false; //this.pickUpItem()==PickUpState.DoNothing;
//return base.performObjectDropInAction(dropInItem, probe, who);
}
public override bool performDropDownAction(Farmer who)
{
return base.performDropDownAction(who);
}
//Checks for any sort of interaction IF and only IF there is a held object on this tile.
public override bool checkForAction(Farmer who, bool justCheckingForActivity = false)
{
MouseState mState = Mouse.GetState();
KeyboardState keyboardState = Game1.GetKeyboardState();
if (mState.RightButton == ButtonState.Pressed && (!keyboardState.IsKeyDown(Keys.LeftShift) || !keyboardState.IsKeyDown(Keys.RightShift)))
{
return this.rightClicked(who);
}
if (mState.RightButton == ButtonState.Pressed && (keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift)))
return this.shiftRightClicked(who);
//return base.checkForAction(who, justCheckingForActivity);
if (justCheckingForActivity)
return true;
return true;
//return this.clicked(who);
//return false;
}
public override bool performToolAction(Tool t, GameLocation location)
{
return base.performToolAction(t, location);
}
public override bool performUseAction(GameLocation location)
{
return base.performUseAction(location);
}
/// <summary>
/// Gets called when there is no actively held item on the tile.
/// </summary>
/// <param name="who"></param>
/// <returns></returns>
public override bool clicked(Farmer who)
{
return base.clicked(who);
}
public override bool rightClicked(Farmer who)
{
if (Game1.menuUp || Game1.currentMinigame != null) return false;
if (this.arcadeInfo.minigame != null)
{
if (this.arcadeInfo.minigame is StardewValley.Minigames.IMinigame)
{
if (this.arcadeInfo.freezeState == false)
{
Game1.currentMinigame = (IMinigame)Activator.CreateInstance(this.arcadeInfo.minigame.GetType());
return true;
}
else
{
Game1.currentMinigame = this.arcadeInfo.minigame;
return true;
}
}
}
return false;
}
public override bool shiftRightClicked(Farmer who)
{
return base.shiftRightClicked(who);
}
public override Item getOne()
{
ArcadeCabinetTile component = new ArcadeCabinetTile(this.data, this.info.Copy(), this.arcadeInfo);
component.containerObject = this.containerObject;
component.offsetKey = this.offsetKey;
return component;
}
public override ICustomObject recreate(Dictionary<string, string> additionalSaveData, object replacement)
{
Vector2 offsetKey = new Vector2(Convert.ToInt32(additionalSaveData["offsetKeyX"]), Convert.ToInt32(additionalSaveData["offsetKeyY"]));
string GUID = additionalSaveData["GUID"];
ArcadeCabinetTile self = Revitalize.ModCore.Serializer.DeserializeGUID<ArcadeCabinetTile>(additionalSaveData["GUID"]);
if (ModCore.IsNullOrDefault<ArcadeCabinetTile>(self)) return null;
try
{
if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["ParentGUID"]))
{
ArcadeCabinetOBJ obj = (ArcadeCabinetOBJ)Revitalize.ModCore.Serializer.DeserializeGUID<ArcadeCabinetOBJ>(additionalSaveData["ParentGUID"]);
self.containerObject = obj;
self.containerObject.removeComponent(offsetKey);
self.containerObject.addComponent(offsetKey, self);
Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["ParentGUID"], obj);
}
else
{
self.containerObject = Revitalize.ModCore.ObjectGroups[additionalSaveData["ParentGUID"]];
self.containerObject.removeComponent(offsetKey);
self.containerObject.addComponent(offsetKey, self);
}
}
catch (Exception err)
{
ModCore.log(err);
}
return self;
}
public override Dictionary<string, string> getAdditionalSaveData()
{
Dictionary<string, string> saveData = base.getAdditionalSaveData();
Revitalize.ModCore.Serializer.SerializeGUID(this.containerObject.childrenGuids[this.offsetKey].ToString(), this);
this.containerObject.getAdditionalSaveData();
return saveData;
}
/// <summary>What happens when the object is drawn at a tile location.</summary>
public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1f)
{
/*
if (this.info.ignoreBoundingBox == true)
{
x *= -1;
y *= -1;
}
*/
if (this.info == null)
{
Revitalize.ModCore.log("info is null");
if (this.syncObject == null) Revitalize.ModCore.log("DEAD SYNC");
}
if (this.animationManager == null) Revitalize.ModCore.log("Animation Manager Null");
if (this.displayTexture == null) Revitalize.ModCore.log("Display texture is null");
//The actual planter box being drawn.
if (this.animationManager == null)
{
if (this.animationManager.getExtendedTexture() == null)
ModCore.ModMonitor.Log("Tex Extended is null???");
spriteBatch.Draw(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)(y * Game1.tileSize) / 10000f));
// Log.AsyncG("ANIMATION IS NULL?!?!?!?!");
}
else
{
//Log.AsyncC("Animation Manager is working!");
float addedDepth = 0;
if (Revitalize.ModCore.playerInfo.sittingInfo.SittingObject == this.containerObject && this.info.facingDirection == Enums.Direction.Up)
{
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)((y + addedDepth) * Game1.tileSize) / 10000f) + .00001f);
try
{
this.animationManager.tickAnimation();
// Log.AsyncC("Tick animation");
}
catch (Exception err)
{
ModCore.ModMonitor.Log(err.ToString());
}
if (this.heldObject.Value != null) SpriteBatchUtilities.Draw(spriteBatch, this, this.heldObject.Value, alpha, addedDepth);
}
}
}
}