Esempio n. 1
0
	public static ABLevel LoadXmlLevel(string xmlString) {

		ABLevel level = new ABLevel();
		level.gameObjects = new List<ABGameObject>();

		using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
		{
			reader.ReadToFollowing("BirdsAmount");
			level.birdsAmount = Convert.ToInt32(reader.ReadElementContentAsString());

			reader.ReadToFollowing("GameObjects");

			while (reader.ReadToFollowing("GameObject"))
			{
				ABGameObject abObj = new ABGameObject();

				reader.MoveToAttribute("label");
				abObj.Label = Convert.ToInt32(reader.Value);

				Vector2 position = new Vector2();

				reader.MoveToAttribute("x");
				position.x = (float)Convert.ToDouble(reader.Value);

				reader.MoveToAttribute("y");
				position.y = (float)Convert.ToDouble(reader.Value);

				abObj.Position = position;

				level.gameObjects.Add(abObj);
			}
		}

		return level;
	}
Esempio n. 2
0
	public static void SaveXmlLevel(ABLevel level) {

		StringBuilder output = new StringBuilder();
		XmlWriterSettings ws = new XmlWriterSettings();
		ws.Indent = true;

		using (XmlWriter writer = XmlWriter.Create(output, ws))
		{
			writer.WriteStartElement("Level");

			writer.WriteStartElement("BirdsAmount");
			writer.WriteValue(level.birdsAmount);
			writer.WriteEndElement();

			writer.WriteStartElement("GameObjects");

			foreach(ABGameObject abObj in level.gameObjects)
			{
				writer.WriteStartElement("GameObject");
				writer.WriteAttributeString("label", abObj.Label.ToString());
				writer.WriteAttributeString("x", abObj.Position.x.ToString());
				writer.WriteAttributeString("y", abObj.Position.y.ToString());
				writer.WriteEndElement();
			}
		}

		int levelsAmountInResources = LoadAllLevels().Length;

		StreamWriter streamWriter = new StreamWriter("Assets/Resources/GeneratedLevels/genetic-level-" + (levelsAmountInResources + 1) + ".xml");
		streamWriter.WriteLine(output.ToString());
		streamWriter.Close();
	}
Esempio n. 3
0
    public void CarregaXmlLevel(string path)
    {
        string levelFileXML = LevelLoader.ReadXmlLevel(path);

        ABLevel levelFromFile = LevelLoader.LoadXmlLevel(levelFileXML);

        ELevel.instance.loadingLevelFromFile = true;
        Command addObject = null;
        int     index;

        foreach (BlockData block in levelFromFile.blocks)
        {
            index     = typePrefabIndex[block.type];
            addObject = new AddBlockCommand(block.type, block.rotation, block.x, block.y, block.material, blocks[index]);
            commandManager.ExecuteCommand(addObject);
        }

        foreach (OBjData pig in levelFromFile.pigs)
        {
            index     = typePrefabIndex[pig.type];
            addObject = new AddPigCommand(pig.type, pig.rotation, pig.x, pig.y, pigs[index]);
            commandManager.ExecuteCommand(addObject);
        }

        foreach (BirdData bird in levelFromFile.birds)
        {
            if (addBirdRef.ContainsKey(bird.type))
            {
                commandManager.ExecuteCommand(addBirdRef[bird.type]);
            }
        }

        ELevel.instance.loadingLevelFromFile = false;
    }
Esempio n. 4
0
    public void GenerateSubset(Vector2 position, int level)
    {
        ABLevel generateSubset = LevelList.Instance.GetLevel(level);

        LevelSimulator.GenerateSubset(generateSubset, position.x, position.y);
        //		countTNT = generateSubset.tnts.Count;
    }
Esempio n. 5
0
    //Change Subset's position into different position.
    public static void ChangeSubsetPosition(ABLevel subset, float x, float y)
    {
        float tempX = subset.triggerX;
        float tempY = subset.triggerY;

        subset.triggerX = x;
        subset.triggerY = y;
        foreach (OBjData gameObj in subset.pigs)
        {
            gameObj.x += x - tempX;
            gameObj.y += y - tempY;
        }

        foreach (BlockData gameObj in subset.blocks)
        {
            gameObj.x += x - tempX;
            gameObj.y += y - tempY;
        }

        foreach (PlatData gameObj in subset.platforms)
        {
            gameObj.x += x - tempX;
            gameObj.y += y - tempY;
        }

        foreach (OBjData gameObj in subset.tnts)
        {
            gameObj.x += x - tempX;
            gameObj.y += y - tempY;
        }
    }
Esempio n. 6
0
    public void SaveLevelOnScene()
    {
        //Use this code to save objs
        ABLevel level = EncodeLevel();

        //Save level to xml file
        SaveXmlLevel(level, Application.dataPath + ABConstants.CUSTOM_LEVELS_FOLDER, false);
    }
Esempio n. 7
0
    /// <summary>
    /// ABLG!!
    /// Functions for Generating Levels
    /// </summary>


    public void GenerateSubset(Vector2 position, int tag, int level)
    {
        ABLevel generateSubset = LevelList.Instance.GetLevel(level);

        LevelSimulator.GenerateSubset(generateSubset, tag, position.x, position.y);
//		Debug.Log ("pos : " + position.ToString () + ", trigger : " + generateSubset.triggerX.ToString () + generateSubset.triggerY.ToString ());
        //		countTNT = generateSubset.tnts.Count;
    }
