using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System;
public class Test_SunnyLand : MonoBehaviour
{
#region 캐릭터 컨트롤 맴버 변수
public GameObject player;
public Button btnIdle, btnRun, btnJump, btnDir;
public float jumpPower;
public Text txt;
private Animator anim;
private Coroutine routine;
private string currentAnim;
private Vector2 jumpVec;
//스위치
private Coroutine jumpRoutine;
private Coroutine runRoutine;
#endregion
#region 맵 컨트롤 맴버 변수
public Transform[] arrBackGround;
public Transform[] arrGround;
public Transform[] arrTree;
public GameObject[] arrGemGo;
public float inGameSpeed;
private float dir;
#endregion
#region 젬 컨트롤 맴버 변수
public GameObject[] arrUIGem;
public Transform targetPos;
#endregion
private int count;
private int i;
private void Awake()
{
this.anim = this.player.GetComponent<Animator>();
this.dir = 1;
}
private void Start()
{
btnDir.onClick.AddListener(() =>
{
Debug.Log("방향전환");
this.dir *= -1;
this.player.transform.localScale = new Vector3(2 * this.dir, 2, 0);
Debug.LogFormat("방향 : {0}", this.dir);
});
btnIdle.onClick.AddListener(() =>
{
StopAllCoroutines();
this.Idle();
});
btnRun.onClick.AddListener(() =>
{
this.Run();
});
btnJump.onClick.AddListener(() =>
{
this.Jump();
});
}
private void Update()
{
this.txt.text = this.count.ToString();
this.player.GetComponent<Player>().onGetGem = (pos) =>
{
this.EatGem(pos);
};
}
#region 캐릭터 컨트롤
private void Idle()
{
if (this.routine != null)
{
StopCoroutine(routine);
}
this.runRoutine = null;
this.routine = StartCoroutine(this.IdleImpl());
}
private void Run()
{
if (this.runRoutine != null)
{
Debug.Log("이미 뛰고있어");
}
else
{
this.runRoutine = StartCoroutine(this.RunImpl());
#region 맵 무브 관련
StartCoroutine(this.GemMoveImpl());
StartCoroutine(this.TreeMoveImpl());
StartCoroutine(this.GroundLoopImpl());
StartCoroutine(this.BGLoopImpl());
#endregion
}
}
private void Jump()
{
if (this.jumpRoutine != null)
{
Debug.Log("지금은 점프할 수 없어");
}
else
{
jumpRoutine = StartCoroutine(this.JumpImpl());
StartCoroutine(this.JumpPosImpl());
}
}
private IEnumerator IdleImpl()
{
Debug.Log("아이들");
this.anim.Play("player_idle");
while (true)
{
yield return null;
}
}
private IEnumerator RunImpl()
{
Debug.Log("런");
this.anim.Play("player_run");
while (true)
{
yield return null;
}
}
private IEnumerator JumpImpl()
{
if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("player_idle"))
{
this.currentAnim = "player_idle";
}
else if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("player_run"))
{
this.currentAnim = "player_run";
}
Debug.Log("쩜");
this.anim.Play("player_jump");
if (this.currentAnim == "player_idle")
{
//아이들일땐 제자리 쩜
yield return new WaitForSeconds(0.5f);
this.anim.Play("player_idle");
}
else if (this.currentAnim == "player_run")
{
//아닐땐 달리면서 쩜?
yield return new WaitForSeconds(0.5f);
this.anim.Play("player_run");
}
yield return null;
}
private IEnumerator JumpPosImpl()
{
//올라가는애
while (true)
{
this.player.transform.position += new Vector3(0, this.jumpPower * 4 * Time.deltaTime, 0);
if (this.player.transform.position.y >= this.jumpPower)
{
break;
}
yield return null;
}
//내려가는애
while (true)
{
this.player.transform.position += new Vector3(0, -this.jumpPower * 4 * Time.deltaTime, 0);
if (this.player.transform.position.y <= 0)
{
this.player.transform.position = new Vector3(0, 0, 0);
this.jumpRoutine = null;
break;
}
yield return null;
}
}
#endregion
#region 맵컨트롤
private IEnumerator BGLoopImpl()
{
if (dir > 0)
{
while (true)
{
for (int i = 0; i < this.arrBackGround.Length; i++)
{
this.arrBackGround[i].Translate(new Vector3(0.1f * this.dir, 0, 0) * this.inGameSpeed / 40);
if (this.arrBackGround[i].transform.localPosition.x >= 7.6f * this.dir)
{
this.arrBackGround[i].transform.localPosition = new Vector3(-14.2f * this.dir, -9.4412f, 0);
}
}
yield return null;
}
}
else
{
while (true)
{
for (int i = 0; i < this.arrBackGround.Length; i++)
{
this.arrBackGround[i].Translate(new Vector3(0.1f * this.dir, 0, 0) * this.inGameSpeed / 40);
if (this.arrBackGround[i].transform.localPosition.x <= -7.6f)
{
this.arrBackGround[i].transform.localPosition = new Vector3(14.2f, -9.4412f, 0);
}
}
yield return null;
}
}
}
private IEnumerator GroundLoopImpl()
{
if (dir > 0)
{
while (true)
{
for (int i = 0; i < this.arrGround.Length; i++)
{
this.arrGround[i].Translate(new Vector3(-0.1f, 0, 0) * this.inGameSpeed * this.dir);
if (this.arrGround[i].transform.localPosition.x <= -4.8 * this.dir)
{
this.arrGround[i].transform.localPosition = new Vector3(6.4f * this.dir, -9.6912f, 0);
}
}
yield return null;
}
}
else
{
while (true)
{
for (int i = 0; i < this.arrGround.Length; i++)
{
this.arrGround[i].Translate(new Vector3(-0.1f, 0, 0) * this.inGameSpeed * this.dir);
if (this.arrGround[i].transform.localPosition.x >= 4.8f)
{
this.arrGround[i].transform.localPosition = new Vector3(-6.4f, -9.6912f, 0);
}
}
yield return null;
}
}
}
private IEnumerator TreeMoveImpl()
{
if (dir > 0)
{
while (true)
{
for (int i = 0; i < this.arrTree.Length; i++)
{
this.arrTree[i].Translate(new Vector3(-0.1f, 0, 0) * this.inGameSpeed / 5 * this.dir);
if (this.arrTree[i].transform.localPosition.x <= -4 * this.dir)
{
this.arrTree[i].transform.localPosition = new Vector3(7.9f * this.dir, -14.298f, 0);
}
}
yield return null;
}
}
else
{
while (true)
{
for (int i = 0; i < this.arrTree.Length; i++)
{
this.arrTree[i].Translate(new Vector3(-0.1f, 0, 0) * this.inGameSpeed / 5 * this.dir);
if (this.arrTree[i].transform.localPosition.x >= 4)
{
this.arrTree[i].transform.localPosition = new Vector3(-8.1f, -14.298f, 0);
}
}
yield return null;
}
}
}
private IEnumerator GemMoveImpl()
{
while (true)
{
for (int i = 0; i < this.arrGemGo.Length; i++)
{
this.arrGemGo[i].transform.position += new Vector3(-0.1f, 0, 0) * this.inGameSpeed * this.dir;
if (this.arrGemGo[i].transform.localPosition.x <= -4.8 * this.dir)
{
this.arrGemGo[i].transform.localPosition = new Vector3(6.4f * this.dir, this.arrGemGo[i].transform.localPosition.y, 0);
this.arrGemGo[i].SetActive(true);
}
}
yield return null;
}
}
#endregion
#region 젬컨트롤
private void EatGem(Vector3 pos)
{
StartCoroutine(this.GetGemMove(this.i, pos));
this.i++;
if (this.i >= this.arrUIGem.Length)
{
this.i = 0;
}
}
private IEnumerator GetGemMove(int i,Vector3 pos)
{
Debug.Log(i);
this.arrUIGem[i].transform.position = Camera.main.WorldToScreenPoint(pos);
this.arrUIGem[i].SetActive(true);
Vector3 dir = (targetPos.position - this.arrUIGem[i].transform.position).normalized;
var dis = Vector3.Distance(targetPos.position, this.arrUIGem[i].transform.position);
while (true)
{
this.arrUIGem[i].transform.position += 500 * dir * Time.deltaTime;
dis -= 500 * Time.deltaTime;
if (dis <= 0)
{
this.arrUIGem[i].SetActive(false);
this.count++;
break;
}
yield return null;
}
}
#endregion
}