Example #1
0
    /****************************************** PRIVATE METHODS ******************************************/

    void Grab()
    {
        if (!isHoldingItem)
        {
            Vector2      grabBox      = new Vector2(grabRadius, GetHeight());
            Collider2D[] gnomesToGrab = Physics2D.OverlapBoxAll(grabCenter.position, grabBox, 0f, whatCanBeGrabbed);

            foreach (Collider2D coll in gnomesToGrab)
            {
                coll.gameObject.transform.parent = gameObject.transform;

                GnomeController gnome = coll.gameObject.GetComponent <GnomeController> ();
                ClockController clock = coll.gameObject.GetComponent <ClockController> ();

                if (gnome != null)
                {
                    Vector3 newGnomePosition = gnomeHolster.position;
                    newGnomePosition.y += gnome.GetWidth();
                    coll.gameObject.transform.position = newGnomePosition;
                    gnome.Flip(90);
                    gnome.Freeze();

                    // Fix the scale
                    Vector3 gnomeScale = new Vector3(1.1f, 1.1f, 0.3f);
                    coll.gameObject.transform.localScale = gnomeScale;

                    currentGnome = coll.gameObject;
                    animator.SetBool("isCarrying", true);
                    this.isHoldingItem = true;
                }
                else if (clock != null)
                {
                    Vector3 newClockPosition = gnomeHolster.position;
                    newClockPosition.y += clock.GetHeight() / 4;
                    coll.gameObject.transform.position = newClockPosition;
                    clock.FixRotation();
                    clock.Freeze();

                    // Fix the scale
                    Vector3 clockScale = new Vector3(0.26f, 0.26f, 0.26f);
                    coll.gameObject.transform.localScale = clockScale;

                    animator.SetBool("isCarrying", true);
                    this.isHoldingItem = true;
                    currentClock       = coll.gameObject;
                    string name = currentClock.GetComponent <ClockController> ().GetName();
                    EventManagerController.FireClockSelectedEvent(name);
                }
            }
        }
    }
Example #2
0
    void FixedUpdate()
    {
        var inputs = _selected && GnomeSelector.gnomesEnabled
            ? InputGrabber.Instance.CurrentGnomeInputs
            : InputGrabber.EmptyInputs;

        var runForce = RunForce * Vector2.right * inputs.WalkAxis;
        if (runForce.x * _rb.velocity.x < 0) runForce *= TurnAroundMultiplier;
        _rb.AddForce(runForce);

        if (_onHead) {
            var hb = _onHead.GetComponent<Rigidbody2D>();
            hb.AddForce(runForce / _rb.mass * hb.mass);
        }

        if (inputs.JumpButton.JustPressed && _grounded) {
            if (_onHead) {
                var hb = _onHead.GetComponent<Rigidbody2D>();
                hb.AddForce(Vector2.up * JUMP_HEAD_BOOST, ForceMode2D.Impulse);
            }
            _rb.AddForce(JumpImpulse * Vector2.up, ForceMode2D.Impulse);
            //SoundPlayer.Instance.Play("Jump");
            _grounded = false;
        }

        if (Mathf.Abs(_rb.velocity.x) > MaxRunSpeed) {
            _rb.velocity = _rb.velocity.WithX(MaxRunSpeed * Mathf.Sign(_rb.velocity.x));
        }

        if (Mathf.Abs(inputs.WalkAxis) < .1f) {
            _rb.velocity = _rb.velocity.WithX(_rb.velocity.x * DecelMultiplier);
        }

        if(Mathf.Abs(_rb.velocity.x) > 1 && _grounded)
        {
            WalkSound();
        }

        _onHead = null;
        _grounded = false;
    }
Example #3
0
    void FixedUpdate()
    {
        float   centerY     = transform.position.y + boxCollider.offset.y;
        Vector2 centerPoint = new Vector2(transform.position.x, centerY);

        Vector2 bedBox = new Vector2(boxCollider.size.x * 3.7f, 20f);

        Collider2D[] gnomesOnTheBed = Physics2D.OverlapBoxAll(centerPoint, bedBox, 0f, ThingsOnTheBed);
        gnomesOntheBedCount = gnomesOnTheBed.Length;
        EventManagerController.FireSetScoreEvent(gnomesOntheBedCount);

        foreach (Collider2D coll in gnomesOnTheBed)
        {
            GnomeController gnome = coll.gameObject.GetComponent <GnomeController> ();
            gnome.BeInBed();
        }

        if (gnomesOntheBedCount == 7)
        {
            EventManagerController.FireBedFullEvent();
        }
    }
Example #4
0
    void Throw()
    {
        if (isHoldingItem)
        {
            if (huhSound != null)
            {
                huhSound.Play();
            }

            if (currentGnome != null)
            {
                GnomeController gnome = currentGnome.GetComponent <GnomeController> ();
                currentGnome.transform.parent = null;
                gnome.UnFreeze();
                gnome.isMidAir = true;
                Rigidbody2D gnomeRb   = currentGnome.GetComponent <Rigidbody2D> ();
                float       direction = facingRight ? 1f : -1f;
                gnomeRb.velocity = new Vector2(7.5f * direction, 15f);
                isHoldingItem    = false;
                currentGnome     = null;
                animator.SetBool("isCarrying", false);
            }
            else if (currentClock)
            {
                ClockController clock = currentClock.GetComponent <ClockController> ();
                currentClock.transform.parent = null;
                clock.UnFreeze();
                Rigidbody2D clockRb   = currentClock.GetComponent <Rigidbody2D> ();
                float       direction = facingRight ? 1f : -1f;
                clockRb.velocity = new Vector2(7.5f * direction, 15f);
                isHoldingItem    = false;
                currentClock     = null;
                animator.SetBool("isCarrying", false);
                EventManagerController.FireClockSelectedEvent("");
            }
        }
    }
Example #5
0
    void OnCollisionStay2D(Collision2D col)
    {
        foreach (var contact in col.contacts) {
            if (contact.normal.y > 0.8f) _grounded = true;
        }

        var colGnome = col.collider.gameObject.GetComponent<GnomeController>();
        if (colGnome == null) return;

        bool isTop = false;
        foreach (var contact in col.contacts) {
            if (contact.normal.y < -0.8f) isTop = true;
        }
        if (!isTop) return;

        _onHead = colGnome;
    }