コード例 #1
0
        /// <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()
        {
            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = "NamelessRogue.log4net.config";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            {
                XmlConfigurator.Configure(LogManager.CreateRepository("NamelessRogue"), stream);
            }

            Log = LogManager.GetLogger(typeof(NamelessGame));

            Log.Info("Application started");


            SaveManager.Init();

            CurrentGame = new GameInstance();
            DebugDevice = this.GraphicsDevice;
            //TODO: move to config later
            int width  = 40;
            int height = 30;


            var commanderEntity = new Entity();

            Commander = new Commander();
            commanderEntity.AddComponent(Commander);

            settings = new GameSettings(width, height);
            graphics.PreferredBackBufferWidth  = (int)(GetActualCharacterWidth() + settings.HudWidth());
            graphics.PreferredBackBufferHeight = GetActualCharacterHeight();

            graphics.IsFullScreen                   = false;
            graphics.PreferMultiSampling            = false;
            graphics.SynchronizeWithVerticalRetrace = true;

            MyraEnvironment.Game = this;

            RenderTarget = new RenderTarget2D(
                GraphicsDevice,
                GraphicsDevice.PresentationParameters.BackBufferWidth,
                GraphicsDevice.PresentationParameters.BackBufferHeight,
                false,
                GraphicsDevice.PresentationParameters.BackBufferFormat,
                DepthFormat.Depth24, 4, RenderTargetUsage.PlatformContents);


            graphics.ApplyChanges();

            //12345 123
            worldSettings = new WorldSettings(4, WorldGenConstants.Resolution, WorldGenConstants.Resolution);

            TerrainFurnitureFactory.CreateFurnitureEntities(this);

            ContextFactory.InitAllContexts(this);



            var viewportEntity = RenderFactory.CreateViewport(settings);

            CameraEntity = viewportEntity;

            TimelineEntity = TimelineFactory.CreateTimeline(this);


            var libraries   = new Entity();
            var ammoLibrary = new AmmoLibrary();

            ammoLibrary.AmmoTypes.Add(new AmmoType()
            {
                Name = "Revolver ammo"
            });
            libraries.AddComponent(ammoLibrary);

            var timelinEntity = TimelineEntity;
            var timeline      = timelinEntity.GetComponentOfType <TimeLine>();

            WorldTile firsTile = null;

            foreach (var worldBoardWorldTile in timeline.CurrentTimelineLayer.WorldTiles)
            {
                if (worldBoardWorldTile.Settlement != null)
                {
                    firsTile = worldBoardWorldTile;
                    break;
                }
            }
            int x, y;

            if (firsTile != null)
            {
                //place everything at the center of newly generated settlement;
                x = firsTile.Settlement.Concrete.Center.X;
                y = firsTile.Settlement.Concrete.Center.Y;
            }
            else
            {
                x = WorldGenConstants.Resolution / 2;
                y = WorldGenConstants.Resolution / 2;
            }
            var player = CharacterFactory.CreateSimplePlayerCharacter(x, y, this);

            PlayerEntity = player;

            FollowedByCameraEntity = player;

            ChunkManagementSystem chunkManagementSystem = new ChunkManagementSystem();

            //initialize reality bubble
            chunkManagementSystem.Update(0, this);
            //for (int i = 1; i < 10; i++)
            //{
            //    for (int j = 1; j < 10; j++)
            //    {
            //        Entities.Add(CharacterFactory.CreateBlankNpc(x - i,
            //            y - j));
            //    }
            //}

            CharacterFactory.CreateBlankNpc(x - 6,
                                            y, this);
            //Entities.Add(CharacterFactory.CreateBlankNpc(x - 3,
            //    y));
            //Entities.Add(CharacterFactory.CreateBlankNpc(x - 5,
            //    y));
            //Entities.Add(CharacterFactory.CreateBlankNpc(x - 7,
            //    y));


            for (int i = 0; i < 2; i++)
            {
                var sword = ItemFactory.CreateSword(x - 2,
                                                    y, i, this);;
            }

            var platemail = ItemFactory.CreatePlateMail(x - 2, y, 1, this);

            var pants = ItemFactory.CreatePants(x - 2, y, 1, this);

            var boots = ItemFactory.CreateBoots(x - 2, y, 1, this);

            var cape = ItemFactory.CreateCape(x - 2, y, 1, this);

            var ring = ItemFactory.CreateRing(x - 2, y, 1, this);

            var shield = ItemFactory.CreateShield(x - 2, y, 1, this);

            var helmet = ItemFactory.CreateHelmet(x - 2, y, 1, this);


            var ammo1 = ItemFactory.CreateLightAmmo(x - 1, y, 1, 20, this, ammoLibrary);


            var ammo2 = ItemFactory.CreateLightAmmo(x - 1, y + 1, 1, 20, this, ammoLibrary);


            var revolver = ItemFactory.CreateRevolver(x + 2, y + 1, 1, this, ammoLibrary);


            var pArmor = ItemFactory.CreatePowerArmor(x - 2, y, 1, this);


            Point worldRiverPosition = new Point();
            bool  anyRivers          = false;

            foreach (var worldBoardWorldTile in timeline.CurrentTimelineLayer.WorldTiles)
            {
                var pos     = worldBoardWorldTile.WorldBoardPosiiton;
                var isWater = timeline.CurrentTimelineLayer.InlandWaterConnectivity[pos.X][pos.Y].isWater;
                if (isWater)
                {
                    anyRivers          = true;
                    worldRiverPosition = pos;
                    break;
                }
            }

            if (anyRivers)
            {
                //move player to some river
                PlayerEntity.GetComponentOfType <Position>().Point = new Point(worldRiverPosition.X * Constants.ChunkSize, worldRiverPosition.Y * Constants.ChunkSize);
                chunkManagementSystem.Update(0, this);
            }

            CursorEntity = GameInitializer.CreateCursor();


            //Paragraph.BaseSize = 1.175f;
            //UserInterface.Initialize(Content, "custom");
            //UserInterface.Active.UseRenderTarget = true;
            CurrentContext = ContextFactory.GetMainMenuContext(this);
            CurrentContext.ContextScreen.Show();
            this.IsMouseVisible = true;
            // UserInterface.Active.ShowCursor = false;
            spriteBatch = new SpriteBatch(GraphicsDevice);


            fpsLabel = new Label();
            fpsLabel.HorizontalAlignment = HorizontalAlignment.Right;
            ContextFactory.GetIngameContext(this).ContextScreen.Panel.Widgets.Add(fpsLabel);

            var stackPanel = new VerticalStackPanel();

            stackPanel.Widgets.Add(fpsLabel);

            // Desktop.Widgets.Add(stackPanel);


            // Inform Myra that external text input is available
            // So it stops translating Keys to chars


            _desktop.HasExternalTextInput = true;

            // Provide that text input
            Window.TextInput += (s, a) =>
            {
                _desktop.OnChar(a.Character);
            };
        }
