示例#1
0
 protected override void Initialize()
 {
     base.Initialize();
     WorldModel = new WorldModel(15);
 }
示例#2
0
        internal static bool ParseInput(WorldModel worldModel)
        {
            _lastKeyboardState = _currentKeyboardState;
            _currentKeyboardState = Keyboard.GetState();

            bool turnPassed = false;

            if (_currentKeyboardState.IsKeyDown(Keys.Down))
            {
                worldModel.ViewOffset += new Vector2(0, ScrollSpeed);
            }
            if (_currentKeyboardState.IsKeyDown(Keys.Up))
            {
                worldModel.ViewOffset -= new Vector2(0, ScrollSpeed);
            }
            if (_currentKeyboardState.IsKeyDown(Keys.Left))
            {
                worldModel.ViewOffset -= new Vector2(ScrollSpeed, 0);
            }
            if (_currentKeyboardState.IsKeyDown(Keys.Right))
            {
                worldModel.ViewOffset += new Vector2(ScrollSpeed, 0);
            }

            if (KeyPressed(Keys.Tab))
            {
                worldModel.ViewOffset = new Vector2(worldModel.Player.CurrentTile.X * worldModel.TileSize - 8 * worldModel.TileSize, worldModel.Player.CurrentTile.Y * worldModel.TileSize - 8 * worldModel.TileSize);
            }

            if (worldModel.Menu == null)
            {
                if (KeyPressed(Keys.F))
                {
                    worldModel.Menu = GetAllActions(worldModel);
                }
                else
                {
                    if (KeyPressed(Keys.A) || KeyPressed(Keys.S)
                        || KeyPressed(Keys.D) || KeyPressed(Keys.W))
                    {
                        int x = worldModel.Player.CurrentTile.X;
                        int y = worldModel.Player.CurrentTile.Y;

                        if (KeyPressed(Keys.A))
                            x -= 1;

                        if (KeyPressed(Keys.S))
                            y += 1;

                        if (KeyPressed(Keys.D))
                            x += 1;

                        if (KeyPressed(Keys.W))
                            y -= 1;

                        if (x > 0 || x < worldModel.Tiles.Length - 1 || y > 0 || y < worldModel.Tiles.Length - 1)
                        {
                            Tile target = worldModel.Tiles[x, y];

                            if (target.Passable(worldModel.Player))
                            {
                                worldModel.Player.Move(target);
                                turnPassed = true;
                            }
                        }
                    }
                }
            }
            else
            {
                if (KeyPressed(Keys.Enter))
                {
                    worldModel.Menu = null;
                    worldModel.ActiveMenu = null;
                }
                else
                {
                    if (KeyPressed(Keys.F))
                    {
                        Menu selectedOption = worldModel.ActiveMenu.MenuOptions[worldModel.Menu.SelectedIndex];

                        if (selectedOption.MenuOptions.Count == 0)
                        {
                            selectedOption.Action.Invoke(selectedOption.Context);
                        }
                        else
                        {
                            if (selectedOption.Action == null)
                            {
                                selectedOption.Selected.Action.Invoke(selectedOption.Selected.Context);
                            }
                            else
                            {
                                selectedOption.Action.Invoke(selectedOption.Context);
                            }
                        }

                        worldModel.Menu = null;
                        turnPassed = true;
                    }

                    if (KeyPressed(Keys.W))
                        worldModel.ActiveMenu.SelectedIndex -= 1;

                    if (KeyPressed(Keys.S))
                        worldModel.ActiveMenu.SelectedIndex += 1;

                    if (KeyPressed(Keys.D))
                    {
                        if (worldModel.ActiveMenu.MenuOptions.Count > 1)
                        {
                            worldModel.ActiveMenu = worldModel.Menu.Selected;
                        }
                    }

                    if (KeyPressed(Keys.A))
                    {
                        if (worldModel.ActiveMenu.Parent != null)
                        {
                            worldModel.ActiveMenu = worldModel.ActiveMenu.Parent;
                        }
                    }
                }
            }

            return turnPassed;
        }
