Ejemplo n.º 1
0
        public override void Update(double deltaTime)
        {
            if (Input.GetKeyDown(Key.Enter))
            {
                /* var go = new GameObject();
                 * go.transform.position = this.transform.position;
                 * go.transform.rotation = this.transform.rotation;
                 * var renderer = go.AddComponent<MeshRenderer>();
                 * renderer.mesh = Factory.GetMesh(path);
                 * renderer.material.albedo = new Vector4(colors[(next++)%colors.Length]);
                 * var rb = go.AddComponent<Rigidbody>();
                 * rb.velocity = this.transform.forward*20;
                 * go.AddComponent<BoxCollider>();*/

                var go = LegoPiece.Create();

                go.transform.position = this.transform.position;
                go.transform.rotation = this.transform.rotation;

                var rb = go.GetComponent <Rigidbody>();
                if (rb)
                {
                    rb.velocity = this.transform.forward * 20;
                }
            }
        }
        public void VizualizeConnectionTo(LegoPiece other, Vector3 targetPos)
        {
            this.transform.rotation = other.transform.rotation;
            connectedToPoint        = other;
            connecedToLocalPoint    = other.FindClosestPoint(other.transform.InverseTransformPoint(targetPos));
            Vector3 worldPoint = other.transform.TransformPoint(connecedToLocalPoint);

            myLocalSlot = this.FindClosestSlot(this.transform.InverseTransformPoint(worldPoint));
            SetPositionMySlotPosition();
        }
Ejemplo n.º 3
0
        public override void Start()
        {
            var r     = new Random();
            int count = 10;

            while (count-- > 0)
            {
                var go = LegoPiece.Create();
                go.transform.position = new Vector3((float)r.NextDouble() * 20 - 10, (float)r.NextDouble() * 5, (float)r.NextDouble() * 20 - 10);
            }
        }
        public void Disconnect()
        {
            if (!IsConnected())
            {
                return;
            }

            gameObject.DestroyComponent(connectionJoint);

            Physics.IgnoreCollision(GetComponent <Collider>(), connectedTo.GetComponent <Collider>(), false);

            connectedTo.connectedToMe.Remove(this);
            connectedTo = null;
        }
        public void ConnectTo(LegoPiece other)
        {
            var p = transform.position;

            Disconnect();

            connectedTo = other;

            GetComponent <BoxCollider>().enabled = true;
            Physics.IgnoreCollision(this.GetComponent <Collider>(), connectedTo.GetComponent <Collider>(), true);

            transform.position = p;

            connectionJoint             = gameObject.AddComponent <FixedJoint>();
            connectionJoint.connectedTo = connectedTo.GetComponent <Rigidbody>();

            transform.position = p;

            connectedTo.connectedToMe.Add(this);

            PositionWarped();
        }
        public override void Update(double deltaTime)
        {
            var        ray = new Ray(this.transform.position, this.transform.forward);
            RaycastHit raycastHit;

            if (Physics.Raycast(ray, out raycastHit))
            {
                visualiseHitTarget.transform.position = raycastHit.point;
                visualiseHitTarget.transform.rotation = QuaternionUtility.LookRotation(raycastHit.normal);
                var legoPiece = raycastHit.collider.gameObject.GetComponent <LegoPiece>();
                if (legoPiece)
                {
                    if (legoPiece != lastLegoPiece)
                    {
                        if (lastLegoPiece)
                        {
                            lastLegoPiece.GetComponent <MeshRenderer>().material.albedo = lastAlbedo;
                        }
                        lastLegoPiece = legoPiece;

                        var m = lastLegoPiece.GetComponent <MeshRenderer>().material;
                        lastAlbedo = m.albedo;
                        const float T = 1.0f;
                        const float F = 0.3f;
                        if (clickedLegoPiece && clickedLegoPiece != lastLegoPiece)
                        {
                            m.albedo = new Vector4(F, T, F, 1);
                        }
                        else
                        {
                            m.albedo = new Vector4(T, F, F, 1);
                        }
                    }

                    if (clickedLegoPiece && lastLegoPiece && clickedLegoPiece != lastLegoPiece)
                    {
                        clickedLegoPiece.VizualizeConnectionTo(lastLegoPiece, visualiseHitTarget.transform.position);
                    }
                }
                else
                {
                    if (lastLegoPiece)
                    {
                        lastLegoPiece.GetComponent <MeshRenderer>().material.albedo = lastAlbedo;
                    }
                    lastLegoPiece = null;
                }
            }
            else
            {
                if (lastLegoPiece)
                {
                    lastLegoPiece.GetComponent <MeshRenderer>().material.albedo = lastAlbedo;
                }
                lastLegoPiece = null;
            }


            if (Input.GeMouseButtonDown(MouseButton.Left))
            {
                if (lastLegoPiece == null)
                {
                    if (clickedLegoPiece != null)
                    {
                        clickedLegoPiece.EndVisualise();
                        var p = visualiseHitTarget.transform.position - clickedLegoPiece.GetComponent <MeshRenderer>().mesh.bounds.center;
                        p.Y += 0.5f;
                        clickedLegoPiece.transform.position = p;
                        clickedLegoPiece.transform.rotation = visualiseHitTarget.transform.rotation;
                        clickedLegoPiece = null;
                    }
                }
                else
                {
                    if (clickedLegoPiece != null)
                    {
                        if (clickedLegoPiece != lastLegoPiece)
                        {
                            clickedLegoPiece.VizualizeConnectionTo(lastLegoPiece, visualiseHitTarget.transform.position);
                            clickedLegoPiece.ConnectTo(lastLegoPiece);
                            clickedLegoPiece = null;
                        }
                    }
                    else
                    {
                        clickedLegoPiece = lastLegoPiece;
                        clickedLegoPiece.StartVisualise();
                    }
                }
            }


            if (Input.GeMouseButton(MouseButton.Right))
            {
                if (lastLegoPiece)
                {
                    var rb = lastLegoPiece.GetComponent <Rigidbody>();
                    if (rb)
                    {
                        rb.AddForceAtPosition(transform.forward * 100, transform.position);
                    }
                }
            }

            if (Input.GetKeyDown(Key.L))
            {
                Debug.Info("a");
                Physics.IgnoreCollision(lastLegoPiece.GetComponent <Collider>(), clickedLegoPiece.GetComponent <Collider>(), true);
            }



            if (Input.GetKey(Key.AltLeft))
            {
                MyEngine.ParticleSimulation.Manager.instance.GenerateParticles(1000, visualiseHitTarget.transform.position, new Vector4(1, 1, 1, 1), new Vector4(1, 1, 1, 1), 1000, 1, 1);
            }
        }