2020-11-07 13:10:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
2020-11-06 00:56:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
2020-12-06 22:12:51 +00:00
|
|
|
|
using UnityEngine.SceneManagement;
|
2020-12-06 17:33:36 +00:00
|
|
|
|
using UnityEngine.Serialization;
|
2020-11-07 13:47:31 +00:00
|
|
|
|
using Random = System.Random;
|
2020-11-06 00:56:50 +00:00
|
|
|
|
|
2020-11-17 15:14:13 +00:00
|
|
|
|
public class OwnCharacterController : CharacterControllerBase
|
2020-11-06 00:56:50 +00:00
|
|
|
|
{
|
2020-11-07 15:16:03 +00:00
|
|
|
|
public float jumpForce;
|
|
|
|
|
public float movementSpeed;
|
|
|
|
|
public float sprintSpeed;
|
2020-11-17 15:14:13 +00:00
|
|
|
|
public PlatformSpawner platformSpawner;
|
2020-12-06 20:31:22 +00:00
|
|
|
|
[FormerlySerializedAs("scoreSystem")] public ScoreAndAchievements scoreAndAchievements;
|
2020-12-06 17:33:36 +00:00
|
|
|
|
public HUDManager hudManager;
|
2020-11-07 15:16:03 +00:00
|
|
|
|
|
2020-11-07 13:10:48 +00:00
|
|
|
|
private Vector3 _spawnPos;
|
2020-11-07 13:47:31 +00:00
|
|
|
|
private float _health = 100f;
|
2020-11-17 15:14:13 +00:00
|
|
|
|
private readonly Random _random = new Random();
|
|
|
|
|
private readonly List<Vector3> _checkpointPosList = new List<Vector3>();
|
2020-11-07 20:50:40 +00:00
|
|
|
|
private Vector3 _checkpointPos;
|
2020-11-17 23:11:03 +00:00
|
|
|
|
private Animator _animator;
|
|
|
|
|
private static readonly int Speed = Animator.StringToHash("Speed");
|
|
|
|
|
private static readonly int Jump = Animator.StringToHash("Jump");
|
|
|
|
|
private static readonly int Sprint = Animator.StringToHash("Sprint");
|
|
|
|
|
private JumpStatus _jumpStatus = JumpStatus.None;
|
2020-11-06 00:56:50 +00:00
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
2020-11-07 13:10:48 +00:00
|
|
|
|
_rb = GetComponent<Rigidbody2D>();
|
|
|
|
|
_spawnPos = transform.position;
|
2020-11-17 23:11:03 +00:00
|
|
|
|
_animator = GetComponent<Animator>();
|
2020-11-06 00:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
2020-11-17 15:14:13 +00:00
|
|
|
|
if (platformSpawner.ShouldRespawn(transform, this))
|
|
|
|
|
Respawn();
|
2020-11-06 00:56:50 +00:00
|
|
|
|
float input = Input.GetAxis("Horizontal");
|
2020-11-07 20:50:40 +00:00
|
|
|
|
var tr = transform;
|
|
|
|
|
if (input < 0 && tr.localScale.x > 0
|
|
|
|
|
|| input > 0 && tr.localScale.x < 0)
|
2020-11-06 00:56:50 +00:00
|
|
|
|
{
|
|
|
|
|
var scale = tr.localScale;
|
|
|
|
|
scale.x *= -1;
|
|
|
|
|
tr.localScale = scale;
|
|
|
|
|
}
|
2020-11-06 20:20:17 +00:00
|
|
|
|
|
2020-11-19 23:48:50 +00:00
|
|
|
|
bool sprinting = Input.GetButton("Fire3");
|
|
|
|
|
if (sprinting)
|
2020-11-07 15:16:03 +00:00
|
|
|
|
input *= sprintSpeed;
|
2020-11-17 23:11:03 +00:00
|
|
|
|
|
|
|
|
|
if (Mathf.Abs(_rb.velocity.x) <= 3)
|
|
|
|
|
_rb.velocity += new Vector2(input * movementSpeed, 0);
|
2020-11-06 00:56:50 +00:00
|
|
|
|
|
|
|
|
|
if (Input.GetButtonDown("Jump") && IsOnGround())
|
2020-11-17 23:11:03 +00:00
|
|
|
|
{
|
|
|
|
|
//_rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
|
|
|
|
|
_rb.velocity += new Vector2(0, jumpForce);
|
2020-11-20 14:34:20 +00:00
|
|
|
|
_jumpStatus = JumpStatus.Up;
|
|
|
|
|
_animator.SetInteger(Jump, (int) _jumpStatus);
|
2020-11-17 23:11:03 +00:00
|
|
|
|
}
|
|
|
|
|
else if (_jumpStatus == JumpStatus.Up && _rb.velocity.y <= 0)
|
|
|
|
|
{
|
|
|
|
|
_jumpStatus = JumpStatus.Down;
|
|
|
|
|
_animator.SetInteger(Jump, (int) _jumpStatus);
|
|
|
|
|
}
|
|
|
|
|
else if (_jumpStatus == JumpStatus.Down && Math.Abs(_rb.velocity.y) <= 0.001f)
|
|
|
|
|
{
|
|
|
|
|
_jumpStatus = JumpStatus.None;
|
|
|
|
|
_animator.SetInteger(Jump, (int) _jumpStatus);
|
|
|
|
|
}
|
2020-11-07 20:50:40 +00:00
|
|
|
|
|
|
|
|
|
if (_checkpointPos.x > 0 && (tr.position - _checkpointPos).magnitude < 2f)
|
|
|
|
|
{
|
2020-12-06 17:33:36 +00:00
|
|
|
|
CheckpointReached();
|
2020-11-07 20:50:40 +00:00
|
|
|
|
_checkpointPosList.RemoveAt(0);
|
|
|
|
|
_checkpointPos = _checkpointPosList.Count > 0 ? _checkpointPosList[0] : Vector3.zero;
|
|
|
|
|
}
|
2020-11-17 23:11:03 +00:00
|
|
|
|
|
|
|
|
|
_animator.SetFloat(Speed, Math.Abs(_rb.velocity.x));
|
|
|
|
|
_animator.SetBool(Sprint, Input.GetButton("Fire3"));
|
2020-12-06 22:12:51 +00:00
|
|
|
|
|
|
|
|
|
if (Input.GetButtonDown("Cancel"))
|
|
|
|
|
SceneManager.LoadScene("Menu");
|
2020-11-07 13:10:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-06 17:33:36 +00:00
|
|
|
|
private void CheckpointReached()
|
|
|
|
|
{
|
|
|
|
|
_spawnPos = _checkpointPos;
|
2020-12-06 20:31:22 +00:00
|
|
|
|
scoreAndAchievements.AddScore(100);
|
|
|
|
|
scoreAndAchievements.NextLevel();
|
2020-12-06 17:33:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 13:10:48 +00:00
|
|
|
|
public void Hit()
|
|
|
|
|
{
|
2020-12-06 17:33:36 +00:00
|
|
|
|
_health -= (float) _random.NextDouble() * 20f;
|
|
|
|
|
hudManager.UpdateHealth(_health);
|
2020-11-07 13:47:31 +00:00
|
|
|
|
if (_health <= 0f)
|
|
|
|
|
Respawn();
|
2020-11-07 13:10:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 20:50:40 +00:00
|
|
|
|
public void Respawn()
|
2020-11-07 13:10:48 +00:00
|
|
|
|
{
|
2020-12-06 20:31:22 +00:00
|
|
|
|
scoreAndAchievements.AddScore(-20);
|
2020-11-07 13:10:48 +00:00
|
|
|
|
transform.position = _spawnPos;
|
2020-11-07 13:47:31 +00:00
|
|
|
|
_health = 100f;
|
2020-12-06 17:33:36 +00:00
|
|
|
|
hudManager.UpdateHealth(_health);
|
2020-11-07 22:02:47 +00:00
|
|
|
|
_rb.velocity = Vector2.zero;
|
2020-11-06 00:56:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 20:50:40 +00:00
|
|
|
|
public void SetCheckpoint(Vector3 pos)
|
2020-11-07 14:12:21 +00:00
|
|
|
|
{
|
2020-11-07 20:50:40 +00:00
|
|
|
|
_checkpointPosList.Add(pos);
|
|
|
|
|
if (_checkpointPos.x <= 0) _checkpointPos = _checkpointPosList[0];
|
2020-11-07 14:12:21 +00:00
|
|
|
|
}
|
2020-11-17 23:11:03 +00:00
|
|
|
|
|
|
|
|
|
enum JumpStatus
|
|
|
|
|
{
|
|
|
|
|
None,
|
|
|
|
|
Up,
|
|
|
|
|
Down
|
|
|
|
|
}
|
2020-11-06 00:56:50 +00:00
|
|
|
|
}
|