120 lines
4 KiB
C#
120 lines
4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PlayerMovement : MonoBehaviour{
|
|
|
|
[SerializeField] Grid startGrid = default;
|
|
[SerializeField] float speed = 5f;
|
|
float currentStep;
|
|
|
|
Grid currentGrid;
|
|
Grid lastGrid;
|
|
|
|
Animator anim;
|
|
SpriteRenderer spriteRenderer;
|
|
|
|
[SerializeField] float timeToLoad = 1f;
|
|
float currentTime;
|
|
|
|
// Start is called before the first frame update
|
|
void Awake(){
|
|
currentGrid = lastGrid = startGrid;
|
|
anim = GetComponent<Animator>();
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
AudioManager.instance.StopAll();
|
|
AudioManager.instance.Play("forest");
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update(){
|
|
if (Input.GetKeyDown(KeyCode.W)){
|
|
anim.SetTrigger("runBack");
|
|
if(transform.position == currentGrid.transform.position){
|
|
if(currentGrid.upGrid != null){
|
|
lastGrid = currentGrid;
|
|
currentGrid = currentGrid.upGrid;
|
|
currentStep = 0;
|
|
StartCoroutine(ChangeIddle("iddleBack"));
|
|
}
|
|
}
|
|
}else if (Input.GetKeyDown(KeyCode.S)){
|
|
anim.SetTrigger("runFront");
|
|
if (transform.position == currentGrid.transform.position){
|
|
if (currentGrid.downGrid != null){
|
|
lastGrid = currentGrid;
|
|
currentGrid = currentGrid.downGrid;
|
|
currentStep = 0;
|
|
StartCoroutine(ChangeIddle("iddleFront"));
|
|
}
|
|
}
|
|
}else if (Input.GetKeyDown(KeyCode.A)){
|
|
anim.SetTrigger("runSide");
|
|
spriteRenderer.flipX = true;
|
|
if (transform.position == currentGrid.transform.position){
|
|
if (currentGrid.leftGrid != null){
|
|
lastGrid = currentGrid;
|
|
currentGrid = currentGrid.leftGrid;
|
|
currentStep = 0;
|
|
StartCoroutine(ChangeIddle("iddleSide"));
|
|
}
|
|
}
|
|
}else if (Input.GetKeyDown(KeyCode.D)){
|
|
anim.SetTrigger("runSide");
|
|
spriteRenderer.flipX = false;
|
|
if (transform.position == currentGrid.transform.position){
|
|
if (currentGrid.rightGrid != null){
|
|
lastGrid = currentGrid;
|
|
currentGrid = currentGrid.rightGrid;
|
|
currentStep = 0;
|
|
StartCoroutine(ChangeIddle("iddleSide"));
|
|
}
|
|
}
|
|
}
|
|
|
|
currentStep += speed * .5f * Time.deltaTime;
|
|
currentStep = Mathf.Clamp(currentStep, 0, 1);
|
|
transform.position = Vector3.Lerp(lastGrid.transform.position, currentGrid.transform.position, currentStep);
|
|
}
|
|
|
|
void OnTriggerStay2D(Collider2D col){
|
|
if (col.CompareTag("AttackGrid")){
|
|
currentTime += Time.deltaTime;
|
|
if (col.GetComponent<Grid>().isBoss){
|
|
//Load Boss
|
|
if (currentTime > timeToLoad){
|
|
GameMaster.instance.LoadBoss(col.GetComponent<Grid>().index);
|
|
}
|
|
}
|
|
else{
|
|
//Load RandomCombat
|
|
if (currentTime > timeToLoad){
|
|
GameMaster.instance.LoadGrid(col.GetComponent<Grid>().index);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator ChangeIddle(string triggerId){
|
|
|
|
yield return new WaitForSeconds(0.05f);
|
|
|
|
Color alphaColor = spriteRenderer.color;
|
|
while(alphaColor.a > 0){
|
|
alphaColor.a -= 0.15f;
|
|
spriteRenderer.color = alphaColor;
|
|
yield return new WaitForSeconds(0.01f);
|
|
}
|
|
|
|
yield return new WaitForSeconds(0.7f);
|
|
|
|
anim.SetTrigger(triggerId);
|
|
|
|
while (alphaColor.a < 1){
|
|
alphaColor.a += 0.15f;
|
|
spriteRenderer.color = alphaColor;
|
|
yield return new WaitForSeconds(0.01f);
|
|
}
|
|
|
|
}
|
|
}
|