Ejemplo n.º 1
0
        // Spawn bullets in the world
        // <bullets> list of bullets to spawn
        // <character> character that shot the bullets
        public async void CreateBullets(List <Bullet> bullets, Character character)
        {
            InvokeOnMain(async() =>
            {
                // handle knife
                if (bullets == null)
                {
                    Bullet b = new Bullet(20)
                    {
                        Owner = character
                    };
                    b.CreateNode(scene, ResourceCache.GetSprite2D("knife.png"), character.WorldNode.Position2D);
                    Bullets.Add(b);

                    var node      = new Scene().CreateChild();
                    node.Position = b.WorldNode.Position;
                    if (character is CharacterPlayer)
                    {
                        PlaySound("sounds/effects/jump.ogg", false);
                    }

                    await Task.Delay(200);
                    if (!b.WorldNode.IsDeleted && Bullets.Contains(b))
                    {
                        try { Bullets.Remove(b); }
                        catch { return; }
                    }

                    // if bullet collides with a player, it will be already removed from the world,
                    // so ignore error if thrown.
                    try { b.WorldNode.Remove(); }
                    catch { return; }

                    return;
                }

                bool playedSound = false;
                foreach (Bullet b in bullets)
                {
                    b.Owner = character;
                    Sprite2D bulletSprite;

                    switch (b.Owner.Class)
                    {
                    case CharacterClass.Support:
                        bulletSprite = ResourceCache.GetSprite2D("shell.png");
                        break;

                    case CharacterClass.Schaub:
                        bulletSprite = ResourceCache.GetSprite2D("cheatShot.png");
                        break;

                    default:
                        bulletSprite = ResourceCache.GetSprite2D("shot.png");
                        break;
                    }

                    b.CreateNode(scene, bulletSprite, character.WorldNode.Position2D);

                    Bullets.Add(b);

                    if (schaubMode && b.Owner is CharacterPlayer)
                    {
                        PlaySound("sounds/effects/schaubShot.ogg", false);
                        playedSound = true;
                        continue;
                    }

                    // Don't repeat sound for shotguns
                    if (bullets.Count >= 4 && playedSound)
                    {
                        continue;
                    }

                    if (bullets.Count >= 4)
                    {
                        PlaySound("sounds/effects/shotgun.ogg", false);
                    }
                    else
                    {
                        PlaySound("sounds/effects/gunshot.ogg", false);
                    }

                    playedSound = true;
                }
            });
        }