/// <summary> /// Tick for Player, override BuffTick for buff effects /// </summary> /// <param name="deltaTime">time since last tick</param> /// <returns>continuity and buff alive status</returns> public Tuple <bool, bool> Tick(float deltaTime) { timeleft -= deltaTime; bool alive = (!MVUtility.Leq0(timeleft)); return(new Tuple <bool, bool>(BuffTick(deltaTime), alive)); }
private void Update() { if (MVMain.Core.haltGame) { return; } deltaTime = Time.deltaTime; if (!stayAlive) { lifeTime -= Time.deltaTime; if (MVUtility.Leq0(lifeTime)) { Destroy(gameObject); } } if (_source == null) { Destroy(gameObject); } if (!localHalt) { AI(); } }
/// <summary> /// Method for giving player damage /// </summary> /// <param name="direction">direction of attack</param> /// <param name="damage">damage</param> /// <param name="ignoreInvincible">is the attack ignoring the invincible status?</param> /// <param name="knockbackMultiplier">knockback power</param> /// <param name="playSound">should it playing hurt sound?</param> /// <param name="drawNumber">should it draw damage number?(currently unused)</param> public void GetHit(Vector2 direction, int damage, bool ignoreInvincible = false, float knockbackMultiplier = 1f, bool playSound = true, bool drawNumber = false) { if (godMode) { return; } if (!invincible || ignoreInvincible) { if (playSound) { MVMain.Sound.Play("hit1", 1.5f, 1); } int damageF = Mathf.RoundToInt((float)damage * MVMain.Difficulty.difficulty); if (damage > 0) { damageF = Mathf.Max(1, damageF); } health -= damageF; if (health < 0) { health = 0; } if (!ignoreInvincible) { invincible = true; invincibleTime = invincibleTimeMax; blinking = true; blinkRate = blinkRateMax; } if (drawNumber) { //GameUtility.SpawnPopupText("-" + damageF.ToString(), rb2d.position + Random.insideUnitCircle * 0.25f, Color.red); } if (!MVUtility.Leq0(knockbackMultiplier)) { Knockback(direction, knockbackMultiplier); } } }
private void Update() { if (MVMain.Core.haltGame) { // animator.speed = 0; return; } else { // animator.speed = 1; } VelocityFromEffects = Vector2.zero; deltaTime = Time.deltaTime; if (health <= 0) { health = 0; Dead(); } freezeInput = false; if (weapons.Count > 0) { foreach (BaseWeapon wep in weapons) { wep.Update(deltaTime); } } #region buff deadBuff = new List <string>(); foreach (var item in buffs) { var resBuff = item.Value.Tick(deltaTime); if (!resBuff.Item1) { freezeInput = true; } if (!resBuff.Item2) { deadBuff.Add(item.Key); } } foreach (var item in deadBuff) { buffs.Remove(item); } #endregion if (!MVUtility.Leq0(weaponAttackMovingPenalty)) { weaponAttackMovingPenalty -= deltaTime; } if (invincibleTime > 0f) { invincibleTime -= deltaTime; } else { invincibleTime = 0f; if (invincible) { spriteRenderer.color = defaultColor; } invincible = false; blinking = false; } if (velocity.y <= 0f) { inJumpBack = false; jumpBack = false; } else if (jumpBack) { inJumpBack = true; } PlayerController(); activeWeaponIndex = (weapons.Count > 0) ? activeWeaponIndex % weapons.Count : 0; /*if (!GameUtility.Geq(weaponPower, (weapons.Count > 0) ? ActiveWeapon.WeaponUsage : 50f)) { * weaponCanRecharge = true; * } * else*/ if (Mathf.Approximately(weaponPower, weaponPowerMax)) { weaponCanRecharge = false; } if (!weaponCanRecharge && weaponRechargeDelayStart) { weaponRechargeDelay -= deltaTime; if (MVUtility.Leq0(weaponRechargeDelay)) { weaponRechargeDelay = weaponRechargeDelayMax; weaponRechargeDelayStart = false; weaponCanRecharge = true; } } else if (weaponCanRecharge) { weaponRechargeDelay = weaponRechargeDelayMax; weaponRechargeDelayStart = false; weaponPower += weaponRechargeSpeed * deltaTime; } weaponPower = Mathf.Clamp(weaponPower, 0f, weaponPowerMax); if (weapons.Count > 0) { PlayerShoot(); } if (blinking) { if (blinkRate > blinkRateMax / 2f) { spriteRenderer.color = new Color(1, 1, 1, .5f); } else { spriteRenderer.color = defaultColor; } blinkRate -= deltaTime; if (blinkRate <= 0) { blinkRate = blinkRateMax; } } velocity += VelocityFromEffects; }
/// <summary> /// Control of player /// </summary> private void PlayerController() { float xMove = 0f; if (!inJumpBack || grounded) { if (MVUtility.Leq0(weaponAttackMovingPenalty)) { xMove = freezeInput? 0 : Input.GetAxis("Horizontal"); } else if (freezeInput) { xMove = 0; } } else { xMove = knockBackXSpeed * knockBackResistance * knockBackDirection; } if (Input.GetButtonDown("Jump") && grounded && !freezeInput) { MVMain.Sound.Play("jump1"); // animator.SetBool("StartJumpBool", true); velocity.y = jumpTakeOffSpd + (godMode ? 5f : 0f); } else if (Input.GetButtonUp("Jump") || freezeInput) { // animator.SetBool("StartJumpBool", false); if (velocity.y > 0) { velocity.y *= .5f; } } else if (Input.GetButton("Jump")) { // animator.SetBool("StartJumpBool", false); } if (weapons.Count > 0 && !ActiveWeapon.fired && !freezeInput) { if (Input.GetButtonDown("Scroll+")) { activeWeaponIndex++; if (activeWeaponIndex >= weapons.Count) { activeWeaponIndex -= weapons.Count; } } else if (Input.GetButtonDown("Scroll-")) { activeWeaponIndex--; if (activeWeaponIndex < 0) { activeWeaponIndex += weapons.Count; } } } velocity.x = xMove * (maxSpeed + (godMode ? 5f : 0f)); bool flipSprite = (SpriteFlipX ? (xMove > 0.01f) : (xMove < -0.01f)); if (flipSprite && !inJumpBack) { transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z); } // animator.SetFloat("Speed", Mathf.Abs(velocity.x)); // animator.SetFloat("VelY", velocity.y); // animator.SetBool("Grounded", grounded); }