コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        List <BeeSwarm> allTheBees = FindObjectsOfType <BeeSwarm>().ToList();

        if (controlling == null)
        {
            if (allTheBees.Count > 0)
            {
                int index = allTheBees.IndexOf(controlling);
                index = (allTheBees.Count + index - 1) % allTheBees.Count;
                SetControlledBeeSwarm(allTheBees[index]);
            }
            else
            {
                // TODO DEATH
                return;
            }
        }

        if (controlling != null && controller == null)
        {
            controller = controlling.GetComponent <CharacterController>();
        }

        // Build the context for an action
        context.hovered    = GetHoveredGameObject();
        context.actor      = controlling;
        context.controller = controller;

        // Perform controller actions
        foreach (string input in actionRegister.Keys)
        {
            // Add individual input values to context
            float inputVal = Input.GetAxisRaw(input);
            context.inputValue = inputVal;
            // Is button being held? Prepare the action
            if (Mathf.Abs(inputVal) > 0)
            {
                actionRegister[input].hold.Invoke();
            }
            // Was the button pushed down? Trigger immediate behaviour
            if (Input.GetButtonDown(input))
            {
                actionRegister[input].down.Invoke();
            }
            // Is the button released? Finish the action
            if (Input.GetButtonUp(input))
            {
                actionRegister[input].up.Invoke();
            }
        }

        if (Input.GetMouseButton(1))
        {
            Vector3 newPos;
            bool    valid = CheckMousePlacement(out newPos);
            if (controlling.numBees < 400)
            {
                valid = false;
            }
            if (previewBees == null)
            {
                previewBees = Instantiate(previewBeesPrefab, newPos, previewBeesPrefab.transform.rotation).GetComponent <Preview>();
            }
            previewBees.transform.position = newPos;
            previewBees.valid = valid;
        }
        if (Input.GetButtonUp("Fire2"))
        {
            // If over hive, destroy it
            bool       onHive = false;
            RaycastHit hit;
            int        layerMask = LAYER_MASK_IGNORE_PLAYER;
            Ray        ray       = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
            {
                if (hit.collider.CompareTag("Hive") && hit.rigidbody != null && controlling.inRange.Contains(hit.rigidbody))
                {
                    Hive hive = hit.collider.GetComponent <Hive>();
                    hive.Die(controlling);
                    onHive = true;
                }
            }
            if (!onHive && !controlling.HasClothes())
            {
                controlling.Split();
            }
            if (previewBees != null)
            {
                Destroy(previewBees.gameObject);
            }
        }

        if (Input.GetButtonDown("Q"))
        {
            int index = allTheBees.IndexOf(controlling);
            index = (allTheBees.Count + index - 1) % allTheBees.Count;
            SetControlledBeeSwarm(allTheBees[index]);
        }

        if (Input.GetButtonDown("E"))
        {
            int index = allTheBees.IndexOf(controlling);
            index = (index + 1) % allTheBees.Count;
            SetControlledBeeSwarm(allTheBees[index]);
        }

        if (Input.GetKey(KeyCode.F))
        {
            Vector3 newPos;
            bool    valid = CheckMousePlacement(out newPos);
            if (controlling.numBees < 400)
            {
                valid = false;
            }
            if (previewHive == null)
            {
                previewHive = Instantiate(previewHivePrefab, newPos, previewHivePrefab.transform.rotation).GetComponent <Preview>();
            }
            previewHive.transform.position = newPos;
            previewHive.valid = valid;
        }
        if (Input.GetButtonUp("F"))
        {
            controlling.BuildHive();
            if (previewHive != null)
            {
                Destroy(previewHive.gameObject);
            }
        }

        if (Input.GetKey(KeyCode.Space) && controlling.HasClothes())
        {
            Vector3 newPos;
            bool    valid = CheckMousePlacement(out newPos);
            if (previewCoat == null)
            {
                previewCoat = Instantiate(previewCoatPrefab, newPos, previewCoatPrefab.transform.rotation).GetComponent <Preview>();
            }
            previewCoat.transform.position = newPos;
            previewCoat.valid = valid;
        }
        if (Input.GetButtonUp("Spacebar"))
        {
            if (controlling.HasClothes())
            {
                controlling.clothes.Drop();
            }
            if (previewCoat != null)
            {
                Destroy(previewCoat.gameObject);
            }
        }

        direction = new Vector3();

        if (Input.GetKey(KeyCode.W))
        {
            direction += Vector3.forward;
        }
        if (Input.GetKey(KeyCode.S))
        {
            direction += Vector3.back;
        }
        if (Input.GetKey(KeyCode.A))
        {
            direction += Vector3.left;
        }
        if (Input.GetKey(KeyCode.D))
        {
            direction += Vector3.right;
        }

        direction.Normalize();

        Vector3 translationWorldSpace = direction * speed * Time.deltaTime;

        if (controlling.HasClothes())
        {
            translationWorldSpace *= controlling.clothes.speedMod;
        }
        Vector3 translationCameraSpace = cameraTransform.TransformDirection(translationWorldSpace);

        controller.Move(translationCameraSpace);
        controller.Move(Vector3.down * (controller.transform.position.y - controlling.lockHeight));

        // DRAGGING
        if (Input.GetButtonDown("Fire1"))
        {
            // Check if the mouse is over any in range objects
            RaycastHit hit;
            int        layerMask = LAYER_MASK_IGNORE_PLAYER;
            Ray        ray       = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
            {
                if (controlling.inRange.Contains(hit.rigidbody))
                {
                    Outline outline = hit.collider.GetComponent <Outline>();
                    if (outline == null)
                    {
                        outline = hit.collider.gameObject.AddComponent <Outline>();
                    }
                    outline.OutlineWidth = 4;
                    outline.OutlineColor = Color.yellow;
                    outline.enabled      = true;
                    if (hit.rigidbody != null && !hit.rigidbody.isKinematic)
                    {
                        draggingObject = hit.rigidbody;
                    }
                }
            }
        }
        if (Input.GetButtonUp("Fire1"))
        {
            if (draggingObject != null)
            {
                Outline outline = draggingObject.GetComponent <Outline>();
                if (outline == null)
                {
                    outline = draggingObject.gameObject.AddComponent <Outline>();
                }
                outline.OutlineWidth = 4;
                outline.OutlineColor = Color.white;
            }
            draggingObject = null;
        }
        if (draggingObject != null)
        {
            if (!controlling.inRange.Contains(draggingObject))
            {
                draggingObject = null;
            }
        }
    }