Esempio n. 8
0
    // Use this for initialization
    void Start()
    {
        _pigs           = new List <ABPig>();
        _birds          = new List <ABBird>();
        _birdTrajectory = new List <ABParticle>();

        _levelCleared = false;

        if (!_isSimulation)
        {
            GetComponent <AudioSource>().PlayOneShot(_clips[0]);
            GetComponent <AudioSource>().PlayOneShot(_clips[1]);
        }

        GameObject slingshot = GameObject.Find("Slingshot");

        // If there are objects in the scene, use them to play
        if (_blocksTransform.childCount > 0 || _birdsTransform.childCount > 0 ||
            _plaftformsTransform.childCount > 0 || slingshot != null)
        {
            foreach (Transform bird in _birdsTransform)
            {
                AddBird(bird.GetComponent <ABBird>());
            }

            foreach (Transform block in _blocksTransform)
            {
                ABPig pig = block.GetComponent <ABPig>();
                if (pig != null)
                {
                    _pigs.Add(pig);
                }
            }

            LevelHeight = ABConstants.LEVEL_ORIGINAL_SIZE.y;
            LevelWidth  = ABConstants.LEVEL_ORIGINAL_SIZE.x;

            _slingshot = GameObject.Find("Slingshot");
        }
        else
        {
            ABLevel currentLevel = LevelList.Instance.GetCurrentLevel();

            if (currentLevel != null)
            {
                DecodeLevel(currentLevel);
                AdaptCameraWidthToLevel();

                _levelTimesTried = 0;
            }
        }
        GameObject gameObject = GameObject.Find("slingshot_base");

        if (gameObject)
        {
            _slingshotBaseTransform = gameObject.transform;
        }
    }
Esempio n. 9
0
    public override void OnCollisionEnter2D(Collision2D collision)
    {
        ABLevel addPoints = LevelList.Instance.GetCurrentLevel();

        if (collision.gameObject.tag == "test" && collision.otherCollider.gameObject.name != "Platform(Clone)" && collision.otherCollider.gameObject.name != "Ground")
        {
            addPoints.triggerX = collision.contacts [0].point.x;
            addPoints.triggerY = collision.contacts [0].point.y;
//            foreach (ContactPoint2D striggerPoint in collision.contacts) {
//                addPoints.triggerX = striggerPoint.point.x;
//                addPoints.triggerY = striggerPoint.point.y;
//            }
//			Debug.Log (collision.contacts [0].point);
        }
        else if (collision.gameObject.name != "Platform(Clone)" && collision.gameObject.name != "Ground" && collision.otherCollider.gameObject.tag == "test")
        {
            addPoints.triggerX = collision.contacts [0].point.x;
            addPoints.triggerY = collision.contacts [0].point.y;
//			Debug.Log (collision.contacts [0].point);
        }
        if (collision.gameObject.name == "Ground")
        {
            foreach (ContactPoint2D groundPoint in collision.contacts)
            {
                if (!addPoints.grounds.Contains(groundPoint.point.x) && groundPoint.point.x <= 5)
                {
                    addPoints.grounds.Add(groundPoint.point.x);
                }
            }
        }
        if (collision.gameObject.tag == "Bird")
        {
            ABBird bird = collision.gameObject.GetComponent <ABBird>();
            float  collisionMagnitude = collision.relativeVelocity.magnitude;
            float  birdDamage         = 1f;

            switch (_material)
            {
            case MATERIALS.wood:
                birdDamage = bird._woodDamage;
                break;

            case MATERIALS.stone:
                birdDamage = bird._stoneDamage;
                break;

            case MATERIALS.ice:
                birdDamage = bird._iceDamage;
                break;
            }

            DealDamage(collisionMagnitude * birdDamage);
        }
        else
        {
            base.OnCollisionEnter2D(collision);
        }
    }
Esempio n. 10
0
    public void UpdateActiveLevelInfo()
    {
        ABLevel          currentLevel = LevelList.Instance.GetCurrentLevel();
        bool             found        = false;
        List <BlockData> toRemove     = new List <BlockData>();

        for (int i = 0; i < currentLevel.blocks.Count; i++)
        {
            foreach (Transform t in _blocksTransform)
            {
                Rigidbody2D[] bodies = t.GetComponentsInChildren <Rigidbody2D>();
                foreach (Rigidbody2D body in bodies)
                {
                    if (body.GetComponent <ABGameObject>().id == currentLevel.blocks[i].id)
                    {
                        currentLevel.blocks[i].averageVel = body.GetComponent <ABGameObject>()._averageMagnitudeVelocity;
                        found = true;
                    }
                }
            }

            if (!found)
            {
                toRemove.Add(currentLevel.blocks[i]);
            }

            found = false;
        }

        for (int i = 0; i < toRemove.Count; i++)
        {
            currentLevel.blocks.Remove(toRemove[i]);
        }

        List <OBjData> pigsRemoved = new List <OBjData>();

        for (int i = 0; i < currentLevel.pigs.Count; i++)
        {
            foreach (ABPig pig in _pigs)
            {
                if (pig.id == currentLevel.pigs[i].id)
                {
                    currentLevel.pigs[i].averageVel = pig._averageMagnitudeVelocity;
                    found = true;
                }
            }
            if (!found)
            {
                pigsRemoved.Add(currentLevel.pigs[i]);
            }
            found = false;
        }

        for (int i = 0; i < pigsRemoved.Count; i++)
        {
            currentLevel.pigs.Remove(pigsRemoved[i]);
        }
    }
