BloodAndGore/Assets/Scripts/Player/PlayerController.cs

167 lines
5.6 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using Saltosion.OneWeapon.Effects;
using Saltosion.OneWeapon.Utils;
2019-08-14 16:17:48 +02:00
using Saltosion.OneWeapon.Guns;
2019-08-14 16:17:48 +02:00
namespace Saltosion.OneWeapon.Player {
public class PlayerController : MonoBehaviour {
public Rigidbody2D Body;
public Transform Hand;
2019-08-04 19:26:02 +02:00
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;
2019-08-04 18:35:08 +02:00
public CameraFX CameraFX;
public PunchingHand PunchingHand;
public PlayerFun PlayerFun;
public Gun Gun { private set; get; }
private Vector2 GunLocation = new Vector2(0, -0.6f);
private Vector3 GunRotation = new Vector3(0, 0, -90);
2019-08-22 19:13:49 +02:00
private bool IsHoldingShoot = false;
2019-08-04 19:26:02 +02:00
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;
}
2019-08-22 20:36:22 +02:00
BodyAnim.SetFloat("Speed", Movement.AnimationSpeed);
HeadAnim.SetFloat("Speed", Movement.AnimationSpeed);
2019-08-22 19:13:49 +02:00
bool Shoot = Input.GetButton("Shoot");
2019-08-04 19:26:02 +02:00
if (Shoot) {
if (Gun != null) {
2019-08-22 19:13:49 +02:00
if (Gun.Shoot(LookDirection, Rotation, IsHoldingShoot)) {
2019-08-04 20:01:28 +02:00
CameraFX.StopFor(0.03f);
CameraFX.ScreenShake(4f);
PlayerFun.ShootingFun();
}
2019-08-22 19:13:49 +02:00
} else if (CurrentHandDistance <= 0 && !IsHoldingShoot) {
2019-08-04 19:26:02 +02:00
CurrentHandDistance = 1.5f;
CurrentHandRotation = 55;
PunchingHand.Punching = true;
2019-08-04 20:01:28 +02:00
PlayerFun.ShootingFun();
2019-08-04 19:26:02 +02:00
}
2019-08-22 19:13:49 +02:00
IsHoldingShoot = true;
}
if (Input.GetButtonUp("Shoot")) {
IsHoldingShoot = false;
}
2019-08-04 19:26:02 +02:00
if (CurrentHandDistance > 0) {
CurrentHandDistance -= HandRetractSpeed * Time.deltaTime;
CurrentHandDistance = Mathf.Max(CurrentHandDistance, 0);
} else {
PunchingHand.Punching = false;
2019-08-04 19:26:02 +02:00
}
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;
}
}
}
2019-08-04 19:26:02 +02:00
}