using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; public class Player : MonoBehaviour { public static bool generatingStick {get; private set;} public GameObject stickPrefab; public Vector3 stickOffset; public Vector3 stickGenPosition {get { return transform.position + stickOffset;}} public Vector3 stickGrowRate; private Transform curStick; public Vector3 currentEdge; public static bool walking{get;private set;} public GameObject gameOverMenu; public TMP_Text moneyTxt; public float moneyRate= 100; public float Score {get{return Mathf.Clamp(transform.position.x * moneyRate,0,float.MaxValue);}} void Start() { CameraFollower.Target= transform; } void Update() { if(walking){return;} moneyTxt.text = "$"+Score.ToString("n0"); if(!generatingStick && Input.GetKeyDown(KeyCode.Space)){ OnStartStick(); generatingStick = true; } if(generatingStick && Input.GetKeyUp(KeyCode.Space)){ generatingStick=false; OnStopStick(); } RaycastHit2D hit= Physics2D.Linecast(transform.position, transform.position- new Vector3(0,2,0)); if(hit!=null && hit.collider != null){ currentEdge = hit.collider.bounds.center + (hit.collider.bounds.size/2f); Debug.DrawLine(transform.position, currentEdge); } } void FixedUpdate(){ if(generatingStick){ if(curStick != null){ curStick.localScale += stickGrowRate; } } } void OnStartStick(){ curStick = Instantiate(stickPrefab, currentEdge, Quaternion.identity).transform; } IEnumerator walk(Vector3 pos){ walking=true; float t =0; Vector3 startPos = transform.position; while(Vector3.Distance(transform.position, pos)> 0.01f){ t += Time.deltaTime; transform.position = Vector3.Lerp(startPos, pos, t); yield return new WaitForFixedUpdate(); } walking=false; CameraFollower.Target=transform; } IEnumerator fall(Vector3 stickEnd){ walking=true; float t =0; Vector3 startPos= transform.position; while(Vector3.Distance(transform.position, stickEnd)> 0.01f){ t += Time.deltaTime; transform.position = Vector3.Lerp(startPos, stickEnd, t); yield return new WaitForFixedUpdate(); } GameOver(); while(transform.position.y > -200){ transform.position-= new Vector3(0,0.1f,0); yield return new WaitForFixedUpdate(); } } void GameOver(){ gameOverMenu.SetActive(true); StartCoroutine(SetHighScore()); } IEnumerator SetHighScore(){ WWWForm f2 = new WWWForm(); f2.AddField("username", DataManager.Username); f2.AddField("best", (int)Score); WWW reqBest = new WWW(DataManager.API_Endpoint + "set_best.php",f2); yield return reqBest; Debug.Log(reqBest.text); } void OnStopStick(){ Rigidbody2D rb = curStick.GetComponent(); rb.AddTorque(-30); rb.simulated = true; curStick.GetComponent().connectedAnchor = transform.position; StartCoroutine(CheckStick(curStick)); curStick=null; } IEnumerator CheckStick(Transform stick){ CameraFollower.Target=null; Rigidbody2D rb = stick.GetComponent(); yield return new WaitForSeconds(0.5f); bool shorten=false; while(Mathf.Abs(rb.velocity.magnitude) > 0.01f){ Debug.Log(rb.velocity.magnitude); if(stick.GetChild(1).transform.position.x < stick.position.x){shorten=true;break;} yield return new WaitForSeconds(0.1f); } rb.simulated=false; RaycastHit2D hit = Physics2D.Linecast(stick.GetChild(1).transform.position, stick.GetChild(1).transform.position- new Vector3(0,1f,0)); if(hit.collider == null){ Debug.Log("Fall down"); StartCoroutine(fall((!shorten) ? stick.GetChild(1).position : stick.position + new Vector3(0,0.5f))); }else{ Debug.Log("Travel to point : " + hit.point + " : " + hit.collider.name); StartCoroutine(walk(hit.point + new Vector2(0,0.5f))); } } }