Inheritance: Leap.Unity.Detector
Esempio n. 1
0
        void Update()
        {
            try
            {
                if (!hookedChop)
                {
                    handModel    = GameObject.Find("LoPoly Rigged Hand Right");
                    chopDetector = handModel.GetComponents <Leap.Unity.ExtendedFingerDetector>()[handModel.GetComponents <Leap.Unity.ExtendedFingerDetector>().Length - 2];
                    chopAction  += this.Chopping;
                    chopDetector.OnActivate.AddListener(chopAction);
                    hookedChop = true;
                }
            } catch { }

            if (PhotonNetwork.isMasterClient && randomSeed == -1)
            {
                System.Random rd = new System.Random();
                // rd.Next(1, 999)
                randomSeed = rd.Next(1, 999);
            }
            switch (state)
            {
            case "attacking":
                Weapon weapon = Constants.Weapons[loadouts[loadoutIndex]];
                myAnimator.SetBool("Run", false);
                myAnimator.SetBool("Walk", false);

                if (photonView.isMine)
                {
                    //if (Constants.Weapons[loadouts[loadoutIndex]].type == "melee")
                    //{
                    if (attackTimer < attackInterval)
                    {
                        attackTimer += Time.deltaTime;
                        target       = null;
                        break;
                    }
                    attackTimer = 0;
                    Debug.Log("Gonna attack");
                    //       }
                    if (/*!animator.GetBool("ToTwoHandAttack") && */ isAttacking)
                    {
                        state       = "";
                        isAttacking = false;
                        myAnimator.SetBool("ToTwoHandedAttack", false);
                        if (weapon.type.Contains("melee"))
                        {
                            RaycastHit hit;
                            Vector3    fwd = transform.TransformDirection(Vector3.forward);

                            Collider[] collis = Physics.OverlapSphere(transform.position, attackRange);

                            foreach (Collider colli in collis)
                            {
                                //Debug.Log("What is collision?" + colli.name);
                                if (DealDamage(colli.gameObject))
                                {
                                    //Debug.Log("I hit this one " + colli.name);
                                    //break;
                                }
                            }
                            if (Physics.Raycast(transform.position, fwd, out hit, attackRange))
                            {
                                //    Debug.Log("I Hit something " + hit.collider.name);
                                if (DealDamage(hit.collider.gameObject))
                                {
                                    Debug.Log(hit.collider.gameObject.GetComponent <NPCControl>().health);
                                }
                            }
                        }
                        else
                        {
                            if (target != null)
                            {
                                DealDamage(target);
                                target = null;
                            }
                        }
                    }
                    else
                    {
                        isAttacking = true;
                        if (weapon.type == "melee")
                        {
                            Debug.Log("Running melee animation");
                            myAnimator.SetBool("ToTwoHandedAttack", true);
                            StartCoroutine(StopSwingingLater());
                            Collider[] collis = Physics.OverlapSphere(transform.position, 4);

                            foreach (Collider colli in collis)
                            {
                                Debug.Log("What is collision?" + colli.name);
                                if (DealDamage(colli.gameObject))
                                {
                                    //Debug.Log("I hit this one " + colli.name);
                                    break;
                                }
                            }
                        }
                    }
                }
                break;

            case "harmed":
                myAnimator.SetBool("Run", false);
                myAnimator.SetBool("Walk", false);
                myAnimator.SetBool("ToTwoHandAttack", false);
                if (!myAnimator.GetBool("Get_Hit") && isMoaning)
                {
                    isMoaning = false;
                    if (health <= 0)
                    {
                        health = 0;
                        myAnimator.SetBool("Run", false);
                        myAnimator.SetBool("Walk", false);
                        myAnimator.SetBool("ToTwoHandAttack", false);
                        myAnimator.SetBool("Dead", true);
                        GameObject.Find("SceneManager").GetComponent <DungeonMission>().FailMission();
                        return;
                    }
                    state = "";
                }
                else
                {
                    isMoaning = true;
                    myAnimator.SetBool("Get_Hit", true);
                }
                break;

            default:
                break;
            }

            Debug.Log("ControlScript update isMINE");
            if (photonView.isMine || isTutMode)
            {
                // Update HUD
                try
                {
                    hud = GameObject.FindGameObjectWithTag("Hud").GetComponent <Hud>();
                }
                catch { }
                if (hud != null)
                {
                    hud.SetHealth(health);
                }

                this.cc.radius = this.colSize;

                //Set the VSpeed and HSpeed floats for our animator to control walking and strafing animations.
                myAnimator.SetFloat("HSpeed", Input.GetAxis("Horizontal"));

                //Set Jump Boolean to true to trigger jump animation, then wait a small time and set to false so we don't jump agani.
                if (Input.GetButtonDown("Jump"))
                {
                    myAnimator.SetBool("Jumping", true);
                    Invoke("StopJumping", 0.1f);
                    ReadyToPlay();
                }
                //!!!switch run mode
                if (Input.GetKey(KeyCode.LeftShift))
                {
                    _VSpeed = 2 * (Input.GetAxis("Vertical"));
                }
                else
                {
                    _VSpeed = Input.GetAxis("Vertical");
                }
                myAnimator.SetFloat("VSpeed", _VSpeed);

                if (Input.GetKey("a"))
                {
                    //Rotate the character procedurally based on Time.deltaTime.  This will give the illusion of moving
                    //Even though the animations don't have root motion
                    transform.Rotate(Vector3.down * Time.deltaTime * 100.0f);

                    //Also, IF we're currently standing still (both vertically and horizontally)
                    if ((Input.GetAxis("Vertical") == 0f) && (Input.GetAxis("Horizontal") == 0))
                    {
                        //change the animation to the 'inplace' animation
                        myAnimator.SetBool("TurnLeft", true);
                    }
                }
                else
                {
                    //Else here means if the Q key is not being held down
                    //Then we make sure that we are not playing the turning animation
                    myAnimator.SetBool("TurnLeft", false);
                }

                //Same thing for E key, just rotating the other way!
                if (Input.GetKey("d"))
                {
                    transform.Rotate(Vector3.up * Time.deltaTime * 100.0f);
                    if ((Input.GetAxis("Vertical") == 0f) && (Input.GetAxis("Horizontal") == 0))
                    {
                        myAnimator.SetBool("TurnRight", true);
                    }
                }
                else
                {
                    myAnimator.SetBool("TurnRight", false);
                }

                if (Input.GetKey(KeyCode.Alpha1))
                {
                    switch (myAnimator.GetInteger("CurrentAction"))
                    {
                    case 0:
                        StartCoroutine(Arming1());
                        break;

                    case 1:
                        StartCoroutine(Disarming1());
                        break;
                    }
                }
                // Shoot once
                if (Input.GetKey("t"))
                {
                    state = "attacking";
                }

                //Combining methods.
                //You can combine all of these methods (and much more advanced logic) in many ways!  Let's go over an example.  We want our character to kneel down
                //Stay in a kneeling loop idle, then stand up when we tell them to.  For this we'll need to combine a triggered 'transition' animation with looping
                //animations.  You can see in the Animator Controller that Action3 (kneeling) is actually comprised of 3 animations.  "kneeling down", "kneeling idle"
                //and "kneeling stand".

                //The first is "kneeling down".  The intro requirement is the same as before, CurrentAction = 3.  We can control this using the same "SetInteger" method
                //we used in our previous examples.  However, the exit transition is based on exit time.  So as soon as that intro "kneeling down" animation plays
                //The animation will transition into a kneeling idle.  Now we want the character to remain there until we tell it to get up, so the exit transition
                //From kneeling idle is "currentaction = 0".  This means we'll need to set up a toggle just like in Example #4.  When we toggle from 3 to 0, we'll transition
                //Into the kneeling stand animation, which will get our character back to their feet.  Finally the exit transition for kneeling stand is exit time, so
                //As soon as they are done standing up they will go the next state ("idle/walk").
                if (Input.GetKey(KeyCode.Alpha2))
                {
                    switch (myAnimator.GetInteger("CurrentAction"))
                    {
                    case 0:
                        StartCoroutine(Draw2());
                        break;

                    case 2:
                        StartCoroutine(Hold2());
                        break;
                    }
                }
                // if (Input.GetKeyDown("2"))
                // {
                //     if (myAnimator.GetInteger("CurrentAction") == 0)
                //     {
                //         myAnimator.SetInteger("CurrentAction", 2);
                //     }
                //     else if (myAnimator.GetInteger("CurrentAction") == 2)
                //     {
                //         myAnimator.SetInteger("CurrentAction", 0);
                //     }
                // }
                //MauryEND
                // if (Input.GetKey ("r") && (myAnimator.GetInteger ("CurrentAction") == 0)) {
                //  myAnimator.SetBool ("2HandIdle", true);
                // }
                // if (Input.GetKey ("t") && (myAnimator.GetInteger ("CurrentAction") == 0)) {
                //  myAnimator.SetBool ("chopTree", true);
                // } else {
                //  myAnimator.SetBool ("chopTree", false);
                // }

                RaycastHit hit2     = new RaycastHit();     // This ray will see where we clicked er chopped
                Vector3    ahead    = RayOrigin.forward;
                Vector3    rayStart = new Vector3(RayOrigin.position.x, RayOrigin.position.y + 1f, RayOrigin.position.z);
                Ray        ray      = new Ray(rayStart, ahead); // Did we hit anything at that point, out as far as 10 units?

                if (Physics.Raycast(ray, out hit2, 1.5f))
                {
                    if (hit2.collider.gameObject.tag == "Resources")
                    {   // Resources object has valueable type which is Tree, Rock and Metal
                        Debug.Log("hited");
                        ResourcesGenerator.Resources res = hit2.collider.gameObject.GetComponent("Resources") as ResourcesGenerator.Resources;
                        switch (res.type)
                        {
                        case "Tree":
                            Debug.Log("treeasdfasdfasdf");
                            closestTreePosition = hit2.transform.position;
                            break;

                        case "Rock":
                            Debug.Log("Rockasdfasdfasdf");
                            closestTreePosition = hit2.transform.position;
                            break;

                        case "Metal":
                            Debug.Log("Metalasdfasdfasdf");
                            closestTreePosition = hit2.transform.position;
                            break;

                        case "Ani":
                            closestTreePosition = hit2.transform.position;
                            break;
                        }
                    }
                    this.temp = hit2;
                    isTemp    = true;

                    if (usingAxe && (Input.GetKey(KeyCode.E) || leapchop))
                    {
                        this.temp = hit2;
                        this.OnChopping();
                    }
                }
            }
            //MauryEND
        }
 void Start()
 {
     fingers = hand.GetComponent <Leap.Unity.ExtendedFingerDetector>();
 }