コード例 #1
0
ファイル: Tilemap.cs プロジェクト: Wishf/Fracture
        public static Tilemap Load(string path)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(new StreamReader(File.Open(path, FileMode.Open)));

            XmlNode meta = xDoc.GetElementsByTagName("map")[0];
            int width = int.Parse(meta.Attributes["width"].InnerText);
            int height = int.Parse(meta.Attributes["height"].InnerText);

            XmlNode data = xDoc.GetElementsByTagName("layer")[0].FirstChild;
            byte[] decodedData = Convert.FromBase64String(data.InnerText);

            int[] tileData = new int[decodedData.Length / 4];

            for (int i = 0; i < tileData.Length; i++)
            {
                tileData[i] = (int)BitConverter.ToUInt32(decodedData, 4 * i) - 1;
            }

            Tilemap map = new Tilemap(width, height);

            for (int x = 0; x < width; x++)
                for (int y = 0; y < height; y++)
                {
                    if (tileData[(y * width) + x] == 1)
                    {
                        if (map.SpawnTilePosition == -Vector2.One)
                            map.SpawnTilePosition = new Vector2(x, y);
                    }
                    else if (tileData[(y * width) + x] == 2)
                    {
                        map.EscapeDoor = new Vector2(x, y);
                    }
                    else if (tileData[(y * width) + x] == 3)
                    {
                        map.EnemySpawnPositions.Add(new Vector2(x, y));
                    }

                    if (tileData[(y * width) + x] == 0)
                    {
                        map[x, y] = new Tile { Collidable = true, TextureID = 0 };
                        map.CollidableTiles.Add(new TaggedVector { Position = new Vector2(x, y), Identifier = map[x, y] });
                    }
                    else if ((tileData[(y * width) + x] > 8 && tileData[(y * width) + x] < 19) || tileData[(y * width) + x] == 7)
                    {
                        map[x, y] = new Tile { Collidable = true, TextureID = tileData[(y * width) + x], KillOnCollide = true };
                        map.CollidableTiles.Add(new TaggedVector { Position = new Vector2(x, y), Identifier = map[x, y] });
                    }
                    else
                        map[x, y] = new Tile { Collidable = false, TextureID = tileData[(y * width) + x] };
                }

            return map;
        }
コード例 #2
0
ファイル: FractureGame.cs プロジェクト: Wishf/Fracture
        /// <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()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Services.AddService(typeof(SpriteBatch), spriteBatch);

            score = new ScoreData();
            Services.AddService(typeof(ScoreData), score);

            camera = new GameCamera { X = 0, Y = 0, Width = Window.ClientBounds.Width, Height = Window.ClientBounds.Height };
            Services.AddService(typeof(GameCamera), camera);

            collisionManager = new CollisionManager(this,24,24);
            Components.Add(collisionManager);

            tmapRenderer = new TilemapRenderer(this);
            Components.Add(tmapRenderer);

            map = Tilemap.Load("testmap.tmx");
            tmapRenderer.Map = map;

            playerManager = new PlayerManager(this);
            Components.Add(playerManager);

            player = new Player();
            playerManager.Player = player;

            escapeeManager = new EscapeeManager(this);
            Components.Add(escapeeManager);

            sfx = new Dictionary<string, SoundEffect>();
            Services.AddService(typeof(Dictionary<string, SoundEffect>), sfx);

            round = new Round { ParticipantCount = 9, WaveInterval = 10 };

            uiManager = new UIManager(this);
            Components.Add(uiManager);

            base.Initialize();
        }