Ejemplo n.º 1
0
        // Load the map
        private void CreateMap()
        {
            // Load map
            TmxFile2D mapFile = ResourceCache.GetTmxFile2D("test/map_1.tmx");

            if (mapFile == null)
            {
                throw new Exception("Map not found");
            }

            Node mapNode = scene.CreateChild("TileMap");

            mapNode.Position = new Vector3(0f, 0f, 10f);
            mapNode.SetScale(1f / 0.7f);

            tileMap         = mapNode.CreateComponent <TileMap2D>();
            tileMap.TmxFile = mapFile;

            // Load pickup sprites
            var weaponSprite = ResourceCache.GetSprite2D("map/levels/platformer-art-complete-pack-0/Request pack/Tiles/raygunBig.png");
            var armorSprite  = ResourceCache.GetSprite2D("map/levels/platformer-art-complete-pack-0/Request pack/Tiles/shieldGold.png");

            if (weaponSprite == null || armorSprite == null)
            {
                throw new Exception("Texture not found");
            }

            // Initialize map
            Random r = new Random();

            for (uint layerID = 0; layerID < tileMap.NumLayers; ++layerID)
            {
                TileMapLayer2D layer = tileMap.GetLayer(layerID);
                for (int x = 0; x < layer.Width; ++x)
                {
                    for (int y = 0; y < layer.Height; ++y)
                    {
                        Node n = layer.GetTileNode(x, y);
                        if (n == null)
                        {
                            continue;
                        }

                        Vector2 pos = tileMap.TileIndexToPosition(x, y);
                        pos /= 0.7f;
                        pos += new Vector2(0.5f, 0.5f);

                        // Solid Block
                        if (layer.GetProperty("Solid") == "True")
                        {
                            MapTile tile = new MapTile(pos);
                            Tiles.Add(tile);
                            collisionObjects.Add(tile);
                            continue;
                        }

                        // Spawner
                        switch (layer.GetProperty("Spawn"))
                        {
                        case "Player":
                            playerSpawns.Add(pos);
                            break;

                        case "Enemy":
                            enemySpawns.Add(pos);
                            break;

                        case "Weapon":
                            if (r.NextDouble() < weaponSpawnRate)
                            {
                                Pickups.Add(new PickupWeaponUpgrade(scene, weaponSprite, pos));
                            }
                            break;

                        case "Armor":
                            if (r.NextDouble() < armorSpawnRate)
                            {
                                Pickups.Add(new PickupArmor(scene, armorSprite, pos));
                            }
                            break;

                        default:
                            continue;
                        }

                        // Skipped if not a spawner
                        n.Remove();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        // This method loads the pickups
        // Recieves a line of serialized pickup data
        private void LoadPickups(string line)
        {
            InvokeOnMain(() =>
            {
                // Create images
                var weaponSprite = ResourceCache.GetSprite2D("map/levels/platformer-art-complete-pack-0/Request pack/Tiles/raygunBig.png");
                var armorSprite  = ResourceCache.GetSprite2D("map/levels/platformer-art-complete-pack-0/Request pack/Tiles/shieldGold.png");
                if (weaponSprite == null || armorSprite == null)
                {
                    throw new Exception("Texture not found");
                }

                // Clear Tilemap of old pickups
                for (uint layerID = 0; layerID < tileMap.NumLayers; ++layerID)
                {
                    TileMapLayer2D layer = tileMap.GetLayer(layerID);
                    for (int x = 0; x < layer.Width; ++x)
                    {
                        for (int y = 0; y < layer.Height; ++y)
                        {
                            Node n = layer.GetTileNode(x, y);
                            if (n == null)
                            {
                                continue;
                            }
                            if (layer.HasProperty("Spawn"))
                            {
                                n.Remove();
                            }
                        }
                    }
                }

                // Remove added pickups from map.
                foreach (Pickup p in Pickups)
                {
                    p.WorldNode.Remove();
                }
                Pickups.Clear();

                // Load each pickup
                string[] pickupsSplit = line.Split(';');
                foreach (var pickup in pickupsSplit.Take(pickupsSplit.Length - 1))
                {
                    Pickup pickupObj;

                    // determine type
                    var pickupType = pickup.Split(',')[0];
                    if (pickupType == "Royale_Platformer.Model.PickupArmor")
                    {
                        pickupObj = new PickupArmor();
                    }
                    else
                    {
                        pickupObj = new PickupWeaponUpgrade();
                    }

                    // determine position
                    var position = pickupObj.Deserialize(pickup);

                    // Load
                    if (pickupType == "Royale_Platformer.Model.PickupArmor")
                    {
                        Pickups.Add(new PickupArmor(scene, armorSprite, new Vector2(position.X, position.Y)));
                    }
                    else
                    {
                        Pickups.Add(new PickupWeaponUpgrade(scene, weaponSprite, new Vector2(position.X, position.Y)));
                    }
                }
            });
        }