2019-08-03 22:21:47 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
2019-08-04 15:43:22 +02:00
|
|
|
|
using UnityEngine.Animations;
|
2019-08-07 21:43:22 +02:00
|
|
|
|
using Saltosion.OneWeapon.Effects;
|
2019-08-07 22:54:18 +02:00
|
|
|
|
using Saltosion.OneWeapon.Utils;
|
2019-08-14 16:17:48 +02:00
|
|
|
|
using Saltosion.OneWeapon.Guns;
|
2019-08-03 22:21:47 +02:00
|
|
|
|
|
2019-08-14 16:17:48 +02:00
|
|
|
|
namespace Saltosion.OneWeapon.Player {
|
|
|
|
|
public class PlayerController : MonoBehaviour {
|
2019-08-03 22:21:47 +02:00
|
|
|
|
|
|
|
|
|
public Rigidbody2D Body;
|
|
|
|
|
public Transform Hand;
|
2019-08-04 19:26:02 +02:00
|
|
|
|
public Transform SubHand;
|
2019-08-07 22:54:18 +02:00
|
|
|
|
|
|
|
|
|
public AcceleratedMovement Movement;
|
2019-08-03 22:21:47 +02:00
|
|
|
|
|
2019-08-04 15:43:22 +02:00
|
|
|
|
public SpriteRenderer BodySprite;
|
|
|
|
|
public Animator BodyAnim;
|
|
|
|
|
public SpriteRenderer HeadSprite;
|
|
|
|
|
public Animator HeadAnim;
|
|
|
|
|
|
2019-08-04 16:12:00 +02:00
|
|
|
|
public Transform HandLeft;
|
|
|
|
|
public Transform HandRight;
|
|
|
|
|
public Transform HandMiddle;
|
|
|
|
|
|
2019-08-04 18:35:08 +02:00
|
|
|
|
public CameraFX CameraFX;
|
|
|
|
|
|
2019-08-04 19:48:26 +02:00
|
|
|
|
public PunchingHand PunchingHand;
|
|
|
|
|
|
|
|
|
|
public PlayerFun PlayerFun;
|
|
|
|
|
|
2019-08-22 20:12:10 +02:00
|
|
|
|
public Gun Gun { private set; get; }
|
2019-08-04 15:43:22 +02:00
|
|
|
|
private Vector2 GunLocation = new Vector2(0, -0.6f);
|
2019-08-04 00:59:18 +02:00
|
|
|
|
private Vector3 GunRotation = new Vector3(0, 0, -90);
|
2019-08-22 19:13:49 +02:00
|
|
|
|
private bool IsHoldingShoot = false;
|
2019-08-04 00:59:18 +02:00
|
|
|
|
|
2019-08-04 19:26:02 +02:00
|
|
|
|
public float HandRetractSpeed = 10;
|
|
|
|
|
public float HandRotationRetractSpeed = 300;
|
|
|
|
|
private float CurrentHandRotation = 0f;
|
|
|
|
|
private float CurrentHandDistance = 0f;
|
|
|
|
|
|
2019-08-07 21:43:22 +02:00
|
|
|
|
|
2019-08-04 17:35:05 +02:00
|
|
|
|
public bool IsMoving {
|
|
|
|
|
private set; get;
|
|
|
|
|
} = false;
|
2019-08-03 22:21:47 +02:00
|
|
|
|
|
|
|
|
|
void Update() {
|
|
|
|
|
float X = Input.GetAxis("Horizontal");
|
|
|
|
|
float Y = Input.GetAxis("Vertical");
|
|
|
|
|
|
2019-08-07 22:54:18 +02:00
|
|
|
|
Movement.Direction = new Vector2(X, Y).normalized;
|
2019-08-03 22:21:47 +02:00
|
|
|
|
|
|
|
|
|
Vector3 MousePos = Input.mousePosition;
|
|
|
|
|
Vector3 PointedPos = Camera.main.ScreenToWorldPoint(MousePos);
|
|
|
|
|
|
2019-08-04 00:59:18 +02:00
|
|
|
|
Vector2 LookDirection = (PointedPos - Hand.position).normalized;
|
2019-08-03 22:21:47 +02:00
|
|
|
|
|
2019-08-04 15:43:22 +02:00
|
|
|
|
Vector3 Rot = Hand.localEulerAngles;
|
2019-08-03 22:21:47 +02:00
|
|
|
|
Rot.z = Mathf.Atan2(LookDirection.y, LookDirection.x) * Mathf.Rad2Deg + 90;
|
|
|
|
|
Hand.localEulerAngles = Rot;
|
|
|
|
|
|
2019-08-04 15:43:22 +02:00
|
|
|
|
float Rotation = Rot.z;
|
|
|
|
|
|
|
|
|
|
if (Rotation >= 45 && Rotation < 135) {
|
2019-08-14 16:48:17 +02:00
|
|
|
|
BodyAnim.Play("HorizontalBody");
|
2019-08-04 15:43:22 +02:00
|
|
|
|
BodySprite.flipX = false;
|
2019-08-14 16:48:17 +02:00
|
|
|
|
HeadAnim.Play("HeadRight");
|
2019-08-04 15:43:22 +02:00
|
|
|
|
HeadSprite.flipX = false;
|
2019-08-04 16:12:00 +02:00
|
|
|
|
Hand.localPosition = HandMiddle.localPosition;
|
2019-08-04 15:43:22 +02:00
|
|
|
|
} else if (Rotation >= 225 || Rotation < -45) {
|
2019-08-14 16:48:17 +02:00
|
|
|
|
BodyAnim.Play("HorizontalBody");
|
2019-08-04 15:43:22 +02:00
|
|
|
|
BodySprite.flipX = true;
|
2019-08-14 16:48:17 +02:00
|
|
|
|
HeadAnim.Play("HeadRight");
|
2019-08-04 15:43:22 +02:00
|
|
|
|
HeadSprite.flipX = true;
|
2019-08-04 16:12:00 +02:00
|
|
|
|
Hand.localPosition = HandMiddle.localPosition;
|
2019-08-04 15:43:22 +02:00
|
|
|
|
} else {
|
|
|
|
|
if (Rotation >= 135 && Rotation < 225) {
|
2019-08-14 16:48:17 +02:00
|
|
|
|
HeadAnim.Play("HeadUp");
|
2019-08-04 16:12:00 +02:00
|
|
|
|
Hand.localPosition = HandRight.localPosition;
|
2019-08-04 15:43:22 +02:00
|
|
|
|
} else {
|
2019-08-14 16:48:17 +02:00
|
|
|
|
HeadAnim.Play("HeadDown");
|
2019-08-04 16:12:00 +02:00
|
|
|
|
Hand.localPosition = HandLeft.localPosition;
|
2019-08-04 15:43:22 +02:00
|
|
|
|
}
|
2019-08-14 16:48:17 +02:00
|
|
|
|
BodyAnim.Play("VerticalBody");
|
2019-08-04 15:43:22 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-07 22:54:18 +02:00
|
|
|
|
if (Movement.SpeedPercentage > 0.05) {
|
2019-08-04 17:35:05 +02:00
|
|
|
|
IsMoving = true;
|
2019-08-04 15:43:22 +02:00
|
|
|
|
} else {
|
2019-08-04 17:35:05 +02:00
|
|
|
|
IsMoving = false;
|
2019-08-04 15:43:22 +02:00
|
|
|
|
}
|
2019-08-22 20:36:22 +02:00
|
|
|
|
BodyAnim.SetFloat("Speed", Movement.AnimationSpeed);
|
|
|
|
|
HeadAnim.SetFloat("Speed", Movement.AnimationSpeed);
|
2019-08-04 15:43:22 +02:00
|
|
|
|
|
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;
|
2019-08-04 19:48:26 +02:00
|
|
|
|
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 00:59:18 +02:00
|
|
|
|
}
|
2019-08-04 19:26:02 +02:00
|
|
|
|
|
|
|
|
|
if (CurrentHandDistance > 0) {
|
|
|
|
|
CurrentHandDistance -= HandRetractSpeed * Time.deltaTime;
|
|
|
|
|
CurrentHandDistance = Mathf.Max(CurrentHandDistance, 0);
|
2019-08-04 19:48:26 +02:00
|
|
|
|
} 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;
|
2019-08-04 19:48:26 +02:00
|
|
|
|
|
|
|
|
|
bool Drop = Input.GetButtonDown("DropWeapon");
|
|
|
|
|
if (Gun != null && Drop) {
|
|
|
|
|
SetGun(null);
|
|
|
|
|
}
|
2019-08-04 00:59:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetGun(Gun gun) {
|
2019-08-04 19:48:26 +02:00
|
|
|
|
PlayerFun.ReceiveNewWeapon();
|
2019-08-04 00:59:18 +02:00
|
|
|
|
if (Gun != null) {
|
|
|
|
|
Destroy(Gun.gameObject);
|
2019-08-03 22:21:47 +02:00
|
|
|
|
}
|
2019-08-04 00:59:18 +02:00
|
|
|
|
Gun = gun;
|
2019-08-04 19:48:26 +02:00
|
|
|
|
if (gun != null) {
|
|
|
|
|
Gun.transform.parent = Hand;
|
|
|
|
|
Gun.transform.localPosition = GunLocation;
|
|
|
|
|
Gun.transform.localEulerAngles = GunRotation;
|
|
|
|
|
}
|
2019-08-03 22:21:47 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-04 19:26:02 +02:00
|
|
|
|
|
2019-08-03 22:21:47 +02:00
|
|
|
|
}
|