Example #1
0
        public Thruster(Ship parent, Vector3 pos, Vector3 dir)
            : base(parent.Game)
        {
            if (pSys == null)
            {
                var pSet = new ParticleSettings()
                {
                    BlendState = BlendState.NonPremultiplied,
                    MaxParticles = 150,
                    Duration = TimeSpan.FromSeconds(0.5),
                    DurationRandomness = 0.1f,
                    EmitterVelocitySensitivity = 0,
                    MinVelocity = 1f,
                    MaxVelocity = 2f,
                    Gravity = Vector3.Zero,
                    EndVelocity = 0,
                    MinColor = Color.White,
                    MaxColor = Color.White,
                    MinRotateSpeed = -0.1f,
                    MaxRotateSpeed = 0.1f,
                    MinStartSize = 0.25f,
                    MaxStartSize = 0.35f,
                    MinEndSize = 0.5f,
                    MaxEndSize = 0.6f
                };

                var pTex = new Texture2D(GraphicsDevice, 5, 5);
                pTex.SetData(Enumerable.Repeat(Color.FromNonPremultiplied(0, 0, 255, 125), 25).ToArray());

                var pEff = Game.Content.Load<Effect>("Shaders/Particles");

                pSys = new ParticleSystem(Game, pSet, pTex, pEff, parent.Camera);
                Game.Components.Add(pSys);
            }

            this.parent = parent;
            this.pos = pos;
            this.Direction = dir;
            this.obj = new Primitive(GraphicsDevice, 0.1f, Primitives.Sphere) { Position = pos };
            parent.Mesh.PrimitiveObjects.Add(this.obj);
            emitter = new ParticleEmitter(pSys) { Position = parent.Position + pos, Direction = dir, ParticlesPerSecond = 3 };
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            Primitives.Initialize(GraphicsDevice);

            sceneTarget = new RenderTarget2D(GraphicsDevice
                , GraphicsDevice.PresentationParameters.BackBufferWidth
                , GraphicsDevice.PresentationParameters.BackBufferHeight
                , false
                , GraphicsDevice.PresentationParameters.BackBufferFormat
                , GraphicsDevice.PresentationParameters.DepthStencilFormat);

            camera = new PerspectiveCamera()
            {
                Position = Vector3.UnitX,
                Target = Vector3.Zero,
                Up = Vector3.Up,
                FieldOfView = MathHelper.PiOver4,
                AspectRatio = GraphicsDevice.Viewport.AspectRatio,
                NearPlane = 0.01f,
                FarPlane = 5000f
            };

            inputSystem = new InputSystem(this);
            inputSystem.RegisterKeyReleasedAction(Keys.Escape, () => Exit());
            inputSystem.RegisterMouseWheelAction(change =>
                {
                    var prevZoomLevel = zoomLevel;
                    if (change < 0)
                    {
                        // closing to focus point
                        if (zoomLevel < 1) zoomLevel -= 0.025f;
                        else zoomLevel -= 0.1f;
                    }
                    else if (change > 0)
                    {
                        if (zoomLevel < 1) zoomLevel += 0.025f;
                        else zoomLevel += 0.1f;
                    }
                    if (zoomLevel <= 0) zoomLevel = prevZoomLevel;
                });
            inputSystem.RegisterMouseMoveAction((x, y) =>
                {
                    var axis = Vector3.Cross(camera.Target - camera.Position, camera.Up);
                    var transform = Quaternion.CreateFromAxisAngle(axis, MathHelper.ToRadians(y * 10));
                    camera.Position = Vector3.Transform(camera.Position, transform);

                    cameraAngle += MathHelper.ToRadians(x);
                    cameraHeight += y % 10;
                });

            primitives[0] = new Primitive(GraphicsDevice, 1, Primitives.Sphere) { Position = Vector3.UnitX };
            primitives[1] = new Primitive(GraphicsDevice, 1, Primitives.Cube) { Position = Vector3.UnitX * 3 };
            primitives[2] = new Primitive(GraphicsDevice, 1, Primitives.Plane) { Position = Vector3.UnitX * 5 };

            base.Initialize();
        }
Example #3
0
        private void CreateSpheres()
        {
            // Create a random number generator
            Random random = new Random();

            // These are the various colors we use when creating the spheres
            Color[] sphereColors = new[]
            {
                Color.Orange
                , Color.White
                , Color.GhostWhite
            };

            // The radius of a sphere
            const float radius = 1f;

            for (int i = 0; i < maximumNumberOfSpheres; i++)
            {
                // Create the sphere
                Primitive sphere = new Primitive(GraphicsDevice, radius, Primitives.Sphere);

                // Position the sphere in our world
                sphere.Position = new Vector3(
                    random.NextFloat(-worldSize + radius, worldSize - radius),
                    random.NextFloat(radius, worldSize - radius),
                    random.NextFloat(-worldSize + radius, worldSize - radius));

                // Pick a random color for the sphere
                sphere.Color = sphereColors[random.Next(sphereColors.Length)];

                // Create a random velocity vector
                sphere.Velocity = new Vector3(
                    random.NextFloat(-10f, 10f),
                    random.NextFloat(-10f, 10f),
                    random.NextFloat(-10f, 10f));

                // Add the sphere to our array
                spheres[i] = sphere;
            }
        }