예제 #1
0
파일: Enemy.cs 프로젝트: etomsky/Homebrew
    // this was for something at some point
    protected virtual void OnCollisionEnter2D(Collision2D other)
    {
        HomebrewFlags flagData = other.gameObject.GetComponent <HomebrewFlags>();

        if (flagData != null)
        {
        }
    }
예제 #2
0
파일: Hazard.cs 프로젝트: etomsky/Homebrew
    void OnCollisionEnter2D(Collision2D other)
    {
        HomebrewFlags flagData = other.gameObject.GetComponent <HomebrewFlags>();

        if (flagData != null)
        {
            if (flagData.Get(Elements.PLAYER))
            {
                Physics2D.IgnoreCollision(GetComponentInChildren <Collider2D>(), other.gameObject.GetComponentInChildren <Collider2D>());
            }
        }
    }
예제 #3
0
    protected virtual void OnCollisionStay2D(Collision2D other)
    {
        HomebrewFlags flagData = other.gameObject.GetComponent <HomebrewFlags>();

        if (flagData != null)
        {
            foreach (Elements element in weaknesses)
            {
                if (PersistentInteraction.Recognized(flagData.Flags, element))
                {
                    OnStay(flagData.Flags);
                }
            }
        }
    }
예제 #4
0
    protected override void Update()
    {
        base.Update();

        float currentSpeed = speedFactor * maxSpeed * (transform.localScale.x > 0 ? 1f : -1f);

        transform.Translate(Vector2.right * currentSpeed * Time.deltaTime);

        Vector2 position2D = new Vector2(transform.position.x, transform.position.y);
        Vector2 testPoint  = position2D + Vector2.right * (transform.localScale.x > 0 ? 2f : -2f);

        // does not seek immediately underneath the foe. seeks off to the side instead.
        bool grounded = Physics2D.OverlapPoint(testPoint + Vector2.down * 0.75f, HomebrewFlags.EnvironmentalCollisionMask());
        bool walled   = Physics2D.OverlapPoint(testPoint, HomebrewFlags.EnvironmentalCollisionMask());

        if (!grounded || walled)
        {
            Turn();
        }
    }
예제 #5
0
    // Because we can't have nice things . . . LIKE CONSTRUCTORS.
    public static void SpawnBlob(GameObject what)
    {
        float r = 0.01f;
        int   n = 32;

        for (int i = 0; i < n; i++)
        {
            Vector2    offset = new Vector2(Random.Range(-r, r), Random.Range(-r, r));
            GameObject blob   = Instantiate(HomebrewGame.Me.prefabHazardGassy, new Vector3(what.transform.position.x /* + offset.x*/,
                                                                                           what.transform.position.y /* + offset.y*/, 0f), Quaternion.identity);
            blob.GetComponentInChildren <SpriteRenderer>().sprite = HomebrewGame.Me.spritesSteam[Random.Range(0, HomebrewGame.Me.spritesSteam.Count - 1)];

            //Rigidbody2D body = blob.GetComponent<Rigidbody2D>();
            //body.AddForce(offset * 2f/* / 1.5f*/, ForceMode2D.Impulse);
            //body.angularVelocity = Random.Range(-2f, 2f);

            HomebrewFlags flags = blob.AddComponent <HomebrewFlags>();
            flags.Set(Elements.STEAM);

            HazardSteam nova = blob.AddComponent <HazardSteam>();
            nova.velocity        = offset;
            nova.angularVelocity = Random.Range(-0.4f, 0.4f);
        }
    }
