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-03 22:21:47 +02:00
|
|
|
|
|
|
|
|
|
namespace Saltosion.OneWeapon {
|
|
|
|
|
public class Player : MonoBehaviour {
|
|
|
|
|
|
|
|
|
|
public Rigidbody2D Body;
|
|
|
|
|
public Transform Hand;
|
|
|
|
|
public float MoveSpeed = 50f;
|
|
|
|
|
|
2019-08-04 15:43:22 +02:00
|
|
|
|
public SpriteRenderer BodySprite;
|
|
|
|
|
public Animator BodyAnim;
|
|
|
|
|
public SpriteRenderer HeadSprite;
|
|
|
|
|
public Animator HeadAnim;
|
|
|
|
|
|
2019-08-04 00:59:18 +02:00
|
|
|
|
private Gun Gun;
|
|
|
|
|
|
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-03 22:21:47 +02:00
|
|
|
|
void Start() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update() {
|
|
|
|
|
float X = Input.GetAxis("Horizontal");
|
|
|
|
|
float Y = Input.GetAxis("Vertical");
|
|
|
|
|
|
|
|
|
|
Vector2 Direction = new Vector2(X, Y).normalized;
|
|
|
|
|
|
2019-08-04 00:59:18 +02:00
|
|
|
|
Body.velocity = Direction * MoveSpeed;
|
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) {
|
|
|
|
|
BodyAnim.Play("HorizontalBody");
|
|
|
|
|
BodySprite.flipX = false;
|
|
|
|
|
HeadAnim.Play("HeadRight");
|
|
|
|
|
HeadSprite.flipX = false;
|
|
|
|
|
} else if (Rotation >= 225 || Rotation < -45) {
|
|
|
|
|
BodyAnim.Play("HorizontalBody");
|
|
|
|
|
BodySprite.flipX = true;
|
|
|
|
|
HeadAnim.Play("HeadRight");
|
|
|
|
|
HeadSprite.flipX = true;
|
|
|
|
|
} else {
|
|
|
|
|
if (Rotation >= 135 && Rotation < 225) {
|
|
|
|
|
HeadAnim.Play("HeadUp");
|
|
|
|
|
} else {
|
|
|
|
|
HeadAnim.Play("HeadDown");
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
BodyAnim.SetFloat("Speed", 0);
|
|
|
|
|
HeadAnim.SetFloat("Speed", 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 22:21:47 +02:00
|
|
|
|
bool Shoot = Input.GetButtonDown("Shoot");
|
2019-08-04 00:59:18 +02:00
|
|
|
|
if (Shoot && Gun != null) {
|
2019-08-04 15:43:22 +02:00
|
|
|
|
Gun.Shoot(LookDirection, Rotation);
|
2019-08-04 00:59:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetGun(Gun gun) {
|
|
|
|
|
if (Gun != null) {
|
|
|
|
|
Destroy(Gun.gameObject);
|
2019-08-03 22:21:47 +02:00
|
|
|
|
}
|
2019-08-04 00:59:18 +02:00
|
|
|
|
Gun = gun;
|
|
|
|
|
Gun.transform.parent = Hand;
|
|
|
|
|
Gun.transform.localPosition = GunLocation;
|
|
|
|
|
Gun.transform.localEulerAngles = GunRotation;
|
2019-08-03 22:21:47 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|