// Upgrade the character's weapon public virtual bool UpgradeWeapon() { if (!HeldWeapon.Upgradeable) { return(false); } // Upgrade weapon HeldWeapon = HeldWeapon.Upgrade(Class); // Increase score if (GameApp.Instance.hardcore) { Score += 30; } else { Score += 20; } // Update image UpdateImage(); Cooldown = -1; CooldownTimer.Enabled = false; return(true); }
/// <summary> Executes an attack on the given creature. </summary> /// <param name="attacked"> The creature to be attacked. </param> protected virtual void PerformAttack(Creature attacked) { double HitChance = (double)Attack / (double)(2 * attacked.BaseDefense); HitChance += GetRandomNumber(-.2, .2); int damage = 0; if (HeldWeapon != null) { damage = (int)Math.Round((1.5 * Attack + HeldWeapon.GetDamage()) / Math.Sqrt(attacked.BaseDefense)); } else { damage = (int)Math.Round((1.5 * Attack) / Math.Sqrt(attacked.BaseDefense)); } attacked.OnAttack(this, HitChance, damage + 1); }
// Update the enemy (called every frame) // <deltatime> is the time in seconds since the last frame public override void Update(float deltatime) { bool onBottom = (Collision.BottomLeft && !Collision.WallLeft) || Collision.BottomMiddle || (Collision.BottomRight && !Collision.WallRight); bool onTop = Collision.TopLeft || Collision.TopMiddle || Collision.TopRight; bool onLeft = Collision.LeftMiddle || Collision.TopLeft; bool onRight = Collision.RightMiddle || Collision.TopRight; if (deltatime > 0.05f) { deltatime = 0f; } if (Class == CharacterClass.Tank) { shieldCooldown -= deltatime; if (shieldCooldown <= 0f) { ShieldUp = !ShieldUp; shieldCooldown = (float)random.NextDouble() * 5; } } if (onLeft) { direction = 1f; } if (onRight) { direction = -1f; } Velocity.X = MoveSpeed * direction; if (onBottom) { Velocity.Y = (random.NextDouble() < jumpChance) ? 10f : 0f; } else { Velocity.Y -= 10f * deltatime; // Gravity } if (onTop && Velocity.Y > 0) { Velocity.Y = 0; } if ((random.NextDouble() < fireChance) && !ShieldUp) // Can't shoot if shield is up { // run timer to count down if (Cooldown <= 0) { Cooldown = HeldWeapon.Cooldown; CooldownTimer.Enabled = true; if (GameApp.Instance.PlayerCharacter.WorldNode.IsDeleted) { return; } Vector2 dir = GameApp.Instance.PlayerCharacter.WorldNode.Position2D - WorldNode.Position2D; dir.Normalize(); GameApp.Instance.CreateBullets(HeldWeapon.Fire(dir), this); } } WorldNode.SetPosition2D(WorldNode.Position2D + Velocity * deltatime); }
// Update the player (called every frame) // <deltatime> is the time in seconds since the last frame public override void Update(float deltatime) { bool onBottom = (Collision.BottomLeft && !Collision.WallLeft) || Collision.BottomMiddle || (Collision.BottomRight && !Collision.WallRight); bool onTop = Collision.TopLeft || Collision.TopMiddle || Collision.TopRight; bool onLeft = Collision.LeftMiddle || Collision.TopLeft; bool onRight = Collision.RightMiddle || Collision.TopRight; ShieldUp = (Class == CharacterClass.Tank) && Input.F; Velocity.X = 0; if (deltatime > 0.05f) { deltatime = 0f; } if (Input.A) { Velocity.X -= MoveSpeed; if (!GameApp.Instance.schaubMode) { CharacterStaticSprite.FlipX = true; } } if (Input.D) { Velocity.X += MoveSpeed; if (!GameApp.Instance.schaubMode) { CharacterStaticSprite.FlipX = false; } } if (onLeft && Velocity.X < 0) { Velocity.X = 0; } if (onRight && Velocity.X > 0) { Velocity.X = 0; } if (onBottom) { Velocity.Y = 0; CharacterStaticSprite.Sprite = PlayerSpriteAttack; if (Input.Space) { Velocity.Y += 10f; if (!GameApp.Instance.schaubMode) { CharacterStaticSprite.Sprite = PlayerSpriteJump; } } } else { Velocity.Y -= 10f * deltatime; // Gravity } if (onTop && Velocity.Y > 0) { Velocity.Y = 0; } WorldNode.SetPosition2D(WorldNode.Position2D + Velocity * deltatime); if (Input.LeftClick && !ShieldUp) // Can't shoot if shield is up { // run timer to count down if (Cooldown <= 0) { Cooldown = HeldWeapon.Cooldown; CooldownTimer.Enabled = true; Vector2 dir = Input.MousePosition; dir.Normalize(); GameApp.Instance.CreateBullets(HeldWeapon.Fire(dir), this); } } }
void Awake() { kenHealth = GetComponent <KenHealth>(); heldWeapon = GetComponentInChildren <HeldWeapon>(); }