2019-10-26 23:08:40 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using SanAndreasUnity.Importing.Items.Placements;
|
2019-10-27 00:43:08 +00:00
|
|
|
|
using SanAndreasUnity.Utilities;
|
2019-10-27 01:07:08 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-07-18 04:03:43 +00:00
|
|
|
|
using System.Linq;
|
2019-10-26 23:08:40 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Behaviours.World
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class EntranceExitMapObject : MapObject
|
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
static List<EntranceExitMapObject> _sAllActiveObjects = new List<EntranceExitMapObject>();
|
|
|
|
|
public static IReadOnlyList<EntranceExitMapObject> AllActiveObjects => _sAllActiveObjects;
|
2019-10-27 01:07:08 +00:00
|
|
|
|
|
2019-10-27 00:43:08 +00:00
|
|
|
|
Coroutine m_animateArrowCoroutine;
|
2019-10-27 01:07:08 +00:00
|
|
|
|
|
2019-10-27 00:43:08 +00:00
|
|
|
|
public float minArrowPos = -1f;
|
|
|
|
|
public float maxArrowPos = 1f;
|
|
|
|
|
public Transform arrowTransform;
|
|
|
|
|
public float arrowMoveSpeed = 0.3f;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-26 23:08:40 +00:00
|
|
|
|
public static EntranceExitMapObject Create(EntranceExit info)
|
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
var obj = Create<EntranceExitMapObject>(Cell.Instance.enexPrefab);
|
2019-10-26 23:08:40 +00:00
|
|
|
|
obj.Initialize(info);
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EntranceExit Info { get; private set; }
|
|
|
|
|
|
2022-01-24 17:07:00 +00:00
|
|
|
|
public void Initialize(EntranceExit info)
|
2019-10-26 23:08:40 +00:00
|
|
|
|
{
|
|
|
|
|
Info = info;
|
|
|
|
|
|
|
|
|
|
name = string.Format("ENEX ({0})", info.Name);
|
|
|
|
|
|
2019-10-27 00:09:58 +00:00
|
|
|
|
float height = 2f;
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.Initialize(
|
|
|
|
|
Cell.Instance.GetPositionBasedOnInteriorLevel(
|
|
|
|
|
info.EntrancePos + Vector3.up * height * 0.5f,
|
|
|
|
|
info.TargetInterior),
|
|
|
|
|
Quaternion.identity);
|
2019-10-26 23:08:40 +00:00
|
|
|
|
|
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
gameObject.isStatic = true;
|
|
|
|
|
|
2019-10-27 00:04:33 +00:00
|
|
|
|
// collider
|
|
|
|
|
var collider = gameObject.GetComponent<BoxCollider>();
|
2019-10-27 00:09:58 +00:00
|
|
|
|
collider.size = new Vector3(info.Size.x, height, info.Size.y);
|
2019-10-26 23:08:40 +00:00
|
|
|
|
collider.isTrigger = true;
|
|
|
|
|
|
|
|
|
|
// need rigid body for detecting collisions
|
2019-10-27 00:04:33 +00:00
|
|
|
|
var rb = gameObject.GetComponent<Rigidbody>();
|
2019-10-26 23:08:40 +00:00
|
|
|
|
rb.mass = 0f;
|
|
|
|
|
rb.isKinematic = true;
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
this.SetDrawDistance(100f);
|
|
|
|
|
|
2019-10-26 23:08:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 00:43:08 +00:00
|
|
|
|
void OnEnable()
|
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
_sAllActiveObjects.Add(this);
|
2019-10-27 00:43:08 +00:00
|
|
|
|
m_animateArrowCoroutine = this.StartCoroutine(this.AnimateArrow());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnDisable()
|
|
|
|
|
{
|
2021-07-18 04:03:43 +00:00
|
|
|
|
_sAllActiveObjects.Remove(this);
|
2019-10-27 01:07:08 +00:00
|
|
|
|
|
2019-10-27 00:43:08 +00:00
|
|
|
|
if (m_animateArrowCoroutine != null)
|
|
|
|
|
this.StopCoroutine(m_animateArrowCoroutine);
|
|
|
|
|
m_animateArrowCoroutine = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-10-26 23:08:40 +00:00
|
|
|
|
void OnDrawGizmosSelected()
|
|
|
|
|
{
|
|
|
|
|
Utilities.F.HandlesDrawText(this.transform.position, this.name, Color.yellow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnLoad()
|
|
|
|
|
{
|
2019-10-30 22:14:03 +00:00
|
|
|
|
//Debug.LogFormat("OnLoad() - {0}", this.gameObject.name);
|
2019-10-26 23:08:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnShow()
|
|
|
|
|
{
|
2019-10-30 22:14:03 +00:00
|
|
|
|
//Debug.LogFormat("OnShow() - {0}", this.gameObject.name);
|
2019-10-26 23:08:40 +00:00
|
|
|
|
this.gameObject.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnTriggerEnter(Collider collider)
|
|
|
|
|
{
|
2019-10-30 22:14:03 +00:00
|
|
|
|
//Debug.LogFormat("OnTriggerEnter() - with {0}", collider.gameObject.name);
|
2019-11-20 12:56:06 +00:00
|
|
|
|
|
|
|
|
|
var ped = collider.gameObject.GetComponent<Ped>();
|
|
|
|
|
if (ped != null)
|
|
|
|
|
ped.OnStartCollidingWithEnex(this);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnTriggerExit(Collider collider)
|
|
|
|
|
{
|
|
|
|
|
//Debug.LogFormat("OnTriggerExit() - with {0}", collider.gameObject.name);
|
|
|
|
|
|
|
|
|
|
var ped = collider.gameObject.GetComponent<Ped>();
|
|
|
|
|
if (ped != null)
|
|
|
|
|
ped.OnStopCollidingWithEnex(this);
|
|
|
|
|
|
2019-10-26 23:08:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 00:43:08 +00:00
|
|
|
|
System.Collections.IEnumerator AnimateArrow()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// place arrow at center
|
|
|
|
|
float center = (this.minArrowPos + this.maxArrowPos) * 0.5f;
|
|
|
|
|
this.arrowTransform.localPosition = this.arrowTransform.localPosition.WithY(center);
|
|
|
|
|
|
|
|
|
|
yield return null;
|
|
|
|
|
|
|
|
|
|
// move arrow up/down
|
|
|
|
|
|
|
|
|
|
// set initial sign (direction of movement)
|
|
|
|
|
float sign = Mathf.Sign(Random.Range(-1f, 1f));
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
float y = this.arrowTransform.localPosition.y;
|
|
|
|
|
y += sign * this.arrowMoveSpeed * Time.deltaTime;
|
|
|
|
|
if (y >= this.maxArrowPos || y <= this.minArrowPos)
|
|
|
|
|
{
|
|
|
|
|
// clamp
|
|
|
|
|
y = Mathf.Clamp(y, this.minArrowPos, this.maxArrowPos);
|
|
|
|
|
// flip direction
|
|
|
|
|
sign = - sign;
|
|
|
|
|
}
|
|
|
|
|
this.arrowTransform.localPosition = this.arrowTransform.localPosition.WithY(y);
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 04:03:43 +00:00
|
|
|
|
public EntranceExit FindMatchingEnex()
|
|
|
|
|
{
|
|
|
|
|
EntranceExit firstMatchingWithDifferentInterior = null;
|
|
|
|
|
EntranceExit firstMatchingWithSameInterior = null;
|
|
|
|
|
|
|
|
|
|
foreach (var enex in Importing.Items.Item.Enexes)
|
|
|
|
|
{
|
|
|
|
|
if (firstMatchingWithDifferentInterior != null && firstMatchingWithSameInterior != null) // both are found
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (enex.Name != this.Info.Name)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (enex == this.Info)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (null == firstMatchingWithDifferentInterior && enex.TargetInterior != this.Info.TargetInterior)
|
|
|
|
|
firstMatchingWithDifferentInterior = enex;
|
|
|
|
|
|
|
|
|
|
if (null == firstMatchingWithSameInterior && enex.TargetInterior == this.Info.TargetInterior)
|
|
|
|
|
firstMatchingWithSameInterior = enex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return firstMatchingWithDifferentInterior ?? firstMatchingWithSameInterior;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 23:08:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|