Exemplo n.º 1
0
        protected override void Draw(GameTime game_time)
        {
            GraphicsDevice.Clear(Color.Black);
            sprite_batch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp);

            // Draw all the tiles to the screen
            foreach (Tile tile in TileManager.GetTileList())
            {
                // We draw the tiles with an offset so that the camera is always centered on the player
                // regardless of where they are in space
                sprite_batch.Draw(tile.Sprite, new Vector2(tile.PosX + Camera.x_offset, tile.PosY + Camera.y_offset), Color.White);
            }

            // Then, draw the entities to the screen
            foreach (Entity entity in EntityManager.GetEntityList())
            {
                // We draw the tiles with an offset so that the camera is always centered on the player
                // regardless of where they are in space
                sprite_batch.Draw(entity.Sprite, new Vector2(entity.PosX + Camera.x_offset, entity.PosY + Camera.y_offset), Color.White);
            }

            // Finally, draw the units to the screen
            foreach (Unit unit in UnitManager.GetUnitList())
            {
                // We draw the tiles with an offset so that the camera is always centered on the player
                // regardless of where they are in space
                sprite_batch.Draw(unit.Sprite, new Vector2(unit.PosX + Camera.x_offset, unit.PosY + Camera.y_offset), Color.White);
            }

            // Draw the player to the screen, we draw it with the camera offset for the same reason as the tiles
            Player player = UnitManager.player;

            sprite_batch.Draw(player.Sprite, new Vector2(player.PosX + Camera.x_offset, player.PosY + Camera.y_offset), Color.White);

            sprite_batch.End();
            base.Draw(game_time);
        }
Exemplo n.º 2
0
        // Get a list of textmaps for the area, convert all the textmaps into tiles,
        // then place the tiles into a list so they can be displayed on the screen
        public static void CreateGameMap()
        {
            // This is a list of file names, each file is a separate layer of the gamemap.
            // The layers are displayed alphabetically, so layer_2 is displayed above layer_1
            var files = Directory.GetFiles($"Maps/{WalkAround.current_map}", "*.txt", SearchOption.TopDirectoryOnly).ToList();

            files.Sort();

            List <List <string> > layers = new List <List <string> >();

            foreach (string file in files)
            {
                layers.Add(File.ReadAllText(file).Split('\n').ToList());
            }

            var tile_list   = new List <Tile>();
            var entity_list = new List <Entity>();
            var unit_list   = new List <Unit>();

            // Create all the tiles and place them in the proper location
            foreach (List <string> layer in layers)
            {
                int x = 0;
                int y = 0;

                foreach (string line in layer)
                {
                    foreach (char symbol in line.Trim())
                    {
                        if (TileCatalog.ContainsKey(symbol))
                        {
                            var tl = GetTileInfo(symbol);
                            tile_list.Add(new Tile(x, y, tl.Width, tl.Height, tl.SpriteLoc, tl.Traversable));
                        }

                        else if (NPCCatalog.ContainsKey(symbol))
                        {
                            var tl = GetNPCInfo(symbol);
                            entity_list.Add(new NPC(x, y, tl.Width, tl.Height, tl.SpriteLoc));
                        }

                        else if (UnitCatalog.ContainsKey(symbol))
                        {
                            var tl = GetUnitInfo(symbol);
                            unit_list.Add(new Player(x, y, tl.Width, tl.Height, tl.SpriteLoc, tl.MoveSpeed));
                        }

                        else
                        {
                            throw new KeyNotFoundException($"Tried to create GameObject with unmapped symbol '{symbol}'");
                        }

                        x += WalkAround.tile_size;
                    }

                    x  = 0;
                    y += WalkAround.tile_size;
                }
            }

            TileManager.UpdateTileList(tile_list);
            EntityManager.UpdateEntityList(entity_list);
            UnitManager.UpdateUnitList(unit_list);
        }
Exemplo n.º 3
0
        // Returns true if moving to the point (PosX + x_delta, PosY + y_delta)
        // would put the entity inside a non-traversable collision box
        public bool PredictCollision(int x_delta, int y_delta)
        {
            var collisions = Logic.FindOverlaps(this, TileManager.GetTileList(), x_delta, y_delta);

            return(collisions.Any(x => !x.Traversable));
        }