minervamaga

Untitled

Sep 9th, 2019
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Microsoft.Xna.Framework;
  5. using StardewModdingAPI;
  6. using StardewModdingAPI.Events;
  7. using StardewValley;
  8. using StardewValley.Locations;
  9. using StardewValley.Network;
  10. using Object = StardewValley.Object;
  11.  
  12. namespace GateOpener
  13. {
  14.     /// <summary>The mod entry class loaded by SMAPI.</summary>
  15.     public class GateOpenerMainClass : Mod
  16.     {
  17.         /*********
  18.         ** Fields
  19.         *********/
  20.         public Dictionary<Vector2, Fence> OpenGates = new Dictionary<Vector2, Fence>();
  21.  
  22.  
  23.         /*********
  24.         ** Public methods
  25.         *********/
  26.         /// <summary>The mod entry point, called after the mod is first loaded.</summary>
  27.         /// <param name="helper">Provides simplified APIs for writing mods.</param>
  28.         public override void Entry(IModHelper helper)
  29.         {
  30.             helper.Events.GameLoop.UpdateTicked += this.OnUpdateTicked;
  31.         }
  32.  
  33.  
  34.         /*********
  35.         ** Private methods
  36.         *********/
  37.         private Fence GetGate(BuildableGameLocation location, Vector2 pos)
  38.         {
  39.             if (!location.objects.TryGetValue(pos, out StardewValley.Object obj))
  40.                 return null;
  41.  
  42.             if (obj is Fence fence && fence.isGate.Value && !this.OpenGates.ContainsKey(pos))
  43.             {
  44.                 this.OpenGates[pos] = fence;
  45.                 return fence;
  46.             }
  47.             return null;
  48.         }
  49.  
  50.         private Fence LookAround(BuildableGameLocation location, List<Vector2> list)
  51.         {
  52.             foreach (Vector2 pos in list)
  53.             {
  54.                 Fence gate = this.GetGate(location, pos);
  55.                 if (gate != null)
  56.                     return gate;
  57.             }
  58.             return null;
  59.         }
  60.  
  61.         private void OnWarped(object sender, WarpedEventArgs e)
  62.         {
  63.             if (!e.IsLocalPlayer)
  64.                 return;
  65.         }
  66.  
  67.         /// <summary>Raised after objects are added or removed in a location.</summary>
  68.         /// <param name="sender">The event sender.</param>
  69.         /// <param name="e">The event data.</param>
  70.         private void OnObjectListChanged(object sender, ObjectListChangedEventArgs e)
  71.         {
  72.             OpenGates = new SerializableDictionary<Vector2, Fence>();
  73.             OverlaidDictionary objects = Game1.currentLocation.objects;
  74.  
  75.             foreach (Vector2 key in objects.Keys)
  76.             {
  77.                 if (objects[key] is Fence fence && fence.isGate.Value)
  78.                 {
  79.                     this.OpenGates.Add(key, fence);
  80.                     this.Monitor.Log("New gate detected!", LogLevel.Trace);
  81.                 }
  82.             }
  83.         }
  84.  
  85.         /// <summary>Raised after the game state is updated (≈60 times per second).</summary>
  86.         /// <param name="sender">The event sender.</param>
  87.         /// <param name="e">The event data.</param>
  88.         private void OnUpdateTicked(object sender, UpdateTickedEventArgs e)
  89.         {
  90.             if (Game1.currentLocation is BuildableGameLocation location)
  91.             {
  92.                 List<Vector2> adj = Utility.getAdjacentTileLocations(Game1.player.getTileLocation());
  93.                 Fence gate = this.LookAround(location, adj);
  94.  
  95.                 foreach (KeyValuePair<Vector2, Fence> gateObj in OpenGates)
  96.                 {
  97.                     if (Game1.player.getTileLocation() == gateObj.Key && adj.Contains(gateObj.Key))
  98.                     {
  99.                         gate.gatePosition.Set(88);
  100.                         Game1.playSound("doorClose");
  101.                         this.Monitor.Log("Gate opened!", LogLevel.Trace);
  102.                         break;
  103.                     }
  104.  
  105.                     //need to close it now...
  106.  
  107.                     if (Game1.player.getTileLocation() != gateObj.Key && !adj.Contains(gateObj.Key))
  108.                     {
  109.                         gateObj.Value.gatePosition.Set(0);
  110.                         Game1.playSound("doorClose");
  111.                         OpenGates.Remove(gateObj.Key);
  112.                         this.Monitor.Log("Gate closed!", LogLevel.Trace);
  113.                         break;
  114.                      
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment