2018-12-26 11:48:45 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-12-30 18:00:05 +08:00
|
|
|
using Microsoft.Xna.Framework;
|
2019-05-18 02:04:16 +08:00
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2019-08-28 07:47:22 +08:00
|
|
|
using Newtonsoft.Json;
|
2018-12-30 18:00:05 +08:00
|
|
|
using StardewValley;
|
2018-12-26 11:48:45 +08:00
|
|
|
|
|
|
|
namespace Revitalize.Framework.Illuminate
|
|
|
|
{
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Deals with handling lights on custom objects.
|
|
|
|
/// </summary>
|
2018-12-26 11:48:45 +08:00
|
|
|
public class LightManager
|
|
|
|
{
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The lights held by this object.
|
|
|
|
/// </summary>
|
2018-12-30 18:00:05 +08:00
|
|
|
public Dictionary<Vector2, LightSource> lights;
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Used to recreate lights at run time.
|
|
|
|
/// </summary>
|
2019-05-18 02:04:16 +08:00
|
|
|
public Dictionary<Vector2, FakeLightSource> fakeLights;
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Are the lights on this object on?
|
|
|
|
/// </summary>
|
2018-12-26 11:48:45 +08:00
|
|
|
public bool lightsOn;
|
|
|
|
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Magic number for positioning.
|
|
|
|
/// </summary>
|
2019-05-18 00:43:50 +08:00
|
|
|
public const int lightBigNumber= 1000000;
|
|
|
|
|
2019-08-28 07:47:22 +08:00
|
|
|
[JsonIgnore]
|
|
|
|
public bool requiresUpdate;
|
|
|
|
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Constructor.
|
|
|
|
/// </summary>
|
2018-12-26 11:48:45 +08:00
|
|
|
public LightManager()
|
|
|
|
{
|
|
|
|
this.lights = new Dictionary<Vector2, LightSource>();
|
2019-05-18 02:04:16 +08:00
|
|
|
this.fakeLights = new Dictionary<Vector2, FakeLightSource>();
|
2018-12-30 18:00:05 +08:00
|
|
|
this.lightsOn = false;
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Add a light to the list of tracked lights.</summary>
|
|
|
|
public bool addLight(Vector2 IdKey, LightSource light, StardewValley.Object gameObject)
|
2018-12-26 11:48:45 +08:00
|
|
|
{
|
2019-05-18 02:04:16 +08:00
|
|
|
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);
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
Vector2 initialPosition = gameObject.TileLocation * Game1.tileSize;
|
2018-12-26 11:48:45 +08:00
|
|
|
initialPosition += IdKey;
|
|
|
|
|
|
|
|
if (this.lights.ContainsKey(IdKey))
|
|
|
|
return false;
|
2018-12-30 18:00:05 +08:00
|
|
|
|
|
|
|
light.position.Value = initialPosition;
|
|
|
|
this.lights.Add(IdKey, light);
|
2019-05-18 02:04:16 +08:00
|
|
|
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));
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
2018-12-30 18:00:05 +08:00
|
|
|
return true;
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
|
|
|
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Adds in a light at the given tile location in the world.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="IdKey"></param>
|
|
|
|
/// <param name="light"></param>
|
|
|
|
/// <param name="gameObjectTileLocation"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public bool addLight(Vector2 IdKey, LightSource light, Vector2 gameObjectTileLocation)
|
|
|
|
{
|
|
|
|
if (gameObjectTileLocation.X < 0) gameObjectTileLocation = new Vector2(gameObjectTileLocation.X * -1, gameObjectTileLocation.Y);
|
|
|
|
if (gameObjectTileLocation.Y < 0) gameObjectTileLocation = new Vector2(gameObjectTileLocation.X, gameObjectTileLocation.Y * -1);
|
|
|
|
|
|
|
|
Vector2 initialPosition = gameObjectTileLocation * 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));
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
2019-08-17 06:32:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Turn off a single light.</summary>
|
2018-12-26 11:48:45 +08:00
|
|
|
public bool turnOffLight(Vector2 IdKey, GameLocation location)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (!this.lights.ContainsKey(IdKey))
|
2018-12-26 11:48:45 +08:00
|
|
|
return false;
|
2018-12-30 18:00:05 +08:00
|
|
|
|
|
|
|
this.lights.TryGetValue(IdKey, out LightSource light);
|
|
|
|
Game1.currentLightSources.Remove(light);
|
2019-05-18 00:43:50 +08:00
|
|
|
location.sharedLights.Remove((int)IdKey.X * lightBigNumber + (int)IdKey.Y);
|
2018-12-30 18:00:05 +08:00
|
|
|
return true;
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Turn on a single light.</summary>
|
|
|
|
public bool turnOnLight(Vector2 IdKey, GameLocation location, StardewValley.Object gameObject)
|
2018-12-26 11:48:45 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (!this.lights.ContainsKey(IdKey))
|
2018-12-26 11:48:45 +08:00
|
|
|
return false;
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
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???");
|
2018-12-26 11:48:45 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
if (location.sharedLights == null)
|
|
|
|
throw new Exception("Locational lights is null!");
|
2018-12-26 11:48:45 +08:00
|
|
|
|
2019-05-18 02:04:16 +08:00
|
|
|
|
|
|
|
if (light.lightTexture == null)
|
|
|
|
{
|
|
|
|
light.lightTexture = this.loadTextureFromConstantValue(light.Identifier);
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
Game1.currentLightSources.Add(light);
|
2019-05-18 00:43:50 +08:00
|
|
|
location.sharedLights.Add((int)IdKey.X*lightBigNumber+(int)IdKey.Y,light);
|
2018-12-30 18:00:05 +08:00
|
|
|
this.repositionLight(light, IdKey, gameObject);
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
2018-12-30 18:00:05 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-26 11:48:45 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Add a light source to this location.</summary>
|
2018-12-26 11:48:45 +08:00
|
|
|
/// <param name="environment">The game location to add the light source in.</param>
|
2018-12-30 18:00:05 +08:00
|
|
|
public virtual void turnOnLights(GameLocation environment, StardewValley.Object gameObject)
|
2018-12-26 11:48:45 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (KeyValuePair<Vector2, LightSource> pair in this.lights)
|
|
|
|
this.turnOnLight(pair.Key, environment, gameObject);
|
|
|
|
this.repositionLights(gameObject);
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Removes a lightsource from the game location.</summary>
|
2018-12-26 11:48:45 +08:00
|
|
|
/// <param name="environment">The game location to remove the light source from.</param>
|
|
|
|
public void turnOffLights(GameLocation environment)
|
|
|
|
{
|
|
|
|
foreach (KeyValuePair<Vector2, LightSource> pair in this.lights)
|
2018-12-30 18:00:05 +08:00
|
|
|
this.turnOffLight(pair.Key, environment);
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
|
|
|
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Repositions all lights for this object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gameObject"></param>
|
2018-12-26 11:48:45 +08:00
|
|
|
public void repositionLights(StardewValley.Object gameObject)
|
|
|
|
{
|
|
|
|
foreach (KeyValuePair<Vector2, LightSource> pair in this.lights)
|
2018-12-30 18:00:05 +08:00
|
|
|
this.repositionLight(pair.Value, pair.Key, gameObject);
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
|
|
|
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Reposition a light for this object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="light"></param>
|
|
|
|
/// <param name="offset"></param>
|
|
|
|
/// <param name="gameObject"></param>
|
2018-12-30 18:00:05 +08:00
|
|
|
public void repositionLight(LightSource light, Vector2 offset, StardewValley.Object gameObject)
|
2018-12-26 11:48:45 +08:00
|
|
|
{
|
|
|
|
Vector2 initialPosition = gameObject.TileLocation * Game1.tileSize;
|
|
|
|
light.position.Value = initialPosition + offset;
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
|
|
|
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Toggles the lights for this object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="location"></param>
|
|
|
|
/// <param name="gameObject"></param>
|
2018-12-30 18:00:05 +08:00
|
|
|
public virtual void toggleLights(GameLocation location, StardewValley.Object gameObject)
|
2018-12-26 11:48:45 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (!this.lightsOn)
|
2018-12-26 11:48:45 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.turnOnLights(location, gameObject);
|
|
|
|
this.lightsOn = true;
|
2019-05-18 00:43:50 +08:00
|
|
|
return;
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
else if (this.lightsOn)
|
2018-12-26 11:48:45 +08:00
|
|
|
{
|
|
|
|
this.turnOffLights(Game1.player.currentLocation);
|
2018-12-30 18:00:05 +08:00
|
|
|
this.lightsOn = false;
|
2019-05-18 00:43:50 +08:00
|
|
|
return;
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:43:50 +08:00
|
|
|
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Removes the lights from the world when this object needs to be cleaned up.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="loc"></param>
|
2019-05-18 00:43:50 +08:00
|
|
|
public virtual void removeForCleanUp(GameLocation loc)
|
|
|
|
{
|
|
|
|
this.turnOffLights(loc);
|
|
|
|
}
|
2019-05-18 02:04:16 +08:00
|
|
|
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Loads in the appropriate texture from sdv depending on the int value used.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value"></param>
|
|
|
|
/// <returns></returns>
|
2019-05-18 02:04:16 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-08-17 06:32:14 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a copy of all of the
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public LightManager Copy()
|
|
|
|
{
|
|
|
|
LightManager copy= new LightManager();
|
|
|
|
if (this.lights != null)
|
|
|
|
{
|
|
|
|
//ModCore.log("Info for file"+Path.GetFileNameWithoutExtension(file)+" has this many lights: " + info.info.lightManager.fakeLights.Count);
|
|
|
|
copy.lights.Clear();
|
|
|
|
foreach (KeyValuePair<Vector2, FakeLightSource> light in this.fakeLights)
|
|
|
|
{
|
|
|
|
Vector2 position = light.Value.positionOffset;
|
|
|
|
position -= light.Key;
|
|
|
|
position /= Game1.tileSize;
|
|
|
|
position = new Vector2((float)Math.Round(position.X), (float)Math.Round(position.Y));
|
|
|
|
copy.addLight(light.Key, new LightSource(light.Value.id, new Vector2(0, 0), light.Value.radius, light.Value.color.Invert()), position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2019-09-17 12:53:21 +08:00
|
|
|
public static LightSource CreateLightSource(float Radius, Color ActualColor,int Texture=4)
|
|
|
|
{
|
|
|
|
return new LightSource(Texture, new Vector2(0, 0), Radius, ActualColor.Invert());
|
|
|
|
}
|
2019-08-17 06:32:14 +08:00
|
|
|
|
2018-12-26 11:48:45 +08:00
|
|
|
}
|
|
|
|
}
|