Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

Commit

Permalink
Add little improves to the code and a fix to uncut ropes not moving a…
Browse files Browse the repository at this point in the history
…fter the candy was caught
  • Loading branch information
joaoborks committed Nov 2, 2016
1 parent 4d19574 commit ae21a74
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
13 changes: 8 additions & 5 deletions Assets/Scripts/BottomLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ public class BottomLine : MonoBehaviour

Frog frog;

Vector2 boxPos
{
get { return (Vector2)transform.position + offset; }
}

void Awake()
{
frog = FindObjectOfType<Frog>();
}

void Update()
void FixedUpdate()
{
if (Physics2D.OverlapBox(transform.position + (Vector3)offset, size, 0, layer))
{
if (Physics2D.OverlapBox(boxPos, size, 0, layer))
frog.Lose();
}
}

void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawWireCube(transform.position + (Vector3)offset, size);
Gizmos.DrawWireCube(boxPos, size);
}
}
5 changes: 4 additions & 1 deletion Assets/Scripts/Candy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using UnityEngine;
using UnityEngine;
using System.Collections;

public class Candy : MonoBehaviour
Expand All @@ -14,5 +14,8 @@ void Awake()
public void GetEaten()
{
gameObject.SetActive(false);
foreach (Rope rope in ropes)
if (!rope.broken)
rope.Break(transform.position, false);
}
}
20 changes: 14 additions & 6 deletions Assets/Scripts/Rope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

public class Rope : MonoBehaviour
{
public bool broken { get; private set; }

Transform fragment,
candy,
tip;
LineRenderer line;
EdgeCollider2D col;
DistanceJoint2D dist;
RaycastHit2D[] hit;
bool broken;

void Awake()
{
Expand Down Expand Up @@ -48,6 +49,10 @@ public void AttachCandy(Transform candy)
}

public void Break(Vector2 point)
{
Break(point, true);
}
public void Break(Vector2 point, bool fragmentate)
{
if (broken)
return;
Expand All @@ -61,11 +66,14 @@ public void Break(Vector2 point)
dist.anchor = Vector2.zero;
dist.distance = Vector2.Distance(point, transform.position);

// The remaining rope should stick with the candy
fragment.SetParent(candy, false);
fragment.transform.localPosition = Vector2.zero;
fragment.gameObject.SetActive(true);
fragment.GetComponent<RopeFragment>().Detach(point);
if (fragmentate)
{
// The remaining rope should stick with the candy
fragment.SetParent(candy, false);
fragment.transform.localPosition = Vector2.zero;
fragment.gameObject.SetActive(true);
fragment.GetComponent<RopeFragment>().Detach(point);
}

// Prevent from breaking again
broken = true;
Expand Down

0 comments on commit ae21a74

Please sign in to comment.