V3.0 追加控制模式,改进代码

追加控制模式,实现鼠标在地图中的坐标映射
追加控制UI
Todo:
游戏流程修改
游戏State对应修改
This commit is contained in:
2023-04-09 23:35:38 +09:00
parent 71f2687422
commit ff094aaba5
56 changed files with 23542 additions and 682 deletions
+44 -23
View File
@@ -4,15 +4,10 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using System.Linq;
using System.Drawing;
using Color = UnityEngine.Color;
using static TargetController;
/*TODO:
√tag 攻击排他
@@ -63,6 +58,7 @@ public class AgentWithGun : Agent
private bool gunReadyToggle = true;
private string myTag = "";
private float lastEnemyFacingDistance = 0f; // record last enemy facing minimum distance
private float lastTargetFacingDistance = 0f; // record last target facing minimum distance
// scripts
private RaySensors raySensors;
private CharacterController PlayerController;
@@ -72,6 +68,7 @@ public class AgentWithGun : Agent
private EnemyContainer eneContainer;
private TargetController targetCon;
private HUDController hudController;
private StartSeneData startSceneData;
// observation
float[] myObserve = new float[4];
@@ -84,8 +81,25 @@ public class AgentWithGun : Agent
[System.NonSerialized] public int finishedState;
// start scene datas
private int gamemode;
private void Start()
{
// initialize startSceneData & datas
// while GameObject StartSceneDataTransfer is exist
try {
startSceneData = GameObject.Find("StartSceneDataTransfer").GetComponent<StartSeneData>();
gamemode = startSceneData.gamemode;
}
// while GameObject StartSceneDataTransfer is not exist
catch
{
Debug.LogError("Run WithOut StartScreen");
gamemode = 0;
}
// initialize scripts
paramContainer = ParameterContainerObj.GetComponent<ParameterContainer>();
eneContainer = EnemyContainerObj.GetComponent<EnemyContainer>();
blockContainer = SceneBlockContainerObj.GetComponent<SceneBlockContainer>();
@@ -94,25 +108,20 @@ public class AgentWithGun : Agent
hudController = HUDObj.GetComponent<HUDController>();
raySensors = GetComponent<RaySensors>();
PlayerController = this.transform.GetComponent<CharacterController>();
// Environment parameters
// initialize Environment parameters
lockMouse = paramContainer.lockMouse;
Damage = paramContainer.Damage;
fireRate = paramContainer.fireRate;
enemyNum = hudController.enemyNum;
lockCameraX = paramContainer.lockCameraX;
lockCameraY = paramContainer.lockCameraY;
//initialize remainTime
// initialize remainTime
// this agent's tag
myTag = gameObject.tag;
}
/* ----------此Update用于debugBuild前删除或注释掉!----------*/
/*void Update()
{
//Debug.Log(RaySensors.rayTagResult[0]);
}*/
/* ----------此Update用于debugBuild前删除或注释掉!----------*/
// ------------动作处理--------------
// moveAgent 用于模拟Input.GetAxis移动
public void moveAgent(int vertical, int horizontal)
@@ -273,7 +282,7 @@ public class AgentWithGun : Agent
return targetCon.hitEnemyReward(gotHitObj.transform.position);
}
}
if (targetCon.targetTypeInt == (int)TargetController.Targets.Attack)
if (targetCon.targetTypeInt == (int)SceneBlockContainer.Targets.Attack)
{
// while if attack mode
float targetDis = Vector3.Distance(blockContainer.thisBlock.transform.position, transform.position);
@@ -311,7 +320,7 @@ public class AgentWithGun : Agent
bool isFacingtoEnemy = false;
float enemyFacingDistance = 0f;
Ray ray = thisCam.ScreenPointToRay(new Vector3(thisCam.pixelWidth / 2, thisCam.pixelHeight / 2, 0));
if (targetCon.targetTypeInt == (int)TargetController.Targets.Free)
if (targetCon.targetTypeInt == (int)SceneBlockContainer.Targets.Free)
{
//free mode
RaycastHit hit;
@@ -356,20 +365,31 @@ public class AgentWithGun : Agent
// Debug.Log("ninimum = " + thisReward);
}
}
else if(targetCon.targetTypeInt == (int)TargetController.Targets.Attack)
else if(targetCon.targetTypeInt == (int)SceneBlockContainer.Targets.Attack)
{
// attack mode
// Target to Agent distance
float targetDis = Vector3.Distance(blockContainer.thisBlock.transform.position, transform.position);
// center of screen between target's distance
float camCenterToTarget = Vector3.Distance(ray.origin + (ray.direction * targetDis), blockContainer.thisBlock.transform.position);
if(targetDis <= raySensors.viewDistance)
{
// Debug.DrawRay(new Vector3(0,0,0), viewPoint, Color.red);
if (Vector3.Distance(ray.origin + (ray.direction * targetDis), blockContainer.thisBlock.transform.position) <= blockContainer.thisBlock.firebasesAreaDiameter / 2)
// while center of screen between target's distance is lower than firebasesAreaDiameter
// while facing to target
if (camCenterToTarget <= blockContainer.thisBlock.firebasesAreaDiameter / 2)
{
// im watching target
// Debug.DrawRay(ray.origin, viewPoint-ray.origin, Color.blue);
thisReward = paramContainer.facingReward;
}else
{
// while not facing to target
thisReward = (lastTargetFacingDistance - camCenterToTarget) * paramContainer.facingTargetReward;
}
}
// update lastTargetFacingDistance
lastTargetFacingDistance = camCenterToTarget;
}
return thisReward;
}
@@ -426,6 +446,7 @@ public class AgentWithGun : Agent
// env开始执行初始化
public override void OnEpisodeBegin()
{
Debug.LogWarning("GameState|START TEST!");
step = 0;
if (lockMouse)
{
@@ -468,7 +489,7 @@ public class AgentWithGun : Agent
sensor.AddObservation(targetStates);// (6) targettype, target x,y,z, firebasesAreaDiameter
sensor.AddObservation(inAreaState); // (1)
sensor.AddObservation(remainTime); // (1)
sensor.AddObservation(gunReadyToggle); //(1) save gun is ready?
sensor.AddObservation(gunReadyToggle); // (1) save gun is ready?
sensor.AddObservation(myObserve); // (4)自机位置xyz+朝向 float[](4,1)
if (oneHotRayTag)
{
@@ -527,14 +548,14 @@ public class AgentWithGun : Agent
// Win or lose Finished
Debug.Log("Finish reward = " + thisRoundReward);
EP += 1;
string targetString = Enum.GetName(typeof(TargetController.Targets), targetCon.targetTypeInt);
string targetString = Enum.GetName(typeof(SceneBlockContainer.Targets), targetCon.targetTypeInt);
switch (finishedState)
{
case (int)TargetController.EndType.Win:
Debug.LogWarning(targetString+"|Win");
Debug.LogWarning("Result|"+targetString+"|Win");
break;
case (int)TargetController.EndType.Lose:
Debug.LogWarning(targetString+"|Lose");
Debug.LogWarning("Result|"+targetString+"|Lose");
break;
default:
Debug.LogWarning("TypeError");
+1 -1
View File
@@ -21,7 +21,7 @@ public class AimbotSideChannel : SideChannel
{
if (type == LogType.Warning)
{
var stringToSend = "result|"+logString;
var stringToSend = "Warning|" + logString;
using (var msgOut = new OutgoingMessage())
{
msgOut.WriteString(stringToSend);
+4 -4
View File
@@ -134,19 +134,19 @@ public class EnvironmentUIControl : MonoBehaviour
{
switch (targetInt)
{
case (int)TargetController.Targets.Go:
case (int)SceneBlockContainer.Targets.Go:
targetTypeText.text = "GOTO";
targetTypeText.color = Color.blue;
break;
case (int)TargetController.Targets.Attack:
case (int)SceneBlockContainer.Targets.Attack:
targetTypeText.text = "Attack!";
targetTypeText.color = Color.red;
break;
case (int)TargetController.Targets.Defence:
case (int)SceneBlockContainer.Targets.Defence:
targetTypeText.text = "Defence";
targetTypeText.color = Color.green;
break;
case (int)TargetController.Targets.Free:
case (int)SceneBlockContainer.Targets.Free:
targetTypeText.text = "Free";
targetTypeText.color = Color.yellow;
break;
+3 -1
View File
@@ -45,6 +45,8 @@ public class ParameterContainer : MonoBehaviour
public float areaTimeBonusPerSec = 1.0f;
[Tooltip("distance reward reward = r*(1-(nowDis/startDis))")]
public float distanceReward = 20.0f;
[Tooltip("facing to Target distance reward reward = r*(1-(nowDis/startDis))")]
public float facingTargetReward = 20.0f;
[Space(10)]
[Tooltip("Goto Win reward")]
public float goWinRewardDefault = 100.0f;
@@ -117,7 +119,7 @@ public class ParameterContainer : MonoBehaviour
private void Update()
{
if (targetCon.targetTypeInt != (int)TargetController.Targets.Free)
if (targetCon.targetTypeInt != (int)SceneBlockContainer.Targets.Free)
{
(agentDistance, agentInArea) = blockCont.getAgentTargetDistanceAndInside(agentObj.transform.position);
// attack goto or defence target
+2 -2
View File
@@ -101,8 +101,8 @@ public class RaySensors : MonoBehaviour
rayDisResult = thisHit.distance;
lineLength = rayDisResult;
rayInfoText += "\n" + Convert.ToString(rayDisResult);
rayDisResult = rayDisResult / viewDistance; // Normalization!
//输出log
// rayDisResult = rayDisResult / viewDistance; // Normalization!
// 输出log
switch (rayTagResult)
{
case 1:// Wall
+5 -4
View File
@@ -5,6 +5,7 @@ using UnityEngine;
public class SceneBlockContainer : MonoBehaviour
{
public enum Targets { Free, Go, Attack, Defence, Num };// Num is use for get total target bumber
public float sceneSize = 10f;
public GameObject EnvironmentObj;
public GameObject[] attackBlockPrefabs = new GameObject[1];
@@ -18,7 +19,7 @@ public class SceneBlockContainer : MonoBehaviour
{
}
// create block random
public void createNewBlock(int targetType, int blockType, Vector3 blockPosition,string tag1,string tag2)
public void createNewBlock(Targets targetType, int blockType, Vector3 blockPosition,string tag1 = "Player", string tag2 = "Enemy")
{
// check if thisBlock is deleted
if (thisBlockObj != null)
@@ -30,7 +31,7 @@ public class SceneBlockContainer : MonoBehaviour
// choose target type
switch (targetType)
{
case (int)TargetController.Targets.Go:
case Targets.Go:
// goto
thisBlockObj = Instantiate(goBlockPrefabs[blockType], blockPosition + EnvironmentObj.transform.position, Quaternion.identity, transform);
thisBlock = thisBlockObj.GetComponent<SceneBlock>();
@@ -38,7 +39,7 @@ public class SceneBlockContainer : MonoBehaviour
thisBlock.group2Tag = tag2;
sceneSize = thisBlock.blockSize;
break;
case (int)TargetController.Targets.Attack:
case Targets.Attack:
// attack
thisBlockObj = Instantiate(attackBlockPrefabs[blockType], blockPosition+ EnvironmentObj.transform.position, Quaternion.identity, transform);
thisBlock = thisBlockObj.GetComponent<SceneBlock>();
@@ -46,7 +47,7 @@ public class SceneBlockContainer : MonoBehaviour
thisBlock.group2Tag = tag2;
sceneSize = thisBlock.blockSize;
break;
case (int)TargetController.Targets.Defence:
case Targets.Defence:
// defence
thisBlockObj = Instantiate(defencePrefabs[blockType], blockPosition + EnvironmentObj.transform.position, Quaternion.identity, transform);
thisBlock = thisBlockObj.GetComponent<SceneBlock>();
+17 -18
View File
@@ -43,7 +43,6 @@ public class TargetController : MonoBehaviour
[System.NonSerialized] public int targetTypeInt;
public float[] targetState = new float[6];
public enum Targets { Free, Go, Attack, Defence, Num };// Num is use for get total target bumber
public enum EndType { Win, Lose, Running, Num };
[System.NonSerialized] public int targetNum = 0;
private Dictionary<int, float[]> oneHotRarget = new Dictionary<int, float[]>();
@@ -87,7 +86,7 @@ public class TargetController : MonoBehaviour
hudCon = HUDObj.GetComponent<HUDController>();
raySensors = AgentObj.GetComponent<RaySensors>();
freeProb = 1 - attackProb - gotoProb - defenceProb;
targetNum = (int)Targets.Num;
targetNum = (int)SceneBlockContainer.Targets.Num;
if (freeProb < 0)
{
Debug.LogError("target percentage wrong");
@@ -120,7 +119,7 @@ public class TargetController : MonoBehaviour
{
// goto target spawn
Debug.Log("GOTO THIS TARGET!");
targetTypeInt = (int)Targets.Go;
targetTypeInt = (int)SceneBlockContainer.Targets.Go;
int randBlockType = Random.Range(0, blockCont.goBlockPrefabs.Length);
// get choosed scene size
sceneSize = blockCont.goBlockPrefabs[randBlockType].GetComponent<SceneBlock>().blockSize;
@@ -131,7 +130,7 @@ public class TargetController : MonoBehaviour
moveAgentToSpwanArea();
// init scene block
blockCont.destroyBlock();
blockCont.createNewBlock(targetTypeInt, randBlockType, targetPosition, group1Tag, group2Tag);
blockCont.createNewBlock(SceneBlockContainer.Targets.Go, randBlockType, targetPosition, group1Tag, group2Tag);
enemyCont.destroyAllEnemys();
enemyCont.randomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneSize);
blockCont.thisBlock.initBlock(EnvironmentObj);
@@ -142,7 +141,7 @@ public class TargetController : MonoBehaviour
{
// attack target spawn
Debug.Log("ATTACK!");
targetTypeInt = (int)Targets.Attack;
targetTypeInt = (int)SceneBlockContainer.Targets.Attack;
int randBlockType = Random.Range(0, blockCont.attackBlockPrefabs.Length);
// get choosed scene size
sceneSize = blockCont.attackBlockPrefabs[randBlockType].GetComponent<SceneBlock>().blockSize;
@@ -153,7 +152,7 @@ public class TargetController : MonoBehaviour
moveAgentToSpwanArea();
// init scene block
blockCont.destroyBlock();
blockCont.createNewBlock(targetTypeInt, randBlockType, targetPosition, group1Tag, group2Tag);
blockCont.createNewBlock(SceneBlockContainer.Targets.Attack, randBlockType, targetPosition, group1Tag, group2Tag);
enemyCont.destroyAllEnemys();
enemyCont.randomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneSize);
blockCont.thisBlock.initBlock(EnvironmentObj);
@@ -165,7 +164,7 @@ public class TargetController : MonoBehaviour
{
// defence target spawn
Debug.Log("DEFENCE!");
targetTypeInt = (int)Targets.Defence;
targetTypeInt = (int)SceneBlockContainer.Targets.Defence;
int randBlockType = Random.Range(0, blockCont.attackBlockPrefabs.Length);
// get choosed scene size
sceneSize = blockCont.defencePrefabs[randBlockType].GetComponent<SceneBlock>().blockSize;
@@ -176,7 +175,7 @@ public class TargetController : MonoBehaviour
moveAgentTo(targetPosition);
// init scene block
blockCont.destroyBlock();
blockCont.createNewBlock(targetTypeInt, randBlockType, targetPosition, group1Tag, group2Tag);
blockCont.createNewBlock(SceneBlockContainer.Targets.Defence, randBlockType, targetPosition, group1Tag, group2Tag);
enemyCont.destroyAllEnemys();
enemyCont.randomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneSize);
blockCont.thisBlock.initBlock(EnvironmentObj);
@@ -186,7 +185,7 @@ public class TargetController : MonoBehaviour
else
{
//Debug.Log("Free");
targetTypeInt = (int)Targets.Free;
targetTypeInt = (int)SceneBlockContainer.Targets.Free;
enemyCont.destroyAllEnemys();
enemyCont.randomInitEnemys(hudCon.enemyNum);
moveAgentToSpwanArea();
@@ -200,7 +199,7 @@ public class TargetController : MonoBehaviour
public void updateTargetStates()
{
// targettype, x,y,z, firebasesAreaDiameter
if (targetTypeInt == (int)Targets.Free)
if (targetTypeInt == (int)SceneBlockContainer.Targets.Free)
{
targetState[0] = targetTypeInt;
for(int i = 1; i < targetState.Length; i++)
@@ -262,7 +261,7 @@ public class TargetController : MonoBehaviour
float nowDistance = 0f;
switch (targetTypeInt)
{
case (int)Targets.Go:
case (int)SceneBlockContainer.Targets.Go:
// goto
(nowDistance, inArea) = blockCont.getAgentTargetDistanceAndInside(AgentObj.transform.position);
envUICon.updateTargetGauge(blockCont.thisBlock.firebasesBelong, blockCont.thisBlock.belongMaxPoint);
@@ -292,7 +291,7 @@ public class TargetController : MonoBehaviour
endTypeInt = (int)EndType.Running;
}
break;
case (int)Targets.Attack:
case (int)SceneBlockContainer.Targets.Attack:
// attack
(nowDistance, inArea) = blockCont.getAgentTargetDistanceAndInside(AgentObj.transform.position);
envUICon.updateTargetGauge(blockCont.thisBlock.firebasesBelong, blockCont.thisBlock.belongMaxPoint);
@@ -323,7 +322,7 @@ public class TargetController : MonoBehaviour
endTypeInt = (int)EndType.Running;
}
break;
case (int)Targets.Defence:
case (int)SceneBlockContainer.Targets.Defence:
//defence
// !!! DIDN't FINISH!!!
(nowDistance, inArea) = blockCont.getAgentTargetDistanceAndInside(AgentObj.transform.position);
@@ -411,7 +410,7 @@ public class TargetController : MonoBehaviour
public float killReward(Vector3 enemyPosition)
{
float thisKillReward = 0f;
if (targetTypeInt == (int)Targets.Attack)
if (targetTypeInt == (int)SceneBlockContainer.Targets.Attack)
{
// attack mode
(_, int isInArea) = blockCont.thisBlock.getDist_inArea(enemyPosition);
@@ -425,7 +424,7 @@ public class TargetController : MonoBehaviour
thisKillReward = paramCon.killReward;
}
}
else if (targetTypeInt == (int)Targets.Free)
else if (targetTypeInt == (int)SceneBlockContainer.Targets.Free)
{
// free mode hit
thisKillReward = paramCon.killTargetEnemyReward;
@@ -442,7 +441,7 @@ public class TargetController : MonoBehaviour
public float hitEnemyReward(Vector3 enemyPosition)
{
float thisHitReward = 0f;
if (targetTypeInt == (int)Targets.Attack)
if (targetTypeInt == (int)SceneBlockContainer.Targets.Attack)
{
// attack mode
(_, int isInArea) = blockCont.thisBlock.getDist_inArea(enemyPosition);
@@ -457,7 +456,7 @@ public class TargetController : MonoBehaviour
thisHitReward = paramCon.hitReward;
}
}
else if (targetTypeInt == (int)Targets.Free)
else if (targetTypeInt == (int)SceneBlockContainer.Targets.Free)
{
// free mode hit
thisHitReward = paramCon.hitTargetReward;
@@ -473,7 +472,7 @@ public class TargetController : MonoBehaviour
// get in area state
public int getInAreaState()
{
if(targetTypeInt == (int)Targets.Go)
if(targetTypeInt == (int)SceneBlockContainer.Targets.Go)
{
return inArea;
}
+6 -6
View File
@@ -14,15 +14,15 @@ public class WorldUIController : MonoBehaviour
// Start is called before the first frame update
void Start()
{
totalGames = new int[(int)TargetController.Targets.Num];
winGames = new int[(int)TargetController.Targets.Num];
Array.Clear(totalGames, 0, (int)TargetController.Targets.Num);
Array.Clear(winGames, 0, (int)TargetController.Targets.Num);
totalGames = new int[(int)SceneBlockContainer.Targets.Num];
winGames = new int[(int)SceneBlockContainer.Targets.Num];
Array.Clear(totalGames, 0, (int)SceneBlockContainer.Targets.Num);
Array.Clear(winGames, 0, (int)SceneBlockContainer.Targets.Num);
//WinChart.Init();
WinChart.RemoveData();
for (int i = 0; i < (int)TargetController.Targets.Num; i++)
for (int i = 0; i < (int)SceneBlockContainer.Targets.Num; i++)
{
string lineName = Enum.GetName(typeof(TargetController.Targets), i);
string lineName = Enum.GetName(typeof(SceneBlockContainer.Targets), i);
WinChart.AddSerie<Line>(lineName);
}
}