예제 #6
0
    // This used to be FixedUpdate but it was causing inputs to be skipped whenever the FixedUpdate was
    // fired at a different time than regular Update because screw you too Unity
    protected override void Update()
    {
        base.Update();

        // because bad things would probably happen if you were allowed to move in the pause menu,
        // where Time.timeScale = 0
        if (HomebrewGame.Me.menu.IsPaused)
        {
            return;
        }
        // invincibility

        if (IFrames > 0f)
        {
            IFrames = Mathf.Max(IFrames - Time.deltaTime, 0f);

            Renderer body = GetComponent <Renderer>();

            if (IFrames > 0f)
            {
                IFrameTime = Mathf.Max(IFrameTime - Time.deltaTime);
                if (IFrameTime <= 0f)
                {
                    body.enabled = (IFrames > 0f ? !body.enabled : true);
                    IFrameTime   = 1 / IFRAME_HZ;
                }
            }
            else
            {
                body.enabled = true;
            }
        }

        float horizontal = Input.GetAxis("Horizontal");

        /*
         * Running
         */

        if (Mathf.Abs(horizontal) > MOVE_THRESHOLD)
        {
            if (Input.GetButtonDown("Run"))
            {
                if (timeToDash > 0f)
                {
                    autodashing = true;
                }
                else
                {
                    timeToDash = DASH_THRESHOLD;
                }
            }

            if (Input.GetButton("Run"))
            {
                dashing    = true;
                timeToDash = DASH_THRESHOLD;
            }
            else
            {
                dashing = false;
            }
        }

        timeToDash = Mathf.Max(0f, timeToDash - Time.deltaTime);

        /*
         * Move around
         */

        if (Mathf.Abs(horizontal) < MOVE_THRESHOLD)
        {
            dashing     = false;
            autodashing = false;
        }

        if (Mathf.Abs(horizontal) > 0.25f)
        {
            Vector3 scale = transform.localScale;
            scale.x = Mathf.Abs(scale.x) * Mathf.Sign(horizontal);
            transform.localScale = scale;
        }

        float f = speedFactor;

        if (dashing || autodashing)
        {
            f = DASH_MULTIPLIER;
        }

        Rigidbody2D plugandplay = GetComponent <Rigidbody2D>();

        if (GameSettings.MovementStyle == MovementStyles.SMOOTH)
        {
            // SMOOTH MOVEMENT
            plugandplay.AddForce(new Vector2(horizontal * f, 0f), ForceMode2D.Impulse);
        }
        else
        {
            // SNAPPY MOVEMENT
            Vector2 currentVelocity = GetComponent <Rigidbody2D>().velocity;
            GetComponent <Rigidbody2D>().velocity = new Vector2(Mathf.Round(horizontal) * f * maxSpeed, currentVelocity.y);
        }

        Vector2 velocity = plugandplay.velocity;

        velocity.x = Mathf.Clamp(velocity.x, -maxSpeed * f, maxSpeed * f);

        bool grounded = Physics2D.OverlapPoint(new Vector2(transform.position.x, transform.position.y - 0.75f), HomebrewFlags.EnvironmentalCollisionMask());

        if (grounded)
        {
            if (Input.GetButtonDown("Jump"))
            {
                timeSinceJump = 0f;
            }

            velocity.x = Mathf.Lerp(velocity.x, 0f, friction);
        }

        timeSinceJump = timeSinceJump + Time.deltaTime;

        plugandplay.velocity = velocity;

        // after the velocity has been set, process jumping. this re-reads and resets the velocity so if
        // you do it before it's set above it'll get overwritten.

        if (GameSettings.MovementStyle == MovementStyles.SMOOTH)
        {
            // SMOOTH MOVEMENT
            if (timeSinceJump < 0.15f && Input.GetButton("Jump"))
            {
                plugandplay.gravityScale = 0f;
                Jump();
            }
            else
            {
                plugandplay.gravityScale = 1f;
            }
        }
        else
        {
            // SNAPPY MOVEMENT
            plugandplay.gravityScale = 1f;
            if (grounded && Input.GetButtonDown("Jump"))
            {
                // this magic number would produce *about-ish* the same jump height as holding your finger
                // down on the button in Smooth mode
                Jump(10f);
                // fact: mathematics tells me the value should be more like 7.3f but that value isn't working
                // and i don't want to touch it now
            }
        }

        /*
         * Throw, but only if the overlay(s) aren't shown
         */

        if (!selectorOverlay.activeInHierarchy)
        {
            Vector3 mousePos2D = Input.mousePosition;
            mousePos2D.z = -Camera.main.transform.position.z;
            Vector3 mousePos3d = Camera.main.ScreenToWorldPoint(mousePos2D);
            Vector3 mouseDelta = mousePos3d - transform.position;

            if (aimingMode && Input.GetButtonUp("Potion Chuck"))
            {
                aimingMode = false;

                Vector3 pvelocity = mouseDelta;
                pvelocity.Normalize();

                GameObject bottleClone = Instantiate(bottle);

                bottleClone.transform.position = transform.position;
                bottleClone.GetComponent <Rigidbody2D>().isKinematic = false;
                bottleClone.GetComponent <Rigidbody2D>().velocity    = pvelocity * bottlespeed;

                bottleClone.transform.position = reticle.transform.position;

                PersistentInteraction.ApplyToBottle(bottleClone, elements[0], elements[1], gameObject);

                reticle.SetActive(false);

                GameAudio.Me.PlayBottle();
            }

            if (Input.GetButtonDown("Potion Chuck"))
            {
                //the player has pressed the mouse button down while over the slingshot
                aimingMode = true;

                reticle.SetActive(true);
            }

            if (aimingMode)
            {
                //limit mouse delta to the radius of the slingshot spherecollider
                float maxmagnitude = launchRadius;

                Vector3 absMouseDelta = mouseDelta;
                absMouseDelta.Normalize();
                absMouseDelta = absMouseDelta * maxmagnitude;

                if (mouseDelta.magnitude > maxmagnitude)
                {
                    mouseDelta.Normalize();
                    mouseDelta *= maxmagnitude;
                }

                reticle.transform.position = transform.position + absMouseDelta;
            }

            GetComponent <Animator>().SetFloat("Speed", Mathf.Abs(horizontal));
        }


        /*
         * Cycle selected element
         */

        if (Input.GetButtonDown("Cycle"))
        {
            activeElement = (++activeElement) % elements.Length;
            SetElementText();
        }

        /*
         * Potions menu: bottom left: (0, 0); top right: (W, H)
         */

        Vector2 position = new Vector2((Input.mousePosition.x / Screen.width) - 0.5f, (Input.mousePosition.y / Screen.height) - 0.5f);

        if (selectorOverlay.activeInHierarchy)
        {
            foreach (GameObject what in quadrants)
            {
                what.SetActive(false);
            }

            if (position.magnitude > 0.1f)
            {
                quadrants[Quadrant(position)].SetActive(true);
            }

            if (Input.GetButtonDown("Select"))
            {
                selectorOverlay.SetActive(false);
                Time.timeScale = 1f;

                if (position.magnitude > 0.1f)
                {
                    elements[activeElement] = (Elements)elementMap[Quadrant(position)];
                    SetElementText();
                }
            }
        }
        else
        {
            if (Input.GetButtonDown("Potion Menu"))
            {
                for (int i = 0; i < 4; i++)
                {
                    Text text = quadrantText[i];
                    text.text = ownedElements[elementMap[i]] ? PersistentInteraction.Me.elementNames[elementMap[i]] : "None";
                }

                selectorOverlay.SetActive(true);
                Time.timeScale = SELECTOR_TIME_SCALE;
            }
        }
    }