// Update is called once per frame protected override void Update() { base.Update(); //This makes the enemy move if (Vector2.Distance(transform.position, player.position) < nearDistance) { transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime); } else if (Vector2.Distance(transform.position, player.position) > stoppingDistance) { transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime); } else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > nearDistance) { transform.position = transform.position; } // This makes the enemy shoot if (timeBetweenShots <= 0) { GameObject fired = Instantiate(shot, transform.position, Quaternion.identity); fired.transform.position = transform.position; fired.GetComponent <Rigidbody2D>().isKinematic = false; //fired.GetComponent<Rigidbody2D>().velocity = pvelocity * bottleSpeed; if (player.transform.position.x > transform.position.x) { fired.GetComponent <Rigidbody2D>().velocity = new Vector2(1f, 0f) * bottleSpeed; } else { fired.GetComponent <Rigidbody2D>().velocity = new Vector2(-1f, 0f) * bottleSpeed; } // I don't like this PersistentInteraction.ApplyToBottle(fired, Elements.FIRE, Elements.FIRE, gameObject); timeBetweenShots = startTimeBetweenShots; } else { timeBetweenShots -= Time.deltaTime; } }
// 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; } } }