using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using StardewValley; namespace Revitalize.Framework.Illuminate { public class LightManager { public Dictionary lights; public Dictionary fakeLights; public bool lightsOn; public const int lightBigNumber= 1000000; public LightManager() { this.lights = new Dictionary(); this.fakeLights = new Dictionary(); this.lightsOn = false; } /// Add a light to the list of tracked lights. public bool addLight(Vector2 IdKey, LightSource light, StardewValley.Object gameObject) { if (gameObject.TileLocation.X < 0) gameObject.TileLocation = new Vector2(gameObject.TileLocation.X * -1, gameObject.TileLocation.Y); if (gameObject.TileLocation.Y < 0) gameObject.TileLocation = new Vector2(gameObject.TileLocation.X, gameObject.TileLocation.Y*-1); Vector2 initialPosition = gameObject.TileLocation * Game1.tileSize; initialPosition += IdKey; if (this.lights.ContainsKey(IdKey)) return false; light.position.Value = initialPosition; this.lights.Add(IdKey, light); if (this.fakeLights.ContainsKey(IdKey)) return true; this.fakeLights.Add(IdKey, new FakeLightSource(light.Identifier, light.position.Value, light.color.Value.Invert(), light.radius.Value)); return true; } /// Turn off a single light. public bool turnOffLight(Vector2 IdKey, GameLocation location) { if (!this.lights.ContainsKey(IdKey)) return false; this.lights.TryGetValue(IdKey, out LightSource light); Game1.currentLightSources.Remove(light); location.sharedLights.Remove((int)IdKey.X * lightBigNumber + (int)IdKey.Y); return true; } /// Turn on a single light. public bool turnOnLight(Vector2 IdKey, GameLocation location, StardewValley.Object gameObject) { if (!this.lights.ContainsKey(IdKey)) return false; this.lights.TryGetValue(IdKey, out var light); if (light == null) throw new Exception("Light is null????"); Game1.currentLightSources.Add(light); if (location == null) throw new Exception("WHY IS LOC NULL???"); if (location.sharedLights == null) throw new Exception("Locational lights is null!"); Game1.showRedMessage("TURN ON!"); if (light.lightTexture == null) { light.lightTexture = this.loadTextureFromConstantValue(light.Identifier); } Game1.currentLightSources.Add(light); location.sharedLights.Add((int)IdKey.X*lightBigNumber+(int)IdKey.Y,light); this.repositionLight(light, IdKey, gameObject); return true; } /// Add a light source to this location. /// The game location to add the light source in. public virtual void turnOnLights(GameLocation environment, StardewValley.Object gameObject) { foreach (KeyValuePair pair in this.lights) this.turnOnLight(pair.Key, environment, gameObject); this.repositionLights(gameObject); } /// Removes a lightsource from the game location. /// The game location to remove the light source from. public void turnOffLights(GameLocation environment) { foreach (KeyValuePair pair in this.lights) this.turnOffLight(pair.Key, environment); } public void repositionLights(StardewValley.Object gameObject) { foreach (KeyValuePair pair in this.lights) this.repositionLight(pair.Value, pair.Key, gameObject); } public void repositionLight(LightSource light, Vector2 offset, StardewValley.Object gameObject) { Vector2 initialPosition = gameObject.TileLocation * Game1.tileSize; light.position.Value = initialPosition + offset; } public virtual void toggleLights(GameLocation location, StardewValley.Object gameObject) { if (!this.lightsOn) { this.turnOnLights(location, gameObject); this.lightsOn = true; return; } else if (this.lightsOn) { this.turnOffLights(Game1.player.currentLocation); this.lightsOn = false; return; } } public virtual void removeForCleanUp(GameLocation loc) { this.turnOffLights(loc); } private Texture2D loadTextureFromConstantValue(int value) { switch (value) { case 1: return Game1.lantern; break; case 2: return Game1.windowLight; break; case 4: return Game1.sconceLight; break; case 5: return Game1.cauldronLight; break; case 6: return Game1.indoorWindowLight; break; } return Game1.sconceLight; } } }