Restrict the bed usage to only one child per bed

This commit is contained in:
Patrice Vignola 2015-08-22 09:40:30 -04:00
parent a030642c8d
commit e76e32efe7
6 changed files with 81 additions and 7 deletions

BIN
Assets/Prefabs/Bed.prefab Normal file

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9f2c38d0d3154bc4d8148a949b98af28
timeCreated: 1440250095
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

22
Assets/Scripts/Bed.cs Normal file
View File

@ -0,0 +1,22 @@
using UnityEngine;
using System.Collections;
public class Bed : MonoBehaviour
{
private bool _isTaken;
public bool IsTaken
{
get { return _isTaken; }
}
public void Take()
{
_isTaken = true;
}
public void Leave()
{
_isTaken = false;
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 49fab454291c16d4185f7f6a511de185
timeCreated: 1440250042
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -6,6 +6,7 @@ public class Child : MonoBehaviour
{ {
public float Speed = 10f; public float Speed = 10f;
public float JumpForce = 10f; public float JumpForce = 10f;
public float MaxInvulnerableTime = 2f;
public GameObject GroundCheck; public GameObject GroundCheck;
public Pillow pillow; public Pillow pillow;
public MomBehavior Mom; public MomBehavior Mom;
@ -15,6 +16,8 @@ public class Child : MonoBehaviour
private float _xValue; private float _xValue;
private float _zValue; private float _zValue;
private bool _isSleeping; private bool _isSleeping;
private float _invulnerableTime;
private Bed _currentBed;
public Transform target; public Transform target;
private int _index; private int _index;
@ -108,11 +111,15 @@ public class Child : MonoBehaviour
public bool Sleep() public bool Sleep()
{ {
_isSleeping = IsOnBed(); Bed bed = GetBed();
// Temporary (only for visual cue until we get the animation) if (bed != null && !bed.IsTaken)
if (_isSleeping)
{ {
_currentBed = bed;
bed.Take();
_isSleeping = true;
// Temporary (only for visual cue until we get the animation)
transform.localEulerAngles = new Vector3(90f, transform.localEulerAngles.y, transform.localEulerAngles.z); transform.localEulerAngles = new Vector3(90f, transform.localEulerAngles.y, transform.localEulerAngles.z);
} }
@ -123,23 +130,48 @@ public class Child : MonoBehaviour
{ {
_isSleeping = false; _isSleeping = false;
_currentBed.Leave();
_currentBed = null;
// Temporary (only for visual cue until we get the animation) // Temporary (only for visual cue until we get the animation)
transform.localEulerAngles = new Vector3(0f, transform.localEulerAngles.y, transform.localEulerAngles.z); transform.localEulerAngles = new Vector3(0f, transform.localEulerAngles.y, transform.localEulerAngles.z);
} }
private bool IsOnBed() private Bed GetBed()
{ {
Collider[] colliders = Physics.OverlapSphere(GroundCheck.transform.position, 0.149f, 1 << LayerMask.NameToLayer("Bed")); Collider[] colliders = Physics.OverlapSphere(GroundCheck.transform.position, 0.149f, 1 << LayerMask.NameToLayer("Bed"));
return colliders.Length > 0; return colliders.Length > 0 ? colliders[0].GetComponent<Bed>() : null;
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Lava")
{
Debug.Log("Player " + _index + " entered lava. Lose one life.");
TakeLavaDamage();
}
} }
void OnCollisionStay(Collision collision) void OnCollisionStay(Collision collision)
{ {
if (collision.gameObject.tag == "Lava") if (collision.gameObject.tag == "Lava")
{ {
// TODO: Lose a life (probably) and become immune for ~ 2 or 3 seconds _invulnerableTime += Time.deltaTime;
Debug.Log("Player " + _index + " is standing on lava.");
if (_invulnerableTime >= MaxInvulnerableTime)
{
Debug.Log("Player " + _index + " is still standing on lava. Lose one life.");
TakeLavaDamage();
}
} }
} }
private void TakeLavaDamage()
{
// TODO: Lose a life (probably) and become immune for ~ 2 or 3 seconds
_invulnerableTime = 0f;
}
} }