コード例 #2
0
        public override void Update(long gameTime, NamelessGame namelessGame)
        {
            IEntity        worldEntity   = namelessGame.TimelineEntity;
            IWorldProvider worldProvider = null;

            if (worldEntity != null)
            {
                worldProvider = worldEntity.GetComponentOfType <TimeLine>().CurrentTimelineLayer.Chunks;
            }


            IEntity playerentity = namelessGame.PlayerEntity;

            if (playerentity != null)
            {
                Chunk    currentChunk    = null;
                Point?   currentChunkKey = null;
                Position playerPosition  = playerentity.GetComponentOfType <Position>();
                //look for current chunk
                foreach (Point key in worldProvider.GetRealityBubbleChunks().Keys)
                {
                    Chunk ch = worldProvider.GetRealityBubbleChunks()[key];
                    if (ch.IsPointInside(playerPosition.Point))
                    {
                        currentChunk    = ch;
                        currentChunkKey = key;
                        break;
                    }
                }

                //if there is none, that means we just loaded the namelessGame, look for current in all chunks
                if (currentChunk == null)
                {
                    foreach (Point key in worldProvider.GetChunks().Keys)
                    {
                        Chunk ch = worldProvider.GetChunks()[key];
                        if (ch.IsPointInside(playerPosition.Point))
                        {
                            currentChunk    = ch;
                            currentChunkKey = key;
                            break;
                        }
                    }
                }

                if (currentChunk != null)
                {
                    for (int x = -Constants.RealityBubbleRangeInChunks + currentChunkKey.Value.X;
                         x <= Constants.RealityBubbleRangeInChunks + currentChunkKey.Value.X;
                         x++)
                    {
                        for (int y = -Constants.RealityBubbleRangeInChunks + currentChunkKey.Value.Y;
                             y <= Constants.RealityBubbleRangeInChunks + currentChunkKey.Value.Y;
                             y++)
                        {
                            Point p = new Point(x, y);
                            if (!worldProvider.GetRealityBubbleChunks().ContainsKey(p))
                            {
                                if (worldProvider.GetChunks().ContainsKey(p))
                                {
                                    Chunk chunk = worldProvider.GetChunks()[p];



                                    worldProvider.GetRealityBubbleChunks().Add(p, chunk);
                                    worldProvider.RealityChunks.Add(chunk);
                                }
                            }
                        }
                    }

                    List <Point> keysToRemove = new List <Point>();
                    foreach (Point key in
                             worldProvider.GetRealityBubbleChunks().Keys)
                    {
                        double distX = Math.Abs(key.X - currentChunkKey.Value.X);
                        double distY = Math.Abs(key.Y - currentChunkKey.Value.Y);
                        if (distX > Constants.RealityBubbleRangeInChunks || distY > Constants.RealityBubbleRangeInChunks)
                        {
                            keysToRemove.Add(key);
                        }
                    }

                    foreach (Point key in
                             keysToRemove)
                    {
                        if (worldProvider.GetRealityBubbleChunks()[key].IsActive)
                        {
                            worldProvider.GetRealityBubbleChunks()[key].Deactivate();
                            worldProvider.RealityChunks.Remove(worldProvider.GetRealityBubbleChunks()[key]);
                            worldProvider.GetRealityBubbleChunks().Remove(key);
                        }
                    }
                }
            }

            var justcreated = worldProvider.GetRealityBubbleChunks().Where(x => x.Value.JustCreated);

            foreach (var realityBubbleChunk in justcreated)
            {
                foreach (var tileArray in realityBubbleChunk.Value.GetChunkTiles())
                {
                    foreach (var tile in tileArray)
                    {
                        var entity = TerrainFurnitureFactory.GetExteriorEntities(namelessGame, tile);
                        if (entity != null)
                        {
                            if (tile.GetEntities().Count == 0)
                            {
                                tile.AddEntity(entity);

                                namelessGame.AddEntity(entity);
                            }
                        }
                    }
                }
                realityBubbleChunk.Value.JustCreated = false;
            }
        }