2015-08-23 02:11:09 -04:00

130 lines
3.1 KiB
C#

using UnityEngine;
using System.Collections;
public class Bed : MonoBehaviour
{
// Values to balance out in playtesting
public float MinSpawnDelay = 7f;
public float MaxSpawnDelay = 15f;
public Pillow PillowObject;
public Vector3 RelativePosition = new Vector3(1.6f, 0.5f, 0f);
private bool _isTaken;
private Pillow _currentPillow;
private float _elapsedTime;
private float _nextSpawnDelay;
public bool IsTaken
{
get { return _isTaken; }
}
void Awake()
{
SpawnPillow();
_nextSpawnDelay = GetNextSpawnDelay();
}
void Update()
{
if (_currentPillow == null)
{
_elapsedTime += Time.deltaTime;
if (_elapsedTime >= _nextSpawnDelay)
{
_elapsedTime = 0f;
SpawnPillow();
_nextSpawnDelay = GetNextSpawnDelay();
}
}
}
private void SpawnPillow()
{
_currentPillow = Instantiate(PillowObject, transform.position, PillowObject.transform.rotation) as Pillow;
Vector3 rot = _currentPillow.transform.eulerAngles;
rot.y = transform.eulerAngles.y - 90f;
_currentPillow.transform.eulerAngles = rot;
Vector3 pos = new Vector3();
if (Mathf.Approximately(transform.eulerAngles.y, 0f))
{
pos = RelativePosition;
}
else if (Mathf.Approximately(transform.eulerAngles.y, 90f))
{
pos = new Vector3(-RelativePosition.z, RelativePosition.y, -RelativePosition.x);
}
else if (Mathf.Approximately(transform.eulerAngles.y, 180f))
{
pos = new Vector3(-RelativePosition.x, RelativePosition.y, -RelativePosition.z);
}
else if (Mathf.Approximately(transform.eulerAngles.y, 270f))
{
pos = new Vector3(RelativePosition.z, RelativePosition.y, RelativePosition.x);
}
_currentPillow.transform.position += pos;
}
private float GetNextSpawnDelay()
{
return UnityEngine.Random.Range(MinSpawnDelay, MaxSpawnDelay);
}
public void Take()
{
_isTaken = true;
}
public void Leave()
{
_isTaken = false;
}
void OnCollisionExit(Collision col)
{
Debug.Log(_currentPillow != null && col.gameObject == _currentPillow.gameObject);
if (_currentPillow != null && col.gameObject == _currentPillow.gameObject)
{
_currentPillow = null;
_elapsedTime = 0f;
}
}
/*
void OnCollisionEnter(Collision col)
{
// TODO: Check if the pillow is owned (otherwise it means the collision is only a player walking by)
if (col.gameObject.tag == "Pillow" && _currentPillow == null)
{
_currentPillow = col.gameObject.GetComponent<Pillow>();
}
}
void OnCollisionExit(Collision col)
{
if (_currentPillow != null && col.gameObject == _currentPillow.gameObject)
{
_currentPillow = null;
}
}*/
/*
void OnCollisionStay(Collision col)
{
if (col.gameObject.tag == "Player")
{
}
}*/
}