This project is read-only.

Checking every bullet instance

Jan 22, 2013 at 3:08 PM

Hi,

I'm kind of stuck with checking ALL my bullets in the game.
The bullets are created by a component, which adds an instance of a template. 
All these components are not in the Game Solution, but in my Data library.

A part of my shooter component:

private void OnKeyDown(object sender, KeyboardEventArgs e)
        {
            if (e.Key == Trigger && allowShooting)
            {
                shotTimer = 0;

                var Player = Parent.FindName<Nine.Graphics.Model>("mmm");
                var PlayerPosition = Parent.FindName<Transformable>("Jon");

                var bullet = new Instance() { Template = BulletTemplate };
                // We take the position of the group that the player is in and the rotation of the model,
                // with this I take groups rotation and multipy it with the players position
                bullet.Transform = Player.Transform * Matrix.CreateTranslation(PlayerPosition.Transform.Translation);
                Scene.Add(bullet);

                allowShooting = false;

            }
        }



My bullet component:

public class RapidBullet : Component
    {
        private float lifetime;

        public float BulletSpeed { get; set; }
        public TimeSpan Duration { get; set; }

        protected override void Update(float elapsedTime)
        {
            lifetime += elapsedTime;
            if (lifetime > Duration.TotalSeconds)
            {
                Scene.Remove(Parent);
                return;
            }

            var transform = Parent.Transform;
            transform.Translation += transform.Forward * BulletSpeed * elapsedTime;
            Parent.Transform = transform;
        }
    }



My bullet template:

<Group Name="bullet"
       xmlns="http://schemas.microsoft.com/nine/2011/xaml"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:my="clr-namespace:NineQData;assembly=NineQData">

    <ParticleEffect Name="Fireball" Texture="{ContentReference ../fire}" Transform.Position="0, 10, 0">
        <ParticleEffect.Emitter>
            <SphereEmitter Emission="1000" Duration="0.1" Speed="2" Size="100" Shell="True" Radius="0.5"/>
        </ParticleEffect.Emitter>

        <SizeController EndSize ="4 ~ 5" />
        <SpeedController EndSpeed ="0" />
        <FadeController />
    </ParticleEffect>

    <Sphere Name="Kogel" Visible="False" Transform.Position="0,10,15" Transform.Scale="10,10,10"/>

    <my:RapidBullet BulletSpeed="400" Duration="00:00:01.0"/> 

</Group>


I can check the Model/Particle used in the Scene, by using scene.Find etc. But this will only check the first bullet shot.
I can have like 5 fireballs alive at the same moment because of timers, it's a piece of gameplay.

How can I check every instance of this bullet? 

 

Jan 23, 2013 at 1:23 AM

use Scene.Traverse

Jan 23, 2013 at 12:38 PM

Thanks, however I have just created new Bullets in my Bullet.XAML, so I'll try this later on.

Another quick question.

I have the Bullet.xaml, which is a Group, but in the Group I've declared multiple groups.
But I'm not succeeding in removing such a Group.
This is how it looks like, roughly:

<Group Name="bullet"
       xmlns="http://schemas.microsoft.com/nine/2011/xaml"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:my="clr-namespace:NineQData;assembly=NineQData">

    <Group Name="Fireball1group">
//ParticleEffect
//Primitive
    </Group>
 

</Group>
 
How can I remove the 'Fireball1Group' group with scene.Remove?
I've tried to scene.FindName<Group>("Fireball1Group"), but it's not working. :(

Am I doing something wrong? 

Jan 24, 2013 at 3:23 AM

The remove method can only remove the child that is directly attached to the scene.

To remove "Fireball1group" in your case, first find the “bullet” group, then remove the fireball using sth like:

var fireball = scene.FindName<Group>("Fireball1group"); fireball.Parent.Remove(fireball);