Muchas cosas

This commit is contained in:
Gerard Gascón 2023-02-27 20:45:11 +01:00
parent b4e11ac33f
commit 9c92dc678e
76 changed files with 15307 additions and 2021 deletions

View file

@ -1,6 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using MyBox;
using SimpleTools.Cinemachine;
using UnityEngine;
using UnityEngine.Serialization;
@ -8,6 +11,7 @@ public class PlayerController : MonoBehaviour {
[Header("Stats")]
[SerializeField] float speed = 50f;
float _currentSpeed;
[SerializeField, Range(0f, .5f)] float acceleration = .1f;
[SerializeField, Range(0f, .5f)] float flipSmooth = .1f;
[SerializeField] float jumpForce = 10f;
@ -31,11 +35,37 @@ public class PlayerController : MonoBehaviour {
[SerializeField, Range(0f, .5f)] float jumpBuffer;
float _jumpBuffer;
[Header("Camera")]
[SerializeField, OverrideLabel("vCam")] CinemachineVirtualCamera vCam;
Transform _cameraTarget;
Animator _anim;
static readonly int Speed = Animator.StringToHash("Speed");
static readonly int Grounded = Animator.StringToHash("Grounded");
static readonly int Die = Animator.StringToHash("Die");
bool _dead;
void Awake() {
_rb2d = GetComponent<Rigidbody2D>();
_anim = GetComponent<Animator>();
_cameraTarget = new GameObject("Camera Target").transform;
vCam.m_Follow = _cameraTarget;
ResetSpeed();
}
void Update() {
Vector3 localScale;
if (_dead) {
localScale = transform.localScale;
localScale = new Vector3(Mathf.SmoothDamp(localScale.x, _facingDirection, ref _flipVelocity, flipSmooth),
localScale.y, localScale.z);
transform.localScale = localScale;
return;
}
_input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
_facingDirection = _input.x switch {
> 0f => 1,
@ -43,12 +73,13 @@ public class PlayerController : MonoBehaviour {
_ => _facingDirection
};
Vector3 localScale = transform.localScale;
localScale = transform.localScale;
localScale = new Vector3(Mathf.SmoothDamp(localScale.x, _facingDirection, ref _flipVelocity, flipSmooth),
localScale.y, localScale.z);
transform.localScale = localScale;
_isGrounded = Physics2D.OverlapBox(feetPos.position, new Vector2(feetWidth, feetHeight), 0f, groundMask);
_anim.SetBool(Grounded, _isGrounded);
if (_isGrounded && _rb2d.velocity.y <= 0f) _coyoteTime = coyoteTime;
if (Input.GetKeyDown(KeyCode.Space)) _jumpBuffer = jumpBuffer;
@ -59,7 +90,10 @@ public class PlayerController : MonoBehaviour {
}
void FixedUpdate() {
_rb2d.velocity = new Vector2(Mathf.SmoothDamp(_rb2d.velocity.x, _input.x * speed, ref _xVelocity, acceleration), _rb2d.velocity.y);
if (_dead) return;
_rb2d.velocity = new Vector2(Mathf.SmoothDamp(_rb2d.velocity.x, _input.x * _currentSpeed, ref _xVelocity, acceleration), _rb2d.velocity.y);
_anim.SetFloat(Speed, Mathf.Abs(_rb2d.velocity.x));
if (_coyoteTime > 0f && _jumpBuffer > 0f) {
_rb2d.velocity = new Vector2(_rb2d.velocity.x, 0f);
@ -73,8 +107,32 @@ public class PlayerController : MonoBehaviour {
}
}
void LateUpdate() {
if (_dead) return;
float yPos = _isGrounded && _rb2d.velocity.y <= 0f ? transform.position.y : _cameraTarget.position.y;
_cameraTarget.position = new Vector3(transform.position.x, yPos);
}
void OnDrawGizmosSelected() {
Gizmos.color = Color.red;
if (feetPos) Gizmos.DrawWireCube(feetPos.position, new Vector3(feetWidth, feetHeight));
}
void OnTriggerEnter2D(Collider2D col) {
if (col.CompareTag("Ghost")) {
_anim.SetTrigger(Die);
_dead = true;
ScreenShake.Shake(20f, .5f);
}
}
#region OrbPowerups
public void ResetSpeed() {
_currentSpeed = speed;
}
public void SetSpeed(float newSpeed) {
_currentSpeed = newSpeed;
}
#endregion
}