Пример #1
0
    protected override void genNextLevel()
    {
        // Destroy old objects.
        for(int i=0; i<currLvlItems.Count; i++)
        {
            DestroyImmediate(GameObject.Find(currLvlItems[i]));
        }

        // Generate new sentence.
        currLvlConfig = (SHeroLevelConfig) lvlConfigGen.getNextLevelConfig(wordPoolMaxCapacity);

        musicMarkerSpeedPerWord_InSec = (3-currLvlConfig.speed)*5f;

        //debugFullSentence = currLvlConfig.getLevelSentence(true);
        recordPresentedConfig(currLvlConfig);

        if(serverCommunication != null) { serverCommunication.wordDisplayed(currLvlConfig.getLevelSentence(false),currLvlConfig.languageArea,currLvlConfig.difficulty); }

        // Show new song sentence.
        renderSongSentence();

        // Populate word pool.
        renderWordPool();

        playerSelectedHoleItems = new Dictionary<int, string>();

        mistakesInCurrSentence = 0;

        //musicMarkerIndex = -1;
        triggerMusicMarkerStartDelay();
    }
Пример #2
0
    private List<SentenceSegment> extractMinimalSentenceSegments(SHeroLevelConfig para_config)
    {
        List<SentenceSegment> retSegmentList = new List<SentenceSegment>();

        string fullSentence = para_config.getLevelSentence(true);
        int sentenceCharLengthWhite = fullSentence.Replace("'","").Length;
        //fullSentence = fullSentence.Replace(" ","");
        //int sentenceCharLength = fullSentence.Replace("'","").Length;

        //Debug.Log(fullSentence+" "+sentenceCharLengthWhite+" characters");

        SentenceSegment regSeg = new SentenceSegment("",0,currLvlConfig.positionIsHole(0),0);
        string[] sentenceWordArr = para_config.sentenceWords;

        int maxHoleCharacters = 0;
        List<int> holeSizes = new List<int>();

        float tmpTotPerc = 0;
        for(int i=0; i<sentenceWordArr.Length; i++)
        {
            string tmpSentenceWord = sentenceWordArr[i];
            bool currIsHole = currLvlConfig.positionIsHole(i);

            if((regSeg.isHole == false)&&(currIsHole == false))
            {
                regSeg.segmentText += (tmpSentenceWord);
                regSeg.sentenceCharPerc = (regSeg.segmentText.Length * 1.0f) / (sentenceCharLengthWhite * 1.0f);
                regSeg.isHole = false;
                //Debug.Log("Accumulated *"+regSeg.segmentText+"* "+regSeg.sentenceCharPerc+ " False");

            }
            else
                /*if(((regSeg.isHole == false)&&(currIsHole == true))
                ||((regSeg.isHole == true)&&(currIsHole == false))
                ||((regSeg.isHole = true)&&(currIsHole == true)))*/
            {
                //Debug.Log("Done");

                retSegmentList.Add(regSeg);

                if(regSeg.isHole){

                    holeSizes.Add(regSeg.segmentText.Length);
                    if(regSeg.segmentText.Length>maxHoleCharacters)
                        maxHoleCharacters = regSeg.segmentText.Length;
                }

                tmpTotPerc += regSeg.sentenceCharPerc;
                regSeg = new SentenceSegment(tmpSentenceWord, ((tmpSentenceWord.Length) * 1.0f ) / (sentenceCharLengthWhite * 1.0f) ,currIsHole,tmpSentenceWord.Length);

                //Debug.Log("accumulated (hole) *"+regSeg.segmentText+"* "+regSeg.sentenceCharPerc+" "+currIsHole);

            }

            if(i == (sentenceWordArr.Length-1))
            {
                retSegmentList.Add(regSeg);
                tmpTotPerc += regSeg.sentenceCharPerc;
                if(regSeg.isHole){

                    holeSizes.Add(regSeg.segmentText.Length);
                    if(regSeg.segmentText.Length>maxHoleCharacters)
                        maxHoleCharacters = regSeg.segmentText.Length;
                }

                //Debug.Log("Done");
            }
        }

        //Replaces the length of each hole for the maximum length, to avoid recognition of correct answer based on size
        foreach(int holeWidth in holeSizes){
            sentenceCharLengthWhite += (maxHoleCharacters-holeWidth);
        }

        if(retSegmentList[0].segmentText.Length == 0)
        {
            retSegmentList.RemoveAt(0);
        }

        tmpTotPerc = 0.0f;
        foreach(SentenceSegment segment in retSegmentList){
            if(segment.isHole)
                segment.numCharacters = maxHoleCharacters;
            else
                segment.numCharacters = segment.segmentText.Length;

            segment.sentenceCharPerc = segment.numCharacters/(1.0f*sentenceCharLengthWhite);
            tmpTotPerc+=segment.sentenceCharPerc;

        }

        //Debug.Log("Overflow? "+tmpTotPerc);
        // If we have a perc overflow due to floating point precision then fix to 1 and redistribute the remainder.
        /*if(tmpTotPerc > 1)
        {
            float remainder = 1 - tmpTotPerc;
            float removalFactor = remainder / (retSegmentList.Count * 1.0f);

            tmpTotPerc = 0;
            for(int i=0; i<retSegmentList.Count; i++)
            {
                retSegmentList[i].sentenceCharPerc -= removalFactor;
                tmpTotPerc += retSegmentList[i].sentenceCharPerc;
            }

            if(tmpTotPerc > 1)
            {
                float subTotal = 0;
                for(int i=0; i<retSegmentList.Count-1; i++)
                {
                    subTotal += retSegmentList[i].sentenceCharPerc;
                }

                retSegmentList[retSegmentList.Count-1].sentenceCharPerc = 1 - subTotal;
            }
        }*/

        return retSegmentList;
    }
