Updated PlayerController.cs

This commit is contained in:
OverflowNarhoym
2022-02-21 18:40:03 +01:00
parent b27aab9c9b
commit 1eb0cfa3de
19 changed files with 1137 additions and 21 deletions

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: b408a30f09ef434ba0049326be060a01
timeCreated: 1645440974

View File

@@ -1,6 +0,0 @@
namespace Entity
{
public interface IMovement
{
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 9f2ac0c9f14b4b33a8a3c656d94ecad2
timeCreated: 1645440963

View File

@@ -1,8 +0,0 @@
using UnityEngine;
namespace Entity.Player
{
public class PlayerController : MonoBehaviour, IMovement
{
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2e7bd18e73b54264ba94803d6535b10c
timeCreated: 1645462463

View File

@@ -0,0 +1,8 @@
namespace Orientation
{
public enum Orientation
{
Left,
Right
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5ec0be57a6f644319bcf493cfbbb7495
timeCreated: 1645462472

View File

@@ -0,0 +1,128 @@
using System;
using Movement;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Player
{
public class PlayerController : MonoBehaviour, IMovement, Input.PlayerInput.IPlayerControlActions
{
private Input.PlayerInput _controls;
private Rigidbody2D _playerRigidbody2D;
private SpriteRenderer _playerSpriteRenderer;
private bool _moveKeyPressed;
private bool _jumpKeyPressed;
private bool _canJump = true;
private bool _isOnAir;
private float _xAxisValue;
private void Awake()
{
this._playerRigidbody2D = GameObject.Find("Player").GetComponent<Rigidbody2D>();
}
private void OnEnable()
{
if (_controls == null)
{
_controls = new Input.PlayerInput();
_controls.PlayerControl.SetCallbacks(this);
}
_controls.PlayerControl.Enable();
}
private void OnDisable()
{
_controls.Disable();
}
private void FixedUpdate()
{
if (_moveKeyPressed)
Move(_xAxisValue);
else
_playerRigidbody2D.velocity = new Vector2(0.0f, _playerRigidbody2D.velocity.y);
if (_jumpKeyPressed)
Jump();
}
// PAUSE METHODS
public bool IsPaused { get; }
public void Pause()
{
throw new NotImplementedException();
}
public void Resume()
{
throw new NotImplementedException();
}
// MOVE METHODS
public float BaseSpeed { get; set; }
public void Move(float value)
{
switch (_xAxisValue)
{
case < 0:
_playerSpriteRenderer.flipX = true;
break;
case > 0:
_playerSpriteRenderer.flipX = false;
break;
}
_playerRigidbody2D.velocity = new Vector2(BaseSpeed * _xAxisValue * Time.fixedDeltaTime,
_playerRigidbody2D.velocity.y);
}
private void Jump()
{
throw new NotImplementedException();
}
private void Climb()
{
throw new NotImplementedException();
}
// INPUT HANDLING
public void OnMove(InputAction.CallbackContext context)
{
switch (context.canceled)
{
case true:
_moveKeyPressed = false;
break;
case false:
_moveKeyPressed = true;
_xAxisValue = context.ReadValue<float>();
break;
}
}
public void OnJump(InputAction.CallbackContext context)
{
switch (context.canceled)
{
case true:
_jumpKeyPressed = false;
_canJump = true;
break;
case false:
_jumpKeyPressed = true;
break;
}
}
}
}