186 lines
5.2 KiB
C#
186 lines
5.2 KiB
C#
using System.Linq;
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
|
|
public class HumanController : MonoBehaviour {
|
|
protected Rigidbody2D _rb;
|
|
protected Animator anim;
|
|
public float maxWalkSpeed = 3.0f;
|
|
public float maxRunSpeed = 6.0f;
|
|
public float jumpAcceleration = 10;
|
|
public LayerMask groundMask;
|
|
public float groundCastRadius = 0.1f;
|
|
public bool grounded;
|
|
protected bool facingLeft;
|
|
protected bool crouch;
|
|
public bool Crouch {
|
|
get { return crouch; }
|
|
set { crouch = value; }
|
|
}
|
|
protected bool freeFall;
|
|
protected Health health;
|
|
public RandomSoundPlayer player;
|
|
protected bool deadStateEnter;
|
|
protected Inventory inventory;
|
|
public ItemSocket[] sockets;
|
|
protected bool gunArmed;
|
|
|
|
private float armsStandOffset;
|
|
public float ArmsStandOffset {
|
|
get { return armsStandOffset; }
|
|
}
|
|
private float armsCrouchOffset;
|
|
public float ArmsCrouchOffset {
|
|
get { return armsCrouchOffset; }
|
|
}
|
|
public bool GunArmed {
|
|
get { return gunArmed; }
|
|
}
|
|
public GameObject eyes;
|
|
|
|
protected virtual void Start() {
|
|
_rb = gameObject.GetComponent<Rigidbody2D>();
|
|
anim = gameObject.GetComponent<Animator>();
|
|
health = GetComponent<Health>();
|
|
inventory = GetComponent<Inventory>();
|
|
|
|
|
|
|
|
|
|
armsStandOffset = 2.7641f;
|
|
armsCrouchOffset = 1.6311f;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
public void Awake() {
|
|
/* var arms = GetSocket(ItemSocketType.Hands).place;
|
|
var animator = GetComponent<Animator>();
|
|
var clips = animator.runtimeAnimatorController.animationClips;
|
|
|
|
var crouchGun = clips.First(v => v.name == "PlayerCrouchGunIdle");
|
|
var standGun = clips.First(v => v.name == "PlayerStandWeaponIdle");
|
|
|
|
standGun.SampleAnimation(gameObject, 0);
|
|
armsStandOffset = arms.transform.position.y - transform.position.y;
|
|
Debug.Log("armsStandOffset = " + armsStandOffset);
|
|
|
|
crouchGun.SampleAnimation(gameObject, 0);
|
|
armsCrouchOffset = arms.transform.position.y - transform.position.y;
|
|
Debug.Log("armsCrouchOffset = " + armsCrouchOffset);*/
|
|
|
|
|
|
}
|
|
|
|
public void OnAnimationGunArmed(bool armed) {
|
|
|
|
}
|
|
|
|
public void OnDeadStateEnter() {
|
|
deadStateEnter = true;
|
|
|
|
inventory.DeleteAllItems();
|
|
}
|
|
|
|
|
|
public bool PrepareItem() {
|
|
var socket = GetSocket(ItemSocketType.Hands);
|
|
if (socket != null) {
|
|
var item = socket.AttachedItem;
|
|
if (item != null) {
|
|
item.gameObject.SetActive(true);
|
|
item.OnPrepareUse();
|
|
gunArmed = true;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void UnprepareItem() {
|
|
var socket = GetSocket(ItemSocketType.Hands);
|
|
if (socket != null) {
|
|
var item = socket.AttachedItem;
|
|
if (item != null) {
|
|
item.gameObject.SetActive(false);
|
|
item.OnEndUse();
|
|
gunArmed = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Flip() {
|
|
facingLeft = !facingLeft;
|
|
var s = transform.localScale;
|
|
s.x *= -1;
|
|
transform.localScale = s;
|
|
}
|
|
|
|
public void UpdateMovement(float targetSpeed) {
|
|
float hspeed = _rb.velocity.x;
|
|
|
|
float dV = targetSpeed - _rb.velocity.x;
|
|
float A = dV / Time.fixedDeltaTime;//Mathf.Clamp(dV / Time.fixedDeltaTime, 0, 50);
|
|
A = Mathf.Clamp(Mathf.Abs(A), 0, 50) * Mathf.Sign(A);
|
|
|
|
float F = _rb.mass * A;
|
|
|
|
float maxForce = 4500;
|
|
F = Mathf.Clamp(F, -maxForce, maxForce);
|
|
_rb.AddForce(Vector2.right * F);
|
|
}
|
|
|
|
void OnFootstep() {
|
|
if (player != null) {
|
|
player.Play();
|
|
}
|
|
}
|
|
|
|
protected virtual void Update() {
|
|
if (_rb.velocity.x > 0.02f && facingLeft) {
|
|
Flip();
|
|
}
|
|
if (_rb.velocity.x < -0.02f && !facingLeft) {
|
|
Flip();
|
|
}
|
|
|
|
|
|
anim.SetBool("lockAnyState", deadStateEnter);
|
|
anim.SetBool("dead", health.Dead);
|
|
anim.SetBool("gunArmed", gunArmed);
|
|
anim.SetBool("FreeFall", freeFall);
|
|
anim.SetBool("Grounded", grounded);
|
|
anim.SetBool("Crouch", crouch);
|
|
anim.SetFloat("hSpeed", _rb.velocity.x);
|
|
anim.SetFloat("vSpeed", _rb.velocity.y);
|
|
anim.SetFloat("hSpeedAbs", Mathf.Abs(_rb.velocity.x));
|
|
}
|
|
|
|
public ItemSocket GetSocket(ItemSocketType t) {
|
|
return sockets.First(v => v.socketType == t);
|
|
}
|
|
|
|
protected virtual void FixedUpdate() {
|
|
grounded = Physics2D.OverlapCircle(transform.position, groundCastRadius, groundMask) != null;
|
|
float freeFallDistance = 1.0f;
|
|
|
|
if (grounded) {
|
|
freeFall = false;
|
|
} else {
|
|
if (!freeFall) {
|
|
var c = Physics2D.CircleCast(transform.position, groundCastRadius, -Vector2.up, freeFallDistance, groundMask);
|
|
|
|
bool p = (((c.point - (Vector2)transform.position).magnitude - groundCastRadius) > freeFallDistance);
|
|
if (c.collider == null || p) {
|
|
if (Mathf.Abs(_rb.velocity.y) > 0.001f) {
|
|
freeFall = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|