Exemplo n.º 1
0
    public void Aim()
    {
        //Raycast from the muzzle forward untill the hit object is either the rootRail or a validCristal
        //place a phantom jewel on the hit slot to show where it would land.
        if (m_nextSphereColorKey != "")
        {
            bool           coodFound = false;
            List <Vector3> positions = new List <Vector3>();
            positions.Add(muzzle.transform.position);
            Vector3 rayDir   = muzzle.transform.forward;
            Vector3 rayStart = muzzle.transform.position;
            while (!coodFound)
            {
                RaycastHit outHit = new RaycastHit();
                if (Physics.SphereCast(rayStart, 0.1f, rayDir, out outHit, 100.0f, LayerMask.GetMask("ClickableCube")))
                {
                    Vector3 endPoint = outHit.point + outHit.normal * 0.5f;
                    positions.Add(endPoint);
                    if (outHit.collider.gameObject.tag == "Jewel")
                    {
                        Vector2Int destinationCoord = outHit.collider.gameObject.GetComponent <ClickableCube>().coord;
                        Vector2    fnormal          = new Vector2(outHit.normal.x, outHit.normal.y);
                        if (fnormal.x != 0 && fnormal.y != 0)
                        {
                            if (Mathf.Abs(fnormal.x) >= Mathf.Abs(fnormal.y))
                            {
                                fnormal.y = 0;
                            }
                            else
                            {
                                fnormal.x = 0;
                            }
                        }
                        Vector2Int adjustment = new Vector2Int(Mathf.RoundToInt(fnormal.x), Mathf.RoundToInt(fnormal.y));
                        destinationCoord += adjustment;

                        gohstJewel.transform.position = cubeGrid.CoordToPosition(new IntVector2(destinationCoord.x, destinationCoord.y));
                        m_lineRenderer.SetPositions(positions.ToArray());
                        coodFound = true;
                    }
                    if (outHit.collider.gameObject.tag == "Bouncer")
                    {
                        Vector3 projection = Vector3.Project(rayDir, outHit.normal);
                        rayDir  -= 2 * projection;
                        rayStart = endPoint;
                    }
                    if (outHit.collider.gameObject.tag == "Root")
                    {
                        IntVector2 destinationCoord = outHit.collider.gameObject.GetComponent <RootRail>().RootCoord;
                        gohstJewel.transform.position = cubeGrid.CoordToPosition(new IntVector2(destinationCoord.x, destinationCoord.y));
                        m_lineRenderer.SetPositions(positions.ToArray());
                        coodFound = true;
                    }
                }
                else
                {
                    break;
                }
            }
            m_lineRenderer.positionCount = positions.Count;
        }
    }