171 lines
4.4 KiB
C#
171 lines
4.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Player : MonoBehaviour
|
|
{
|
|
public GameObject StartPanel;
|
|
// public Color[] pogeColors;
|
|
[SerializeField]private Animator anim;
|
|
[SerializeField]private Transform body;
|
|
[field:SerializeField]public Vector2 Velocity {get; private set;}
|
|
[SerializeField]private float gravity=1;
|
|
[SerializeField]private float jumpForce= 5;
|
|
[SerializeField]private float forwardSpeed= 0.1f;
|
|
[SerializeField]private float jumpCooldown = 0.5f;
|
|
[SerializeField]private float radius = 0.5f;
|
|
[SerializeField]private Animator jumpAnim;
|
|
[SerializeField]private Transform paralEffect;
|
|
[SerializeField]private Vector3 paralEffectVector;
|
|
|
|
[Header("Sprites")]
|
|
public Sprite normalFace;
|
|
public Sprite sadFace;
|
|
public Sprite jumpFace;
|
|
public bool isReadyToJump { get{ return jumpTimer >= jumpCooldown;}}
|
|
|
|
|
|
private float jumpTimer= 0;
|
|
|
|
private Vector3 startPos;
|
|
public bool isAlive{get;private set;}
|
|
|
|
|
|
private SpriteRenderer spriteRenderer;
|
|
|
|
private TimeAliveMoney timeAliveMoney;
|
|
public bool gameStarted = false;
|
|
|
|
|
|
[SerializeField]private Rigidbody2D playerRigidbody;
|
|
|
|
void Start(){
|
|
StartPanel.SetActive(true);
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
timeAliveMoney = GetComponent<TimeAliveMoney>();
|
|
isAlive = true;
|
|
spriteRenderer.sprite =normalFace;
|
|
// spriteRenderer.color = pogeColors[DataManager.selectedPogeLevel];
|
|
foreach(PogeSkinItem skinItem in GetComponentsInChildren<PogeSkinItem>()){
|
|
skinItem.SetSprite(DataManager.selectedPogeLevel);
|
|
Debug.Log("Changing skin of " + skinItem.name + " to " + DataManager.selectedPogeLevel);
|
|
}
|
|
|
|
startPos = body.position;
|
|
Velocity = new Vector2(forwardSpeed,0);
|
|
|
|
CameraFollower.Target= body;
|
|
CameraFollower.UpdateFrame();
|
|
|
|
playerRigidbody.bodyType = RigidbodyType2D.Static;
|
|
playerRigidbody.gravityScale = 0;
|
|
|
|
defaultY = paralEffect.position.y;
|
|
}
|
|
int lastX = 0;
|
|
void FixedUpdate()
|
|
{
|
|
if(Input.GetKey(KeyCode.Space)){
|
|
Jump();
|
|
}
|
|
|
|
if (!gameStarted)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if(!isAlive){ return; }
|
|
HandleDeath();
|
|
|
|
|
|
HandleJump();
|
|
|
|
if(lastX != (int)body.position.x){
|
|
lastX = (int)body.position.x;
|
|
LevelGenerator.instance.GenerateAhead(2);
|
|
}
|
|
|
|
//Gravity
|
|
Velocity -= new Vector2(0, gravity);
|
|
|
|
body.Translate(Velocity);
|
|
|
|
CameraFollower.UpdateFrame();
|
|
Parallex();
|
|
}
|
|
float defaultY = -100000;
|
|
void Parallex(){
|
|
paralEffect.Translate(paralEffectVector);
|
|
paralEffect.position = Vector3.Lerp(paralEffect.position,new Vector3(paralEffect.position.x, defaultY),0.01f);
|
|
}
|
|
|
|
void HandleDeath(){
|
|
Collider2D col = Physics2D.OverlapCircle(body.position, radius);
|
|
if(col == null){ return;}
|
|
if(col.tag == "Death"){
|
|
Die();
|
|
return;
|
|
}
|
|
|
|
if(body.position.y < 0){
|
|
Die();
|
|
}
|
|
}
|
|
|
|
void Die(){
|
|
Debug.Log("Im dead bro");
|
|
timeAliveMoney.OnPlayerDeath();
|
|
isAlive = false;
|
|
spriteRenderer.sprite = sadFace;
|
|
|
|
GameUI.ShowDeath();
|
|
}
|
|
|
|
void HandleJump(){
|
|
if(jumpTimer < jumpCooldown){
|
|
jumpTimer += Time.deltaTime;
|
|
}
|
|
}
|
|
|
|
|
|
public void Jump(){
|
|
if (!gameStarted)
|
|
{
|
|
gameStarted = true;
|
|
StartPanel.SetActive(false);
|
|
playerRigidbody.bodyType = RigidbodyType2D.Dynamic;
|
|
playerRigidbody.gravityScale = gravity;
|
|
}
|
|
if(!isReadyToJump){ return; }
|
|
jumpTimer =0;
|
|
Velocity = new Vector2(Velocity.x, jumpForce);
|
|
anim.CrossFadeInFixedTime("jump",0.01f);
|
|
jumpAnim.CrossFadeInFixedTime("jump",0.01f);
|
|
paralEffect.position -= new Vector3(0,20);
|
|
StartCoroutine(switchToJumpFace());
|
|
}
|
|
|
|
|
|
IEnumerator switchToJumpFace(){
|
|
spriteRenderer.sprite = jumpFace;
|
|
yield return new WaitForSeconds(0.5f);
|
|
spriteRenderer.sprite = normalFace;
|
|
}
|
|
|
|
|
|
void OnValidate(){
|
|
if(body == null){
|
|
body = transform;
|
|
}
|
|
}
|
|
|
|
|
|
void OnTriggerEnter2D(Collider2D other){
|
|
Debug.Log(other.name);
|
|
}
|
|
|
|
void OnGizmos(){
|
|
Gizmos.DrawWireSphere(body.position, radius);
|
|
}
|
|
}
|