Пример #1
0
        void loadAnimal(Vector3 initPosition)
        {
            string animalName = animalHandler.GetNewAnimal();

            speechText = animalName.Split("-".ToCharArray()) [0].ToLower();
            Logger.Log("animal name: " + speechText);
            SceneObjectProperties pops = new SceneObjectProperties();

            pops.setAll(animalName, Constants.TAG_ANIMAL_OBJECT, initPosition, true);
            ObjectHandler.InstantiateSceneObject(pops, null, new GameObject());
            animalPresent = true;
        }
        /// <summary>
        /// Instantiate a new game object with the specified properties
        /// </summary>
        /// <param name="pops">properties of the play object.</param>
        public static void InstantiateSceneObject(SceneObjectProperties pops, Sprite spri, GameObject go)
        {
            //GameObject go = new GameObject();

            // set object name
            go.name = pops.Name();

            Logger.Log("Creating new play object: " + pops.Name() + ", with properties: "
                       + pops.ToString());

            // load sprite/image for object
            SpriteRenderer spriteRenderer = go.AddComponent <SpriteRenderer>();

            // if we were not given a sprite for this object, try loading one
            if (spri == null)
            {
                // don't need file extension to load from resources folder -- strip if it exists
                Sprite sprite = Resources.Load <Sprite>(Constants.ANIMALS_IMAGE_PATH + pops.Name());

                if (sprite == null)
                {
                    Logger.LogWarning("Could not load sprite from Resources: "
                                      + Constants.ANIMALS_IMAGE_PATH + pops.Name()
                                      + "\nGoing to try file path...");

                    // TODO add filepath to pops! don't use Name
                    //sprite = Utilities.LoadSpriteFromFile(pops.Name());
                }

                // got sprite!
                spriteRenderer.sprite = sprite;
            }
            // otherwise, we were given a sprite, try using that one
            else
            {
                spriteRenderer.sprite = spri;
            }

            // move object to specified initial position
            go.transform.position = pops.InitPosition();

            go.transform.localScale = pops.Scale();
            // set the scale of the sprite to the specified scale
            //go.transform.localScale = pops.Scale();

            // set tag
            go.tag = pops.Tag();


            // if tag is ANIMAL, keep reference and set as invisible
            if (go.tag.Equals(Constants.TAG_ANIMAL_OBJECT) &&
                go.GetComponent <Renderer>() != null)
            {
                go.GetComponent <Renderer>().enabled = true;
            }

            if (pops.RigidBody2D())
            {
                // add rigidbody if this is a rigidbody object
                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 = false;

                // true: don't want gravity, otherwise objects will fall
                // though with the isKinematic flag set this may not matter
                //rb2d.gravityScale = 0;
                // set collision detection to 'continuous' instead of discrete
                //rb2d.collisionDetectionMode = CollisionDetectionMode2D.Continuous;

                // add collision manager so we get trigger enter/exit events
                CollisionManager cm = go.AddComponent <CollisionManager>();
                // subscribe to log events from the collision manager
                //cm.logEvent += new LogEventHandler(HandleLogEvent);
            }

            // add polygon collider that matches shape of object and set as a
            // trigger so enter/exit events fire when this collider is hit
            PolygonCollider2D pc = go.AddComponent <PolygonCollider2D>();

            pc.isTrigger = false;

            // add and subscribe to gestures
            //			if(this.gestureManager == null) {
            //				Logger.Log("ERROR no gesture manager");
            //				FindGestureManager();
            //			}
            //
            //			try {
            //				// add gestures and register to get event notifications
            //				this.gestureManager.AddAndSubscribeToGestures(go, pops.draggable, false);
            //			}
            //			catch (Exception e)
            //			{
            //				Logger.LogError("Tried to subscribe to gestures but failed! " + e);
            //			}

            // add pulsing behavior (draws attention to actionable objects)
            // go.AddComponent<GrowShrinkBehavior>();
            // Removing this because it messes with collision detection when
            // objects are close to each other (continuously colliding/uncolliding)
            // go.GetComponent<GrowShrinkBehavior>().StartPulsing();
        }