Esempio n. 11
0
    // Use this for get another level
    public ABLevel GetLevel(int index)
    {
        if (index < 0 || index >= _levels.Length)
        {
            return(null);
        }

        ABLevel level = _levels[index];

        return(level);
    }
Esempio n. 12
0
	public static ABLevel[] LoadAllLevels() {
	
		TextAsset []levelsXmlData = Resources.LoadAll<TextAsset>("GeneratedLevels/");

		ABLevel []levels = new ABLevel[levelsXmlData.Length];

		for(int i = 0; i < levelsXmlData.Length; i++)
			levels[i] = LoadXmlLevel(levelsXmlData[i].text);

		return levels;
	}
Esempio n. 13
0
 public void AddPigIntoSubset(ABLevel level)
 {
     if (level.tnts.Count != 0)
     {
         //string[] types = new string[3]{"BasicBig","BasicMedium","BasicSmall"};
         //string type = types[UnityEngine.Random.Range(0,types.Length)];
         string  type      = "BasicSmall";
         int     randomTNT = UnityEngine.Random.Range(0, level.tnts.Count);      // find tnt
         OBjData tnt       = level.tnts[randomTNT];
         level.pigs.Add(new OBjData(type, 0, tnt.x, tnt.y + 0.5f));              // add pig
     }
 }
Esempio n. 14
0
    public static void GenerateSubset(ABLevel subset, int tag, float x, float y)
    {
        float tempX = subset.triggerX;
        float tempY = subset.triggerY;

        subset.triggerX = x;
        subset.triggerY = y;
        foreach (OBjData gameObj in subset.pigs)
        {
            gameObj.x += x - tempX;
            gameObj.y += y - tempY;
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);
            GameObject temp     = ABWorldAssets.PIGS [gameObj.type];
            temp.tag = tag.ToString();
            ABGameWorld.Instance.AddPig(temp, pos, rotation);
        }

        foreach (BlockData gameObj in subset.blocks)
        {
            gameObj.x += x - tempX;
            gameObj.y += y - tempY;
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);
            GameObject temp     = ABWorldAssets.BLOCKS[gameObj.type];
            temp.tag = tag.ToString();
            GameObject block    = ABGameWorld.Instance.AddBlock(temp, pos, rotation);
            MATERIALS  material = (MATERIALS)System.Enum.Parse(typeof(MATERIALS), gameObj.material);
            block.GetComponent <ABBlock> ().SetMaterial(material);
        }

        foreach (PlatData gameObj in subset.platforms)
        {
            gameObj.x += x - tempX;
            gameObj.y += y - tempY;
            Vector2    pos  = new Vector2(gameObj.x, gameObj.y);
            GameObject temp = ABWorldAssets.PLATFORM;
            temp.tag = tag.ToString();
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);
            ABGameWorld.Instance.AddPlatform(temp, pos, rotation, gameObj.scaleX, gameObj.scaleY);
        }

        foreach (OBjData gameObj in subset.tnts)
        {
            gameObj.x += x - tempX;
            gameObj.y += y - tempY;
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);
            GameObject temp     = ABWorldAssets.TNT;
            temp.tag = tag.ToString();
            ABGameWorld.Instance.AddBlock(temp, pos, rotation);
        }
    }
Esempio n. 15
0
    // Use this for initialization
    public ABLevel SetLevel(int index)
    {
        if (index < 0 || index >= _levels.Length)
        {
            return(null);
        }

        CurrentIndex = index;
        ABLevel level = _levels [CurrentIndex];

        return(level);
    }
Esempio n. 16
0
    public ABLevel EncodeSymmetricalLevel()
    {
        float   platformMiddlePoint = 0;
        ABLevel level = new ABLevel();

        level.width      = LevelList.Instance.GetCurrentLevel().width;
        level.camera     = LevelList.Instance.GetCurrentLevel().camera;
        level.slingshot  = LevelList.Instance.GetCurrentLevel().slingshot;
        level.birds      = LevelList.Instance.GetCurrentLevel().birds;
        level.platforms  = LevelList.Instance.GetCurrentLevel().platforms;
        level.horizontal = LevelList.Instance.GetCurrentLevel().horizontal;

        foreach (Transform child in GameObject.Find("Platforms").transform)
        {
            PlatData obj = new PlatData();
            obj.type             = child.name;
            obj.x                = child.transform.position.x;
            obj.y                = child.transform.position.y;
            obj.rotation         = child.transform.rotation.eulerAngles.z;
            obj.scaleX           = child.transform.localScale.x;
            obj.scaleY           = child.transform.localScale.y;
            platformMiddlePoint += child.transform.position.x;
//            level.platforms.Add(obj);
        }
        platformMiddlePoint /= level.platforms.Count;
        PlatformMiddle       = platformMiddlePoint;
        foreach (Transform child in GameObject.Find("Blocks").transform)
        {
            string type = child.name;
            float  x    = 2 * platformMiddlePoint;
            x = x - child.transform.position.x;
            float y        = child.transform.position.y;
            float rotation = child.transform.rotation.eulerAngles.z;

            if (child.GetComponent <ABPig>() != null)
            {
                level.pigs.Add(new OBjData(type, rotation, x, y));
            }
            else if (child.GetComponent <ABBlock>() != null)
            {
                string material = child.GetComponent <ABBlock>()._material.ToString();
                level.blocks.Add(new BlockData(type, rotation, x, y, material));
            }
            else if (child.GetComponent <ABTNT>() != null)
            {
                level.tnts.Add(new OBjData(type, rotation, x, y));
            }
        }


        return(level);
    }
