|
|
@@ -17,25 +17,24 @@ public abstract class EntityBase : MonoBehaviour {
|
|
|
|
|
|
public bool canCrouch = false;
|
|
|
public float movementSpeed = 10f, jumpForce = 20f, horizontalF = 0.9f, horizontalA = 0.3f, jumpThreshold = 0.1f, downA = 0.3f, fallA = 0.1f, filpSpeed = 0.3f;
|
|
|
- public float jumpBufferTime = 0.2f, jumpWolfTime = 0.2f, baseMass = 1f;
|
|
|
- internal float jumpBuffer = 0f, jumpWolf = 0f, jumping = 0f;
|
|
|
+ public float jumpBufferTime = 0.2f, jumpWolfTime = 0.2f; internal float jumpBuffer = 0f, jumpWolf = 0f, jumping = 0f;
|
|
|
internal bool towardsRight = true, isLand = false;
|
|
|
void Update() {
|
|
|
OnUpdate();
|
|
|
- // 获取当前情况
|
|
|
+ // 鑾峰彇褰撳墠鎯呭喌
|
|
|
float speedX = rb.velocity.x, speedY = rb.velocity.y;
|
|
|
bool isCrouch = isPressingCrouch();
|
|
|
- isLand = Physics2D.OverlapCircle(transform.position + Vector3.left * 0.1f, jumpThreshold, LayerMask.GetMask("Tilemap")) ||
|
|
|
+ isLand = Physics2D.OverlapCircle(transform.position + Vector3.left * 0.1f, jumpThreshold, LayerMask.GetMask("Tilemap")) ||
|
|
|
Physics2D.OverlapCircle(transform.position + Vector3.left * -0.1f, jumpThreshold, LayerMask.GetMask("Tilemap"));
|
|
|
- float moveHorizontal = (isCrouch && isLand ? 0.35f : 1) * // 下蹲减速
|
|
|
- (isPressingLeft() == isPressingRight() ? 0 : // 没有同时按住左右
|
|
|
- (isPressingLeft() ? -1 : 1)); // 获取方向
|
|
|
- // 判断跳跃
|
|
|
+ float moveHorizontal = (isCrouch && isLand ? 0.35f : 1) * // 涓嬭共鍑忛€�
|
|
|
+ (isPressingLeft() == isPressingRight() ? 0 : // 娌℃湁鍚屾椂鎸変綇宸﹀彸
|
|
|
+ (isPressingLeft() ? -1 : 1)); // 鑾峰彇鏂瑰悜
|
|
|
+ // 鍒ゆ柇璺宠穬
|
|
|
jumpBuffer -= Time.deltaTime; jumpWolf -= Time.deltaTime; jumping -= Time.deltaTime;
|
|
|
if (isPressDownJump()) jumpBuffer = jumpBufferTime;
|
|
|
if (isLand) jumpWolf = jumpWolfTime;
|
|
|
if (jumpBuffer > 0 && jumpWolf > 0 && jumping < 0) {
|
|
|
- // 播放音效
|
|
|
+ // 鎾�斁闊虫晥
|
|
|
AudioM.Play("jump");
|
|
|
|
|
|
speedY = jumpForce;
|
|
|
@@ -44,14 +43,14 @@ public abstract class EntityBase : MonoBehaviour {
|
|
|
} else if (isCrouch) {
|
|
|
speedY -= downA;
|
|
|
}
|
|
|
- // 加速下落
|
|
|
+ // 鍔犻€熶笅钀�
|
|
|
if (speedY < 0) speedY -= fallA;
|
|
|
- // 横向移动
|
|
|
+ // 妯�悜绉诲姩
|
|
|
if (moveHorizontal != 0) {
|
|
|
speedX = speedX * (1 - horizontalA) + movementSpeed * horizontalA * moveHorizontal;
|
|
|
}
|
|
|
speedX *= horizontalF;
|
|
|
- // 蹲下
|
|
|
+ // 韫蹭笅
|
|
|
if (canCrouch) {
|
|
|
if (isCrouch) {
|
|
|
sprite.sprite = spriteDown;
|
|
|
@@ -63,11 +62,10 @@ public abstract class EntityBase : MonoBehaviour {
|
|
|
coll.offset = new Vector2(0, 0.7f);
|
|
|
}
|
|
|
}
|
|
|
- // 设置
|
|
|
+ // 璁剧疆
|
|
|
rb.velocity = new Vector2(speedX, speedY);
|
|
|
- rb.mass = baseMass * (towardsRight ? 1.2f : 0.8f);
|
|
|
if (towardsRight != (Mathf.Abs(speedX) > filpSpeed ? speedX > 0 : towardsRight)) {
|
|
|
- // 反向
|
|
|
+ // 鍙嶅悜
|
|
|
towardsRight = !towardsRight;
|
|
|
transform.DOScaleX(towardsRight ? 1 : -1, 0.1f);
|
|
|
}
|
|
|
@@ -76,9 +74,9 @@ public abstract class EntityBase : MonoBehaviour {
|
|
|
internal abstract bool isPressingRight();
|
|
|
internal abstract bool isPressDownJump();
|
|
|
internal abstract bool isPressingCrouch();
|
|
|
- internal virtual void OnUpdate() {}
|
|
|
+ internal virtual void OnUpdate() { }
|
|
|
|
|
|
- // 碰撞检测
|
|
|
+ // 纰版挒妫€娴�
|
|
|
private void OnCollisionEnter2D(Collision2D collision) {
|
|
|
if (collision.gameObject.CompareTag("Border")) OnHitBorder();
|
|
|
if (collision.gameObject.CompareTag("Bullet")) OnHitBullet();
|
|
|
@@ -86,7 +84,7 @@ public abstract class EntityBase : MonoBehaviour {
|
|
|
if (collision.gameObject.CompareTag("Player") && gameObject.CompareTag("Enemy")) OnHitEnemy(collision);
|
|
|
}
|
|
|
internal abstract void OnHitBorder();
|
|
|
- internal virtual void OnHitBullet() {}
|
|
|
+ internal virtual void OnHitBullet() { }
|
|
|
internal abstract void OnHitEnemy(Collision2D collision);
|
|
|
|
|
|
-}
|
|
|
+}
|