Skip to content

Commit

Permalink
游戏结束逻辑完善
Browse files Browse the repository at this point in the history
  • Loading branch information
moshuying committed Jul 7, 2024
1 parent 0b4032f commit e4b7099
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Assets/Scripts/CarriedCrateController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class CarriedCrateController : MonoBehaviour
Expand All @@ -17,4 +18,11 @@ void Update()
{

}
void OnJointBreak(float breakForce)
{
// ÐèÒªÖªµÀËùÔÚµÄplayer
Debug.Log("Crate is broken");
var root = this.GetComponent<ConfigurableJoint>().connectedBody.transform.parent.root.GetComponent<PlayerController>();
root.OnPlayerJointBreak(breakForce);
}
}
3 changes: 1 addition & 2 deletions Assets/Scripts/GameMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ public void StopGame()
Debug.Log("EndGame");

// 控制 UI Panel
uiController.EnterStartPanel();
uiController.EnterEndPanel();

player.gameObject.SetActive(false);
//player
GameObject.Destroy(player.gameObject);
player2.gameObject.SetActive(false);
GameObject.Destroy(player2.gameObject);
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/GrabSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ private void scare(PlayerController from, PlayerController to)
{
var dir = to.transform.position - from.transform.position;
dir = dir.normalized;
to.rigidBody.AddForce(dir * to.horizontalSpeed * 200, ForceMode.Force);
to.rigidBody.AddForce(dir * to.rigidBody.mass * 200, ForceMode.Force);
Debug.Log("玩家"+from.name+"让"+to.name+"远离自己");
}
// 让对手靠近自己
private void attract(PlayerController from, PlayerController to)
{
var dir = from.transform.position - to.transform.position;
dir = dir.normalized;
to.rigidBody.AddForce(dir * to.horizontalSpeed * 200, ForceMode.Force);
to.rigidBody.AddForce(dir * to.rigidBody.mass * 200, ForceMode.Force);
Debug.Log("玩家" + from.name + "让" + to.name + "靠近自己");
}
// 给自己回一滴血
Expand Down
20 changes: 18 additions & 2 deletions Assets/Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void Start()
}
}

void endGame()
{
GameObject.Find("GameMode").GetComponent<GameMode>().StopGame();
}

void Update()
{
//GROUNDED
Expand All @@ -62,14 +67,19 @@ void Update()
grounded = false;
animator.SetBool("gr", false);
}

if (Input.GetKey(moveLeftKey) && grounded)
{
rigidBody.AddForce(transform.right * -horizontalSpeed * rigidBody.mass, ForceMode.Force);
rigidBody.AddForce(transform.right * -horizontalSpeed * rigidBody.mass , ForceMode.Force);
//单靠力量移动会很缓慢,手感不好,所以在此基础上加一个位置移动
transform.position += transform.right * -horizontalSpeed * Time.deltaTime;
}
else if (Input.GetKey(moveRightKey) && grounded)
{
rigidBody.AddForce(transform.right * horizontalSpeed * rigidBody.mass, ForceMode.Force);
transform.position += transform.right * horizontalSpeed * Time.deltaTime;
}

animator.SetFloat("turn", -rigidBody.velocity.z);
if (Input.GetKey(jump) && grounded)
{
Expand Down Expand Up @@ -120,7 +130,7 @@ public void RemoveCrate()
// 如果没有箱子了,游戏结束
if (allCarriedCrates.Count == 0)
{
GameObject.Find("UI").GetComponent<UIController>().EnterGamePanel();
endGame();
}
}

Expand All @@ -143,6 +153,12 @@ public void AddCrate()
rigidBody.mass += newCrate.GetComponent<CarriedCrateController>().mass;
}

public void OnPlayerJointBreak(float breakForce)
{
// 箱子断开,游戏结束
endGame();
}

private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Crate")
Expand Down

0 comments on commit e4b7099

Please sign in to comment.