// instantiate new game object with specified properties public static void InstantiateObject(ObjectProperties prop) { // create new game object GameObject go = new GameObject(); // set name go.name = prop.Name(); Debug.Log("Created new object " + go.name); // set tag go.tag = prop.Tag(); // set initial position go.transform.position = prop.InitPosn(); // set scale of sprite image go.transform.localScale = prop.Scale(); // load sprite image SpriteRenderer spriteRenderer = go.AddComponent <SpriteRenderer>(); // image file needs to be in an existing Assets/Resources folder or subfolder Sprite sprite = Resources.Load <Sprite>("Graphics/" + prop.Sprite()); if (sprite == null) { Debug.Log("ERROR: could not load sprite " + prop.Name()); } spriteRenderer.sprite = sprite; // load audio clip if (prop.AudioFile() != null) { AudioSource audioSource = go.AddComponent <AudioSource>(); // audio file needs to be in an existing Assets/Resources folder or subfolder AudioClip clip = Resources.Load("Audio/" + prop.AudioFile()) as AudioClip; if (clip == null) { Debug.Log("ERROR: could not load audioclip " + prop.AudioFile()); } audioSource.clip = clip; audioSource.playOnAwake = false; } if (go.tag != "TargetLetter" && go.tag != "TargetBlank" && go.tag != "Jar") { // add polygon collider that matches shape of object // used to detect touches and collisions go.AddComponent <PolygonCollider2D> (); } if (go.tag == "TargetLetter" || go.tag == "TargetBlank") { // add circle collider // used to detect touches and collisions CircleCollider2D cc2d = go.AddComponent <CircleCollider2D>(); // set as trigger so OnTriggerEnter2D function is called when collider is hit cc2d.isTrigger = true; // set radius for circle collider // want radius to be small, to make it easier for users to drag letters or sound blanks to where they want // without accidentally colliding with another object if (go.tag == "TargetLetter") { if (ProgressManager.currentMode == 1) { cc2d.radius = .1f; } if (ProgressManager.currentMode == 3) { cc2d.radius = 3f; } } if (go.tag == "TargetBlank") { cc2d.radius = .3f; } } if (go.tag == "Jar") { // add circle collider // used to detect collisions CircleCollider2D cc2d = go.AddComponent <CircleCollider2D> (); // set radius for circle collider // want to set radius exactly so that it extends to the rim/opening of the jar // so collisions will be detected when the user dragging a sound blank hits the rim cc2d.radius = .5f; } if (go.tag == "MovableLetter" || go.tag == "MovableBlank") { // add rigidbody if object is draggable Rigidbody2D rb2d = go.AddComponent <Rigidbody2D>(); // remove object from physics engine's control, because we don't want // the object to move with gravity, forces, etc. - we do the moving rb2d.isKinematic = true; // don't want gravity, otherwise object will fall rb2d.gravityScale = 0; } // subscribe to gestures GestureManager gm = go.AddComponent <GestureManager> (); gm.AddAndSubscribeToGestures(go); // add pulse behavior - draws attention to interactive objects go.AddComponent <PulseBehavior> (); if (go.tag == "Hint") { // set hint letter color to black go.GetComponent <SpriteRenderer>().color = Color.black; // create hint letter behind the background go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y, 3); } }
//<summary> //Called on start, used to initialize stuff //</summary> void Start() { //Scale graphics to screen size Utilities.setCameraViewForScreen(); //create instance of grestureManager GestureManager gestureManager = GameObject. FindGameObjectWithTag(Constants.Tags.TAG_GESTURE_MANAGER).GetComponent <GestureManager>(); if (gestureManager != null) { //Create objects and background LoadLevel(ProgressManager.currentLevel); //Set up kid GameObject kid = GameObject.FindGameObjectWithTag(Constants.Tags.TAG_KID); //check if kid is attached if (kid != null) { kid.GetComponent <SpriteRenderer>().sprite = Resources.Load <Sprite>("Graphics/" + ProgressManager.chosenKid); } else { Debug.LogWarning("Cannot find kid in scene"); } //check if sprite is attached if (kid.GetComponent <SpriteRenderer>().sprite != null) { //Play Grow Animation for kid GrowKid(); //Load audio for kid kid.AddComponent <AudioSource>().clip = Resources.Load("Audio/KidSpeaking/" + ProgressManager.currentLevel) as AudioClip; //Check if audio clip is attached if (kid.GetComponent <AudioSource>().clip != null) { kid.GetComponent <AudioSource>().priority = 0; kid.GetComponent <AudioSource>().volume = 1.0f; //Play audio clip attached to kid if there is one kid.GetComponent <AudioSource>().Play(); } else { Debug.LogWarning("No audio found"); } } else { Debug.LogWarning("Cannot load sprite"); } //Find ChooseObjectDirector gameObject GameObject dir = GameObject.Find("ChooseObjectDirector"); if (dir != null) { //Load background music for scene onto ChooseObjectDirector dir.AddComponent <AudioSource>().clip = Resources.Load("Audio/BackgroundMusic/" + ProgressManager.currentLevel) as AudioClip; //Check if audio clip is attached if (dir.GetComponent <AudioSource>().clip != null) { dir.GetComponent <AudioSource>().priority = 0; dir.GetComponent <AudioSource>().volume = .25f; //Start playing background music if attached dir.GetComponent <AudioSource>().Play(); } else { Debug.LogWarning("No audio file found"); } } else { Debug.LogWarning("Cannot find ChooseObjectDirector"); } //Subscribe buttons to touch gestures GameObject button = GameObject.FindGameObjectWithTag(Constants.Tags.TAG_BUTTON); if (button != null) { button.AddComponent <GestureManager>().AddAndSubscribeToGestures(button); } else { Debug.LogWarning("Cannot find button"); } //Find word objects GameObject[] gos = GameObject.FindGameObjectsWithTag(Constants.Tags.TAG_WORD_OBJECT); foreach (GameObject go in gos) { //Start pulsing for each object Debug.Log("Started pulsing for " + go.name); go.GetComponent <PulseBehavior>().StartPulsing(go); //Check if word has been completed by user //If word not completed, darken and fade out object if (!ProgressManager.IsWordCompleted(go.name)) { SetColorAndTransparency(go, Color.grey, .9f); } //If word completed, brighten and fill in object else { Debug.Log("Word Completed: " + go.name); SetColorAndTransparency(go, Color.white, 1f); } } //Check if this level has been completed, i.e. if all words in the level have been completed if (CheckCompletedLevel()) { //If level completed, add to completedLevels list and unlock the next level ProgressManager.AddCompletedLevel(ProgressManager.currentLevel); ProgressManager.UnlockNextLevel(ProgressManager.currentLevel); } } else { Debug.LogError("Cannot find gesture manager component"); } }
//<summary> // Called when an object enters the collider of another object // Set the target object as the trigger //</summary> void OnTriggerEnter2D(Collider2D other) { GestureManager gestureManager = GameObject. FindGameObjectWithTag(Constants.Tags.TAG_GESTURE_MANAGER).GetComponent <GestureManager>(); if (gestureManager != null) { // if the current scene is Learn Spelling if (Application.loadedLevelName == "4. Learn Spelling") { // if the collided objects are the same letter // i.e. if the movable letter that entered matches the target letter if (other.name == gameObject.name) { Debug.Log("Collision on " + other.name); // change color of target letter to match that of movable letter gameObject.GetComponent <SpriteRenderer>().color = other.GetComponent <SpriteRenderer>() .color; Color color = gameObject.GetComponent <Renderer>().material.color; color.a = 1.0f; gameObject.GetComponent <Renderer>().material.color = color; // destroy movable letter Debug.Log("Destroyed draggable letter " + other.gameObject.name); Destroy(other.gameObject); // disable collisions for target letter // so all other movable letters can no longer collide with this letter Debug.Log("Disabled collisions for " + gameObject.name); Destroy(gameObject.GetComponent <CollisionManager>()); // pulse target letter once LeanTween.scale(gameObject, new Vector3(WordCreation.letterScale * 1.2f, WordCreation.letterScale * 1.2f, 1), .6f); LeanTween.scale(gameObject, new Vector3(WordCreation.letterScale, WordCreation.letterScale, 1), .5f).setDelay(.4f); // check if user has completed word if (CheckCompletedWord()) { // sound out the word GameObject[] tar = GameObject.FindGameObjectsWithTag (Constants.Tags.TAG_TARGET_LETTER); GameObject audioManager = GameObject.Find("AudioManager"); if (audioManager != null) { audioManager.GetComponent <AudioManager>().SpellOutWord(tar); // play celebratory animation LearnSpellingDirector.CelebratoryAnimation((tar.Length + 1.5f) * AudioManager.clipLength); // add word to completedWord list ProgressManager.AddCompletedWord(ProgressManager.currentWord); } else { Debug.LogError("Cannot find audio manager"); } } } } //only want to get these components if on the spelling or sound game if (Application.loadedLevelName != "4. Learn Spelling") { //get properties component Properties prop = other.gameObject.GetComponent <Properties>(); //We track whether or not letters have been dragged into the right spots using this isinblank flag, //which is set to true when the letter is in the right place. //only change the flag if the gameObject has a properties component if (prop != null) { prop.isinblank = true; } else { Debug.LogError("Properties are not attached"); } //check if word is spelled correctly if (other.name == gameObject.name) { prop.iscorrect = true; } // if the current scene is Spelling Game if (Application.loadedLevelName == "5. Spelling Game") { Debug.Log("Collision on " + other.name); // stop pulsing letter other.gameObject.GetComponent <PulseBehavior>().StopPulsing(other.gameObject); // move letter to center of target blank // z-position of letter = -1, so letter is in front of the blank (z-position of blank = 0) other.gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, -1); // change color of letter to indicate collision succesfully occurred other.gameObject.GetComponent <SpriteRenderer>().color = Color.blue; Debug.Log("I changed my color"); // pulse letter once LeanTween.scale(other.gameObject, new Vector3(WordCreation.letterScale * 1.25f, WordCreation.letterScale * 1.25f, 1), .3f); LeanTween.scale(other.gameObject, new Vector3(WordCreation.letterScale, WordCreation.letterScale, 1), .3f).setDelay(.2f); // disable collisions for target blank // so no other movable letters can collide with this blank anymore Debug.Log("Disabled collisions for Blank " + gameObject.name); Destroy(gameObject.GetComponent <CollisionManager>()); // if all letters have been dragged to a blank if (CheckCompletedTargets(Constants.Tags.TAG_MOVABLE_LETTER)) { // if user spelled word incorrectly if (!CheckCorrectSpelling(Constants.Tags.TAG_MOVABLE_LETTER)) { // play try again animation SpellingGameDirector.TryAgainAnimation(); // mark the correct letters by changing their color MarkCorrectLetters(1f); // move incorrect letters back to original position ResetIncorrectLetters(1f); // play word's sound GameObject.FindGameObjectWithTag(Constants.Tags.TAG_WORD_OBJECT). GetComponent <AudioSource>().PlayDelayed(1f); // flash hint button to call attention to it FlashHintButton(2f); } // if the user spelled the word correctly else { // find all movable letters GameObject[] mov = GameObject.FindGameObjectsWithTag (Constants.Tags.TAG_MOVABLE_LETTER); // disable touch gestures for sound & hint buttons gestureManager.DisableGestures(GameObject.Find("SoundButton")); gestureManager.GetComponent <GestureManager>().DisableGestures(GameObject.Find ("HintButton")); // mark all correct letters by changing their colors MarkCorrectLetters(0f); // sound out the word SpellingGameDirector.SpellOutWord(); // play celebratory animation SpellingGameDirector.CelebratoryAnimation((mov.Length + 1.5f) * AudioManager.clipLength); // add word to completedWord list ProgressManager.AddCompletedWord(ProgressManager.currentWord); //reset letters in case user wants to play again on the same screen foreach (GameObject letter in mov) { letter.GetComponent <Properties>().isinblank = false; letter.GetComponent <Properties>().iscorrect = false; } } } } // if current scene is Sound Game if (Application.loadedLevelName == "6. Sound Game") { Debug.Log("Collision on " + other.name); // stop pulsing sound blank other.gameObject.GetComponent <PulseBehavior>().StopPulsing(other.gameObject); // place sound blank at opening of jar other.gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + 2.2f, 1); // pulse sound blank once LeanTween.scale(other.gameObject, new Vector3(.4f, .4f, 1), .3f); LeanTween.scale(other.gameObject, new Vector3(.3f, .3f, 1), .3f).setDelay(.2f); // change color of jar to match color of sound blank // to indicate successful collision Color color = other.gameObject.GetComponent <SpriteRenderer>().color; Vector2 posn = gameObject.transform.position; Collider2D[] jar = Physics2D.OverlapCircleAll(posn, .1f, 1, 1.5f, 0f); LeanTween.color(jar[0].gameObject, color, .01f); // disable collisions for target letter Debug.Log("Disabled collisions for Letter " + gameObject.name); Destroy(gameObject.GetComponent <CollisionManager>()); // if all sound blanks have been dragged onto a jar if (CheckCompletedTargets(Constants.Tags.TAG_MOVABLE_BLANK)) { // if user did not drag all sound blanks to correct letter if (!CheckCorrectSpelling(Constants.Tags.TAG_MOVABLE_BLANK)) { // play try again animation SoundGameDirector.TryAgainAnimation(); // reset correct sound blanks back to original size MarkCorrectSounds(1f); // move incorrect sound blanks back to original position ResetIncorrectSounds(1f); // play word's sound GameObject.FindGameObjectWithTag(Constants.Tags.TAG_WORD_OBJECT). GetComponent <AudioSource>().PlayDelayed(1f); // flash hint button to call attention to it FlashHintButton(2f); } // if user dragged all sound blanks to their correct corresponding letters else { // find all sound blanks GameObject[] mov = GameObject.FindGameObjectsWithTag (Constants.Tags.TAG_MOVABLE_BLANK); // disable touch gestures for sound + hint buttons gestureManager.DisableGestures(GameObject.Find("SoundButton")); gestureManager.DisableGestures(GameObject.Find("HintButton")); // reset correct sound blanks back to original size MarkCorrectSounds(0f); // sound out word GameObject[] tar = GameObject.FindGameObjectsWithTag (Constants.Tags.TAG_TARGET_LETTER); GameObject audioManager = GameObject.Find("AudioManager"); audioManager.GetComponent <AudioManager>().SpellOutWord(tar); // play celebratory animation SoundGameDirector.CelebratoryAnimation((mov.Length + 1.5f) * AudioManager.clipLength); // add completed word to completedWord list ProgressManager.AddCompletedWord(ProgressManager.currentWord); //reset letters in case user wants to play again on the same screen //or user wants to listen to the sounds foreach (GameObject letter in mov) { letter.GetComponent <Properties>().isinblank = false; letter.GetComponent <Properties>().iscorrect = false; } } } } } } else { Debug.LogError("Cannot find gesture manager"); } }