示例#3
0
        public static void UpdateView(WorldModel worldModel, GraphicsDevice device, SpriteBatch spriteBatch)
        {
            device.Clear(Color.Black);
            spriteBatch.Begin();

            foreach (Tile tile in worldModel.Tiles)
            {
                Vector2 location = tile.GetLocation(worldModel.TileSize) - worldModel.ViewOffset;

                Vector2 itemOffset = new Vector2(2, 2);
                if (OnScreen(location, tile.Width, tile.Height))
                {
                    spriteBatch.Draw(GetTexture(tile, device), location, worldModel.GetAmbientColor(tile));

                    for (int i = 0; i < tile.TileContents.Count; i++)
                    {
                        IEntity e = tile.TileContents[i];

                        float offsetX = (worldModel.TileSize - e.Width) / 2;
                        float offsetY = (worldModel.TileSize - e.Height) / 2;

                        {
                            Creature creature = e as Creature;
                            if (creature != null)
                            {
                                if (creature.Health > 0)
                                {
                                    spriteBatch.Draw(GetTexture(creature, device), location + new Vector2(offsetX, offsetY), worldModel.GetAmbientColor(creature));
                                }
                                else
                                {
                                    spriteBatch.Draw(GetTexture(creature.Height, creature.Width, Color.Black, "DEAD" + creature.Name, device), location, worldModel.GetAmbientColor(creature));
                                }
                            }
                        }

                        {
                            Grass g = e as Grass;
                            if (g != null)
                            {
                                spriteBatch.Draw(GetTexture(g.Width, g.Height, Color.YellowGreen, "Grass" + g.Lenght, device), location + new Vector2(offsetX, offsetY), worldModel.GetAmbientColor(g));
                            }
                        }

                        {
                            Tree t = e as Tree;
                            if (t != null)
                            {
                                spriteBatch.Draw(GetTexture(t.Width - 5, worldModel.TileSize, Color.Brown, "TreeBranch", device), location + new Vector2(offsetX + 3, 0), worldModel.GetAmbientColor(t));
                                spriteBatch.Draw(GetTexture(worldModel.TileSize - 5, t.Height / 2 - 5, Color.DarkGreen, "TreeLeaves", device), location + new Vector2(3, 0), worldModel.GetAmbientColor(t));
                            }
                        }

                        {
                            IItem item = e as IItem;
                            if (item != null)
                            {
                                Color c = Color.Black;
                                if (item is Rock)
                                {
                                    c = Color.SlateGray;
                                }

                                if (item is Flint)
                                {
                                    c = Color.Black;
                                }

                                if (item is Apple)
                                {
                                    c = Color.Red;
                                }

                                spriteBatch.Draw(GetTexture(e.Width, e.Height, c, "Item" + e.Name, device), location + itemOffset, worldModel.GetAmbientColor(e));

                                itemOffset += new Vector2(e.Width + 1, 0);

                                if (itemOffset.X > worldModel.TileSize)
                                {
                                    itemOffset = new Vector2(2, e.Height + 2);
                                }
                            }
                        }

                        {
                            Wall wall = e as Wall;
                            if (wall != null)
                            {
                                Color c = Color.SlateGray;

                                spriteBatch.Draw(GetTexture(e.Width, e.Height, c, "Construct" + e.Name, device), location, worldModel.GetAmbientColor(wall));
                            }
                        }
                    }
                }
            }

            #region Debug

            #if DEBUG
            //foreach (Tile tile in worldModel.Tiles)
            //{
            //    Vector2 location = tile.GetLocation(worldModel.TileSize) - worldModel.ViewOffset;
            //    if (OnScreen(location, tile.Width, tile.Height))
            //    {
            //        for (int i = 0; i < tile.TileContents.Count; i++)
            //        {
            //            IEntity e = tile.TileContents[i];

            //            if (e is Creature)
            //            {
            //                Creature creature = e as Creature;
            //                // debug, draw vision of creture on tile
            //                if (creature.VisibleTiles != null)
            //                    foreach (var x in creature.VisibleTiles)
            //                    {
            //                        spriteBatch.Draw(GetTexture(10, 10, Color.DarkViolet, "Vision", device),
            //                            x.GetLocation(worldModel.TileSize) - worldModel.ViewOffset + new Vector2(5, 5), drawcol);
            //                    }

            //                // debug, draw adjacent of creture on tile
            //                if (creature.AdjacentTiles != null)
            //                    foreach (var x in creature.AdjacentTiles)
            //                    {
            //                        spriteBatch.Draw(GetTexture(5, 5, Color.Blue, "Movement", device),
            //                            x.GetLocation(worldModel.TileSize) - worldModel.ViewOffset, drawcol);
            //                    }
            //            }
            //        }
            //    }
            //}

            FontRenderer.DrawText(spriteBatch, 150, 1, "Average Update Time: " + worldModel.UpdateInterval);

            FontRenderer.DrawText(spriteBatch, 1, 1, "HP: " + worldModel.Player.Health + "/" + worldModel.Player.MaxHealth);
            FontRenderer.DrawText(spriteBatch, 1, 25, "Hunger: " + worldModel.Player.CurrentHunger);
            FontRenderer.DrawText(spriteBatch, 1, 45, "Turn: " + worldModel.Turn);

            #endif

            #endregion Debug

            Vector2 menuLoc = new Vector2((worldModel.Player.CurrentTile.X * worldModel.TileSize) + (worldModel.TileSize / 2),
                                               (worldModel.Player.CurrentTile.Y * worldModel.TileSize) + (worldModel.TileSize / 2))
                                                - worldModel.ViewOffset;
            DrawMenu(worldModel.Menu, menuLoc, spriteBatch, device);

            spriteBatch.End();
        }
