682 lines
25 KiB
C#
682 lines
25 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using Random = UnityEngine.Random;
|
||
|
||
public class TargetController : MonoBehaviour
|
||
{
|
||
public GameObject environmentObj;
|
||
public GameObject agentObj;
|
||
public GameObject HUDObj;
|
||
public GameObject sceneBlockContainerObj;
|
||
public GameObject enemyContainerObj;
|
||
public GameObject parameterContainerObj;
|
||
public GameObject environmentUIObj;
|
||
public GameObject worldUIObj;
|
||
|
||
// area
|
||
public GameObject edgeUp;
|
||
|
||
public GameObject edgeDown;
|
||
public GameObject edgeLeft;
|
||
public GameObject edgeRight;
|
||
public GameObject edgeAgent_Enemy;
|
||
|
||
//group
|
||
public string group1Tag = "Player";
|
||
|
||
public string group2Tag = "Enemy";
|
||
|
||
public float minEnemyAreaX;
|
||
[System.NonSerialized] public float maxEnemyAreaX;
|
||
[System.NonSerialized] public float minEnemyAreaZ;
|
||
[System.NonSerialized] public float maxEnemyAreaZ;
|
||
[System.NonSerialized] public float minAgentAreaX;
|
||
[System.NonSerialized] public float maxAgentAreaX;
|
||
[System.NonSerialized] public float minAgentAreaZ;
|
||
[System.NonSerialized] public float maxAgentAreaZ;
|
||
[System.NonSerialized] public float startTime = 0f;
|
||
[System.NonSerialized] public float leftTime = 0f;
|
||
|
||
[SerializeField, Range(0f, 1f)] public float attackProb = 0.2f;
|
||
[SerializeField, Range(0f, 1f)] public float gotoProb = 0.2f;
|
||
[SerializeField, Range(0f, 1f)] public float defenceProb = 0.2f;
|
||
|
||
[System.NonSerialized] public int targetTypeInt;
|
||
[System.NonSerialized] public int gotoLevelNum;
|
||
[System.NonSerialized] public int attackLevelNum;
|
||
public float[] targetState = new float[6];
|
||
|
||
public enum EndType
|
||
{ Win, Lose, Running, Num };
|
||
|
||
[System.NonSerialized] public int targetNum = 0;
|
||
private Dictionary<int, float[]> oneHotRarget = new Dictionary<int, float[]>();
|
||
|
||
private int inArea = 0;
|
||
private float freeProb;
|
||
private float sceneBlockSize;
|
||
private float lastDistance;
|
||
private int randBlockType = 0;
|
||
private int randLevel = 0;
|
||
public Vector3 targetPosition;
|
||
private bool firstRewardFlag = true;
|
||
private bool targetEnemySpawnFinish = false;
|
||
private SceneBlockContainer sceneBlockCon;
|
||
private EnemyContainer enemyCon;
|
||
private EnvironmentUIControl envUICon;
|
||
private ParameterContainer paramCon;
|
||
private CharacterController agentCharaCon;
|
||
private WorldUIController worldUICon;
|
||
private HUDController hudCon;
|
||
private MessageBoxController messageBoxCon;
|
||
|
||
// start scene datas 0=train 1=play
|
||
private int gamemode;
|
||
|
||
// Start is called before the first frame update
|
||
private void Start()
|
||
{
|
||
sceneBlockCon = sceneBlockContainerObj.GetComponent<SceneBlockContainer>();
|
||
envUICon = environmentUIObj.GetComponent<EnvironmentUIControl>();
|
||
enemyCon = enemyContainerObj.GetComponent<EnemyContainer>();
|
||
agentCharaCon = agentObj.GetComponent<CharacterController>();
|
||
paramCon = parameterContainerObj.GetComponent<ParameterContainer>();
|
||
worldUICon = worldUIObj.GetComponent<WorldUIController>();
|
||
hudCon = HUDObj.GetComponent<HUDController>();
|
||
messageBoxCon = HUDObj.GetComponent<MessageBoxController>();
|
||
|
||
// get parameter from ParameterContainer
|
||
gamemode = paramCon.gameMode;
|
||
attackProb = paramCon.attackProb;
|
||
gotoProb = paramCon.gotoProb;
|
||
defenceProb = paramCon.defenceProb;
|
||
|
||
// initialize spawn area
|
||
minEnemyAreaX = edgeLeft.transform.localPosition.x + 1.0f;
|
||
maxEnemyAreaX = edgeRight.transform.localPosition.x - 1.0f;
|
||
minEnemyAreaZ = edgeAgent_Enemy.transform.localPosition.z + 1.0f;
|
||
maxEnemyAreaZ = edgeUp.transform.localPosition.z - 1.0f;
|
||
|
||
minAgentAreaX = edgeLeft.transform.localPosition.x + 1.0f;
|
||
maxAgentAreaX = edgeRight.transform.localPosition.x - 1.0f;
|
||
minAgentAreaZ = edgeDown.transform.localPosition.z + 1.0f;
|
||
maxAgentAreaZ = edgeAgent_Enemy.transform.localPosition.z - 1.0f;
|
||
|
||
freeProb = 1 - attackProb - gotoProb - defenceProb;
|
||
targetNum = (int)Targets.Num;
|
||
gotoLevelNum = paramCon.scenePrefabSet.GetLevelNumber(Targets.Go);
|
||
attackLevelNum = paramCon.scenePrefabSet.GetLevelNumber(Targets.Attack);
|
||
if (freeProb < 0)
|
||
{
|
||
Debug.LogError("TargetController.Start: target percentage wrong");
|
||
}
|
||
|
||
// initialize a simple fake onehot encoder.
|
||
for (int i = 0; i < targetNum; i++)
|
||
{
|
||
float[] onehotList = new float[targetNum];
|
||
for (int j = 0; j < targetNum; j++)
|
||
{
|
||
onehotList[j] = 0;
|
||
}
|
||
onehotList[i] = 1;
|
||
oneHotRarget.Add(i, onehotList);
|
||
}
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
// if gamemode is play, then time will keep paramCon.timeLimit
|
||
if (gamemode == 1)
|
||
{
|
||
leftTime = paramCon.timeLimit;
|
||
// print out time
|
||
// Debug.Log("Playing Time: " + leftTime);
|
||
}
|
||
else
|
||
{
|
||
leftTime = paramCon.timeLimit - Time.time + startTime;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Generates a new scene configuration by selecting a random target type and spawning related scene blocks.
|
||
/// </summary>
|
||
/// <remarks>
|
||
/// This method is responsible for creating a new scene configuration, which involves selecting a target type
|
||
/// (Go, Attack, Defence, or Free) based on predefined probabilities. Depending on the chosen target type,
|
||
/// the method spawns the associated scene blocks, updates various flags, and informs the user interface about
|
||
/// the selected target type.
|
||
/// </remarks>
|
||
public void RollNewScene()
|
||
{
|
||
startTime = Time.time;// Reset StartTime as now time
|
||
leftTime = paramCon.timeLimit - Time.time + startTime;
|
||
float randTargetType = UnityEngine.Random.Range(0f, 1f);
|
||
if (randTargetType <= gotoProb)
|
||
{
|
||
// goto target spawn
|
||
Debug.Log("GOTO THIS TARGET!");
|
||
targetTypeInt = (int)Targets.Go;
|
||
RandomSpawnSceneBlock(Targets.Go);
|
||
// set startDistance
|
||
firstRewardFlag = true;
|
||
}
|
||
else if (randTargetType > gotoProb && randTargetType <= gotoProb + attackProb)
|
||
{
|
||
// attack target spawn
|
||
Debug.Log("ATTACK Mode Start");
|
||
targetTypeInt = (int)Targets.Attack;
|
||
RandomSpawnSceneBlock(Targets.Attack);
|
||
// set startDistance
|
||
firstRewardFlag = true;
|
||
targetEnemySpawnFinish = false;
|
||
}
|
||
else if (randTargetType > gotoProb + attackProb && randTargetType <= gotoProb + attackProb + defenceProb)
|
||
{
|
||
// defence target spawn
|
||
Debug.Log("DEFENCE Mode Start");
|
||
targetTypeInt = (int)Targets.Defence;
|
||
RandomSpawnSceneBlock(Targets.Defence);
|
||
// set startDistance
|
||
firstRewardFlag = true;
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("Free Mode Start");
|
||
targetTypeInt = (int)Targets.Free;
|
||
enemyCon.DestroyAllEnemys();
|
||
enemyCon.RandomInitEnemys(hudCon.enemyNum);
|
||
MoveAgentToSpwanArea();
|
||
sceneBlockCon.DestroyBlock();
|
||
}
|
||
UpdateTargetStates();
|
||
envUICon.UpdateTargetType(targetTypeInt);
|
||
}
|
||
|
||
#region Agent Move Method
|
||
|
||
/// <summary>
|
||
/// Move the agent to the spawn area.
|
||
/// 将Agent移动到生成区域。
|
||
/// </summary>
|
||
private void MoveAgentToSpwanArea()
|
||
{
|
||
float randX = UnityEngine.Random.Range(minAgentAreaX, maxAgentAreaX); ;
|
||
float randZ = 0f;
|
||
if (paramCon.spawnAgentInAllMap)
|
||
{
|
||
// spawn agent in all around map
|
||
randZ = UnityEngine.Random.Range(minAgentAreaZ, maxEnemyAreaZ);
|
||
}
|
||
else
|
||
{
|
||
// spawn agent in only agent spawn area
|
||
randZ = UnityEngine.Random.Range(minAgentAreaZ, maxAgentAreaZ);
|
||
}
|
||
|
||
int Y = 1;
|
||
Vector3 initAgentLoc = new Vector3(randX, Y, randZ);
|
||
MoveAgentTo(initAgentLoc);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Move the agent to the specified position.
|
||
/// 将代理移动到指定位置。
|
||
/// </summary>
|
||
/// <param name="position">要移动到的位置。</param>
|
||
/// <remarks>
|
||
/// When moving the character using transform.localPosition,
|
||
/// must disable the character controller, or it won't work properly.
|
||
/// 使用 transform.localPosition 移动角色时,
|
||
/// 必须禁用角色控制器,否则它将无法正常工作。
|
||
/// </remarks>
|
||
public void MoveAgentTo(Vector3 position)
|
||
{
|
||
// while using transform.localPosition to move character
|
||
// u should turn off character Controller or it won't work
|
||
agentCharaCon.enabled = false;
|
||
agentObj.transform.localPosition = position;
|
||
agentCharaCon.enabled = true;
|
||
}
|
||
|
||
#endregion Agent Move Method
|
||
|
||
#region Random SceneBlock Spawn Method
|
||
|
||
/// <summary>
|
||
/// Randomly spawns a scene block based on the target type.
|
||
/// 根据目标类型随机生成场景块。
|
||
/// </summary>
|
||
/// <param name="targetType">要生成的场景块的目标类型。The target type of the scene block to be generated.</param>
|
||
/// <remarks>
|
||
/// This method generates a random scene block based on the target type and spawns enemies at the specified location.
|
||
/// 此方法根据目标类型生成一个随机场景块,并在指定位置生成敌人。
|
||
/// </remarks>
|
||
private void RandomSpawnSceneBlock(Targets targetType)
|
||
{
|
||
randLevel = RollRandomLevelIndex(targetType);
|
||
randBlockType = Random.Range(0, paramCon.scenePrefabSet.GetBlockNumber(randLevel,targetType));
|
||
sceneBlockSize = paramCon.scenePrefabSet.GetBlockSize(randLevel, randBlockType, targetType);
|
||
|
||
float randX = UnityEngine.Random.Range(minEnemyAreaX + sceneBlockSize / 2 + 1f, maxEnemyAreaX - sceneBlockSize / 2 - 1f);
|
||
float randZ = UnityEngine.Random.Range(minEnemyAreaZ + sceneBlockSize / 2 + 1f, maxEnemyAreaZ - sceneBlockSize / 2 - 1f);
|
||
targetPosition = new Vector3(randX, 0, randZ);
|
||
|
||
// init scene block
|
||
sceneBlockCon.DestroyBlock();
|
||
sceneBlockCon.CreateNewBlock(targetType, randLevel, randBlockType, targetPosition, group1Tag, group2Tag);
|
||
enemyCon.DestroyAllEnemys();
|
||
enemyCon.RandomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneBlockSize);
|
||
sceneBlockCon.nowBlock.InitBlock(environmentObj);
|
||
}
|
||
|
||
#endregion Random SceneBlock Spawn Method
|
||
|
||
#region Reward function
|
||
|
||
/// <summary>
|
||
/// Checks the game's end state and retrieves rewards.
|
||
/// </summary>
|
||
/// <returns>A tuple containing the game's end type, current reward, and final reward.
|
||
/// 1 = success,2 = overtime,0 = notover</returns>
|
||
public (int, float, float) CheckOverAndRewards()
|
||
{
|
||
int endTypeInt = 0;
|
||
float nowReward = 0;
|
||
float endReward = 0;
|
||
float nowDistance = 0f;
|
||
switch (targetTypeInt)
|
||
{
|
||
case (int)Targets.Go:
|
||
// goto
|
||
(nowDistance, inArea) = sceneBlockCon.GetAgentTargetDistanceAndInside(agentObj.transform.position);
|
||
envUICon.UpdateTargetGauge(sceneBlockCon.nowBlock.firebasesBelong, sceneBlockCon.nowBlock.belongMaxPoint);
|
||
float areaTargetReward = GetDistanceReward(nowDistance, inArea);
|
||
//if(inArea != 0)
|
||
if (sceneBlockCon.nowBlock.firebasesBelong >= sceneBlockCon.nowBlock.belongMaxPoint)
|
||
{
|
||
// win
|
||
// let the area belongs to me
|
||
nowReward = areaTargetReward;
|
||
endReward = paramCon.goWinReward;
|
||
//nowReward = (paramCon.inAreaReward * inArea) + getDistanceReward(nowDistance);
|
||
endTypeInt = (int)EndType.Win;
|
||
}
|
||
else if (leftTime <= 0)
|
||
{
|
||
// time out lose
|
||
nowReward = areaTargetReward;
|
||
endReward = paramCon.loseReward;
|
||
endTypeInt = (int)EndType.Lose;
|
||
}
|
||
else
|
||
{
|
||
// keep on keeping on!
|
||
nowReward = areaTargetReward;
|
||
endReward = 0;
|
||
endTypeInt = (int)EndType.Running;
|
||
}
|
||
break;
|
||
|
||
case (int)Targets.Attack:
|
||
// attack
|
||
(nowDistance, inArea) = sceneBlockCon.GetAgentTargetDistanceAndInside(agentObj.transform.position);
|
||
envUICon.UpdateTargetGauge(sceneBlockCon.nowBlock.firebasesBelong, sceneBlockCon.nowBlock.belongMaxPoint);
|
||
if (sceneBlockCon.nowBlock.GetInAreaNumber(group2Tag) <= 0 && targetEnemySpawnFinish)
|
||
{
|
||
// win
|
||
// let the area belongs to me and kill every enmy in this area.
|
||
nowReward = 0;
|
||
endReward = paramCon.attackWinReward;
|
||
//nowReward = (paramCon.inAreaReward * inArea) + getSceneReward(nowDistance);
|
||
endTypeInt = (int)EndType.Win;
|
||
targetEnemySpawnFinish = false;
|
||
}
|
||
else if (leftTime <= 0 && targetEnemySpawnFinish)
|
||
{
|
||
// time out lose
|
||
nowReward = 0;
|
||
endReward = paramCon.loseReward;
|
||
//nowReward = (paramCon.inAreaReward * inArea) + getSceneReward(nowDistance);
|
||
endTypeInt = (int)EndType.Lose;
|
||
targetEnemySpawnFinish = false;
|
||
}
|
||
else
|
||
{
|
||
// keep on keeping on!
|
||
// nowReward = (paramCon.inAreaReward * inArea) + getDistanceReward(nowDistance);
|
||
nowReward = 0;
|
||
endReward = 0;
|
||
targetEnemySpawnFinish = true;
|
||
endTypeInt = (int)EndType.Running;
|
||
}
|
||
break;
|
||
|
||
case (int)Targets.Defence:
|
||
//defence
|
||
// !!! DIDN't FINISH!!!
|
||
(nowDistance, inArea) = sceneBlockCon.GetAgentTargetDistanceAndInside(agentObj.transform.position);
|
||
envUICon.UpdateTargetGauge(sceneBlockCon.nowBlock.firebasesBelong, sceneBlockCon.nowBlock.belongMaxPoint);
|
||
if (leftTime <= 0 && sceneBlockCon.nowBlock.firebasesBelong >= 0f)
|
||
{
|
||
// win
|
||
// time over and the area still mine
|
||
nowReward = paramCon.defenceWinReward;
|
||
//nowReward = (paramCon.inAreaReward * inArea) + getSceneReward(nowDistance);
|
||
endTypeInt = (int)EndType.Win;
|
||
}
|
||
else if (sceneBlockCon.nowBlock.firebasesBelong <= sceneBlockCon.nowBlock.belongMaxPoint)
|
||
{
|
||
// lost area lose
|
||
nowReward = paramCon.loseReward;
|
||
//nowReward = (paramCon.inAreaReward * inArea) + getSceneReward(nowDistance);
|
||
endTypeInt = (int)EndType.Lose;
|
||
}
|
||
else
|
||
{
|
||
// keep on keeping on!
|
||
// nowReward = (paramCon.inAreaReward * inArea) + getDistanceReward(nowDistance);
|
||
endTypeInt = (int)EndType.Running;
|
||
}
|
||
break;
|
||
|
||
case (int)Targets.Stay:
|
||
// Stay
|
||
// endless
|
||
nowReward = 0;
|
||
endReward = 0;
|
||
endTypeInt = (int)EndType.Running;
|
||
break;
|
||
|
||
default:
|
||
//free kill
|
||
if (enemyContainerObj.transform.childCount <= 0)
|
||
{
|
||
// win
|
||
// nowReward = paramCon.winReward + (paramCon.timeBonusPerSecReward * leftTime);
|
||
nowReward = 0;
|
||
endReward = paramCon.freeWinReward;
|
||
endTypeInt = (int)EndType.Win;
|
||
}
|
||
else if (leftTime <= 0)
|
||
{
|
||
// lose
|
||
//nowReward = paramCon.loseReward;
|
||
nowReward = 0;
|
||
endReward = paramCon.loseReward;
|
||
endTypeInt = (int)EndType.Lose;
|
||
}
|
||
else
|
||
{
|
||
// keep on keeping on!
|
||
nowReward = 0;
|
||
endReward = 0;
|
||
endTypeInt = (int)EndType.Running;
|
||
}
|
||
break;
|
||
}
|
||
envUICon.ShowResult(endTypeInt);
|
||
worldUICon.UpdateChart(targetTypeInt, endTypeInt);
|
||
return (endTypeInt, nowReward, endReward);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Calculates scene reward based on distance, granting higher rewards for being closer to the target.
|
||
/// 根据距离计算场景奖励,靠近目标则获得更高奖励。
|
||
/// </summary>
|
||
/// <param name="nowDistance">The current distance.</param>
|
||
/// <param name="inarea">Whether inside an area.</param>
|
||
/// <returns>The reward value calculated based on distance.</returns>
|
||
private float GetDistanceReward(float nowDistance, int inarea)
|
||
{
|
||
if (firstRewardFlag)
|
||
{
|
||
// first distance record
|
||
(lastDistance, _) = sceneBlockCon.GetAgentTargetDistanceAndInside(agentObj.transform.position);
|
||
firstRewardFlag = false;
|
||
}
|
||
float nowSeneReward = 0f;
|
||
if (inarea != 0)
|
||
{
|
||
// in area
|
||
nowSeneReward = paramCon.inAreaReward;
|
||
}
|
||
else
|
||
{
|
||
// out of area
|
||
// nowSeneReward = paramCon.distanceReward * Math.Clamp(lastDistance - nowDistance, 0, 100);
|
||
nowSeneReward = paramCon.distanceReward * (lastDistance - nowDistance);
|
||
}
|
||
lastDistance = nowDistance;
|
||
return nowSeneReward;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Calculates kill reward based on the position of the killed enemy.
|
||
/// 根据击杀的敌人位置计算击杀奖励。
|
||
/// </summary>
|
||
/// <param name="enemyPosition">The position of the killed enemy.</param>
|
||
/// <returns>The reward value calculated based on the kill position.</returns>
|
||
public float KillReward(Vector3 enemyPosition)
|
||
{
|
||
float nowKillReward = 0f;
|
||
if (targetTypeInt == (int)Targets.Attack)
|
||
{
|
||
// attack mode
|
||
(_, int isInArea) = sceneBlockCon.nowBlock.GetDistInArea(enemyPosition);
|
||
if (isInArea == 1)
|
||
{
|
||
// kill in area enemy
|
||
nowKillReward = paramCon.killTargetEnemyReward;
|
||
}
|
||
else
|
||
{
|
||
nowKillReward = paramCon.killNonTargetReward;
|
||
}
|
||
}
|
||
else if (targetTypeInt == (int)Targets.Free)
|
||
{
|
||
// free mode hit
|
||
nowKillReward = paramCon.killTargetEnemyReward;
|
||
}
|
||
else
|
||
{
|
||
// goto & defence
|
||
nowKillReward = paramCon.killNonTargetReward;
|
||
}
|
||
return nowKillReward;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Calculates hit reward based on the position of the hit enemy and the current mode.
|
||
/// 根据击中的敌人位置和当前模式计算击中Reward。
|
||
/// </summary>
|
||
/// <param name="enemyPosition">The position of the hit enemy.</param>
|
||
/// <returns>The reward value calculated based on the hit position and mode.</returns>
|
||
public float HitEnemyReward(Vector3 enemyPosition)
|
||
{
|
||
float nowHitReward = 0f;
|
||
if (targetTypeInt == (int)Targets.Attack)
|
||
{
|
||
// attack mode
|
||
(_, int isInArea) = sceneBlockCon.nowBlock.GetDistInArea(enemyPosition);
|
||
if (isInArea == 1)
|
||
{
|
||
// hit in area enemy
|
||
nowHitReward = paramCon.hitTargetReward;
|
||
}
|
||
else
|
||
{
|
||
// hit not in area enemy
|
||
nowHitReward = paramCon.hitNonTargetReward;
|
||
}
|
||
}
|
||
else if (targetTypeInt == (int)Targets.Free)
|
||
{
|
||
// free mode hit
|
||
nowHitReward = paramCon.hitTargetReward;
|
||
}
|
||
else
|
||
{
|
||
// goto & defence
|
||
nowHitReward = paramCon.hitNonTargetReward;
|
||
}
|
||
return nowHitReward;
|
||
}
|
||
|
||
#endregion Reward function
|
||
|
||
#region Play Mode Method
|
||
|
||
/// <summary>
|
||
/// Initializes the game in play mode.
|
||
/// 初始化游戏playMode。
|
||
/// </summary>
|
||
/// <remarks>
|
||
/// This method is used to initialize the game in play mode,
|
||
/// including setting the target type, updating target states,
|
||
/// updating UI display, moving the agent to the spawn area,
|
||
/// destroying all enemies, and scene blocks.
|
||
/// 该方法用于初始化游戏播放模式,包括设置目标类型、更新目标状态、更新UI显示、
|
||
/// 将代理移动到生成区域、销毁所有敌人和场景块。
|
||
/// </remarks>
|
||
public void PlayInitialize()
|
||
{
|
||
targetTypeInt = (int)Targets.Stay;
|
||
UpdateTargetStates();
|
||
envUICon.UpdateTargetType(targetTypeInt);
|
||
MoveAgentToSpwanArea();
|
||
enemyCon.DestroyAllEnemys();
|
||
sceneBlockCon.DestroyBlock();
|
||
}
|
||
|
||
// change to attack mode
|
||
public void AttackModeChange(Vector3 targetPosition)
|
||
{
|
||
targetTypeInt = (int)Targets.Attack;
|
||
UpdateTargetStates(targetPosition);
|
||
envUICon.UpdateTargetType(targetTypeInt);
|
||
}
|
||
|
||
// change to free mode
|
||
public void FreeModeChange()
|
||
{
|
||
targetTypeInt = (int)Targets.Free;
|
||
UpdateTargetStates();
|
||
envUICon.UpdateTargetType(targetTypeInt);
|
||
}
|
||
|
||
// change to goto mode
|
||
public void GotoModeChange(Vector3 targetPosition)
|
||
{
|
||
targetTypeInt = (int)Targets.Go;
|
||
UpdateTargetStates(targetPosition);
|
||
envUICon.UpdateTargetType(targetTypeInt);
|
||
}
|
||
|
||
// change to stay mode
|
||
public void StayModeChange()
|
||
{
|
||
targetTypeInt = (int)Targets.Stay;
|
||
UpdateTargetStates();
|
||
envUICon.UpdateTargetType(targetTypeInt);
|
||
}
|
||
|
||
#endregion Play Mode Method
|
||
|
||
/// <summary>
|
||
/// Gets the target observation states.
|
||
/// 获取目标观测状态。
|
||
/// </summary>
|
||
/// <param name="targetPosition">The target position (optional).</param>
|
||
private void UpdateTargetStates(Vector3? targetPosition = null)
|
||
{
|
||
// targettype, x,y,z, firebasesAreaDiameter
|
||
targetState[0] = targetTypeInt;
|
||
if (targetPosition != null)
|
||
{
|
||
this.targetPosition = (Vector3)targetPosition;
|
||
}
|
||
if (targetTypeInt == (int)Targets.Free || targetTypeInt == (int)Targets.Stay)
|
||
{
|
||
for (int i = 1; i < targetState.Length; i++)
|
||
// set target position state to 0
|
||
targetState[i] = 0f;
|
||
}
|
||
else
|
||
{
|
||
targetState[1] = this.targetPosition.x;
|
||
targetState[2] = this.targetPosition.y;
|
||
targetState[3] = this.targetPosition.z;
|
||
targetState[4] = sceneBlockCon.nowBlock.firebasesAreaDiameter;
|
||
targetState[5] = sceneBlockCon.nowBlock.belongRatio;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Gets the in-area state.
|
||
/// 获取是否在区域内的State
|
||
/// </summary>
|
||
/// <returns>The in-area state.</returns>
|
||
public int GetInAreaState()
|
||
{
|
||
if (targetTypeInt == (int)Targets.Go)
|
||
{
|
||
return inArea;
|
||
}
|
||
else
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Gets a random level index based on the target type.
|
||
/// 根据目标类型获取随机关卡索引。
|
||
/// </summary>
|
||
/// <param name="target">The target type.</param>
|
||
/// <returns>A random level index.</returns>
|
||
public int RollRandomLevelIndex(Targets target)
|
||
{
|
||
List<float> targetProbs;
|
||
|
||
switch (target)
|
||
{
|
||
case Targets.Attack:
|
||
targetProbs = paramCon.attackLevelProbs;
|
||
break;
|
||
case Targets.Go:
|
||
targetProbs = paramCon.gotoLevelProbs;
|
||
break;
|
||
case Targets.Defence:
|
||
targetProbs = paramCon.defenceLevelProbs;
|
||
break;
|
||
default:
|
||
messageBoxCon.PushMessage(
|
||
new List<string> { "[ERROR]TargetController:RandomLevel", "target type error" },
|
||
new List<string> { "#800000ff" });
|
||
Debug.LogWarning("[ERROR]TargetController:RandomLevel:target type error");
|
||
return -1; // Exit early on default case
|
||
}
|
||
|
||
// sample random level depends on the target probabilities
|
||
float randomNum = UnityEngine.Random.Range(0f, 1f);
|
||
float sumProb = 0f;
|
||
for (int i = 0; i < targetProbs.Count; i++)
|
||
{
|
||
sumProb += targetProbs[i];
|
||
if (randomNum < sumProb)
|
||
{
|
||
return i;
|
||
}
|
||
}
|
||
|
||
// If no level was returned, log an error and return -1
|
||
messageBoxCon.PushMessage(
|
||
new List<string> { "[ERROR]TargetController:RandomLevel", "level index out of range" },
|
||
new List<string> { "orange" });
|
||
return -1;
|
||
}
|
||
} |