/* * Copyright 2022 Peter Han * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace PeterHan.PLib.Lighting { /// /// Arguments which are passed to lighting callbacks to perform lighting calculations. /// /// The range is the light radius supplied during the Light2D creation; do not light up /// tiles outside of this radius (measured by a square around SourceCell)! /// /// The source cell is the cell nearest to where the Light2D is currently located. /// /// Use the IDictionary interface to store the relative brightness of cells by their cell /// location. These values should be between 0 and 1 normally, with the maximum brightness /// being set by the intensity parameter of the Light2D. The user is responsible for /// ensuring that cells are valid before lighting them up. /// public sealed class LightingArgs : EventArgs, IDictionary { /// /// The location where lighting results are stored. /// public IDictionary Brightness { get; } /// /// The maximum range to use for cell lighting. Do not light up cells beyond this /// range from SourceCell. /// public int Range { get; } /// /// The source of the light. /// public GameObject Source { get; } /// /// The originating cell. Actual lighting can begin elsewhere, but the range limit is /// measured from this cell. /// public int SourceCell { get; } internal LightingArgs(GameObject source, int cell, int range, IDictionary output) { if (source == null) // Cannot use "throw" expression because of UnityEngine.Object.operator== throw new ArgumentNullException(nameof(source)); Brightness = output ?? throw new ArgumentNullException(nameof(output)); Range = range; Source = source; SourceCell = cell; } #region IDictionary public ICollection Keys => Brightness.Keys; public ICollection Values => Brightness.Values; public int Count => Brightness.Count; public bool IsReadOnly => Brightness.IsReadOnly; public float this[int key] { get => Brightness[key]; set => Brightness[key] = value; } public bool ContainsKey(int key) { return Brightness.ContainsKey(key); } public void Add(int key, float value) { Brightness.Add(key, value); } public bool Remove(int key) { return Brightness.Remove(key); } public bool TryGetValue(int key, out float value) { return Brightness.TryGetValue(key, out value); } public void Add(KeyValuePair item) { Brightness.Add(item); } public void Clear() { Brightness.Clear(); } public bool Contains(KeyValuePair item) { return Brightness.Contains(item); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { Brightness.CopyTo(array, arrayIndex); } public bool Remove(KeyValuePair item) { return Brightness.Remove(item); } public IEnumerator> GetEnumerator() { return Brightness.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return Brightness.GetEnumerator(); } #endregion public override string ToString() { return string.Format("LightingArgs[source={0:D},range={1:D}]", SourceCell, Range); } } }