Пример #3
0
    public void renderSongSentence()
    {
        // Create world items representing the sentence.

        if(currLvlConfig == null)
        {
            //Debug.LogWarning("DEPRECATED");
            currLvlConfig = (SHeroLevelConfig) lvlConfigGen.getNextLevelConfig(wordPoolMaxCapacity);
            musicMarkerSpeedPerWord_InSec = (3-currLvlConfig.speed)*5f;

            recordPresentedConfig(currLvlConfig);
            if(serverCommunication != null) { serverCommunication.wordDisplayed(currLvlConfig.getLevelSentence(false),currLvlConfig.languageArea,currLvlConfig.difficulty); }
        }

        GameObject songBannerObj = GameObject.Find("WordScroll");
        GameObject songWordDisplayAreaObj = songBannerObj.transform.FindChild("SongWordDisplayArea").gameObject;
        Rect sentenceAreaBounds = CommonUnityUtils.get2DBounds(songWordDisplayAreaObj.renderer.bounds);

        List<SentenceSegment> sentenceSegments = extractMinimalSentenceSegments(currLvlConfig);

        float totalSentencePercForHoles = 0;
        float tmpTotal = 0;
        int numHoles = 0;
        for(int i=0; i<sentenceSegments.Count; i++)
        {
            SentenceSegment tmpSeg = sentenceSegments[i];
            if(tmpSeg.isHole)
            {
                numHoles++;
                totalSentencePercForHoles += tmpSeg.sentenceCharPerc;
            }
            tmpTotal += tmpSeg.sentenceCharPerc;
        }
        //Debug.Log("100%:"+tmpTotal);
        float allocatedWidthPercPerHole = totalSentencePercForHoles / (numHoles * 1.0f);

        List<GameObject> tmpWordBoxes = new List<GameObject>();
        List<GameObject> tmpWordBoxesAndHoles = new List<GameObject>();
        List<int> holeIdxs = new List<int>();
        songBannerItemPositions = new List<Vector3>();

        //Debug.Log("Total area: "+sentenceAreaBounds.x+" "+sentenceAreaBounds.y+" "+sentenceAreaBounds.height+" "+sentenceAreaBounds.width);

        Vector3 currSegWorldTopLeft = new Vector3(sentenceAreaBounds.x,sentenceAreaBounds.y,songWordDisplayAreaObj.transform.position.z);
        int nxtHoleIDArrIndex = 0;
        for(int i=0; i<sentenceSegments.Count; i++)
        {
            SentenceSegment tmpSeg = sentenceSegments[i];
            float blockWidth = sentenceAreaBounds.width * tmpSeg.sentenceCharPerc;
            if(tmpSeg.isHole) {
                blockWidth = sentenceAreaBounds.width * allocatedWidthPercPerHole; }

            Rect blockBounds = new Rect(currSegWorldTopLeft.x,currSegWorldTopLeft.y,blockWidth,sentenceAreaBounds.height);

            if( ! tmpSeg.isHole)
            {
                // Create text block.

                //Debug.Log(tmpSeg.segmentText+" Width: "+blockBounds.width+" from "+currSegWorldTopLeft.x);

                GameObject nwWordBox = WordBuilderHelper.buildWordBox(99,tmpSeg.segmentText,blockBounds,currSegWorldTopLeft.z,upAxisArr,wordBoxPrefab);
                Destroy(nwWordBox.GetComponent<BoxCollider>());
                Destroy(nwWordBox.transform.FindChild("Board").gameObject);
                nwWordBox.transform.FindChild("Text").gameObject.renderer.sortingOrder = 1000;
                //Debug.Log("TextMeshWidth-"+i+"-"+(nwWordBox.transform.FindChild("Text").renderer.bounds.size.x));

                tmpWordBoxes.Add(nwWordBox);
                tmpWordBoxesAndHoles.Add(nwWordBox);
                songBannerItemPositions.Add(nwWordBox.transform.position);
                currLvlItems.Add(nwWordBox.name);
            }
            else
            {
                // Create hole block.

                //Debug.Log(tmpSeg.segmentText+" Width: "+blockBounds.width+" from "+currSegWorldTopLeft.x);

                string text =tmpSeg.segmentText;
                while(text.Length<tmpSeg.numCharacters)
                    text+="*";
                //Debug.Log(text);
                GameObject nwWordBoxHole = WordBuilderHelper.buildWordBox(99,text,blockBounds,currSegWorldTopLeft.z,upAxisArr,wordBoxPrefab);
                Destroy(nwWordBoxHole.GetComponent<BoxCollider>());
                Destroy(nwWordBoxHole.transform.FindChild("Board").gameObject);
                nwWordBoxHole.transform.FindChild("Text").gameObject.renderer.sortingOrder = 1000;
                //Debug.Log("TextMeshWidth-"+i+"-"+(nwWordBox.transform.FindChild("Text").renderer.bounds.size.x));

                holeIdxs.Add(tmpWordBoxesAndHoles.Count);
                tmpWordBoxesAndHoles.Add(nwWordBoxHole);

            }

            currSegWorldTopLeft.x += blockBounds.width;
        }

        //songBannerFontCharacterSize = WordBuilderHelper.setBoxesToUniformTextSize(tmpWordBoxesAndHoles,0.10f);
        songBannerFontCharacterSize = WordBuilderHelper.setBoxesToTextSizeInRange(tmpWordBoxesAndHoles,0.04f,0.1f);

        Debug.Log("WorldBoxes font "+songBannerFontCharacterSize);

        float holeNewWidth = 0f;

        tmpTotal = 0;
        for(int i=0; i<tmpWordBoxesAndHoles.Count; i++)
        {
            GameObject nxtWordBox = tmpWordBoxesAndHoles[i];
            Transform textChild = nxtWordBox.transform.FindChild("Text");
            Transform boardChild = nxtWordBox.transform.FindChild("Board");

            float origBoardWidth = boardChild.renderer.bounds.size.x;

            float reqBoardWidth = textChild.renderer.bounds.size.x;

            if(sentenceSegments[i].isHole){

                if(reqBoardWidth>holeNewWidth)
                    holeNewWidth = reqBoardWidth;
            }else{

            float reqXScale = reqBoardWidth / origBoardWidth;

            Vector3 tmpScaleVect = boardChild.localScale;
            tmpScaleVect.x *= reqXScale;
            boardChild.localScale = tmpScaleVect;

            tmpTotal += boardChild.renderer.bounds.size.x;
            //freedWidth += (origBoardWidth - reqBoardWidth);
            //Debug.Log("Adjust word "+nxtWordBox.transform.FindChild("Text").gameObject.GetComponent<TextMesh>().text+" by "+reqXScale+ " (size: "+ (boardChild.renderer.bounds.size.x) +")");
            }
        }

        for(int i=0; i<tmpWordBoxesAndHoles.Count; i++)
        {

            if(!sentenceSegments[i].isHole)
                continue;

            GameObject nxtWordBox = tmpWordBoxesAndHoles[i];
            Transform textChild = nxtWordBox.transform.FindChild("Text");

            float origTextWidth = textChild.renderer.bounds.size.x;

            float reqXScale = holeNewWidth / origTextWidth;

            Vector3 tmpScaleVect = textChild.localScale;
            tmpScaleVect.x *= reqXScale;
            textChild.localScale = tmpScaleVect;

            tmpTotal += textChild.renderer.bounds.size.x;
                //freedWidth += (origBoardWidth - reqBoardWidth);
            Debug.Log("Adjust hole "+nxtWordBox.transform.FindChild("Text").gameObject.GetComponent<TextMesh>().text+" by "+reqXScale+ " (size: "+ (textChild.renderer.bounds.size.x) +")");
        }

        //Destroy holeWords and replace them with holes of the same size
        for(int i = 0;i<holeIdxs.Count;i++){
            GameObject holeWord = tmpWordBoxesAndHoles[ holeIdxs[i] ];

            Bounds bounds = holeWord.transform.FindChild("Text").renderer.bounds;
            Rect blockBounds = CommonUnityUtils.get2DBounds(bounds);

            GameObject nwHole = WorldSpawnHelper.initObjWithinWorldBounds(holePrefab,1,1,"Hole-"+currLvlConfig.holeList[nxtHoleIDArrIndex],blockBounds,null,songWordDisplayAreaObj.transform.position.z,upAxisArr);
            nxtHoleIDArrIndex++;

            HoleScript hs = nwHole.GetComponent<HoleScript>();
            hs.registerListener("AcScen",this);

            nwHole.transform.renderer.material.color = Color.gray;
            nwHole.transform.GetComponent<BoxCollider>().size = new Vector3(1,1,50);

            songBannerItemPositions.Add(nwHole.transform.position);
            songWordWorldScale = nwHole.transform.renderer.bounds.size;

            currLvlItems.Add(nwHole.name);

            DestroyImmediate(holeWord);

        }

        /*float addedWidthPerHole = freedWidth / (numHoles * 1.0f);
        if((numHoles == 1)&&(currLvlConfig.isWord)) { addedWidthPerHole = 0; }
        for(int i=0; i<numHoles; i++)
        {
            GameObject nxtHoldObj = GameObject.Find("Hole-"+currLvlConfig.holeList[i]);

            float addition = addedWidthPerHole;
            float origHoleWidth = nxtHoldObj.renderer.bounds.size.x;

            if(i == (numHoles-1))
            {
                if((tmpTotal + addition) > sentenceAreaBounds.width)
                {
                    addition = sentenceAreaBounds.width - tmpTotal;
                }
            }

            float reqHoleWidth = origHoleWidth + addition;

            Vector3 tmpScaleVect = nxtHoldObj.transform.localScale;
            tmpScaleVect.x *= (reqHoleWidth / origHoleWidth);
            nxtHoldObj.transform.localScale = tmpScaleVect;

            tmpTotal += reqHoleWidth;
        }*/

        //Position the boxes
        currSegWorldTopLeft = new Vector3(sentenceAreaBounds.x + (sentenceAreaBounds.width/2f) - (tmpTotal/2f),sentenceAreaBounds.y,songWordDisplayAreaObj.transform.position.z);
        int nxtWordBoxArrIndex = 0;
        int nxtHoleArrIndex = 0;

        for(int i=0; i<sentenceSegments.Count; i++)
        {
            SentenceSegment tmpSeg = sentenceSegments[i];

            float boxWidth = 0;

            if( ! tmpSeg.isHole)
            {
                GameObject reqBox = tmpWordBoxes[nxtWordBoxArrIndex];
                boxWidth = reqBox.transform.FindChild("Board").renderer.bounds.size.x;
                reqBox.transform.position = new Vector3(currSegWorldTopLeft.x + (boxWidth/2f),reqBox.transform.position.y,reqBox.transform.position.z);
                nxtWordBoxArrIndex++;
            }
            else
            {
                GameObject reqBox = GameObject.Find("Hole-"+currLvlConfig.holeList[nxtHoleArrIndex]);
                boxWidth = reqBox.renderer.bounds.size.x;
                reqBox.transform.position = new Vector3(currSegWorldTopLeft.x + (boxWidth/2f),reqBox.transform.position.y,reqBox.transform.position.z);
                nxtHoleArrIndex++;
            }

            currSegWorldTopLeft.x += boxWidth;
        }

        // Apply hole asthetics (shirnk slightly)
        /*for(int i=0; i<currLvlConfig.holeList.Length; i++)
        {
            GameObject holeObj = GameObject.Find("Hole-"+currLvlConfig.holeList[i]);

            Vector3 tmpScale = holeObj.transform.localScale;
            tmpScale.x *= 0.9f;
            tmpScale.y *= 0.8f;
            holeObj.transform.localScale = tmpScale;

            songWordWorldScale = holeObj.transform.localScale;
        }*/

        AcSHIntroSequenceScript introScript = transform.gameObject.GetComponent<AcSHIntroSequenceScript>();
        if(introScript != null)
        {
            introScript.respondToEvent("AcScen","SentenceDisplayed",null);
        }
    }