using System.Collections.Generic; using System.Linq; using UnityEngine; using System.Collections; using System; [Serializable] public class ItemSocket{ public GameObject place; public ItemSocketType socketType; private PickableItem _attachedItem; public PickableItem AttachedItem { get { return _attachedItem; } } public void AttachItem(PickableItem item) { if (item == null || place == null) { return; } item.transform.parent = place.transform; item.transform.localPosition = Vector3.zero; item.transform.localRotation = Quaternion.identity; item.transform.localScale = Vector3.one; item.equipped = true; item.OnEquipped(); _attachedItem = item; } /* public PickableItem[] GetAttachedItems() { if (place.transform.childCount == 0) { return null; } var result = new List(); for (int i = 0; i < place.transform.childCount; ++i) { var child = place.transform.GetChild(i).GetComponent(); if (child != null) { result.Add(child); } } return result.ToArray(); }*/ public PickableItem DetachItem(Transform newParent = null) { var item = _attachedItem; if (item != null) { item.transform.parent = newParent; item.gameObject.SetActive(false); item.equipped = false; item.OnUnequipped(); } _attachedItem = null; return item; } } [RequireComponent(typeof(Health))] [RequireComponent(typeof(Inventory))] public class PlayerController : HumanController { private GameController _game; protected override void Start () { base.Start(); _game = FindObjectOfType(); } // Update is called once per frame protected override void Update () { base.Update(); if (_game.State == GameState.Gameplay) { if (Input.GetKeyDown(KeyCode.Alpha1)) { inventory.EquipItem(inventory.QueryItemIndex("Pistol", null, null)); } if (Input.GetKeyDown(KeyCode.K)) { PrepareItem(); } if (Input.GetKeyUp(KeyCode.K)) { UnprepareItem(); } if (gunArmed) { if (Input.GetKeyDown(KeyCode.J)) { var socket = GetSocket(ItemSocketType.Hands); if (socket != null) { var item = socket.AttachedItem; if (item != null) { item.OnBeginUse(); } } } if (Input.GetKeyUp(KeyCode.J)) { var socket = GetSocket(ItemSocketType.Hands); if (socket != null) { var item = socket.AttachedItem; if (item != null) { item.OnEndUse(); } } } } if (grounded && Input.GetKey(KeyCode.LeftControl)) { crouch = true; var v = _rb.velocity; v.x = 0; _rb.velocity = v; } else { crouch = false; } if (!health.Dead) { if (Input.GetKeyDown(KeyCode.Space)) { if (grounded) { _rb.AddForce(Vector2.up * jumpAcceleration * _rb.mass); anim.SetTrigger("Jump"); } else { } } } } } protected override void FixedUpdate(){ base.FixedUpdate(); if(!health.Dead && _game.State == GameState.Gameplay){ float maxSpeed = maxWalkSpeed; if(Input.GetKey(KeyCode.LeftShift)){ maxSpeed = maxRunSpeed; } if(!crouch && grounded){ if(Input.GetKey(KeyCode.D)){ UpdateMovement(maxSpeed); } if(Input.GetKey(KeyCode.A)){ UpdateMovement(-maxSpeed); } } } } }