protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            graphics = SharedGraphicsDeviceManager.Current.GraphicsDevice;

            game.Run();

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            var environment = new Conesoft.Game.DefaultEnvironment()
            {
                Random          = new Random(),
                ModelBoundaries = new Dictionary <string, BoundingSphere>()
            };

            game.Resources.Load((to, from) =>
            {
                to.Models.Add(from.Load <Model>(Data.Ship, Data.Drone, Data.Spaceship));
                to.Sprites.Add(from.Load <Texture2D>(Data.Fireball, Data.Energyball, Data.Bullet, Data.Grass, Data.TutorialOverlay, Data.GameWonOverlay, Data.GameOverOverlay));

                var texture = new Texture2D(graphics, 1, 1);
                texture.SetData(new Color[] { Color.Black });
                to.Sprites.Add(Data.BlackBackground, texture);

                to.Sounds.Add(from.Load <SoundEffect>(Data.ExplosionSound, Data.GameOverSound, Data.GoSound, Data.GoodSound, Data.LaserSound));

                to.Fonts.Add(from.Load <SpriteFont>(Data.Font, Data.SmallFont));

                var terrainModel = new TerrainModel()
                {
                    Position = Vector3.Down * 64 * 32,
                    Size     = new Vector3(1024 * 128, 256 * 32, 1024 * 128)
                };
                terrainModel.LoadFromTexture2D(from.Load <Texture2D>(Data.LandscapeGround), environment);
                to.Terrains.Add(Data.Landscape, terrainModel);

                foreach (var model in to.Models)
                {
                    var boundingSphere = default(BoundingSphere);
                    foreach (var mesh in model.Value.Meshes)
                    {
                        boundingSphere = BoundingSphere.CreateMerged(boundingSphere, mesh.BoundingSphere);
                    }
                    environment.ModelBoundaries[model.Key] = boundingSphere;
                }
            });

            environment.Sounds = game.Resources.Sounds;

            // Start the timer
            timer.Start();

            App.Current.Appl.NavigationService.NavigateTo <LastManStandingLevel>();

            level = new DefaultLevel(environment);

            var spaceShips = level.Objects3D.OfType <Spaceship>();

            if (spaceShips.Count() > 0)
            {
                localPlayer = new LocalPlayer()
                {
                    ControlledObject = spaceShips.First()
                };
                level.Players.Add(localPlayer);
            }

            startTime = null;

            fadeInTime            = App.Current.FirstTime ? 3 : 1;
            App.Current.FirstTime = false;

            base.OnNavigatedTo(e);
        }
示例#2
0
        public void LoadFromTexture2D(Texture2D TerrainTexture, Conesoft.Game.DefaultEnvironment Environment)
        {
            if (TerrainTexture.Width != TerrainTexture.Height)
            {
                throw new Exception("Terrain has to be Square in Size");
            }
            DataWidth  = TerrainTexture.Width;
            HeightData = new byte[DataWidth * DataWidth];

            var colorData = new Color[DataWidth * DataWidth];

            TerrainTexture.GetData(colorData);

            foreach (var index in Enumerable.Range(0, DataWidth * DataWidth))
            {
                HeightData[index] = colorData[index].R;
            }

            Grid = new VertexPositionColorTexture[DataWidth * DataWidth];
            foreach (var y in Enumerable.Range(0, DataWidth))
            {
                foreach (var x in Enumerable.Range(0, DataWidth))
                {
                    var Height = HeightData[x + DataWidth * y];
                    var Point  = new Vector3((float)x / (float)DataWidth, (float)Height / 256f, (float)y / (float)DataWidth);
                    Point = 2 * Point - new Vector3(1, 1, 1);

                    Point = Point * Size / 2 + Position;

                    var halfWidthOfSmallerStepSize = Math.Min(Size.X, Size.Z) / (float)DataWidth / 1.414f;

                    Point = Point + Environment.RandomPointInUnitSphere() * halfWidthOfSmallerStepSize;

                    var c     = Height / 255f;
                    var color = new Color();
                    if (c < 0.02f)
                    {
                        color = Color.DarkBlue;
                    }
                    if (x > 0 && x < DataWidth - 1 && y > 0 && y < DataWidth - 1)
                    {
                        var dx = (HeightData[x - 1 + DataWidth * y] - HeightData[x + 1 + DataWidth * y]) / 256f;
                        var dy = (HeightData[x + DataWidth * (y - 1)] - HeightData[x + DataWidth * (y + 1)]) / 256f;

                        var dxVector = new Vector3(Size.X * 2 / DataWidth, dx * Size.Y, 0);
                        var dyVector = new Vector3(0, dy * Size.Y, Size.Z * 2 / DataWidth);
                        var normal   = Vector3.Cross(dyVector, dxVector);
                        normal.Normalize();
                        var light = new Vector3(1, 1, 0);
                        light.Normalize();
                        var shade      = Vector3.Dot(normal, light);
                        var snowColor  = new Vector3(1, 1, 1);
                        var grassColor = new Vector3(0.1f, 0.4f, 0.01f);
                        {
                            c -= 0.7f;
                            c *= 2f;
                            if (c < 0)
                            {
                                c = 0;
                            }
                            else
                            {
                                c *= 2;
                            }
                            if (c > 1)
                            {
                                c = 1;
                            }
                            color = new Color(shade * MathHelper.Lerp(grassColor.X, snowColor.X, c), shade * MathHelper.Lerp(grassColor.Y, snowColor.Y, c), shade * MathHelper.Lerp(grassColor.Z, snowColor.Z, c));
                        }
                        var h = Height / 255f;
                        if (h < 0.03f)
                        {
                            var blend = h;
                            blend = blend / 0.03f;
                            var clr1 = new Color(0, 0, 0.2f).ToVector3();
                            var clr2 = color.ToVector3();
                            var clr  = (1 - blend) * clr1 + blend * clr2;
                            color = new Color(clr.X, clr.Y, clr.Z);
                        }
                    }
                    else
                    {
                        color = new Color(0.2f, 0.3f, 0.8f);
                    }
                    color *= 1.3f;
                    Grid[x + DataWidth * y] = new VertexPositionColorTexture(Point, color, new Vector2(0.5f * Point.X / Size.X + 0.5f, 0.5f * Point.Z / Size.Z + 0.5f) * 64);
                }
            }

            Indicees = new short[DataWidth * 2];
            foreach (var i in Enumerable.Range(0, Indicees.Length))
            {
                if (i % 2 == 0)
                {
                    Indicees[i] = (short)(i + DataWidth);
                }
                else
                {
                    Indicees[i] = (short)(i - 1);
                }
            }
        }