コード例 #1
0
        /// <summary>Modify the alpha (transparency) value of this instance after its per-tick update.</summary>
        /// <param name="__instance">The instance calling the original method.</param>
        private static void Bush_tickUpdate(Bush __instance, Vector2 tileLocation)
        {
            try
            {
                if (ModEntry.Config.BushSettings.Enable)                                                                                                                                                   //if this type of custom transparency is enabled
                {
                    if (__instance.size.Value != 3)                                                                                                                                                        //if this bush is NOT a tea bush
                    {
                        var   alpha       = Helper.Reflection.GetField <float>(__instance, "alpha", true);                                                                                                 //get this bush's alpha field
                        Point centerPixel = __instance.getRenderBounds(__instance.tilePosition.Value).Center;                                                                                              //get the center pixel of the bush's visible area

                        if (InputManager.FullTransparency.Value ||                                                                                                                                         //if full transparency is toggled on, or ALL of the following is true:
                            (InputManager.DisableTransparency.Value == false &&                                                                                                                            //if transparency is NOT toggled off
                             (ModEntry.Config.BushSettings.BelowPlayerOnly == false || CacheManager.CurrentPlayerTile.Y < __instance.tilePosition.Value.Y) &&                                              //AND if the tree is below the player's Y level OR that option is disabled,
                             Vector2.Distance(new Vector2(centerPixel.X, centerPixel.Y), CacheManager.CurrentPlayerTile * Game1.tileSize) < (ModEntry.Config.BushSettings.TileDistance * Game1.tileSize))) //AND if the player is within range of this bush's center pixel
                        {
                            alpha.SetValue(CacheManager.GetAlpha(__instance, -0.05f));                                                                                                                     //make this bush MORE transparent
                        }
                        else
                        {
                            alpha.SetValue(CacheManager.GetAlpha(__instance, 0.05f)); //make this bush LESS transparent
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Monitor.LogOnce($"Harmony patch \"{nameof(HarmonyPatch_BushTransparency)}\" has encountered an error. Custom bush transparency might not work correctly. Full error message: \n{ex.ToString()}", LogLevel.Error);
                return; //run the original method
            }
        }