updates to scene. Working on pickup
This commit is contained in:
102
Assets/Scripts/Item/DoorInteractable.cs
Normal file
102
Assets/Scripts/Item/DoorInteractable.cs
Normal file
@ -0,0 +1,102 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class DoorInteractable : InteractableItem
|
||||
{
|
||||
[SerializeField]
|
||||
private Transform powerCoreCenter;
|
||||
private HeavyInteractableItem insertedCore;
|
||||
private Vector3 priorLocalPos;
|
||||
private Vector3 priorLocalRot;
|
||||
private Vector3 priorScale;
|
||||
|
||||
[SerializeField]
|
||||
private float minAttractDist = 5;
|
||||
[SerializeField]
|
||||
private string nameSearched = "Power Core";
|
||||
|
||||
public bool Powered { get { return this.insertedCore!= null; } }
|
||||
[SerializeField]
|
||||
private Animator anim;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
anim.SetBool("IsPowered", Powered);
|
||||
}
|
||||
public override bool Interact()
|
||||
{
|
||||
//print("INTERACTED!");
|
||||
if(this.insertedCore== null)
|
||||
{
|
||||
HeavyInteractableItem[] worldHeavyItems = GameObject.FindObjectsOfType<HeavyInteractableItem>();
|
||||
//print("Found:" + worldHeavyItems.Length);
|
||||
for(int i = 0; i < worldHeavyItems.Length; i++)
|
||||
{
|
||||
HeavyInteractableItem item = worldHeavyItems[i];
|
||||
|
||||
if (!item.ItemName.Contains(nameSearched))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
float dist = Vector3.Distance(item.transform.position, powerCoreCenter.transform.position);
|
||||
//print("DIST:" + dist);
|
||||
if (dist <= minAttractDist)
|
||||
{
|
||||
Inventory _i = PlayerInteractionHandler.instance.Inventory;
|
||||
this.Interact(ref _i, ref item);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
public override bool Interact(ref Inventory inventory,ref HeavyInteractableItem heavyInvent)
|
||||
{
|
||||
//print("INTERACTED 2");
|
||||
// print(heavyInvent);
|
||||
|
||||
if (heavyInvent!=null&&heavyInvent.ItemName.Contains(nameSearched))
|
||||
{
|
||||
//print("DOOR OPEN!");
|
||||
heavyInvent.GetComponent<Rigidbody>().isKinematic = true;
|
||||
|
||||
heavyInvent.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
|
||||
priorLocalPos = heavyInvent.transform.localPosition;
|
||||
priorLocalRot = heavyInvent.transform.localEulerAngles;
|
||||
priorScale = heavyInvent.transform.localScale;
|
||||
|
||||
heavyInvent.transform.parent = powerCoreCenter;
|
||||
|
||||
heavyInvent.gameObject.transform.localPosition = Vector3.zero;
|
||||
heavyInvent.gameObject.transform.localEulerAngles = Vector3.zero;
|
||||
heavyInvent.transform.parent = null;
|
||||
heavyInvent.gameObject.transform.localScale = priorScale;
|
||||
|
||||
insertedCore = heavyInvent;
|
||||
|
||||
return true;
|
||||
}
|
||||
else if(insertedCore!=null&&heavyInvent==null)
|
||||
{
|
||||
heavyInvent = insertedCore;
|
||||
//get ref of player perhaps
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Item/DoorInteractable.cs.meta
Normal file
11
Assets/Scripts/Item/DoorInteractable.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1f575b137603a4b4282a6551120c979a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -4,10 +4,11 @@ using UnityEngine;
|
||||
|
||||
public class HeavyInteractableItem : InteractableItem
|
||||
{
|
||||
private Vector3 init_rot;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
init_rot = transform.eulerAngles;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
@ -18,10 +19,24 @@ public class HeavyInteractableItem : InteractableItem
|
||||
public void DisableAll()
|
||||
{
|
||||
this.GetComponent<Collider>().enabled = false;
|
||||
|
||||
}
|
||||
public override bool Interact()
|
||||
{
|
||||
//Todo
|
||||
return false;
|
||||
}
|
||||
public override bool Interact(ref Inventory inventory,ref HeavyInteractableItem heavyInvent)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public void EnableAll()
|
||||
{
|
||||
this.GetComponent<Collider>().enabled = true;
|
||||
this.transform.eulerAngles = new Vector3(init_rot.x,transform.eulerAngles.y,init_rot.z);
|
||||
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
|
@ -7,7 +7,7 @@ using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(Collider))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class InteractableItem : CarryableItem
|
||||
public abstract class InteractableItem : CarryableItem
|
||||
{
|
||||
|
||||
|
||||
@ -38,10 +38,9 @@ public class InteractableItem : CarryableItem
|
||||
target_alpha = 0;
|
||||
isEnabled = true;
|
||||
}
|
||||
public void Interact()
|
||||
{
|
||||
|
||||
}
|
||||
public abstract bool Interact();
|
||||
public abstract bool Interact(ref Inventory inventory,ref HeavyInteractableItem heavyInvent);
|
||||
|
||||
protected void BaseAwake()
|
||||
{
|
||||
interaction_texts = interactionCanvas.GetComponentsInChildren<TMP_Text>();
|
||||
|
@ -17,4 +17,13 @@ public class KeyItem : InteractableItem
|
||||
{
|
||||
|
||||
}
|
||||
public override bool Interact()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public override bool Interact(ref Inventory inventory,ref HeavyInteractableItem heavyInvent)
|
||||
{
|
||||
return this.Interact();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
@ -10,6 +11,8 @@ public class PlayerInteractionHandler : MonoBehaviour
|
||||
private List<InteractableItem> itemsInRange= new List<InteractableItem>();
|
||||
private List<HeavyInteractableItem> heavyItemsInRange = new List<HeavyInteractableItem>();
|
||||
private Inventory invent;
|
||||
public Inventory Inventory { get { return invent; } }
|
||||
public static PlayerInteractionHandler instance;
|
||||
[SerializeField]
|
||||
private Light flashlight;
|
||||
[SerializeField]
|
||||
@ -23,10 +26,12 @@ public class PlayerInteractionHandler : MonoBehaviour
|
||||
public bool IsCarrying { get { return heavyInvent !=null; } }
|
||||
[SerializeField]
|
||||
private Transform carryingPos;
|
||||
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
instance = this;
|
||||
invent = this.transform.parent.GetComponent<Inventory>();
|
||||
initColor = flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex].GetColor("_BaseColor");
|
||||
selMaterial = flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex];
|
||||
@ -39,26 +44,62 @@ public class PlayerInteractionHandler : MonoBehaviour
|
||||
//
|
||||
if(Input.GetButtonDown("Fire1"))
|
||||
{
|
||||
if(itemsInRange.Count > 0)
|
||||
{
|
||||
invent.AddItem(itemsInRange[0]);
|
||||
itemsInRange[0].transform.gameObject.SetActive(false);
|
||||
itemsInRange.Remove(itemsInRange[0]);
|
||||
}
|
||||
else if(heavyItemsInRange.Count > 0)
|
||||
if (!IsCarrying)
|
||||
{
|
||||
int t_index = 0;
|
||||
bool pickupFound = false;
|
||||
if (itemsInRange.Count > 0)
|
||||
{
|
||||
while (t_index<itemsInRange.Count&&!itemsInRange[t_index].CanPickup)
|
||||
{
|
||||
t_index++;
|
||||
}
|
||||
if (t_index != itemsInRange.Count)
|
||||
{
|
||||
pickupFound = true;
|
||||
invent.AddItem(itemsInRange[t_index]);
|
||||
itemsInRange[0].transform.gameObject.SetActive(false);
|
||||
itemsInRange.Remove(itemsInRange[t_index]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else if (heavyItemsInRange.Count > 0)
|
||||
{
|
||||
pickupFound = true;
|
||||
heavyInvent = heavyItemsInRange[0];
|
||||
|
||||
heavyInvent= heavyItemsInRange[0];
|
||||
heavyInvent.transform.parent = carryingPos;
|
||||
heavyInvent.transform.localPosition = Vector3.zero;
|
||||
heavyInvent.GetComponent<Rigidbody>().isKinematic = true;
|
||||
heavyItemsInRange.Remove(heavyItemsInRange[0]);
|
||||
heavyInvent.Disable();
|
||||
heavyInvent.DisableAll();
|
||||
}
|
||||
if (!pickupFound)
|
||||
{
|
||||
foreach(InteractableItem item in itemsInRange)
|
||||
{
|
||||
if (!item.CanPickup)
|
||||
{
|
||||
if(!item.Interact(ref invent, ref heavyInvent))
|
||||
{
|
||||
item.Interact();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
heavyInvent.transform.parent = null;
|
||||
heavyInvent.GetComponent<Rigidbody>().isKinematic = false;
|
||||
heavyInvent.EnableAll();
|
||||
heavyInvent = null;
|
||||
|
||||
heavyInvent.transform.parent = carryingPos;
|
||||
heavyInvent.transform.localPosition = Vector3.zero;
|
||||
heavyInvent.GetComponent<Rigidbody>().isKinematic = true;
|
||||
heavyItemsInRange.Remove(heavyItemsInRange[0]);
|
||||
heavyInvent.Disable();
|
||||
heavyInvent.DisableAll();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if (Input.GetButtonDown("Fire2"))
|
||||
{
|
||||
@ -66,7 +107,7 @@ public class PlayerInteractionHandler : MonoBehaviour
|
||||
if (flashlight.gameObject.activeSelf)
|
||||
{
|
||||
flashlight3D.GetComponent<MeshRenderer>().materials[materialIndex].SetColor("_BaseColor", initColor);
|
||||
selMaterial.SetColor("_EmissionColor", new Color(0, 0, 0, 0));
|
||||
selMaterial.SetColor("_EmissionColor", new Color(255,255, 255, 255));
|
||||
flashlight3D.gameObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
|
@ -42,6 +42,7 @@ public class PlayerMovementController : MonoBehaviour
|
||||
movement += transform.forward * Mathf.Abs(y) * Time.deltaTime * speed;//+(transform.right*x*Time.deltaTime*speed);
|
||||
//movement += transform.right * Mathf.Abs(x) * Time.deltaTime * sideSpeed;
|
||||
movement += transform.forward * Mathf.Abs(x) * Time.deltaTime * speed;
|
||||
movement += Vector3.down * 9.8f;
|
||||
ccontroller.Move(movement);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user