Esempio n. 17
0
    // Use this for initialization
    public ABLevel NextLevel()
    {
        if (CurrentIndex == _levels.Length - 1)
        {
            return(null);
        }

        ABLevel level = _levels [CurrentIndex];

        CurrentIndex++;

        return(level);
    }
Esempio n. 18
0
    // Use this for initialization
    void Start()
    {
        _pigs           = new List <ABPig>();
        _birds          = new List <ABBird>();
        _birdTrajectory = new List <ABParticle>();

        _levelCleared = false;

        if (!_isSimulation)
        {
            GetComponent <AudioSource>().PlayOneShot(_clips[0]);
            GetComponent <AudioSource>().PlayOneShot(_clips[1]);
        }

        // If there are objects in the scene, use them to play
        if (_blocksTransform.childCount > 0 || _birdsTransform.childCount > 0)
        {
            foreach (Transform bird in _birdsTransform)
            {
                AddBird(bird.GetComponent <ABBird>());
            }

            foreach (Transform block in _blocksTransform)
            {
                ABPig pig = block.GetComponent <ABPig>();
                if (pig != null)
                {
                    _pigs.Add(pig);
                }
            }
        }
        else
        {
            ABLevel currentLevel = LevelList.Instance.GetCurrentLevel();

            if (currentLevel != null)
            {
                DecodeLevel(currentLevel);
                AdaptCameraWidthToLevel();

                _levelTimesTried = 0;

                _slingshotBaseTransform = GameObject.Find("slingshot_base").transform;
            }
        }
        if (_isSimulation)
        {
            StartCoroutine(SkipSimulationLevel());
        }
    }
    public ABLevel EncodeLevel()
    {
        ABLevel level = new ABLevel();

        level.birds     = new List <BirdData>();
        level.pigs      = new List <OBjData>();
        level.blocks    = new List <BlockData>();
        level.platforms = new List <PlatData>();

        foreach (Transform child in GameObject.Find("Birds").transform)
        {
            string type = child.name;
            level.birds.Add(new BirdData(type));
        }

        foreach (Transform child in GameObject.Find("Blocks").transform)
        {
            string type     = child.name;
            float  x        = child.transform.position.x;
            float  y        = child.transform.position.y;
            float  rotation = child.transform.rotation.eulerAngles.z;

            if (child.GetComponent <ABPig> () != null)
            {
                level.pigs.Add(new OBjData(type, rotation, x, y));
            }
            else if (child.GetComponent <ABBlock> () != null)
            {
                string material = child.GetComponent <ABBlock> ()._material.ToString();
                level.blocks.Add(new BlockData(type, rotation, x, y, material));
            }
        }

        foreach (Transform child in GameObject.Find("Platforms").transform)
        {
            PlatData obj = new PlatData();

            obj.type     = child.name;
            obj.x        = child.transform.position.x;
            obj.y        = child.transform.position.y;
            obj.rotation = child.transform.rotation.eulerAngles.z;
            obj.scaleX   = child.transform.localScale.x;
            obj.scaleY   = child.transform.localScale.y;

            level.platforms.Add(obj);
        }

        return(level);
    }
    public void DecodeLevel(ABLevel level)
    {
        foreach (OBjData gameObj in level.pigs)
        {
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);

            GameObject pig = InstantiateGameObject(ABWorldAssets.PIGS[gameObj.type]);
            pig.transform.parent   = GameObject.Find("Blocks").transform;
            pig.transform.position = pos;
            pig.transform.rotation = rotation;
        }

        _birdsAdded = 0;
        foreach (BirdData gameObj in level.birds)
        {
            CreateBird(gameObj.type);
            _birdsAdded++;
        }

        foreach (BlockData gameObj in level.blocks)
        {
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);

            GameObject block = InstantiateGameObject(ABWorldAssets.BLOCKS[gameObj.type]);
            block.transform.parent   = GameObject.Find("Blocks").transform;
            block.transform.position = pos;
            block.transform.rotation = rotation;

            MATERIALS material = (MATERIALS)Enum.Parse(typeof(MATERIALS), gameObj.material);
            BlockEditor.UpdateBlockMaterial(block.GetComponent <ABBlock>(), material);
        }

        foreach (OBjData gameObj in level.platforms)
        {
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);

            GameObject platform = InstantiateGameObject(ABWorldAssets.PLATFORM);
            platform.transform.parent   = GameObject.Find("Platforms").transform;
            platform.transform.position = pos;
            platform.transform.rotation = rotation;
        }
    }
    void LoadLevel()
    {
        string path = EditorUtility.OpenFilePanel("Select level to open", "Assets/Resources/Levels/", "");

        if (path != "")
        {
            CleanLevel();

            Debug.Log(path);

//			TextAsset levelFile = (TextAsset)Resources.Load (finalPath);
            string levelText = LevelLoader.ReadXmlLevel(path);
            Debug.Log(levelText);
            ABLevel level = LevelLoader.LoadXmlLevel(levelText);

            DecodeLevel(level);
        }
    }
Esempio n. 22
0
    /// <summary>
    /// ABLG!!
    /// Functions for Generating Levels
    /// </summary>


    public void GenerateLevel()
    {
        /// Generate Level
        if (LevelSimulator.generateLevel)
        {
            int round = UnityEngine.Random.Range(2, 3);
            for (int i = 0; i < round; i++)
            {
                //Random subset for generate level
                int     selectedLevel   = UnityEngine.Random.Range(0, LevelList.Instance.GetAllLevel().Length / 2);
                ABLevel nextLevelSubset = LevelList.Instance.GetLevel(selectedLevel);
                LevelSimulator.ChangeSubsetPosition(nextLevelSubset, UnityEngine.Random.Range(4f, 6f), UnityEngine.Random.Range(-3f, 2f));
                LevelSimulator.GenerateSubset(nextLevelSubset, nextLevelSubset.triggerX, nextLevelSubset.triggerY);
            }
            //Save generate level on scene to xml
            _levelLoader.SaveLevelOnScene();
        }
    }
Esempio n. 23
0
 private void init()
 {
     birdsType    = new List <string> ();
     currentLevel = LevelList.Instance.GetCurrentLevel();
     foreach (BirdData gameObj in currentLevel.birds)
     {
         birdsType.Add(gameObj.type);
     }
     _blocksTransform     = GameObject.Find("Blocks").transform;
     _birdsTransform      = GameObject.Find("Birds").transform;
     _plaftformsTransform = GameObject.Find("Platforms").transform;
     this.DataID          = 0;
     this.FullGameData    = "";
     if (GameDataList != null)
     {
         GameDataList.Clear();
     }
     //GameDataList = new List<string> ();
 }
Esempio n. 24
0
    public void initx()
    {
        ClearWorld();

        if (_levelFailedBanner.activeSelf)
        {
            _levelFailedBanner.SetActive(false);
        }

        if (_levelClearedBanner.activeSelf)
        {
            _levelClearedBanner.SetActive(false);
        }

        GameplayCam = GameObject.Find("Camera").GetComponent <ABGameplayCamera>();

        _pigs           = new List <ABPig>();
        _birds          = new List <ABBird>();
        _birdTrajectory = new List <ABParticle>();
        _levelCleared   = false;

        if (!_isSimulation)
        {
            GetComponent <AudioSource>().PlayOneShot(_clips[0]);
            GetComponent <AudioSource>().PlayOneShot(_clips[1]);
        }

        ABLevel level = LevelList.Instance.GetCurrentLevel();

        DecodeLevel(level);

        _slingshotBaseTransform = GameObject.Find("slingshot_base").transform;
        _blocksTransform        = GameObject.Find("Blocks").transform;
        _birdsTransform         = GameObject.Find("Birds").transform;
        _plaftformsTransform    = GameObject.Find("Platforms").transform;

        HUD.Instance.gameObject.SetActive(true);
        //if (LevelSimulator.generateLevel == false)
        //{
        //    CurrentLevel.usefulLevel = false;
        //}
    }
Esempio n. 25
0
	public static ABLevel GameObjectsToABLevel(GameObject []gameObjs)
	{
		ABLevel level = new ABLevel();

		for(int i = 0; i < gameObjs.Length; i++)
		{
			if(gameObjs[i].tag == "Bird")
			{
				level.birdsAmount++;
			}
			else
			{
				ABGameObject abGameObj = new ABGameObject();
				abGameObj.Position = gameObjs[i].transform.position;
				abGameObj.Label = GameWorld.Instance.GetTemplateIndex(gameObjs[i]);
				level.gameObjects.Add(abGameObj);
			}
		}

		return level;
	}
Esempio n. 26
0
    public void ChangePigIntoBlock(ABLevel level)
    {
        if (level.pigs.Count != 0)
        {
            string[] types = new string[2] {
                "CircleSmall", "RectTiny"
            };
            string type = types[UnityEngine.Random.Range(0, types.Length)];            //random block type

            string[] materials = new string[3] {
                "wood", "ice", "stone"
            };
            string material = materials[UnityEngine.Random.Range(0, materials.Length)];  //random block material

            int     randomPig = UnityEngine.Random.Range(0, level.pigs.Count);           // find pig
            OBjData pig       = level.pigs [randomPig];                                  //Get pig`

            level.blocks.Add(new BlockData(type, pig.rotation, pig.x, pig.y, material)); // add block
            level.pigs.RemoveAt(randomPig);                                              // remove pig
        }
    }
Esempio n. 27
0
    public void AddSubsetIntoList(int id, ABLevel lvl, Vector2 pos)
    {
        Vector2 temp = new Vector2(0, 0);

        foreach (BlockData b in lvl.blocks)
        {
            temp += new Vector2(b.x, b.y);
        }
        foreach (PlatData p in lvl.platforms)
        {
            temp += new Vector2(p.x, p.y);
        }
        temp /= (lvl.platforms.Count + lvl.blocks.Count);
        subsetList.Add(new SubsetInfo {
            id           = id,
            triggerPoint = pos,
            lvl          = lvl,
            horizontal   = lvl.horizontal,
            center       = temp,
            movement     = 0
        });
    }
Esempio n. 28
0
 public static void SetSubsetTrigger(ABLevel subset, float x, float y)
 {
     subset.triggerX = x;
     subset.triggerY = y;
 }
