public void Start() { eventController.OnEnvChangeShapeEvent.AddListener(ResetAndCleanUp); Instance = this; tilemapCollider = GetComponent <CompositeCollider2D>(); shadowCasterContainer = shadow_caster_go; //shadowCasterContainer = GetComponent<ShadowCaster2D>().gameObject; for (int i = 0; i < tilemapCollider.pathCount; i++) { Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)]; tilemapCollider.GetPath(i, pathVertices); GameObject shadowCaster = new GameObject("shadow_caster_^" + i); shadowCasters.Add(shadowCaster); PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D)); shadowPolygons.Add(shadowPolygon); shadowCaster.transform.parent = shadowCasterContainer.transform; shadowPolygon.points = pathVertices; shadowPolygon.enabled = true; shadowPolygon.isTrigger = true; //if (shadowCaster.GetComponent<ShadowCaster2D>() != null) // remove existing caster? // Destroy(shadowCaster.GetComponent<ShadowCaster2D>()); ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>(); shadowCasterComponents.Add(shadowCasterComponent); shadowCasterComponent.selfShadows = true; } }
public void Start() { Instance = this; tilemapCollider = GetComponent <CompositeCollider2D>(); shadowCasterContainer = new GameObject("Shadow Casters"); Vector3 lP = shadowCasterContainer.transform.localPosition; shadowCasterContainer.transform.parent = gameObject.transform; shadowCasterContainer.transform.localPosition = lP; for (int i = 0; i < tilemapCollider.pathCount; i++) { Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)]; tilemapCollider.GetPath(i, pathVertices); GameObject shadowCaster = new GameObject("shadow_caster_" + i); shadowCasters.Add(shadowCaster); PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D)); shadowPolygons.Add(shadowPolygon); lP = shadowCaster.transform.localPosition; shadowCaster.transform.parent = shadowCasterContainer.transform; shadowCaster.transform.localPosition = lP; shadowPolygon.points = pathVertices; shadowPolygon.enabled = false; //if (shadowCaster.GetComponent<ShadowCaster2D>() != null) // remove existing caster? // Destroy(shadowCaster.GetComponent<ShadowCaster2D>()); ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>(); shadowCasterComponents.Add(shadowCasterComponent); shadowCasterComponent.selfShadows = selfShadows; } }
public void Generate() { DestroyAllChildren(); tilemapCollider = GetComponent <CompositeCollider2D>(); for (int i = 0; i < tilemapCollider.pathCount; i++) { Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)]; tilemapCollider.GetPath(i, pathVertices); GameObject shadowCaster = new GameObject("shadow_caster_" + i); shadowCaster.transform.parent = gameObject.transform; ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>(); shadowCasterComponent.selfShadows = this.selfShadows; Vector3[] testPath = new Vector3[pathVertices.Length]; for (int j = 0; j < pathVertices.Length; j++) { testPath[j] = pathVertices[j]; } shapePathField.SetValue(shadowCasterComponent, testPath); meshField.SetValue(shadowCasterComponent, new Mesh()); generateShadowMeshMethod.Invoke(shadowCasterComponent, new object[] { meshField.GetValue(shadowCasterComponent), shapePathField.GetValue(shadowCasterComponent) }); } // Debug.Log("Generate"); }
private void Reset() { toDelete = new List <GameObject>(shadowCasters); shadowCasters.Clear(); shadowPolygons.Clear(); shadowCasterComponents.Clear(); for (int i = 0; i < tilemapCollider.pathCount; i++) { Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)]; tilemapCollider.GetPath(i, pathVertices); GameObject shadowCaster = new GameObject("shadow_caster_" + i); shadowCasters.Add(shadowCaster); PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D)); shadowPolygons.Add(shadowPolygon); Vector3 lP = shadowCaster.transform.localPosition; shadowCaster.transform.parent = shadowCasterContainer.transform; shadowCaster.transform.localPosition = lP; shadowPolygon.points = pathVertices; shadowPolygon.enabled = false; //if (shadowCaster.GetComponent<ShadowCaster2D>() != null) // remove existing caster? // Destroy(shadowCaster.GetComponent<ShadowCaster2D>()); ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>(); shadowCasterComponents.Add(shadowCasterComponent); shadowCasterComponent.selfShadows = selfShadows; } doCleanup = true; }
/// <summary> /// Replaces the hash key of the shadow caster, which produces an internal data rebuild. /// </summary> /// <remarks> /// A change in the shape of the shadow caster will not block the light, it has to be rebuilt using this function. /// </remarks> /// <param name="shadowCaster">The object to modify.</param> /// <param name="hash">The new hash key to store. It must be different from the previous key to produce the rebuild. You can use a random number.</param> public static void SetPathHash(this ShadowCaster2D shadowCaster, int hash) { FieldInfo hashField = typeof(ShadowCaster2D).GetField("m_ShapePathHash", BindingFlags.NonPublic | BindingFlags.Instance); hashField.SetValue(shadowCaster, hash); }
/// <summary> /// Replaces the path that defines the shape of the shadow caster. /// </summary> /// <remarks> /// Calling this method will change the shape but not the mesh of the shadow caster. Call SetPathHash afterwards. /// </remarks> /// <param name="shadowCaster">The object to modify.</param> /// <param name="path">The new path to define the shape of the shadow caster.</param> public static void SetPath(this ShadowCaster2D shadowCaster, Vector3[] path) { FieldInfo shapeField = typeof(ShadowCaster2D).GetField("m_ShapePath", BindingFlags.NonPublic | BindingFlags.Instance); shapeField.SetValue(shadowCaster, path); }
void Awake() { rb = GetComponent <Rigidbody2D>(); coll = GetComponent <Collider2D>(); sprite = GetComponentInChildren <SpriteRenderer>(); shadowCaster2D = GetComponentInChildren <ShadowCaster2D>(); myMaterial = sprite.material; }
public static void ShadowCasterReset(ShadowCaster2D shadowCaster) { FieldInfo field = typeof(ShadowCaster2D).GetField("m_Mesh", BindingFlags.NonPublic | BindingFlags.Instance); field.SetValue(shadowCaster, null); MethodInfo method = typeof(ShadowCaster2D).GetMethod("Awake", BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(shadowCaster, new object[] { }); method = typeof(ShadowCaster2D).GetMethod("OnEnable", BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(shadowCaster, new object[] { }); }
private void Start() { torchesCollected = hitPoints; playerTorch = GetComponentInChildren <Light2D>(); shadowCaster = GetComponent <ShadowCaster2D>(); torchFX = GetComponentInChildren <TorchFX>(); action = GetComponent <PlayerAction>(); torchCounter = FindObjectOfType <TorchCounter>(); gameController = FindObjectOfType <GameController>(); audioSource = GetComponent <AudioSource>(); torchCounter.SetText(hitPoints, torchesCollected); }
private void Awake() { tileRenderer = GetComponent <SpriteRenderer>(); tileCollider = GetComponent <BoxCollider2D>(); tileShadowCaster = GetComponent <ShadowCaster2D>(); GrabbableBody = GetComponent <Rigidbody2D>(); lifeWait = new WaitForSeconds(lifeSpan); fadeWait = new WaitUntil(() => { fadeElapsed = Mathf.Clamp(fadeElapsed + Time.deltaTime, 0, fadeOutTime); tileRenderer.material.SetFloat("_DissolveFade", 1 - (fadeElapsed / fadeOutTime)); return(fadeElapsed == fadeOutTime); }); }
private void Awake() { _startTransform = _doorSprite; _selfShadow = gameObject.GetComponent <ShadowCaster2D>(); if (_leftOpening) { _openRotation = new Vector3(0, 180, 0); } else { _openRotation = Vector3.zero; } _playerEvent = FindObjectOfType <PlayerEvent>(); }
public void Start() { var tilemapCollider = GetComponent <CompositeCollider2D>(); for (int i = 0; i < tilemapCollider.pathCount; i++) { Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)]; tilemapCollider.GetPath(i, pathVertices); GameObject shadowCaster = new GameObject("shadow_caster_" + i); PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D)); shadowPolygon.points = pathVertices; shadowPolygon.enabled = false; ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>(); shadowCasterComponent.selfShadows = true; } }
protected override void Disappear() { Debug.Log("Disappear"); GameManager.instance.DisablePlayer(); agent.enabled = false; state = EnemyState.Unactive; gameObject.tag = "Untagged"; shadow = GetComponent <ShadowCaster2D>(); if (shadow != null) { shadow.enabled = false; } Heartbeat.instance?.ScanEnemy(); GameManager.instance?.LosePlayer(); StartCoroutine(GFXVanishing()); }
public bool HasRenderer() { if (targets != null) { for (int i = 0; i < targets.Length; i++) { ShadowCaster2D shadowCaster = (ShadowCaster2D)targets[i]; Renderer renderer = shadowCaster.GetComponent <Renderer>(); if (renderer != null) { return(true); } } } return(false); }
private void Generate() { if (Caster.compositeCollider == null) { Debug.Log("You have to asign a CompositeCollider2D"); return; } for (int casterIndex = 0; casterIndex < Caster.shadowCasters.Count || casterIndex < Caster.compositeCollider.pathCount; casterIndex++) { if (casterIndex >= Caster.shadowCasters.Count) { GameObject go = new GameObject("Shadow Caster " + casterIndex); go.transform.SetParent(Caster.transform); Caster.shadowCasters.Add(go.AddComponent <ShadowCaster2D>()); } ShadowCaster2D shadowCaster = Caster.shadowCasters[casterIndex]; if (casterIndex < Caster.compositeCollider.pathCount) { SerializedObject shadowO = new SerializedObject(shadowCaster); shadowO.Update(); SerializedProperty shapePathProp = shadowO.FindProperty("m_ShapePath"); Vector2[] points = new Vector2[Caster.compositeCollider.GetPathPointCount(casterIndex)]; Caster.compositeCollider.GetPath(casterIndex, points); shapePathProp.ClearArray(); for (int i = points.Length - 1; i >= 0; i--) { shapePathProp.InsertArrayElementAtIndex(0); shapePathProp.GetArrayElementAtIndex(0).vector3Value = points[i]; } shadowO.ApplyModifiedProperties(); } else { Caster.shadowCasters.RemoveAt(casterIndex); DestroyImmediate(shadowCaster.gameObject); } } }
public virtual void Initialize() { triggered = false; npcTriggered = false; collider = GetComponent <Collider2D>(); shadow = GetComponent <ShadowCaster2D>(); colliders.Clear(); if (triggerEnabled) { Activate(); } else { Deactivate(); } }
/// <summary> /// Given a Composite Collider 2D, it replaces existing Shadow Caster 2Ds (children) with new Shadow Caster 2D objects whose /// shapes coincide with the paths of the collider. /// </summary> /// <remarks> /// It is recommended that the object that contains the collider component has a Composite Shadow Caster 2D too. /// It is recommended to call this method in editor only. /// </remarks> /// <param name="collider">The collider which will be the parent of the new shadow casters.</param> /// <param name="selfShadows">Whether the shadow casters will have the Self Shadows option enabled..</param> public static void GenerateTilemapShadowCasters(CompositeCollider2D collider, bool selfShadows) { // First, it destroys the existing shadow casters ShadowCaster2D[] existingShadowCasters = collider.GetComponentsInChildren <ShadowCaster2D>(); for (int i = 0; i < existingShadowCasters.Length; ++i) { if (existingShadowCasters[i].transform.parent != collider.transform) { continue; } GameObject.DestroyImmediate(existingShadowCasters[i].gameObject); } // Then it creates the new shadow casters, based on the paths of the composite collider int pathCount = collider.pathCount; List <Vector2> pointsInPath = new List <Vector2>(); List <Vector3> pointsInPath3D = new List <Vector3>(); for (int i = 0; i < pathCount; ++i) { collider.GetPath(i, pointsInPath); GameObject newShadowCaster = new GameObject("ShadowCaster2D"); newShadowCaster.isStatic = true; newShadowCaster.transform.SetParent(collider.transform, false); for (int j = 0; j < pointsInPath.Count; ++j) { pointsInPath3D.Add(pointsInPath[j]); } ShadowCaster2D component = newShadowCaster.AddComponent <ShadowCaster2D>(); component.SetPath(pointsInPath3D.ToArray()); component.SetPathHash(Random.Range(int.MinValue, int.MaxValue)); // The hashing function GetShapePathHash could be copied from the LightUtility class component.selfShadows = selfShadows; component.Update(); pointsInPath.Clear(); pointsInPath3D.Clear(); } }
public void ShadowCaster2DSceneGUI() { ShadowCaster2D shadowCaster = target as ShadowCaster2D; Transform t = shadowCaster.transform; Vector3[] shape = shadowCaster.shapePath; Handles.color = Color.white; for (int i = 0; i < shape.Length - 1; ++i) { Handles.DrawAAPolyLine(4, new Vector3[] { t.TransformPoint(shape[i]), t.TransformPoint(shape[i + 1]) }); } if (shape.Length > 1) { Handles.DrawAAPolyLine(4, new Vector3[] { t.TransformPoint(shape[shape.Length - 1]), t.TransformPoint(shape[0]) }); } }
public void AddShadows() { int sortingLayerId = GetComponent <TilemapRenderer>().sortingLayerID; for (int i = 0; i < tilemapCollider.pathCount; i++) { Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)]; tilemapCollider.GetPath(i, pathVertices); GameObject shadowCaster = new GameObject("shadow_caster_" + i); shadowCaster.layer = gameObject.layer; PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D)); shadowCaster.transform.parent = shadowCasterContainer.transform; //OffsetVertices(pathVertices); shadowPolygon.points = OffsetVertices(pathVertices); shadowPolygon.enabled = false; ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>(); shadowCasterComponent.selfShadows = false; shadowCasterComponent.m_ApplyToSortingLayers = new int[] { sortingLayerId }; } }
internal Bounds GetBounds() { ShadowCaster2D shadowCaster = (ShadowCaster2D)owner; Renderer m_Renderer = shadowCaster.GetComponent <Renderer>(); if (m_Renderer != null) { return(m_Renderer.bounds); } else { Collider2D collider = shadowCaster.GetComponent <Collider2D>(); if (collider != null) { return(collider.bounds); } } return(new Bounds(shadowCaster.transform.position, shadowCaster.transform.lossyScale)); }
private void Awake() { shadowCaster = GetComponent <ShadowCaster2D>(); edgeCollider = GetComponent <EdgeCollider2D>(); polyCollider = GetComponent <PolygonCollider2D>(); if (!setOnAwake) { return; } if (edgeCollider != null) { UpdateShadowFromPoints(edgeCollider.points); } else if (polyCollider != null) { UpdateShadowFromPoints(polyCollider.points); } }
void Start() { var tilemap = GetComponent <Tilemap>(); GameObject shadowCasterContainer = GameObject.Find("shadow_casters"); int i = 0; foreach (var position in tilemap.cellBounds.allPositionsWithin) { if (tilemap.GetTile(position) == null) { continue; } GameObject newShadowCaster = new GameObject("ShadowCaster2D"); newShadowCaster.isStatic = true; newShadowCaster.transform.SetParent(shadowCasterContainer.transform, false); ShadowCaster2D component = newShadowCaster.AddComponent <ShadowCaster2D>(); newShadowCaster.transform.position = new Vector3(position.x + 0.5f, position.y + 0.5f); i++; } }
/// <summary> /// Given a Composite Collider 2D, it replaces existing Shadow Caster 2Ds (children) with new Shadow Caster 2D objects whose /// shapes coincide with the paths of the collider. /// </summary> /// <remarks> /// It is recommended that the object that contains the collider component has a Composite Shadow Caster 2D too. /// It is recommended to call this method in editor only. /// </remarks> /// <param name="collider">The collider which will be the parent of the new shadow casters.</param> private static void GenerateTilemapShadowCasters(CompositeCollider2D collider) { // First, it destroys the existing shadow casters ShadowCaster2D[] existingShadowCasters = collider.GetComponentsInChildren <ShadowCaster2D>(); foreach (ShadowCaster2D existingShadowCaster in existingShadowCasters) { Object.DestroyImmediate(existingShadowCaster.gameObject); } // Then it creates the new shadow casters, based on the paths of the composite collider int pathCount = collider.pathCount; List <Vector2> pointsInPath = new List <Vector2>(); List <Vector3> pointsInPath3D = new List <Vector3>(); for (int i = 0; i < pathCount; ++i) { collider.GetPath(i, pointsInPath); GameObject newShadowCaster = new GameObject("ShadowCaster2D") { isStatic = true }; newShadowCaster.transform.SetParent(collider.transform, false); pointsInPath3D.AddRange(pointsInPath.Select(pointInPath => (Vector3)pointInPath)); ShadowCaster2D component = newShadowCaster.AddComponent <ShadowCaster2D>(); component.selfShadows = true; component.SetPath(pointsInPath3D.ToArray()); component.SetPathHash(Random.Range(int.MinValue, int.MaxValue)); // The hashing function GetShapePathHash could be copied from the LightUtility class pointsInPath.Clear(); pointsInPath3D.Clear(); } UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty(); }
protected override void SetShape(ScriptablePath shapeEditor, SerializedObject serializedObject) { serializedObject.Update(); var pointsProperty = serializedObject.FindProperty(k_ShapePath); pointsProperty.arraySize = shapeEditor.pointCount; for (var i = 0; i < shapeEditor.pointCount; ++i) { pointsProperty.GetArrayElementAtIndex(i).vector3Value = shapeEditor.GetPoint(i).position; } // This is untracked right now... serializedObject.ApplyModifiedProperties(); ShadowCaster2D shadowCaster = target as ShadowCaster2D; if (shadowCaster != null) { int hash = LightUtility.GetShapePathHash(shadowCaster.shapePath); shadowCaster.shapePathHash = hash; } }
public void GenerateShadowCasters(bool clearExisting) { if (clearExisting) { // Clear existing shadow casters if (Application.isPlaying) { foreach (Transform child in shadowCasterContainer.transform) { Destroy(child.gameObject); } } else { while (shadowCasterContainer.transform.childCount != 0) { DestroyImmediate(shadowCasterContainer.transform.GetChild(0).gameObject); } } } previousPointCount = tilemapCollider.pointCount; for (int i = 0; i < tilemapCollider.pathCount; i++) { Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)]; tilemapCollider.GetPath(i, pathVertices); GameObject shadowCaster = new GameObject(ShadowCasterContainerName + "_" + i); PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D)); shadowCaster.transform.parent = shadowCasterContainer.transform; shadowPolygon.points = pathVertices; shadowPolygon.enabled = false; ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <ShadowCaster2D>(); shadowCasterComponent.selfShadows = true; } }
public static void SetShadowCasterPath(ShadowCaster2D shadowCaster, Vector3[] shadowPath) { FieldInfo field = typeof(ShadowCaster2D).GetField("m_ShapePath", BindingFlags.NonPublic | BindingFlags.Instance); field.SetValue(shadowCaster, shadowPath); }
public void GenerateShadowCasters() { if (Tilemap == null) { Tilemap = GetComponent <Tilemap>(); } if (TilemapRenderer == null) { TilemapRenderer = GetComponent <TilemapRenderer>(); } DestroyPreviouslyGenerated(); filledTiles.Clear(); filledTiles.TrimExcess(); foreach (Vector3Int pos in Tilemap.cellBounds.allPositionsWithin) { Vector3Int localPlace = new Vector3Int(pos.x, pos.y, pos.z); if (Tilemap.HasTile(localPlace)) { Vector3 place = Tilemap.GetCellCenterWorld(localPlace); filledTiles.Add(place); } } Debug.Log("number of tiles in tilemap: " + filledTiles.Count); int n = 0; for (int i = 0; i < filledTiles.Count; i++) { if (IsEdgeTile((filledTiles[i]))) { n++; GameObject casterGO = (GameObject)PrefabUtility.InstantiatePrefab(ShadowCastersPrefab); casterGO.transform.localScale = Vector3.Scale(casterGO.transform.localScale, Tilemap.cellSize); casterGO.transform.position = filledTiles[i]; casterGO.transform.SetParent(gameObject.transform); ShadowCaster2D caster = casterGO.GetComponent <ShadowCaster2D>(); SpriteRenderer casterSR = casterGO.GetComponent <SpriteRenderer>(); casterSR.sortingLayerID = TilemapRenderer.sortingLayerID; // Add all sorting layers below the caster's to its target layers. List <int> targetLayers = new List <int>(); bool isAbove = true; for (int j = SortingLayer.layers.Length - 1; j >= 0; j--) { if (isAbove) { if (casterSR.sortingLayerID == SortingLayer.layers[j].id) { isAbove = false; } } else { targetLayers.Add(j); } } ShadowCasters.Add(casterGO); NameShadowCaster(ref casterGO, casterSR.sortingLayerName); } } Debug.Log(n + " shadow casters generated"); }
// Start is called before the first frame update void Start() { shadowCaster = this.GetComponent <ShadowCaster2D>(); SnapShadowCaster(); }
// Start is called before the first frame update void Start() { shadowCaster2D = gameObject.GetComponent <ShadowCaster2D>(); }
public static Vector3[] GetShadowCasterPath(ShadowCaster2D shadowCaster) { FieldInfo field = typeof(ShadowCaster2D).GetField("m_ShapePath", BindingFlags.NonPublic | BindingFlags.Instance); return((Vector3[])field.GetValue(shadowCaster)); }