示例#4
0
        private static void UpdateTileStatics(Tile tile, WorldModel worldModel)
        {
            for (int i = 0; i < tile.TileContents.Count; i++)
            {
                LivingEntity e = tile.TileContents[i] as LivingEntity;

                if (e != null)
                {
                    IAnimate animate = e as IAnimate;
                    if (animate != null)
                    {
                        if (animate.Health <= 0)
                        {
                            // unit is dead
                            continue;
                        }
                    }

                    ISighted sighted = e as ISighted;
                    if (sighted != null)
                    {
                        sighted.VisibleTiles = worldModel.GetVisibleTiles(tile.X, tile.Y, sighted);
                    }

                    IMobile mobile = e as IMobile;
                    if (mobile != null)
                    {
                        mobile.AdjacentTiles = worldModel.GetAdjacentTiles(tile.X, tile.Y);
                    }

                    IFeeder feeder = e as IFeeder;
                    if (feeder != null)
                    {
                        feeder.CurrentHunger += feeder.HungerRate;

                        ILiving living = e as ILiving;
                        if (feeder.CurrentHunger > 50 && living != null)
                        {
                            living.Health -= feeder.HungerRate;
                        }
                    }

                    IIlluminator iluminator = e as IIlluminator;
                    if (iluminator != null)
                    {
                        iluminator.Illuminate();
                    }
                }
            }
        }
示例#5
0
        private static void UpdateTileDynamics(Tile tile, List<IEntity> processedEntities, WorldModel worldModel)
        {
            for (int i = 0; i < tile.TileContents.Count; i++)
            {
                LivingEntity e = tile.TileContents[i] as LivingEntity;

                if (e != null && !processedEntities.Contains(e))
                {
                    IAnimate animate = e as IAnimate;
                    if (animate != null)
                    {
                        if (animate.Health <= 0)
                        {
                            // unit is dead
                            processedEntities.Add(e);
                            continue;
                        }
                    }

                    e.Update();

                    // entity moved or died
                    if (e.CurrentTile != tile)
                    {
                        i--;
                    }

                    processedEntities.Add(e);
                }
            }
        }
示例#6
0
        private static Menu GetAllActions(WorldModel worldModel)
        {
            Tile current = worldModel.Player.CurrentTile;

            Menu menu = new Menu();

            Menu atackOption = new Menu(menu, "Attack", null);
            Menu eatOption = new Menu(menu, "Eat", null);
            Menu pickUpOption = new Menu(menu, "Pick Up", null);
            Menu buildOption = new Menu(menu, "Build", null);
            Menu useOption = new Menu(menu, "Use", null);

            List<IEntity> entities = current.TileContents.Where(f => !(f is Player)).ToList();
            entities.AddRange(worldModel.Player.Inventory);
            foreach (IEntity entity in entities)
            {
                if (entity is IEdible)
                {
                    IAnimate i = entity as IAnimate;
                    if (i != null)
                    {
                        // if the entity is animate it can fight back, so kill it before eating it
                        if (i.Health > 0)
                        {
                            atackOption.AddOption(entity.Name, worldModel.Player.Attack, entity);
                        }
                        else
                        {
                            // entity is dead, can be eaten
                            CheckIfCanBeEaten(worldModel, entity, eatOption);
                        }
                    }
                    else
                    {
                        // entity is not animate so it cannot attack, eat it without struggle
                        CheckIfCanBeEaten(worldModel, entity, eatOption);
                    }
                }

                IItem item = entity as IItem;
                if (item != null)
                {
                    if (!worldModel.Player.Inventory.Contains(entity) && item.CanLoot())
                    {
                        pickUpOption.AddOption(entity.Name, worldModel.Player.PickUp, entity);
                    }
                }
            }

            if (worldModel.Player.Inventory.Any(i => i is Rock))
            {
                buildOption.AddOption("Rock wall", worldModel.Player.Build, new Wall());
            }

            foreach (var item in worldModel.Player.Inventory.Where(i => i is IUseable))
            {
                useOption.AddOption(item.Name, worldModel.Player.Use, worldModel.Player.Inventory.First(f => f is IUseable));
            }

            if (atackOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(atackOption);

            if (eatOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(eatOption);

            if (pickUpOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(pickUpOption);

            if (buildOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(buildOption);

            if (useOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(useOption);

            menu.MenuOptions.Add(new Menu(menu, "Stay", worldModel.Player.Stay));

            worldModel.ActiveMenu = menu;

            return menu;
        }
示例#7
0
 private static void CheckIfCanBeEaten(WorldModel worldModel, IEntity entity, Menu eatOption)
 {
     IEdible e = entity as IEdible;
     if (e != null && worldModel.Player.DesiredFood.HasFlag(e.ProvidesFoodType) && e.CanBeEaten())
     {
         eatOption.AddOption(e.FoodName, worldModel.Player.Eat, entity);
     }
 }
示例#8
0
        internal static void Update(WorldModel worldModel, GameTime gameTime)
        {
            DiagStopwatch.Reset();
            DiagStopwatch.Start();

            List<IEntity> processedEntities = new List<IEntity>();

            Parallel.ForEach(worldModel.Tiles.Cast<Tile>(), tile => UpdateTileStatics(tile, worldModel));

            foreach (Tile tile in worldModel.Tiles)
            {
                UpdateTileDynamics(tile, processedEntities, worldModel);
            }

            DiagStopwatch.Stop();
            Spans.Add(DiagStopwatch.Elapsed);

            worldModel.UpdateInterval = GetAverageUpdateTime().TotalMilliseconds;

            worldModel.Turn++;
        }