Esempio n. 29
0
    public void DecodeLevel(ABLevel currentLevel)
    {
        ClearWorld();

        LevelHeight = ABConstants.LEVEL_ORIGINAL_SIZE.y;
        LevelWidth  = (float)currentLevel.width * ABConstants.LEVEL_ORIGINAL_SIZE.x;

        Vector3 cameraPos = GameplayCam.transform.position;

        cameraPos.x = currentLevel.camera.x;
        cameraPos.y = currentLevel.camera.y;
        GameplayCam.transform.position = cameraPos;

        GameplayCam._minWidth = currentLevel.camera.minWidth;
        GameplayCam._maxWidth = currentLevel.camera.maxWidth;

        Vector3 landscapePos  = ABWorldAssets.LANDSCAPE.transform.position;
        Vector3 backgroundPos = ABWorldAssets.BACKGROUND.transform.position;

        if (currentLevel.width > 1)
        {
            landscapePos.x  -= LevelWidth / 4f;
            backgroundPos.x -= LevelWidth / 4f;
        }

        for (int i = 0; i < currentLevel.width; i++)
        {
            GameObject landscape = (GameObject)Instantiate(ABWorldAssets.LANDSCAPE, landscapePos, Quaternion.identity);
            landscape.transform.parent = transform;

            float screenRate = currentLevel.camera.maxWidth / LevelHeight;
            if (screenRate > 2f)
            {
                for (int j = 0; j < (int)screenRate; j++)
                {
                    Vector3 deltaPos = Vector3.down * (LevelHeight / 1.5f + (j * 2f));
                    Instantiate(ABWorldAssets.GROUND_EXTENSION, landscapePos + deltaPos, Quaternion.identity);
                }
            }

            landscapePos.x += ABConstants.LEVEL_ORIGINAL_SIZE.x - 0.01f;

            if (!RatingSystem.IsGenerating)
            {
                GameObject background = (GameObject)Instantiate(ABWorldAssets.BACKGROUND, backgroundPos, Quaternion.identity);
                background.transform.parent = GameplayCam.transform;
                backgroundPos.x            += ABConstants.LEVEL_ORIGINAL_SIZE.x - 0.01f;
            }
        }

        Vector2 slingshotPos = new Vector2(currentLevel.slingshot.x, currentLevel.slingshot.y);

        _slingshot                  = (GameObject)Instantiate(ABWorldAssets.SLINGSHOT, slingshotPos, Quaternion.identity);
        _slingshot.name             = "Slingshot";
        _slingshot.transform.parent = transform;

        foreach (BirdData gameObj in currentLevel.birds)
        {
            AddBird(ABWorldAssets.BIRDS[gameObj.type], ABWorldAssets.BIRDS[gameObj.type].transform.rotation);
        }

        foreach (OBjData gameObj in currentLevel.pigs)
        {
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);
            AddPig(ABWorldAssets.PIGS[gameObj.type], pos, rotation);
        }

        foreach (BlockData gameObj in currentLevel.blocks)
        {
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);

            GameObject block = AddBlock(ABWorldAssets.BLOCKS[gameObj.type], pos, rotation);

            MATERIALS material = (MATERIALS)System.Enum.Parse(typeof(MATERIALS), gameObj.material);
            block.GetComponent <ABBlock> ().SetMaterial(material);
        }

        foreach (PlatData gameObj in currentLevel.platforms)
        {
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);

            AddPlatform(ABWorldAssets.PLATFORM, pos, rotation, gameObj.scaleX, gameObj.scaleY);
        }

        foreach (OBjData gameObj in currentLevel.tnts)
        {
            Vector2    pos      = new Vector2(gameObj.x, gameObj.y);
            Quaternion rotation = Quaternion.Euler(0, 0, gameObj.rotation);

            AddBlock(ABWorldAssets.TNT, pos, rotation);
        }

        StartWorld();
    }
Esempio n. 30
0
    // Use this for initialization
    void Start()
    {
        //_isSimulation = RatingSystem.IsGenerating;
        if (RatingSystem.IsGenerating)
        {
            //Debug.Log("In level " + LevelList.Instance.CurrentIndex);
            HideViewCam.GetCamera().enabled = true;
            hudObject.SetActive(false);
            stabilityCounter = 0.3f;
            timeToStable     = 0f;
            GameplayCam.GetCamera().backgroundColor = new Color(0, 0, 0);
        }
        else
        {
            //Debug.Log("Not generating screenshots for level " + LevelList.Instance.CurrentIndex);
            //HideViewCam.GetCamera().enabled = false;
            HideViewCam.gameObject.SetActive(false);
            hudObject.SetActive(true);
        }

        _pigs           = new List <ABPig>();
        _birds          = new List <ABBird>();
        _birdTrajectory = new List <ABParticle>();

        _levelCleared = false;

        if (!_isSimulation)
        {
            GetComponent <AudioSource>().PlayOneShot(_clips[0]);
            GetComponent <AudioSource>().PlayOneShot(_clips[1]);
        }

        GameObject slingshot = GameObject.Find("Slingshot");

        // If there are objects in the scene, use them to play
        if (_blocksTransform.childCount > 0 || _birdsTransform.childCount > 0 ||
            _plaftformsTransform.childCount > 0 || slingshot != null)
        {
            foreach (Transform bird in _birdsTransform)
            {
                AddBird(bird.GetComponent <ABBird>());
            }

            foreach (Transform block in _blocksTransform)
            {
                ABPig pig = block.GetComponent <ABPig>();
                if (pig != null)
                {
                    _pigs.Add(pig);
                }
            }

            LevelHeight = ABConstants.LEVEL_ORIGINAL_SIZE.y;
            LevelWidth  = ABConstants.LEVEL_ORIGINAL_SIZE.x;

            _slingshot = GameObject.Find("Slingshot");
        }
        else
        {
            ABLevel currentLevel = LevelList.Instance.GetCurrentLevel();

            if (currentLevel != null)
            {
                DecodeLevel(currentLevel);
                AdaptCameraWidthToLevel();

                _levelTimesTried = 0;
            }
        }

        _slingshotBaseTransform = GameObject.Find("slingshot_base").transform;
    }
