BloodAndGore/Assets/Scripts/Player/PlayerController.cs
2019-08-22 20:19:07 +03:00

167 lines
5.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using Saltosion.OneWeapon.Effects;
using Saltosion.OneWeapon.Utils;
using Saltosion.OneWeapon.Guns;
namespace Saltosion.OneWeapon.Player {
public class PlayerController : MonoBehaviour {
public Rigidbody2D Body;
public Transform Hand;
public Transform SubHand;
public AcceleratedMovement Movement;
public SpriteRenderer BodySprite;
public Animator BodyAnim;
public SpriteRenderer HeadSprite;
public Animator HeadAnim;
public Transform HandLeft;
public Transform HandRight;
public Transform HandMiddle;
public CameraFX CameraFX;
public PunchingHand PunchingHand;
public PlayerFun PlayerFun;
private Gun Gun;
private Vector2 GunLocation = new Vector2(0, -0.6f);
private Vector3 GunRotation = new Vector3(0, 0, -90);
private bool IsHoldingShoot = false;
public float HandRetractSpeed = 10;
public float HandRotationRetractSpeed = 300;
private float CurrentHandRotation = 0f;
private float CurrentHandDistance = 0f;
public bool IsMoving {
private set; get;
} = false;
void Update() {
float X = Input.GetAxis("Horizontal");
float Y = Input.GetAxis("Vertical");
Movement.Direction = new Vector2(X, Y).normalized;
Vector3 MousePos = Input.mousePosition;
Vector3 PointedPos = Camera.main.ScreenToWorldPoint(MousePos);
Vector2 LookDirection = (PointedPos - Hand.position).normalized;
Vector3 Rot = Hand.localEulerAngles;
Rot.z = Mathf.Atan2(LookDirection.y, LookDirection.x) * Mathf.Rad2Deg + 90;
Hand.localEulerAngles = Rot;
float Rotation = Rot.z;
if (Rotation >= 45 && Rotation < 135) {
BodyAnim.Play("HorizontalBody");
BodySprite.flipX = false;
HeadAnim.Play("HeadRight");
HeadSprite.flipX = false;
Hand.localPosition = HandMiddle.localPosition;
} else if (Rotation >= 225 || Rotation < -45) {
BodyAnim.Play("HorizontalBody");
BodySprite.flipX = true;
HeadAnim.Play("HeadRight");
HeadSprite.flipX = true;
Hand.localPosition = HandMiddle.localPosition;
} else {
if (Rotation >= 135 && Rotation < 225) {
HeadAnim.Play("HeadUp");
Hand.localPosition = HandRight.localPosition;
} else {
HeadAnim.Play("HeadDown");
Hand.localPosition = HandLeft.localPosition;
}
BodyAnim.Play("VerticalBody");
BodySprite.flipX = false;
HeadSprite.flipX = false;
}
if (Gun != null) {
if (Rotation >= 180 || Rotation <= -45) {
Gun.FlipGun(true);
} else if (Rotation < 180 || Rotation > 45) {
Gun.FlipGun(false);
}
}
if (Movement.SpeedPercentage > 0.05) {
IsMoving = true;
} else {
IsMoving = false;
}
BodyAnim.SetFloat("Speed", Movement.SpeedPercentage);
HeadAnim.SetFloat("Speed", Movement.SpeedPercentage);
bool Shoot = Input.GetButton("Shoot");
if (Shoot) {
if (Gun != null) {
if (Gun.Shoot(LookDirection, Rotation, IsHoldingShoot)) {
CameraFX.StopFor(0.03f);
CameraFX.ScreenShake(4f);
PlayerFun.ShootingFun();
}
} else if (CurrentHandDistance <= 0 && !IsHoldingShoot) {
CurrentHandDistance = 1.5f;
CurrentHandRotation = 55;
PunchingHand.Punching = true;
PlayerFun.ShootingFun();
}
IsHoldingShoot = true;
}
if (Input.GetButtonUp("Shoot")) {
IsHoldingShoot = false;
}
if (CurrentHandDistance > 0) {
CurrentHandDistance -= HandRetractSpeed * Time.deltaTime;
CurrentHandDistance = Mathf.Max(CurrentHandDistance, 0);
} else {
PunchingHand.Punching = false;
}
if (CurrentHandRotation > 0) {
CurrentHandRotation -= HandRotationRetractSpeed * Time.deltaTime;
CurrentHandRotation = Mathf.Max(CurrentHandRotation, 0);
}
Vector3 HandPosition = SubHand.localPosition;
HandPosition.y = -CurrentHandDistance;
SubHand.localPosition = HandPosition;
Vector3 HandRotation = SubHand.localEulerAngles;
HandRotation.z = CurrentHandRotation;
SubHand.localEulerAngles = HandRotation;
bool Drop = Input.GetButtonDown("DropWeapon");
if (Gun != null && Drop) {
SetGun(null);
}
}
public void SetGun(Gun gun) {
PlayerFun.ReceiveNewWeapon();
if (Gun != null) {
Destroy(Gun.gameObject);
}
Gun = gun;
if (gun != null) {
Gun.transform.parent = Hand;
Gun.transform.localPosition = GunLocation;
Gun.transform.localEulerAngles = GunRotation;
}
}
}
}