BloodAndGore/Assets/Scripts/Player.cs
2019-08-04 19:57:14 +03:00

114 lines
3.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
namespace Saltosion.OneWeapon {
public class Player : MonoBehaviour {
public Rigidbody2D Body;
public Transform Hand;
public float MoveSpeed = 50f;
public SpriteRenderer BodySprite;
public Animator BodyAnim;
public SpriteRenderer HeadSprite;
public Animator HeadAnim;
public Transform HandLeft;
public Transform HandRight;
public Transform HandMiddle;
public CameraFX CameraFX;
private Gun Gun;
private Vector2 GunLocation = new Vector2(0, -0.6f);
private Vector3 GunRotation = new Vector3(0, 0, -90);
public bool IsMoving {
private set; get;
} = false;
void Update() {
float X = Input.GetAxis("Horizontal");
float Y = Input.GetAxis("Vertical");
Vector2 Direction = new Vector2(X, Y).normalized;
Body.velocity = Direction * MoveSpeed;
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 (Direction.magnitude > 0.05) {
BodyAnim.SetFloat("Speed", 1);
HeadAnim.SetFloat("Speed", 1);
IsMoving = true;
} else {
BodyAnim.SetFloat("Speed", 0);
HeadAnim.SetFloat("Speed", 0);
IsMoving = false;
}
bool Shoot = Input.GetButtonDown("Shoot");
if (Shoot && Gun != null) {
Gun.Shoot(LookDirection, Rotation);
CameraFX.StopFor(0.03f);
CameraFX.ScreenShake(4f);
}
}
public void SetGun(Gun gun) {
if (Gun != null) {
Destroy(Gun.gameObject);
}
Gun = gun;
Gun.transform.parent = Hand;
Gun.transform.localPosition = GunLocation;
Gun.transform.localEulerAngles = GunRotation;
}
}
}