Esempio n. 31
0
    public static ABLevel LoadXmlLevel(string xmlString)
    {
        ABLevel level = new ABLevel();

        using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
        {
            reader.ReadToFollowing("Level");

            level.width = 1;
            if (reader.GetAttribute("width") != null)
            {
                reader.MoveToAttribute("width");
                level.width = (int)Convert.ToInt32(reader.Value);
            }

            //reader.ReadToFollowing("Simulation");
            //reader.MoveToAttribute("time");
            //level.time = (float)Convert.ToDouble(reader.Value);
            //level.simulated = level.time != 0;

            reader.ReadToFollowing("Camera");

            reader.MoveToAttribute("x");
            level.camera.x = (float)Convert.ToDouble(reader.Value);

            reader.MoveToAttribute("y");
            level.camera.y = (float)Convert.ToDouble(reader.Value);

            reader.MoveToAttribute("minWidth");
            level.camera.minWidth = (float)Convert.ToDouble(reader.Value);

            reader.MoveToAttribute("maxWidth");
            level.camera.maxWidth = (float)Convert.ToDouble(reader.Value);

            reader.ReadToFollowing("Birds");
            reader.Read();

            while (reader.Read())
            {
                string nodeName = reader.LocalName;
                if (nodeName == "Birds")
                {
                    break;
                }

                reader.MoveToAttribute("type");
                string type = reader.Value;

                level.birds.Add(new BirdData(type));
                reader.Read();
            }

            reader.ReadToFollowing("Slingshot");

            reader.MoveToAttribute("x");
            level.slingshot.x = (float)Convert.ToDouble(reader.Value);

            reader.MoveToAttribute("y");
            level.slingshot.y = (float)Convert.ToDouble(reader.Value);

            reader.ReadToFollowing("GameObjects");
            reader.Read();
            int i = 0;
            while (reader.Read())
            {
                string nodeName = reader.LocalName;
                if (nodeName == "GameObjects")
                {
                    break;
                }

                reader.MoveToAttribute("type");
                string type = reader.Value;

                string material = "";
                if (reader.GetAttribute("material") != null)
                {
                    reader.MoveToAttribute("material");
                    material = reader.Value;
                }

                reader.MoveToAttribute("x");
                float x = (float)Convert.ToDouble(reader.Value);

                reader.MoveToAttribute("y");
                float y = (float)Convert.ToDouble(reader.Value);

                float rotation = 0f;
                if (reader.GetAttribute("rotation") != null)
                {
                    reader.MoveToAttribute("rotation");
                    rotation = (float)Convert.ToDouble(reader.Value);
                }

                if (nodeName == "Block")
                {
                    level.blocks.Add(new BlockData(type, rotation, x, y, material));
                    reader.Read();
                    level.blocks[level.blocks.Count - 1].id = i;
                }
                else if (nodeName == "Pig")
                {
                    level.pigs.Add(new OBjData(type, rotation, x, y));
                    reader.Read();
                    level.pigs[level.pigs.Count - 1].id = i;
                }
                else if (nodeName == "TNT")
                {
                    level.tnts.Add(new OBjData(type, rotation, x, y));
                    reader.Read();
                    level.tnts[level.tnts.Count - 1].id = i;
                }
                else if (nodeName == "Platform")
                {
                    float scaleX = 1f;
                    if (reader.GetAttribute("scaleX") != null)
                    {
                        reader.MoveToAttribute("scaleX");
                        scaleX = (float)Convert.ToDouble(reader.Value);
                    }

                    float scaleY = 1f;
                    if (reader.GetAttribute("scaleY") != null)
                    {
                        reader.MoveToAttribute("scaleY");
                        scaleY = (float)Convert.ToDouble(reader.Value);
                    }

                    level.platforms.Add(new PlatData(type, rotation, x, y, scaleX, scaleY));
                    reader.Read();
                    level.platforms[level.platforms.Count - 1].id = i;
                }
                i++;
            }
        }

        return(level);
    }
