示例#1
0
        private static void updatePhysics(GameObject item)
        {
            ItemProperties properties = item.GetComponent <ItemProperties>();
            double         itemArea   = 1;

            switch (properties.shape)
            {
            case ItemShape.Circle:
                CircleCollider2D circleCollider = item.GetComponent <CircleCollider2D>();
                circleCollider.radius = properties.width / 2;

                itemArea = Math.PI * (properties.width / 2) * (properties.width / 2);
                break;

            case ItemShape.Rectangle:
                BoxCollider2D boxCollider = item.GetComponent <BoxCollider2D>();
                boxCollider.size = new Vector2(properties.width, properties.height);

                itemArea = properties.width * properties.height;
                break;

            case ItemShape.Triangle:
                PolygonCollider2D polygonCollider = item.GetComponent <PolygonCollider2D>();
                Vector2[]         points          = new Vector2[3];
                points[0] = new Vector2(-properties.width / 2, -properties.height / 2);
                points[1] = new Vector2(-properties.width / 2, properties.height / 2);
                points[2] = new Vector2(properties.width / 2, -properties.height / 2);
                polygonCollider.points = points;

                itemArea = properties.width * properties.height / 2;
                break;

            default:
                break;
            }


            //item.rigidbody2D.isKinematic = false;
            switch (properties.material)
            {
            case ItemMaterial.FixedMetal:
                //item.rigidbody2D.isKinematic = true;
                item.GetComponent <Rigidbody2D>().mass        = 7.9f * (float)itemArea;
                item.GetComponent <Rigidbody2D>().drag        = 0.1f;
                item.GetComponent <Rigidbody2D>().angularDrag = 0.2f;
                break;

            case ItemMaterial.Ice:
                item.GetComponent <Rigidbody2D>().mass        = 0.91f * (float)itemArea;
                item.GetComponent <Rigidbody2D>().drag        = 0.1f;
                item.GetComponent <Rigidbody2D>().angularDrag = 0.2f;
                break;

            case ItemMaterial.Metal:
                item.GetComponent <Rigidbody2D>().mass        = 7.9f * (float)itemArea;
                item.GetComponent <Rigidbody2D>().drag        = 0.1f;
                item.GetComponent <Rigidbody2D>().angularDrag = 0.2f;
                break;

            case ItemMaterial.Rubber:
                item.GetComponent <Rigidbody2D>().mass        = 1.0f * (float)itemArea;
                item.GetComponent <Rigidbody2D>().drag        = 0.1f;
                item.GetComponent <Rigidbody2D>().angularDrag = 0.2f;
                break;

            case ItemMaterial.Wood:
                item.GetComponent <Rigidbody2D>().mass        = 0.65f * (float)itemArea;
                item.GetComponent <Rigidbody2D>().drag        = 0.1f;
                item.GetComponent <Rigidbody2D>().angularDrag = 0.2f;
                break;

            default:
                break;
            }

            SetPhysicsMaterialGeneric(item);
        }
示例#2
0
        public static GameObject Create(ItemShape shape, ItemMaterial itemMaterial, float width, float height)
        {
            Mesh       objMesh, objEffectMesh;
            ItemEffect effect;

            if (itemMaterial == ItemMaterial.Ice)
            {
                effect = ItemEffect.Ice;
            }
            else
            {
                effect = ItemEffect.Solid;
            }

            switch (shape)
            {
            case ItemShape.Circle:
                objMesh       = createCircleMesh(width / 2, itemMaterial);
                objEffectMesh = createCircleEffectMesh(width / 2, effect);
                break;

            case ItemShape.Rectangle:
                objMesh       = createRectangleMesh(width, height, itemMaterial);
                objEffectMesh = createRectangleEffectMesh(width, height, effect);
                break;

            case ItemShape.Triangle:
                objMesh       = createTriangleMesh(width, height, itemMaterial);
                objEffectMesh = createTriangleEffectMesh(width, height, effect);
                break;

            default:
                objMesh       = createTriangleMesh(width, height, itemMaterial);
                objEffectMesh = createTriangleEffectMesh(width, height, effect);
                break;
            }

            //create the object
            GameObject obj = new GameObject();

            obj.AddComponent <MeshRenderer>();
            obj.AddComponent <MeshFilter>().mesh   = objMesh;
            obj.GetComponent <Renderer>().material = itemMaterials[(int)itemMaterial];

            //create the object effect
            GameObject objEffect = new GameObject();

            objEffect.AddComponent <MeshRenderer>();
            objEffect.AddComponent <MeshFilter>().mesh   = objEffectMesh;
            objEffect.GetComponent <Renderer>().material = atlas1Material;

            //setup the object and the object effect in the scene
            obj.name       = "Item: " + shape + " " + frontLayerIndex;
            objEffect.name = shape + " Effect " + frontLayerIndex;

            objEffect.transform.parent = obj.transform;
            obj.layer = 0;

            //bring the effect in front of the object, bring the object to front
            Vector3 pos = objEffect.transform.localPosition;

            objEffect.transform.localPosition = new Vector3(pos.x, pos.y, -layerSpacing / 2);
            BringToFront(obj);

            //add the object properties
            ItemProperties itemProperties = obj.AddComponent <ItemProperties>();

            itemProperties.shape    = shape;
            itemProperties.material = itemMaterial;
            itemProperties.width    = width;
            itemProperties.height   = height;

            setPhysics(obj);

            return(obj);
        }
示例#3
0
        private static void update(GameObject item, ItemMaterial itemMaterial, float width, float height)
        {
            ItemProperties itemProperties = item.GetComponent <ItemProperties>();

            Mesh       objMesh, objEffectMesh;
            ItemEffect effect;

            if (itemMaterial == ItemMaterial.Ice)
            {
                effect = ItemEffect.Ice;
            }
            else
            {
                effect = ItemEffect.Solid;
            }

            switch (itemProperties.shape)
            {
            case ItemShape.Circle:
                objMesh       = createCircleMesh(width / 2, itemMaterial);
                objEffectMesh = createCircleEffectMesh(width / 2, effect);
                break;

            case ItemShape.Rectangle:
                objMesh       = createRectangleMesh(width, height, itemMaterial);
                objEffectMesh = createRectangleEffectMesh(width, height, effect);
                break;

            case ItemShape.Triangle:
                objMesh       = createTriangleMesh(width, height, itemMaterial);
                objEffectMesh = createTriangleEffectMesh(width, height, effect);
                break;

            default:
                objMesh       = createTriangleMesh(width, height, itemMaterial);
                objEffectMesh = createTriangleEffectMesh(width, height, effect);
                break;
            }

            //update the object
            item.GetComponent <MeshFilter>().mesh   = objMesh;
            item.GetComponent <Renderer>().material = itemMaterials[(int)itemMaterial];

            //update the object effect
            GameObject itemEffect = item.transform.GetChild(0).gameObject;

            itemEffect.GetComponent <MeshFilter>().mesh = objEffectMesh;

            //update the object properties
            itemProperties.material = itemMaterial;
            itemProperties.width    = width;
            if (itemProperties.shape == ItemShape.Circle)
            {
                itemProperties.height = width;
            }
            else
            {
                itemProperties.height = height;
            }

            //update physics
            updatePhysics(item);
        }