Terraria ModLoader  0.11.7.8
A mod to make and play Terraria mods
WaterStyleLoader.cs
Go to the documentation of this file.
1 using Microsoft.Xna.Framework.Graphics;
2 using System;
3 using System.Collections.Generic;
4 using Terraria.GameContent.Liquid;
5 
6 namespace Terraria.ModLoader
7 {
8  //todo: further documentation
12  public static class WaterStyleLoader
13  {
17  public const int vanillaWaterCount = Main.maxLiquidTypes;
18  private static int nextWaterStyle = vanillaWaterCount;
19  internal static readonly IList<ModWaterStyle> waterStyles = new List<ModWaterStyle>();
20 
24  public static int WaterStyleCount => nextWaterStyle;
25 
26  internal static int ReserveStyle() {
27  int reserve = nextWaterStyle;
28  nextWaterStyle++;
29  return reserve;
30  }
31 
35  public static ModWaterStyle GetWaterStyle(int style) {
36  if (style < vanillaWaterCount || style >= nextWaterStyle) {
37  return null;
38  }
39  return waterStyles[style - vanillaWaterCount];
40  }
41 
42  internal static void ResizeArrays() {
43  Array.Resize(ref LiquidRenderer.Instance._liquidTextures, nextWaterStyle);
44  Array.Resize(ref Main.liquidAlpha, nextWaterStyle);
45  Array.Resize(ref Main.liquidTexture, nextWaterStyle);
46  }
47 
48  internal static void Unload() {
49  nextWaterStyle = vanillaWaterCount;
50  waterStyles.Clear();
51  }
52 
53  public static void ChooseWaterStyle(ref int style) {
54  foreach (ModWaterStyle waterStyle in waterStyles) {
55  if (waterStyle.ChooseWaterStyle()) {
56  style = waterStyle.Type;
57  }
58  }
59  WorldHooks.ChooseWaterStyle(ref style);
60  }
61 
62  public static void UpdateLiquidAlphas() {
63  if (Main.waterStyle >= vanillaWaterCount) {
64  for (int k = 0; k < vanillaWaterCount; k++) {
65  if (k == 1 || k == 11) {
66  continue;
67  }
68  Main.liquidAlpha[k] -= 0.2f;
69  if (Main.liquidAlpha[k] < 0f) {
70  Main.liquidAlpha[k] = 0f;
71  }
72  }
73  }
74  foreach (ModWaterStyle waterStyle in waterStyles) {
75  int type = waterStyle.Type;
76  if (Main.waterStyle == type) {
77  Main.liquidAlpha[type] += 0.2f;
78  if (Main.liquidAlpha[type] > 1f) {
79  Main.liquidAlpha[type] = 1f;
80  }
81  }
82  else {
83  Main.liquidAlpha[type] -= 0.2f;
84  if (Main.liquidAlpha[type] < 0f) {
85  Main.liquidAlpha[type] = 0f;
86  }
87  }
88  }
89  }
90 
91  public static void DrawWatersToScreen(bool bg) {
92  for (int k = vanillaWaterCount; k < nextWaterStyle; k++) {
93  if (Main.liquidAlpha[k] > 0f) {
94  if (bg) {
95  if (Main.waterStyle < k) {
96  Main.instance.DrawWater(bg, k, Main.liquidAlpha[k]);
97  }
98  else {
99  Main.instance.DrawWater(bg, k, 1f);
100  }
101  }
102  else {
103  Main.instance.DrawWater(bg, k, Main.liquidAlpha[k]);
104  }
105  }
106  }
107  }
108 
109  public static void DrawWaterfall(WaterfallManager waterfallManager, SpriteBatch spriteBatch) {
110  foreach (ModWaterStyle waterStyle in waterStyles) {
111  if (Main.liquidAlpha[waterStyle.Type] > 0f) {
112  waterfallManager.DrawWaterfall(spriteBatch, waterStyle.ChooseWaterfallStyle(),
113  Main.liquidAlpha[waterStyle.Type]);
114  }
115  }
116  }
117 
118  public static void LightColorMultiplier(int style, ref float r, ref float g, ref float b) {
119  ModWaterStyle waterStyle = GetWaterStyle(style);
120  if (waterStyle != null) {
121  waterStyle.LightColorMultiplier(ref r, ref g, ref b);
122  r *= Lighting.negLight * Lighting.blueWave;
123  g *= Lighting.negLight * Lighting.blueWave;
124  b *= Lighting.negLight * Lighting.blueWave;
125  }
126  }
127  }
128 
129  public static class WaterfallStyleLoader
130  {
131  public const int vanillaWaterfallCount = WaterfallManager.maxTypes;
132  private static int nextWaterfallStyle = vanillaWaterfallCount;
133  internal static readonly IList<ModWaterfallStyle> waterfallStyles = new List<ModWaterfallStyle>();
134 
135  internal static int ReserveStyle() {
136  int reserve = nextWaterfallStyle;
137  nextWaterfallStyle++;
138  return reserve;
139  }
140 
144  public static ModWaterfallStyle GetWaterfallStyle(int style) {
145  if (style < vanillaWaterfallCount || style >= nextWaterfallStyle) {
146  return null;
147  }
148  return waterfallStyles[style - vanillaWaterfallCount];
149  }
150 
151  internal static void ResizeArrays() {
152  Array.Resize(ref Main.instance.waterfallManager.waterfallTexture, nextWaterfallStyle);
153  }
154 
155  internal static void Unload() {
156  nextWaterfallStyle = vanillaWaterfallCount;
157  waterfallStyles.Clear();
158  }
159  }
160 }
static ModWaterfallStyle GetWaterfallStyle(int style)
Returns the ModWaterfallStyle with the given ID.
static void LightColorMultiplier(int style, ref float r, ref float g, ref float b)
static void DrawWatersToScreen(bool bg)
This is where all ModWorld hooks are gathered and called.
Definition: WorldHooks.cs:12
Represents a style of water that gets drawn, based on factors such as the background. This is used to determine the color of the water, as well as other things as determined by the hooks below.
Definition: ModWaterStyle.cs:8
virtual void LightColorMultiplier(ref float r, ref float g, ref float b)
Allows you to modify the light levels of the tiles behind the water. The light color components will ...
static void ChooseWaterStyle(ref int style)
abstract int ChooseWaterfallStyle()
The ID of the waterfall style the game should use when this water style is in use.
Represents a style of waterfalls that gets drawn. This is mostly used to determine the color of the w...
This serves as the central class from which WaterStyle functions are supported and carried out...
static void DrawWaterfall(WaterfallManager waterfallManager, SpriteBatch spriteBatch)
virtual bool ChooseWaterStyle()
Whether the conditions have been met for this water style to be used. Typically Main.bgStyle is checked to determine whether a water style should be used. Returns false by default.
static void ChooseWaterStyle(ref int style)
Definition: WorldHooks.cs:92
static ModWaterStyle GetWaterStyle(int style)
Returns the ModWaterStyle with the given ID.
int Type
The ID of the water style.