Esempio n. 32
0
    public static void SaveXmlLevel(ABLevel level, string path)
    {
        //StringBuilder output = new StringBuilder();
        MemoryStream      output = new MemoryStream();
        XmlWriterSettings ws     = new XmlWriterSettings();

        ws.Encoding = new UTF8Encoding(false);
        ws.Indent   = true;

        File.SetAttributes(path, FileAttributes.Normal);
        FileIOPermission filePermission =
            new FileIOPermission(FileIOPermissionAccess.AllAccess, path);

        using (FileStream fs = new FileStream(path, FileMode.Create))
        {
            using (XmlWriter writer = XmlWriter.Create(output, ws))
            {
                writer.WriteStartElement("Level");
                writer.WriteAttributeString("width", level.width.ToString());

                writer.WriteStartElement("Camera");
                writer.WriteAttributeString("x", level.camera.x.ToString());
                writer.WriteAttributeString("y", level.camera.y.ToString());
                writer.WriteAttributeString("minWidth", level.camera.minWidth.ToString());
                writer.WriteAttributeString("maxWidth", level.camera.maxWidth.ToString());
                writer.WriteEndElement();

                writer.WriteStartElement("Birds");

                foreach (BirdData abBird in level.birds)
                {
                    writer.WriteStartElement("Bird");
                    writer.WriteAttributeString("type", abBird.type.ToString());
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();

                writer.WriteStartElement("Slingshot");
                writer.WriteAttributeString("x", level.slingshot.x.ToString());
                writer.WriteAttributeString("y", level.slingshot.y.ToString());
                writer.WriteEndElement();

                writer.WriteStartElement("GameObjects");

                foreach (BlockData abObj in level.blocks)
                {
                    writer.WriteStartElement("Block");
                    writer.WriteAttributeString("type", abObj.type.ToString());
                    writer.WriteAttributeString("material", abObj.material.ToString());
                    writer.WriteAttributeString("x", abObj.x.ToString());
                    writer.WriteAttributeString("y", abObj.y.ToString());
                    writer.WriteAttributeString("rotation", abObj.rotation.ToString());
                    writer.WriteAttributeString("id", abObj.id.ToString());
                    writer.WriteAttributeString("aVelocity", abObj.averageVel.ToString());

                    writer.WriteEndElement();
                }

                foreach (OBjData abObj in level.pigs)
                {
                    writer.WriteStartElement("Pig");
                    writer.WriteAttributeString("type", abObj.type.ToString());
                    writer.WriteAttributeString("x", abObj.x.ToString());
                    writer.WriteAttributeString("y", abObj.y.ToString());
                    writer.WriteAttributeString("rotation", abObj.rotation.ToString());
                    writer.WriteAttributeString("id", abObj.id.ToString());
                    writer.WriteAttributeString("aVelocity", abObj.averageVel.ToString());

                    writer.WriteEndElement();
                }

                foreach (OBjData abObj in level.tnts)
                {
                    writer.WriteStartElement("TNT");
                    writer.WriteAttributeString("type", abObj.type.ToString());
                    writer.WriteAttributeString("x", abObj.x.ToString());
                    writer.WriteAttributeString("y", abObj.y.ToString());
                    writer.WriteAttributeString("rotation", abObj.rotation.ToString());
                    writer.WriteAttributeString("id", abObj.id.ToString());
                    writer.WriteAttributeString("aVelocity", abObj.averageVel.ToString());

                    writer.WriteEndElement();
                }

                foreach (PlatData abObj in level.platforms)
                {
                    writer.WriteStartElement("Platform");
                    writer.WriteAttributeString("type", abObj.type.ToString());
                    writer.WriteAttributeString("x", abObj.x.ToString());
                    writer.WriteAttributeString("y", abObj.y.ToString());
                    writer.WriteAttributeString("rotation", abObj.rotation.ToString());
                    writer.WriteAttributeString("scaleX", abObj.scaleX.ToString());
                    writer.WriteAttributeString("scaleY", abObj.scaleY.ToString());
                    writer.WriteEndElement();
                }
            }
        }

        StreamWriter streamWriter = new StreamWriter(path, false, Encoding.UTF8);

        streamWriter.WriteLine(Encoding.Default.GetString(output.ToArray()));
        streamWriter.Close();
    }
Esempio n. 33
0
    void Update()
    {
        // Check if birds was trown, if it died and swap them when needed
//        ManageBirds();
        //if(Input.GetKey(KeyCode.Space))
        //{
        //}

        //Evaluate Subsets

        if (!LevelSimulator.generateLevel)
        {
            if (!PreLevelCheck)
            {
                LevelShaking();
                if (IsLevelStable())
                {
                    if (CurrentLevel.levelShaking)
                    {
                        NextLevel();
                    }
                    PreLevelCheck = true;
//					CurrentLevel = _levelLoader.EncodeLevel ();
                    SymmetricalLevel = _levelLoader.EncodeSymmetricalLevel();
                }
            }
            else
            {
                if (!HorizontalEvaluationStart)
                {
                    CurrentLevel.horizontal     = true;
                    SymmetricalLevel.horizontal = true;
                    SubsetSimulationHorizontal();
                }
                else
                {
                    CurrentLevel.horizontal     = false;
                    SymmetricalLevel.horizontal = false;
                    SubsetSimulationVertical();
                }
                //check useful levels
                CheckUseful(_blocksTransform);
            }
        }
        else
        //Generate Levels
        {
            CheckOverlap();

            if (initiate)
            {
                trajectory = new List <ObjTrack> ();
                ShootInitiator();

                AdaptCameraWidthToLevel();
            }
            else if (!IsLevelStable())
            {
                RecordTrajectory();
            }
//			else (!initiate && IsLevelStable ()) {
            else
            {
//				DeleteStaticSubsetFromList ();
                SaveCurrentLevel();
                int levelID = UnityEngine.Random.Range(0, LevelList.Instance.GetAllLevel().Length);
                if (trajectory.Count == 0)
                {
                    LevelList.Instance.CurrentIndex = UnityEngine.Random.Range(0, levelID);
                    ABSceneManager.Instance.LoadScene(SceneManager.GetActiveScene().name);
                    subsetList = new List <SubsetInfo> ();
                    print("Finding different initial subset...");
                    return;
                }

                Vector2 subsetPos = trajectory [UnityEngine.Random.Range(trajectory.Count / 2, trajectory.Count)].pos;
                ABLevel lvl       = LevelList.Instance.GetLevel(levelID);
                AddSubsetIntoList(levelID, lvl, subsetPos);
                initiate = true;
                initx();
//				mapChanges.Add (trans.GetInstanceID (), new MapChanges {
//					lastPosition = trans.position,
//					distanceObjectMoved = 0f,
//					timeObjectMoved = 0f
//				});
            }
        }
    }
Esempio n. 34
0
 void Start()
 {
     CurrentLevel     = LevelList.Instance.GetCurrentLevel();
     SaveLevel        = new LevelLoader();
     CurrentGameWorld = new ABGameWorld();
 }