You are on page 1of 4

using UnityEngine;

using System.Collections;

/*IMPORTANT NOTE WHEN USING GRAPPLING HOOK: Grappling Hook does not require you to touch the
grapple point, just that you touch in the direction of the point*/
public class gHookS : touchInfoS {
GameObject vine;
//Vector2 dirVect;
public Vector2 vineEnd;
//rate which you climb the vine
public float pullRate=12f;
//length of hook
public int vineMax=10;
LayerMask collisionMask;
public Vector2 pullVector;
int testnum=0;

// Use this for initialization


void Start ()
{
collisionMask=this.gameObject.GetComponent<controllerS>().collisionMask;
pullVector = new Vector3 (0, 0, 0);
}

//playerINput and player are to differentiate between player input and ai


public bool createVine(bool playerInput=true)
{
RaycastHit2D vHit;
if (playerInput == true)
{
this.onTouch(false);
vHit = Physics2D.Raycast((Vector2)this.gameObject.GetComponent<Collider2D>().bounds.center,
dirVect, vineMax, collisionMask);

/*This block of code is only executed if the vine hits a surface within range and the mouseClick/touch
position is above the player height
*/
if (dirVect.y >= 0 && (vHit == true && vHit.collider.name.Contains("GrapplePoint")==true))
{
float angle = (180 * Mathf.Acos(dirVect.x)) / Mathf.PI;
/*This code creates the vine and transforms it accordingly*/
if (vine == null)
{
vine = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
vine.name = "Vine";
vine.transform.position = new Vector3(0, 0, 0);

}
Vector3 vScale = vine.transform.localScale;
vScale.y = vHit.distance / 2;
vScale.x = vScale.z = 0.2f;
vine.transform.localScale = vScale;
Vector2 vPos;
vPos = (float)(vine.transform.localScale.y) * dirVect +
(Vector2)this.gameObject.GetComponent<Collider2D>().bounds.center;
vine.transform.position = vPos;
vine.transform.rotation = Quaternion.Euler(0, 0, angle - 90);
vineEnd = vine.transform.localScale.y * dirVect + (Vector2)vine.transform.position;
//Debug.Log(tempCos);
return true;
}
else
{
destroyVine();
return false;
}
}
//If the vine is summoned by an enemy
else
{
dirVect = Vector2.up;
vHit = Physics2D.Raycast(this.gameObject.GetComponent<Collider2D>().bounds.center,
Vector2.up, 100, collisionMask);
vineMax = 100;
//Debug.Log(vHit.collider.name+" "+vHit.distance);

vine = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
vine.name = "Web";
vine.transform.position = new Vector3(0, 0, 0);
Vector3 vScale = vine.transform.localScale;
vScale.y = vHit.distance / 2;
vScale.x = vScale.z = 0.2f;
vine.transform.localScale = vScale;
Vector2 vPos;
vPos = (float)(vine.transform.localScale.y) * dirVect +
(Vector2)this.gameObject.GetComponent<Collider2D>().bounds.center;
vine.transform.position = vPos;
vineEnd = vine.transform.localScale.y * dirVect + (Vector2)vine.transform.position;
return true;
}
}

public Vector2 climbVine()


{
//Code for the pulling vine (and the player) up
if (vine != null) {
//Vector2 temp=this.gameObject.GetComponent<playerS> ().getVelocity();
if ((vine.transform.localScale.y*2) > 0) {
testnum++;
//Debug.Log (testnum);
/*Given that scale scales the object in both directions,
the position of the vine has to be offset in order to get the desired affect*/
Vector3 vScale = vine.transform.localScale;
vScale.y -= pullRate*Time.deltaTime;

Vector2 vPos = (Vector2)vine.transform.position;


vPos = ((-vine.transform.localScale.y + (pullRate * Time.deltaTime)) * dirVect + vineEnd);
vine.transform.position = new Vector3(vPos.x, vPos.y, vine.transform.position.z);
if ((vScale.y) < 0)
{
vScale.y = 0;
}
vine.transform.localScale = vScale;
pullVector.y = pullRate * 2 * dirVect.y;
pullVector.x = pullRate * 2 * dirVect.x;
}
else //Reaching the top of the vine
{
pullVector.y = 0;
pullVector.x = 0;
}
}
return pullVector;
}

public Vector2 vineIdle()


{
pullVector.x = 0;
pullVector.y = 0;
return pullVector;
}

public void destroyVine()


{
if (vine != null)
{
GameObject.DestroyObject (vine);
}
}

public Vector2 dismountVine()


{
destroyVine ();
return pullVector;
}

public Vector3 getPullVector()


{
//Vector3 temp=new Vector3(pullVector.x, pullVector.y,0);
return new Vector3(pullVector.x, pullVector.y,0);
}
}

You might also like