해피글라스 만들기 [켠김에 완성까지]

 





GameManager.cs 소스입니다


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public Image BarImage;
    public LineManager LM;
    public Water2D.Water2D_Spawner WS;

    public void Win() 
    {
        print("성공");
    }

    public void WaterStart() 
    {
        WS.enabled = true;
    }

void Update()
    {
        BarImage.fillAmount = 1 - LM.lineMass * 0.01f;
    }
}











LineManager.cs 소스입니다




using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineManager : MonoBehaviour
{
    public GameManager GM;
    public GameObject Line;
    public int lineMass;

    GameObject CurLine;
    LineRenderer LR;
    PolygonCollider2D PC;
    List<Vector2> points = new List<Vector2>();
    List<Vector2> polygon2DPoints = new List<Vector2>();
    Vector2 tempVector, direction, sum;
    float angle, halfWidth;
    bool isCol, firstClick;



    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            CurLine = Instantiate(Line);
            LR = CurLine.GetComponent<LineRenderer>();
            PC = CurLine.GetComponent<PolygonCollider2D>();

            points.Add(GetMousePos());
            LR.positionCount = 1;
            LR.SetPosition(0, points[0]);
            halfWidth = LR.startWidth * 0.5f;

            if (!firstClick) GM.WaterStart();
            firstClick = true;
            isCol = false;
        }

        else if (Input.GetMouseButton(0))
        {
            Vector2 pos = GetMousePos();
            int lastIndex = points.Count - 1;

            Collider2D col = Physics2D.Raycast(pos, Vector2.zero, 100).collider;
            if (col != null && col.CompareTag("Wall")) isCol = true; 
            if (col != null && col.CompareTag("Line") && col.gameObject != CurLine) isCol = true;
            if (isCol) return;

            if (Vector2.SqrMagnitude(points[lastIndex] - pos) > 0.1f)
            {
                ++lineMass;
                sum += points[lastIndex];
                points.Add(pos);
                LR.SetPosition(++LR.positionCount - 1, pos);
                AddPointToCollider(lastIndex);
            }
        }
        else if (Input.GetMouseButtonUp(0)) 
        {
            sum /= points.Count;
            CurLine.AddComponent<Rigidbody2D>().centerOfMass = new Vector3(sum.x, sum.y, 0);

            points.Add(GetMousePos());
            AddPointToCollider(points.Count - 1);
            points.Clear();
        }
    }


    Vector3 GetMousePos()
    {
        return Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }


    void AddPointToCollider(int index)
    {
        direction = points[index] - points[index + 1 < points.Count ? index + 1 : (index - 1 >= 0 ? index - 1 : index)];
        angle = Mathf.Atan2(direction.x, -direction.y);

        tempVector = points[index];
        tempVector.x = tempVector.x + halfWidth * Mathf.Cos(angle);
        tempVector.y = tempVector.y + halfWidth * Mathf.Sin(angle);
        polygon2DPoints.Insert(polygon2DPoints.Count, tempVector);

        tempVector = points[index];
        tempVector.x = tempVector.x - halfWidth * Mathf.Cos(angle);
        tempVector.y = tempVector.y - halfWidth * Mathf.Sin(angle);
        polygon2DPoints.Insert(0, tempVector);

        PC.points = polygon2DPoints.ToArray();
    }
}














CupScript.cs 소스입니다




using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CupScript : MonoBehaviour
{
public GameManager GM;
public SpriteRenderer SR;
public Sprite[] CupSprites;
public int waterCount;
bool isWin;


void OnTriggerEnter2D(Collider2D col)
{
if(col.CompareTag("Metaball_liquid")) ++waterCount;
}

void OnTriggerExit2D(Collider2D col)
{
if (col.CompareTag("Metaball_liquid")) --waterCount;
}

void Update()
{
if (waterCount > 14) // 성공
{
SR.sprite = CupSprites[2];
if(!isWin) GM.Win();
isWin = true;
}
else if (waterCount == 0) SR.sprite = CupSprites[0];
else SR.sprite = CupSprites[1];
}
}