Skip to content

Commit

Permalink
【MA基礎開発】物資ボックスが落下し続ける問題と、buildingを指定範囲内で初期化するように修正。 #1 #13
Browse files Browse the repository at this point in the history
  • Loading branch information
tsyu1234 committed Feb 26, 2024
1 parent 0436424 commit f2e2905
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions MAEasySimulator/Assets/DroneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void AddCommunicateTarget(string target) {

public void InHeuristicCtrl(in ActionBuffers actionsOut) {
var action = actionsOut.ContinuousActions;
//WASD定義
//WASD定義 TODO:Enumにでもまとめること
if(Input.GetKey(KeyCode.W)) {
//前進
action[1] = 1f;
Expand Down Expand Up @@ -175,7 +175,7 @@ private IEnumerator BatteryDrainCoroutine() {
//Debug.Log($"Battery Level: {batteryLevel}%");
}
onEmptyBattery?.Invoke();
FreeFall();
FreeFall(); //TODO:イベントハンドラーに記載する
}

private void Charge() {
Expand Down
19 changes: 12 additions & 7 deletions MAEasySimulator/Assets/EnvManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class EnvManager : MonoBehaviour {
[Header("GameObjects")]
public GameObject FieldPlane;
public GameObject Field;
public GameObject SubFieldPlane;

public List<GameObject> buildings;
public GameObject DroneStation;
Expand All @@ -34,28 +35,32 @@ public void InitializeRandomPositions(float someMinimumDistance = 10f) {
Vector3 fieldPlaneSize = FieldPlane.GetComponent<Collider>().bounds.size;
Vector3 fieldPlaneCenter = FieldPlane.transform.position;
int maxAttempts = 100; // 最大試行回数を設定
int attempts = 0;
int attempts;

//buildingsの位置をランダムに設定(DroneStationとTODO:他のbuildingと重ならないように)
// buildingsの位置をランダムに設定, SubFieldPlaneの範囲内でランダムに配置
foreach (GameObject building in buildings) {
Vector3 newBuildingPos;
attempts = 0; // 試行回数のリセット
do {
newBuildingPos = GenerateRandomPosition(fieldPlaneCenter, fieldPlaneSize);
newBuildingPos = GenerateRandomPosition(SubFieldPlane.transform.position, SubFieldPlane.GetComponent<Collider>().bounds.size);
attempts++;
} while (Vector3.Distance(newBuildingPos, DroneStation.transform.position) < someMinimumDistance && attempts < maxAttempts);

if (attempts >= maxAttempts) {
Debug.LogWarning("Failed to place building sufficiently apart from DroneStation");
return; // 適切な位置を見つけられなかった場合は処理を中断 無限ループを防ぐため
}
building.transform.position = newBuildingPos;
}
building.transform.position = newBuildingPos; // ローカル座標を使用して位置を設定
}
}

private Vector3 GenerateRandomPosition(Vector3 center, Vector3 size) {
float x = Random.Range(center.x - size.x / 2, center.x + size.x / 2);
float z = Random.Range(center.z - size.z / 2, center.z + size.z / 2);
return new Vector3(x, 0, z); // y座標は0としておき、後で変更する

// 生成されたXとZ座標を使用して新しい位置Vector3を返す
// y座標はSubFieldPlaneのy座標に合わせるか、必要に応じて調整
return new Vector3(x, center.y, z);
}

}
4 changes: 2 additions & 2 deletions MAEasySimulator/Assets/Scenes/test0.4.unity
Git LFS file not shown
1 change: 1 addition & 0 deletions MAEasySimulator/Assets/SpyAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void Start() {
_controller.AddCommunicateTarget(targetTag);
_controller.onCrash += OnCrash;
_controller.onEmptyBattery += OnEmpty;
_controller.onChargingBattery += OnChargingBattery;
Sensor = transform.Find("Sensor");
StartPosition = transform.localPosition;
}
Expand Down
6 changes: 6 additions & 0 deletions MAEasySimulator/Assets/SurpplieBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ void Start() {
StartPos = transform.localPosition;
}

void Update() {
if(transform.position.y < 0) {
Reset();
}
}

private void OnTriggerEnter(Collider other) {
///Debug.LogWarning("SurpplieBox: OnTriggerEnter" + other.tag);
if (other.CompareTag("Shelter")) {
Expand Down

0 comments on commit f2e2905

Please sign in to comment.