*
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class VisibilityZone : Zone {
|
||||
private GameObject obj;
|
||||
private float startupDepth;
|
||||
private Material startupMaterial;
|
||||
private bool startupVisibility;
|
||||
private SpriteRenderer renderer;
|
||||
private bool initialized;
|
||||
private bool hidden;
|
||||
public ZoneVisibilityControl control;
|
||||
|
||||
public Predicate[] visibilityPredicates;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
Init ();
|
||||
}
|
||||
|
||||
public void Init(){
|
||||
if(!initialized){
|
||||
obj = gameObject;
|
||||
startupDepth = obj.transform.position.z;
|
||||
renderer = obj.GetComponent<SpriteRenderer>();
|
||||
startupMaterial = renderer.sharedMaterial;
|
||||
startupVisibility = obj.activeSelf;
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsInside(Vector2 position){
|
||||
var b = renderer.bounds;
|
||||
|
||||
var r = new Rect(b.min.x, b.min.y, b.max.x - b.min.x, b.max.y - b.min.y);
|
||||
return r.Contains(position);
|
||||
//Vector3 tp = new Vector3(position.x, position.y, b.min.z);
|
||||
//return b.Contains(tp);
|
||||
}
|
||||
|
||||
public void Hide(Material hideMaterial, float hideDepth){
|
||||
hidden = true;
|
||||
var pos = obj.transform.position;
|
||||
pos.z = hideDepth;
|
||||
obj.transform.position = pos;
|
||||
renderer.sharedMaterial = hideMaterial;
|
||||
obj.SetActive(true);
|
||||
}
|
||||
|
||||
public void Show(){
|
||||
hidden = false;
|
||||
var pos = obj.transform.position;
|
||||
pos.z = startupDepth;
|
||||
obj.transform.position = pos;
|
||||
renderer.sharedMaterial = startupMaterial;
|
||||
obj.SetActive(startupVisibility);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
if (control == null) {
|
||||
return;
|
||||
}
|
||||
foreach(var p in visibilityPredicates){
|
||||
if(p != null && p.Check(this)){
|
||||
Show ();
|
||||
return;
|
||||
}
|
||||
}
|
||||
Hide (control.hideMaterial, control.hideDepth);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user