Aimbot Enviroment very first
Basic environment include Multi scene, Reward Change, Visible chart, etc....
This commit is contained in:
@@ -0,0 +1,638 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
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 XCharts;
|
||||
using XCharts.Runtime;
|
||||
|
||||
/*主要ML-Agent控制*/
|
||||
|
||||
public class AgentWithGun : Agent
|
||||
{
|
||||
public GameObject thisAgentObj;
|
||||
public Transform thisAgent;
|
||||
public Camera thisCam;
|
||||
public CharacterController PlayerController;
|
||||
public GameObject enemyPrefab;
|
||||
|
||||
[Header("Rewards")]
|
||||
[Tooltip("Nothing happened reward")]
|
||||
public float nonRewardDefault = -0.05f;
|
||||
[Tooltip("Agent Do shoot action reward")]
|
||||
public float shootRewardDefault = -0.1f;
|
||||
[Tooltip("Agent Do shoot action but gun is not read")]
|
||||
public float shootWithoutReadyRewardDefault = -1.0f;
|
||||
[Tooltip("Hit Enemy reward")]
|
||||
public float hitRewardDefault = 2.0f;
|
||||
[Tooltip("Episode Win reward")]
|
||||
public float winRewardDefault = 10.0f;
|
||||
[Tooltip("Episode Lose reward")]
|
||||
public float loseRewardDefault = -10.0f;
|
||||
[Tooltip("Enemy down reward")]
|
||||
public float killRewardDefault = 5.0f;
|
||||
|
||||
[Header("Env")]
|
||||
public bool lockMouse = false;
|
||||
public float Damage = 50; // damage to enemy
|
||||
public float fireRate = 0.5f;
|
||||
public int enemyNum = 3;
|
||||
public int timeLimit = 30;
|
||||
public bool lockCameraX = false;
|
||||
public bool lockCameraY = true;
|
||||
//public Vector3 startPosition = new Vector3(9, 1, 18);
|
||||
public int minEnemyAreaX = -12;
|
||||
public int maxEnemyAreaX = 11;
|
||||
public int minEnemyAreaY = -20;
|
||||
public int maxEnemyAreaY = 20;
|
||||
public int minAgentAreaX = -12;
|
||||
public int maxAgentAreaX = 11;
|
||||
public int minAgentAreaY = -28;
|
||||
public int maxAgentAreaY = -22;
|
||||
|
||||
[Header("GetAxis() Simulate")]
|
||||
public float MoveSpeed = 2.0f;
|
||||
public float vX = 0f;
|
||||
public float vZ = 0f;
|
||||
public float acceleration = 0.1f; // 加速度
|
||||
public float mouseXSensitivity = 100;
|
||||
public float mouseYSensitivity = 200;
|
||||
public float yRotation = 0.1f;//定义一个浮点类型的量,记录‘围绕’X轴旋转的角度
|
||||
|
||||
|
||||
private float startTime = 0;
|
||||
private int shoot = 0;
|
||||
private float lastShootTime = 0.0f;
|
||||
private int nowEnemyNum = 0;
|
||||
private int enemyKillCount = 0;
|
||||
private int step = 0;
|
||||
private int EP = 0;
|
||||
private string LoadDirDate;
|
||||
private string LoadDirTime;
|
||||
private float LoadDirDateF;
|
||||
private float loadDirTimeF;
|
||||
private StartSeneData DataTransfer;
|
||||
private UIController UICon;
|
||||
private HistoryRecorder HistoryRec;
|
||||
private RaySensors rayScript;
|
||||
|
||||
[System.NonSerialized]public float nonReward;
|
||||
[System.NonSerialized] public float shootReward;
|
||||
[System.NonSerialized] public float shootWithoutReadyReward;
|
||||
[System.NonSerialized] public float hitReward;
|
||||
[System.NonSerialized] public float winReward;
|
||||
[System.NonSerialized] public float loseReward;
|
||||
[System.NonSerialized] public float killReward;
|
||||
[System.NonSerialized] public float saveNow = 0;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
DataTransfer = GameObject.Find("StartSeneDataTransfer").GetComponent<StartSeneData>();
|
||||
UICon = transform.GetComponent<UIController>();
|
||||
HistoryRec = transform.GetComponent<HistoryRecorder>();
|
||||
rayScript = GetComponent<RaySensors>();
|
||||
|
||||
// get load directory.
|
||||
LoadDirDate = DataTransfer.LoadDirDate;
|
||||
LoadDirTime = DataTransfer.LoadDirTime;
|
||||
LoadDirDateF = float.Parse(LoadDirDate);
|
||||
loadDirTimeF = float.Parse(LoadDirTime);
|
||||
|
||||
// get Default reward.
|
||||
nonRewardDefault = DataTransfer.nonReward;
|
||||
shootRewardDefault = DataTransfer.shootReward;
|
||||
shootWithoutReadyRewardDefault = DataTransfer.shootWithoutReadyReward;
|
||||
hitRewardDefault = DataTransfer.hitReward;
|
||||
killRewardDefault = DataTransfer.killReward;
|
||||
winRewardDefault = DataTransfer.winReward;
|
||||
loseRewardDefault = DataTransfer.loseReward;
|
||||
|
||||
// give default Reward to Reward value will be used.
|
||||
nonReward = nonRewardDefault;
|
||||
shootReward = shootRewardDefault;
|
||||
shootWithoutReadyReward = shootWithoutReadyRewardDefault;
|
||||
hitReward = hitRewardDefault;
|
||||
winReward = winRewardDefault;
|
||||
loseReward = loseRewardDefault;
|
||||
killReward = killRewardDefault;
|
||||
|
||||
// change Decision Period & Take Actions Between Decisions
|
||||
transform.GetComponent<DecisionRequester>().DecisionPeriod = DataTransfer.DecisionPeriod;
|
||||
transform.GetComponent<DecisionRequester>().TakeActionsBetweenDecisions = DataTransfer.ActionsBetweenDecisions;
|
||||
}
|
||||
|
||||
/* ----------此Update用于debug,Build前删除或注释掉!----------*/
|
||||
/*void Update()
|
||||
{
|
||||
//Debug.Log(RaySensors.rayTagResult[0]);
|
||||
}*/
|
||||
/* ----------此Update用于debug,Build前删除或注释掉!----------*/
|
||||
|
||||
// --------------初始化---------------
|
||||
// randomInitEnemys随机生成enemy
|
||||
public void randomInitEnemys(int EnemyNum)
|
||||
{
|
||||
for (int i = 0; i < EnemyNum; i++)
|
||||
{
|
||||
int randX = UnityEngine.Random.Range(minEnemyAreaX, maxEnemyAreaX);
|
||||
int randZ = UnityEngine.Random.Range(minEnemyAreaY, maxEnemyAreaY);
|
||||
int Y = 1;
|
||||
Instantiate(enemyPrefab, new Vector3(randX, Y, randZ), Quaternion.identity);
|
||||
}
|
||||
}
|
||||
|
||||
// --------------初始化---------------
|
||||
// randomInitAgent随机位置初始化Agent
|
||||
public void randomInitAgent()
|
||||
{
|
||||
int randX = UnityEngine.Random.Range(minAgentAreaX, maxAgentAreaX);
|
||||
int randZ = UnityEngine.Random.Range(minAgentAreaY, maxAgentAreaY);
|
||||
int Y = 1;
|
||||
Vector3 initAgentLoc = new Vector3(randX, Y, randZ);
|
||||
thisAgent.localPosition = initAgentLoc;
|
||||
}
|
||||
|
||||
// ------------动作处理--------------
|
||||
// moveAgent 用于模拟Input.GetAxis移动
|
||||
public void moveAgent(int kW, int kS,int kA,int kD)
|
||||
{
|
||||
Vector3 thisMovement;
|
||||
int horizontal = 0;
|
||||
int vertical = 0;
|
||||
if (kW==1 && kS != 1)
|
||||
{
|
||||
vertical = 1;
|
||||
}
|
||||
else if (kS==1 && kW!=1)
|
||||
{
|
||||
vertical = -1;
|
||||
}
|
||||
if (kD==1 && kA!=1)
|
||||
{
|
||||
horizontal = 1;
|
||||
}
|
||||
else if (kA ==1 && kD!=1)
|
||||
{
|
||||
horizontal = -1;
|
||||
}
|
||||
|
||||
if (horizontal != 0)//当按下按键(水平方向)
|
||||
{
|
||||
if (vX < MoveSpeed && vX > -MoveSpeed)//当前速度小于最大速度
|
||||
{
|
||||
vX += (float)horizontal * acceleration;//增加加速度
|
||||
}
|
||||
else
|
||||
{
|
||||
//防止在一瞬间切换输入时速度仍保持不变
|
||||
if ((vX * horizontal) > 0)//输入与当前速度方向同向
|
||||
{
|
||||
vX = (float)horizontal * MoveSpeed; //限制最大速度
|
||||
}
|
||||
else
|
||||
{
|
||||
vX += (float)horizontal * acceleration;//增加加速度
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Math.Abs(vX) > 0.001)
|
||||
{
|
||||
vX -= (vX / Math.Abs(vX)) * acceleration;//减少加速度
|
||||
}
|
||||
else
|
||||
{
|
||||
vX = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (vertical != 0)//当按下按键(垂直方向)
|
||||
{
|
||||
if (vZ < MoveSpeed && vZ > -MoveSpeed)//当前速度小于最大速度
|
||||
{
|
||||
vZ += (float)vertical * acceleration;//增加加速度
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((vZ * vertical) > 0)//输入与当前速度方向同向
|
||||
{
|
||||
vZ = (float)vertical * MoveSpeed; //限制最大速度
|
||||
}
|
||||
else
|
||||
{
|
||||
vZ += (float)vertical * acceleration;//增加加速度
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Math.Abs(vZ) > 0.001)
|
||||
{
|
||||
vZ -= (vZ / Math.Abs(vZ)) * acceleration;//减少加速度
|
||||
}
|
||||
else
|
||||
{
|
||||
vZ = 0;
|
||||
}
|
||||
}
|
||||
thisMovement = (transform.forward * vZ + transform.right * vX) * MoveSpeed;
|
||||
//PlayerController下的.Move为实现物体运动的函数
|
||||
//Move()括号内放入一个Vector3类型的量,本例中为Player_Move
|
||||
PlayerController.Move(thisMovement * Time.deltaTime);
|
||||
// update Key Viewer
|
||||
}
|
||||
|
||||
// ------------动作处理--------------
|
||||
// cameraControl 用于控制Agent视角转动
|
||||
public void cameraControl(float Mouse_X,float Mouse_Y)
|
||||
{
|
||||
//Mouse_X = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
|
||||
//Debug.Log(Input.GetAxis("Mouse X"));
|
||||
//Mouse_Y = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;
|
||||
if (lockCameraX)
|
||||
{
|
||||
Mouse_X = 0;
|
||||
}
|
||||
if (lockCameraY)
|
||||
{
|
||||
Mouse_Y = 0;
|
||||
}
|
||||
yRotation = yRotation - Mouse_Y;
|
||||
//xRotation值为正时,屏幕下移,当xRotation值为负时,屏幕上移
|
||||
//当鼠标向上滑动,Mouse_Y值为正,xRotation-Mouse_Y的值为负,xRotation总的值为负,屏幕视角向上滑动
|
||||
//当鼠标向下滑动,Mouse_Y值为负,xRotation-Mouse_Y的值为正,xRotation总的值为正,屏幕视角向下滑动
|
||||
//简单来说就是要控制鼠标滑动的方向与屏幕移动的方向要相同
|
||||
|
||||
//limit UP DOWN between -90 -> 90
|
||||
yRotation = Mathf.Clamp(yRotation, -90f, 90f);
|
||||
|
||||
|
||||
//相机左右旋转时,是以Y轴为中心旋转的,上下旋转时,是以X轴为中心旋转的
|
||||
thisAgent.Rotate(Vector3.up * Mouse_X);
|
||||
//Vector3.up相当于Vector3(0,1,0),CameraRotation.Rotate(Vector3.up * Mouse_X)相当于使CameraRotation对象绕y轴旋转Mouse_X个单位
|
||||
//即相机左右旋转时,是以Y轴为中心旋转的,此时Mouse_X控制着值的大小
|
||||
|
||||
//相机在上下旋转移动时,相机方向不会随着移动,类似于低头和抬头,左右移动时,相机方向会随着向左向右移动,类似于向左向右看
|
||||
//所以在控制相机向左向右旋转时,要保证和父物体一起转动
|
||||
thisCam.transform.localRotation = Quaternion.Euler(yRotation, 0, 0);
|
||||
//this.transform指这个CameraRotation的位置,localRotation指的是旋转轴
|
||||
//transform.localRotation = Quaternion.Eular(x,y,z)控制旋转的时候,按照X-Y-Z轴的旋转顺规
|
||||
//即以围绕X轴旋转x度,围绕Y轴旋转y度,围绕Z轴旋转z度
|
||||
//且绕轴旋转的坐标轴是父节点本地坐标系的坐标轴
|
||||
}
|
||||
|
||||
// GotKill 获得击杀时用于呼出
|
||||
public void GotKill()
|
||||
{
|
||||
enemyKillCount += 1;
|
||||
}
|
||||
|
||||
// check gun is ready to shoot
|
||||
bool gunReady()
|
||||
{
|
||||
if ((Time.time - lastShootTime) >= fireRate)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ballistic 射击弹道处理,并返回获得reward
|
||||
float ballistic()
|
||||
{
|
||||
Vector3 point = new Vector3(thisCam.pixelWidth / 2, thisCam.pixelHeight / 2, 0);//发射位置
|
||||
Ray ray = thisCam.ScreenPointToRay(point);
|
||||
RaycastHit hit;
|
||||
Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue);
|
||||
bool isGunReady = gunReady();
|
||||
UICon.updateShootKeyViewer(shoot,isGunReady);
|
||||
//按下鼠标左键
|
||||
if (shoot != 0 && isGunReady == true)
|
||||
{
|
||||
|
||||
lastShootTime = Time.time;
|
||||
if (Physics.Raycast(ray, out hit, 100))
|
||||
{
|
||||
if (hit.collider.tag == "Enemy")
|
||||
{
|
||||
GameObject gotHitObj = hit.transform.gameObject;//获取受到Ray撞击的对象
|
||||
gotHitObj.GetComponent<Enemy>().ReactToHit(Damage, thisAgentObj);
|
||||
shoot = 0;
|
||||
return hitReward;
|
||||
}
|
||||
}
|
||||
shoot = 0;
|
||||
return shootReward;
|
||||
}
|
||||
else if (shoot != 0 && isGunReady == false)
|
||||
{
|
||||
shoot = 0;
|
||||
return shootWithoutReadyReward;
|
||||
}
|
||||
else
|
||||
{
|
||||
shoot = 0;
|
||||
return nonReward;
|
||||
}
|
||||
}
|
||||
|
||||
// destroyEnemy消除除了自己以外的所有Enemy
|
||||
public void destroyAllEnemys()
|
||||
{
|
||||
GameObject[] EnemyGameObjs;
|
||||
EnemyGameObjs = GameObject.FindGameObjectsWithTag("Enemy");
|
||||
//遍历所有Enemy
|
||||
foreach (GameObject EnemyObj in EnemyGameObjs)
|
||||
{
|
||||
Vector3 thisEnemyPosition = EnemyObj.transform.position;
|
||||
Vector3 thisEnemyScale = EnemyObj.transform.localScale;
|
||||
Vector3 MyselfPosition = thisAgent.position;
|
||||
|
||||
//探测到Agent为自己时的处理
|
||||
if (thisEnemyPosition == MyselfPosition)
|
||||
{
|
||||
//Debug.Log("OH It's me");
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(EnemyObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// checkFinish 检查是否结束回合返回int值
|
||||
// 1 = success,2 = overtime,0 = notover
|
||||
int checkFinish()
|
||||
{
|
||||
GameObject[] EnemyGameObjs;
|
||||
EnemyGameObjs = GameObject.FindGameObjectsWithTag("Enemy");
|
||||
if(EnemyGameObjs.Length <= 1)
|
||||
{
|
||||
//成功击杀所有Enemy
|
||||
return 1;
|
||||
}
|
||||
else if(Time.time - startTime >= timeLimit)
|
||||
{
|
||||
//超时失败
|
||||
return 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// getEnemyNum 获取现场除了自己以外的敌人数量
|
||||
int getEnemyNum()
|
||||
{
|
||||
int enemyNum = 0;
|
||||
GameObject[] EnemyGameObjs;
|
||||
EnemyGameObjs = GameObject.FindGameObjectsWithTag("Enemy");
|
||||
//遍历所有Enemy
|
||||
foreach (GameObject EnemyObj in EnemyGameObjs)
|
||||
{
|
||||
Vector3 thisEnemyPosition = EnemyObj.transform.position;
|
||||
Vector3 thisEnemyScale = EnemyObj.transform.localScale;
|
||||
Vector3 MyselfPosition = thisAgent.position;
|
||||
|
||||
//探测到Agent为自己时的处理
|
||||
if (thisEnemyPosition == MyselfPosition)
|
||||
{
|
||||
//Debug.Log("OH It's me");
|
||||
}
|
||||
else
|
||||
{
|
||||
enemyNum += 1;
|
||||
}
|
||||
}
|
||||
return enemyNum;
|
||||
}
|
||||
|
||||
// enemyNumDiff 获取与上一把相比敌人数量的区别
|
||||
int enemyNumDiff()
|
||||
{
|
||||
int diff = 0;
|
||||
int nowEnemyNum = getEnemyNum();
|
||||
diff = enemyNum - nowEnemyNum;
|
||||
return diff;
|
||||
}
|
||||
|
||||
// ------------Reward--------------
|
||||
// rewardCalculate 计算本动作的Reward
|
||||
public float rewardCalculate()
|
||||
{
|
||||
float epreward = 0f;
|
||||
// 击杀reward判断
|
||||
if(enemyKillCount > 0)
|
||||
{
|
||||
for(int i = 0;i < enemyKillCount; i++)
|
||||
{
|
||||
epreward += killReward;
|
||||
}
|
||||
enemyKillCount = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
enemyKillCount = 0;
|
||||
}
|
||||
// 射击动作reward判断
|
||||
epreward += ballistic();
|
||||
return epreward;
|
||||
}
|
||||
|
||||
|
||||
// ML-AGENTS处理-------------------------------------------------------------------------------------------ML-AGENTS
|
||||
// env开始执行初始化
|
||||
public override void OnEpisodeBegin()
|
||||
{
|
||||
step = 0;
|
||||
if (EP == 0)
|
||||
{
|
||||
UICon.iniChart();
|
||||
}
|
||||
if (lockMouse)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked; // 隐藏并且锁定鼠标
|
||||
}
|
||||
//iniCharts();
|
||||
thisAgentObj.name = thisAgentObj.GetInstanceID().ToString();
|
||||
destroyAllEnemys();
|
||||
startTime = Time.time;// Reset StartTime as now time
|
||||
randomInitAgent();
|
||||
randomInitEnemys(enemyNum);
|
||||
nowEnemyNum = getEnemyNum(); // Reset Enemy number
|
||||
}
|
||||
|
||||
// ML-AGENTS处理-------------------------------------------------------------------------------------------ML-AGENTS
|
||||
// 观察情报
|
||||
public override void CollectObservations(VectorSensor sensor)
|
||||
{
|
||||
|
||||
//List<float> enemyLDisList = RaySensors.enemyLDisList;// All Enemy Lside Distances
|
||||
//List<float> enemyRDisList = RaySensors.enemyRDisList;// All Enemy Rside Distances
|
||||
|
||||
|
||||
int allEnemyNum = RaySensors.allEnemyNum;
|
||||
float[] myObserve = { thisAgent.position.x, thisAgent.position.y, thisAgent.position.z, thisAgent.rotation.w };
|
||||
float[] rayTagResult = RaySensors.rayTagResult;// 探测用RayTag结果 float[](raySensorNum,1)
|
||||
float[] rayDisResult = RaySensors.rayDisResult; // 探测用RayDis结果 float[](raySensorNum,1)
|
||||
float[] focusEnemyObserve = RaySensors.focusEnemyInfo;// 最近的Enemy情报 float[](3,1) MinEnemyIndex,x,z
|
||||
int raySensorNum = rayScript.rayNum;// raySensor数量 int
|
||||
|
||||
//sensor.AddObservation(allEnemyNum); // 敌人数量 int
|
||||
sensor.AddObservation(myObserve); // 自机位置xyz+朝向 float[](4,1)
|
||||
sensor.AddObservation(rayTagResult); // 探测用RayTag结果 float[](raySensorNum,1)
|
||||
sensor.AddObservation(rayDisResult); // 探测用RayDis结果 float[](raySensorNum,1)
|
||||
sensor.AddObservation(focusEnemyObserve); // 最近的Enemy情报 float[](3,1) MinEnemyIndex,x,z
|
||||
//sensor.AddObservation(raySensorNum); // raySensor数量 int
|
||||
sensor.AddObservation(LoadDirDateF); // 用于loadModel的第一级dir
|
||||
sensor.AddObservation(loadDirTimeF); // 用于loadModel的第二级dir
|
||||
//sensor.AddObservation(remainTime); // RemainTime int
|
||||
}
|
||||
|
||||
// ML-AGENTS处理-------------------------------------------------------------------------------------------ML-AGENTS
|
||||
// agent 输入处理
|
||||
public override void OnActionReceived(ActionBuffers actionBuffers)
|
||||
{
|
||||
//获取输入
|
||||
int kW = actionBuffers.DiscreteActions[0];
|
||||
int kS = actionBuffers.DiscreteActions[1];
|
||||
int kA = actionBuffers.DiscreteActions[2];
|
||||
int kD = actionBuffers.DiscreteActions[3];
|
||||
int mouseShoot = actionBuffers.DiscreteActions[4];
|
||||
float Mouse_X = actionBuffers.ContinuousActions[0];
|
||||
//float Mouse_Y = actionBuffers.ContinuousActions[1];
|
||||
//int timeLimitControl = (int)actionBuffers.ContinuousActions[2];
|
||||
//float nonRewardIn = actionBuffers.ContinuousActions[1];
|
||||
//float shootRewardIn = actionBuffers.ContinuousActions[2];
|
||||
//float shootWithoutReadyRewardIn = actionBuffers.ContinuousActions[3];
|
||||
//float hitRewardIn = actionBuffers.ContinuousActions[4];
|
||||
//float winRewardIn = actionBuffers.ContinuousActions[5];
|
||||
// loseRewardIn = actionBuffers.ContinuousActions[6];
|
||||
//float killRewardIn = actionBuffers.ContinuousActions[7];
|
||||
//Rewards Update
|
||||
int remainTime = (int)(timeLimit - Time.time + startTime);
|
||||
|
||||
//应用输入
|
||||
shoot = mouseShoot;
|
||||
HistoryRec.realTimeKeyCounter(kW, kS, kA, kD, shoot);
|
||||
(int kWCount, int kSCount, int kACount, int kDCount, int shootCount) = HistoryRec.getKeyCount();
|
||||
UICon.updateRemainTime(remainTime);
|
||||
UICon.updateWASDKeyViewer(kW, kS, kA, kD);
|
||||
UICon.updateKeyCounterChart(kWCount, kSCount, kACount, kDCount, shootCount);
|
||||
UICon.updateMouseMovementViewer(Mouse_X);
|
||||
UICon.updateRewardViewer(nonReward, shootReward, shootWithoutReadyReward, hitReward, winReward, loseReward, killReward);
|
||||
cameraControl(Mouse_X, 0);
|
||||
moveAgent(kW, kS, kA, kD);
|
||||
float thisRoundReward = rewardCalculate();
|
||||
|
||||
//判断结束
|
||||
int finished = checkFinish();
|
||||
if (finished == 1)
|
||||
{
|
||||
//Win Finished
|
||||
HistoryRec.addRealTimeReward(winReward);
|
||||
HistoryRec.EPTotalRewardsUpdate();
|
||||
UICon.epUpdateChart(EP, HistoryRec.getLastEPTotalReward());
|
||||
UICon.resetStepChart();
|
||||
UICon.resetCounterChat();
|
||||
EP += 1;
|
||||
SetReward(winReward);
|
||||
Debug.Log("reward = " + winReward);
|
||||
EndEpisode();
|
||||
}
|
||||
else if(finished == 2)
|
||||
{
|
||||
//Lose Finished
|
||||
HistoryRec.addRealTimeReward(loseReward);
|
||||
HistoryRec.EPTotalRewardsUpdate();
|
||||
UICon.epUpdateChart(EP, HistoryRec.getLastEPTotalReward());
|
||||
UICon.resetStepChart();
|
||||
UICon.resetCounterChat();
|
||||
EP += 1;
|
||||
SetReward(loseReward);
|
||||
Debug.Log("reward = " + loseReward);
|
||||
EndEpisode();
|
||||
}
|
||||
else
|
||||
{
|
||||
// game not over yet
|
||||
HistoryRec.addRealTimeReward(thisRoundReward);
|
||||
UICon.stepUpdateChart(step, thisRoundReward);
|
||||
step += 1;
|
||||
SetReward(thisRoundReward);
|
||||
Debug.Log("reward = " + thisRoundReward);
|
||||
}
|
||||
}
|
||||
|
||||
// ML-AGENTS处理-------------------------------------------------------------------------------------------ML-AGENTS
|
||||
// 控制调试
|
||||
public override void Heuristic(in ActionBuffers actionsOut)
|
||||
{
|
||||
//
|
||||
//-------------------BUILD
|
||||
ActionSegment<float> continuousActions = actionsOut.ContinuousActions;
|
||||
ActionSegment<int> discreteActions = actionsOut.DiscreteActions;
|
||||
|
||||
int kW = 0;
|
||||
int kS = 0;
|
||||
int kA = 0;
|
||||
int kD = 0;
|
||||
if (Input.GetKey(KeyCode.W))
|
||||
{
|
||||
kW = 1;
|
||||
}
|
||||
if (Input.GetKey(KeyCode.S))
|
||||
{
|
||||
kS = 1;
|
||||
}
|
||||
if (Input.GetKey(KeyCode.A))
|
||||
{
|
||||
kA = 1;
|
||||
}
|
||||
if (Input.GetKey(KeyCode.D))
|
||||
{
|
||||
kD = 1;
|
||||
}
|
||||
discreteActions[0] = kW;
|
||||
discreteActions[1] = kS;
|
||||
discreteActions[2] = kA;
|
||||
discreteActions[3] = kD;
|
||||
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
// Debug.Log("mousebuttonhit");
|
||||
shoot = 1;
|
||||
}
|
||||
discreteActions[4] = shoot;
|
||||
//^^^^^^^^^^^^^^^^^^^^^discrete-Control^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvcontinuous-Controlvvvvvvvvvvvvvvvvvvvvvv
|
||||
float Mouse_X = Input.GetAxis("Mouse X") * mouseXSensitivity * Time.deltaTime;
|
||||
float Mouse_Y = Input.GetAxis("Mouse Y") * mouseYSensitivity * Time.deltaTime;
|
||||
continuousActions[0] = Mouse_X;
|
||||
//continuousActions[1] = nonReward;
|
||||
//continuousActions[2] = shootReward;
|
||||
//continuousActions[3] = shootWithoutReadyReward;
|
||||
//continuousActions[4] = hitReward;
|
||||
//continuousActions[5] = winReward;
|
||||
//continuousActions[6] = loseReward;
|
||||
//continuousActions[7] = killReward;
|
||||
//continuousActions[1] = Mouse_Y;
|
||||
//continuousActions[2] = timeLimit;
|
||||
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^continuous-Control^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4faf6e358e53cc24582eaff8dd830f97
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraChange : MonoBehaviour
|
||||
{
|
||||
public Camera FPSCamera;
|
||||
public Camera TPSCamera;
|
||||
|
||||
public void switchCamera()
|
||||
{
|
||||
if (TPSCamera.enabled && !FPSCamera.enabled)
|
||||
{
|
||||
ShowFPSView();
|
||||
}else if(FPSCamera.enabled && !TPSCamera.enabled)
|
||||
{
|
||||
ShowTPSView();
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowFPSView();
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowTPSView()
|
||||
{
|
||||
TPSCamera.enabled = true;
|
||||
FPSCamera.enabled = false;
|
||||
}
|
||||
public void ShowFPSView()
|
||||
{
|
||||
FPSCamera.enabled = true;
|
||||
TPSCamera.enabled = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 136f4ef424a10ea47b5981794fff8a7c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Camera_Control : MonoBehaviour
|
||||
{
|
||||
public Transform Agent;
|
||||
public Camera Cam;
|
||||
public float MouseSensitivity = 100;
|
||||
public float yRotation = 0.1f;//定义一个浮点类型的量,记录‘围绕’X轴旋转的角度
|
||||
public float viewDistance = 100;
|
||||
|
||||
private float Mouse_X;
|
||||
private float Mouse_Y;
|
||||
|
||||
void Start()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;// 隐藏并且锁定鼠标
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
//Mouse_X = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
|
||||
//Debug.Log(Input.GetAxis("Mouse X"));
|
||||
//Mouse_Y = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime;
|
||||
|
||||
//yRotation = yRotation - Mouse_Y;
|
||||
//xRotation值为正时,屏幕下移,当xRotation值为负时,屏幕上移
|
||||
//当鼠标向上滑动,Mouse_Y值为正,xRotation-Mouse_Y的值为负,xRotation总的值为负,屏幕视角向上滑动
|
||||
//当鼠标向下滑动,Mouse_Y值为负,xRotation-Mouse_Y的值为正,xRotation总的值为正,屏幕视角向下滑动
|
||||
//简单来说就是要控制鼠标滑动的方向与屏幕移动的方向要相同
|
||||
|
||||
//limit UP DOWN between -90 -> 90
|
||||
//yRotation = Mathf.Clamp(yRotation, -90f, 90f);
|
||||
|
||||
|
||||
//相机左右旋转时,是以Y轴为中心旋转的,上下旋转时,是以X轴为中心旋转的
|
||||
//Agent.Rotate(Vector3.up * Mouse_X);
|
||||
//Vector3.up相当于Vector3(0,1,0),CameraRotation.Rotate(Vector3.up * Mouse_X)相当于使CameraRotation对象绕y轴旋转Mouse_X个单位
|
||||
//即相机左右旋转时,是以Y轴为中心旋转的,此时Mouse_X控制着值的大小
|
||||
|
||||
//相机在上下旋转移动时,相机方向不会随着移动,类似于低头和抬头,左右移动时,相机方向会随着向左向右移动,类似于向左向右看
|
||||
//所以在控制相机向左向右旋转时,要保证和父物体一起转动
|
||||
//this.transform.localRotation = Quaternion.Euler(yRotation, 0, 0);
|
||||
//this.transform指这个CameraRotation的位置,localRotation指的是旋转轴
|
||||
//transform.localRotation = Quaternion.Eular(x,y,z)控制旋转的时候,按照X-Y-Z轴的旋转顺规
|
||||
//即以围绕X轴旋转x度,围绕Y轴旋转y度,围绕Z轴旋转z度
|
||||
//且绕轴旋转的坐标轴是父节点本地坐标系的坐标轴
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ab8f48d4eac07f44b093f714ede051c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class DecisionPeriodChanger : MonoBehaviour
|
||||
{
|
||||
public GameObject DataTransfer;
|
||||
public Slider DecisionPeriodSlide;
|
||||
public Text DecisionPeriodDataText;
|
||||
public Toggle TakeActionsBetweenDecisionsToggle;
|
||||
|
||||
public void onSlideValueChanged()
|
||||
{
|
||||
DataTransfer.GetComponent<StartSeneData>().DecisionPeriod = (int)(DecisionPeriodSlide.GetComponent<Slider>().value);
|
||||
DecisionPeriodDataText.text = DataTransfer.GetComponent<StartSeneData>().DecisionPeriod.ToString();
|
||||
}
|
||||
public void onToggleChanged()
|
||||
{
|
||||
DataTransfer.GetComponent<StartSeneData>().ActionsBetweenDecisions = TakeActionsBetweenDecisionsToggle.isOn;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0d097ecbef2d7b843b772144dd301b2b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Enemy : MonoBehaviour
|
||||
{
|
||||
float EnemyHP = 100;
|
||||
public float EnemyMaxHP = 100;
|
||||
|
||||
void Start()
|
||||
{
|
||||
EnemyHP = EnemyMaxHP;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
detactDeath();
|
||||
}
|
||||
private void detactDeath()
|
||||
{
|
||||
if (EnemyHP <= 0){
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
||||
public void ReactToHit(float Damage,GameObject damageSource)
|
||||
{
|
||||
EnemyHP -= Damage;
|
||||
Debug.Log("HP:"+ EnemyHP);
|
||||
if(EnemyHP <= 0)
|
||||
{
|
||||
damageSource.GetComponent<AgentWithGun>().GotKill();
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
||||
public float getnowHP()
|
||||
{
|
||||
return EnemyHP;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8cded1019e262a04e8d9ccb536d1ff20
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class EnemyHPBar : MonoBehaviour
|
||||
{
|
||||
GameObject EnemyOBJ;
|
||||
GameObject BGOBJ;
|
||||
GameObject gaugeImgOBJ;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
EnemyOBJ = transform.parent.gameObject;
|
||||
BGOBJ = transform.GetChild(0).gameObject;
|
||||
gaugeImgOBJ = BGOBJ.transform.GetChild(0).gameObject;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
Vector3 v = Camera.main.transform.position - transform.position;
|
||||
v.x = v.z = 0.0f;
|
||||
transform.LookAt(Camera.main.transform.position - v);
|
||||
transform.Rotate(0,180,0);
|
||||
|
||||
float maxHP = EnemyOBJ.GetComponent<Enemy>().EnemyMaxHP;
|
||||
float nowHP = EnemyOBJ.GetComponent<Enemy>().getnowHP();
|
||||
gaugeImgOBJ.GetComponent<Image>().fillAmount = nowHP / maxHP;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b293d3b541b20b7448f3ca4d09a9c38c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,114 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
/*??????????????*/
|
||||
|
||||
public class HistoryRecorder : MonoBehaviour
|
||||
{
|
||||
private List<float> realTimeReward = new List<float> ();
|
||||
private List<float> EPTotalRewards = new List<float> ();
|
||||
private List<int> EPTotalShootCount = new List<int> ();
|
||||
|
||||
private int realTimeWKeyCount = 0;
|
||||
private int realTimeAKeyCount = 0;
|
||||
private int realTimeSKeyCount = 0;
|
||||
private int realTimeDKeyCount = 0;
|
||||
private int realTimeShootCount = 0;
|
||||
|
||||
|
||||
// add Record History -----------------------------
|
||||
public void addRealTimeReward(float reward)
|
||||
{
|
||||
realTimeReward.Add(reward);
|
||||
}
|
||||
public void addEPTotalRewards(float EPTotalReward)
|
||||
{
|
||||
EPTotalRewards.Add(EPTotalReward);
|
||||
}
|
||||
public void addEPTotalShootCount(int TotalShootCount)
|
||||
{
|
||||
EPTotalShootCount.Add(TotalShootCount);
|
||||
}
|
||||
public void realTimeKeyCounter(int kW, int kS, int kA, int kD, int shoot)
|
||||
{
|
||||
if (kW == 1)
|
||||
{
|
||||
realTimeWKeyCount += 1;
|
||||
}
|
||||
if (kS == 1)
|
||||
{
|
||||
realTimeSKeyCount += 1;
|
||||
}
|
||||
if (kA == 1)
|
||||
{
|
||||
realTimeAKeyCount += 1;
|
||||
}
|
||||
if (kD == 1)
|
||||
{
|
||||
realTimeDKeyCount += 1;
|
||||
}
|
||||
if (shoot == 1)
|
||||
{
|
||||
realTimeShootCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// math job---------------------------------------
|
||||
// delete RealTimeReward item
|
||||
public void resetRealTimeReward()
|
||||
{
|
||||
realTimeReward.Clear();
|
||||
}
|
||||
// set all realTimeKeyCount to 0
|
||||
public void resetrealTimeKeyCounter()
|
||||
{
|
||||
realTimeAKeyCount = 0;
|
||||
realTimeDKeyCount = 0;
|
||||
realTimeWKeyCount = 0;
|
||||
realTimeSKeyCount = 0;
|
||||
realTimeShootCount = 0;
|
||||
}
|
||||
// calc RealTimeReward's Average and Add to EPTotalRewards
|
||||
public void EPTotalRewardsUpdate()
|
||||
{
|
||||
float EPSumRealTimeReward = getSumRealTimeReward();
|
||||
resetRealTimeReward();
|
||||
addEPTotalRewards(EPSumRealTimeReward);
|
||||
}
|
||||
|
||||
// get Record History -----------------------------
|
||||
// get EPTotalReward List
|
||||
public List<float> getEPTotalReward()
|
||||
{
|
||||
return (EPTotalRewards);
|
||||
}
|
||||
// get EPTotalShootCount List
|
||||
public List<int> getEPTotalShootCount()
|
||||
{
|
||||
return (EPTotalShootCount);
|
||||
}
|
||||
// get RealTimeReward's Mean
|
||||
public float getMeanRealTimeReward()
|
||||
{
|
||||
return (realTimeReward.Average());
|
||||
}
|
||||
// get RealTimeReward's Sum
|
||||
public float getSumRealTimeReward()
|
||||
{
|
||||
return realTimeReward.Sum();
|
||||
}
|
||||
// get LastEPTotalReward last item
|
||||
public float getLastEPTotalReward()
|
||||
{
|
||||
return (EPTotalRewards.Last());
|
||||
}
|
||||
//get KeyCount
|
||||
public (int w,int s,int a,int d,int shoot) getKeyCount()
|
||||
{
|
||||
return (realTimeWKeyCount, realTimeSKeyCount, realTimeAKeyCount, realTimeDKeyCount, realTimeShootCount);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a3e891536ddde24b86cbac3ad3837e6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class LoadDirDateTextChange : MonoBehaviour
|
||||
{
|
||||
public GameObject DataTransfer;
|
||||
public void OnValueChanged()
|
||||
{
|
||||
string input = this.GetComponent<InputField>().text;
|
||||
DataTransfer.GetComponent<StartSeneData>().LoadDirDate = input;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cce4f6a22ca8eba4b94c1bfc6ac08072
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class LoadDirTimeTextChange : MonoBehaviour
|
||||
{
|
||||
public GameObject DataTransfer;
|
||||
public void OnValueChanged()
|
||||
{
|
||||
string input = this.GetComponent<InputField>().text;
|
||||
DataTransfer.GetComponent<StartSeneData>().LoadDirTime = input;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8396a1a162e012447a0e4f7626e70dc7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class LoadDirToggle : MonoBehaviour
|
||||
{
|
||||
public InputField LoadDirDateTextBox;
|
||||
public InputField LoadDirTimeTextBox;
|
||||
public Toggle Toggle;
|
||||
public GameObject DataTransfer;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnToggleChanged()
|
||||
{
|
||||
// if loadDirToggle is on then turn off the input text boxs.
|
||||
LoadDirDateTextBox.interactable = Toggle.isOn;
|
||||
LoadDirTimeTextBox.interactable = Toggle.isOn;
|
||||
// if loadDirToggle is off set loaddirs as 0.
|
||||
if (!Toggle.isOn)
|
||||
{
|
||||
DataTransfer.GetComponent<StartSeneData>().LoadDirDate = "0";
|
||||
DataTransfer.GetComponent<StartSeneData>().LoadDirTime = "0";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e18f417f93a39c74481660da3236c133
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class OnCam : MonoBehaviour
|
||||
{
|
||||
public void OnBecameInvisible()
|
||||
{
|
||||
Debug.Log("I SEE U");
|
||||
}
|
||||
|
||||
// ...and enable it again when it becomes visible.
|
||||
public void OnBecameVisible()
|
||||
{
|
||||
Debug.Log("NO Visual");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d1f4766daac5b854db2634c914619261
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,321 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/*该scrip用于创建复数条ray于视角内,并探测被ray射到的物体*/
|
||||
|
||||
|
||||
public class RaySensors : MonoBehaviour
|
||||
{
|
||||
|
||||
public Camera agentCam;
|
||||
public GameObject myself;
|
||||
public int rayNum = 6;
|
||||
public string EnemyTagName;
|
||||
public string WallTagName;
|
||||
public float viewDistance = 100; // how long the ray can detect
|
||||
public float Damage = 50; // damage to enemy
|
||||
public float attentionRange = 1f; //注意力范围,1为最大
|
||||
public float MaxDistance = 9999999999f;
|
||||
public float EnemyWidthRedundancy = 0.01f; //为了确保Ray可以击中Enemy,用于缩小EnemyWidth的长度
|
||||
//public List<string> tagNames = new List<string>();
|
||||
public static int allEnemyNum = 0;//All Enemy Num
|
||||
public static float[] focusEnemyInfo = new float[3];
|
||||
public static float[] rayTagResult; // Array to save Tag Result
|
||||
public static float[] rayDisResult; // Array to save Distance Result
|
||||
public static List<float> enemyLDisList = new List<float>();// All Enemy Lside Distances
|
||||
public static List<float> enemyRDisList = new List<float>();// All Enemy Rside Distances
|
||||
|
||||
|
||||
|
||||
public bool showEnemySensor = true;
|
||||
public bool showEyeSensor = true;
|
||||
|
||||
|
||||
static int tagToInt(string tag)
|
||||
{
|
||||
switch (tag)
|
||||
{
|
||||
case "Wall":
|
||||
return 1;
|
||||
case "Enemy":
|
||||
return 2;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 敌人Ray探测处理
|
||||
// 返回
|
||||
(int, List<float>, List<float>, float[]) enemySensorRay(GameObject myself, Camera agentCam, float attentionRange)
|
||||
{
|
||||
List<float> thisLDistanceList = new List<float>();
|
||||
List<float> thisRDistanceList = new List<float>();
|
||||
|
||||
GameObject[] EnemyGameObjs;
|
||||
EnemyGameObjs = GameObject.FindGameObjectsWithTag("Enemy");
|
||||
int EnemyIndex = 0;
|
||||
float MinEnemyDis = MaxDistance+1.0f;
|
||||
int MinEnemyIndex = 0;
|
||||
float[] MinEnemyInfo = new float[3];
|
||||
|
||||
//遍历所有Enemy
|
||||
foreach (GameObject EnemyObj in EnemyGameObjs)
|
||||
{
|
||||
Vector3 thisEnemyPosition = EnemyObj.transform.position;
|
||||
Vector3 thisEnemyScale = EnemyObj.transform.localScale;
|
||||
Vector3 MyselfPosition = myself.transform.position;
|
||||
float thisEnemyWidth = (float)(thisEnemyScale.x / 2) - EnemyWidthRedundancy;
|
||||
float thisEnemyDistance = Vector3.Distance(MyselfPosition, thisEnemyPosition);
|
||||
|
||||
//探测到Agent为自己时的处理
|
||||
if (thisEnemyPosition == MyselfPosition)
|
||||
{
|
||||
//Debug.Log("OH It's me");
|
||||
thisLDistanceList.Add(MaxDistance);
|
||||
thisRDistanceList.Add(MaxDistance);
|
||||
}
|
||||
//非己Agent处理
|
||||
else
|
||||
{
|
||||
EnemyIndex += 1;
|
||||
Vector3 Vertical = new Vector3(0, 100, 0);//垂直向上的向量
|
||||
Vector3 EnemytoMe = MyselfPosition - thisEnemyPosition;//Enemy和自机连线,指向自机的向量
|
||||
Vector3 LHorizontal = Vector3.Cross(Vertical, EnemytoMe);// 垂直于EnemytoMe与Vertical向量所组成的面的,且指向<-左侧的小向量<- Enemy
|
||||
Vector3 RHorizontal = Vector3.Cross(EnemytoMe, Vertical);// 垂直于EnemytoMe与Vertical向量所组成的面的,且指向->右侧的小向量Enemy ->
|
||||
//此时RHorizontal,LHorizontal的长度巨几把长,需要下面操作标准化为1/2Enemy宽度
|
||||
float standaedization = (float)thisEnemyWidth / Vector3.Distance(thisEnemyPosition, RHorizontal);//计算需要缩小的比例
|
||||
RHorizontal *= standaedization;//应用缩小比例,标准化完成
|
||||
LHorizontal *= standaedization;//应用缩小比例,标准化完成
|
||||
Vector3 LMetoEnemy = LHorizontal - EnemytoMe;//自机与左侧边界连线Me<- Enemy
|
||||
Vector3 RMetoEnemy = RHorizontal - EnemytoMe;//自机与右侧边界连线Enemy ->Me
|
||||
Vector3 L0toEnemy = LHorizontal + thisEnemyPosition;// Enemy左侧绝对坐标
|
||||
Vector3 R0toEnemy = RHorizontal + thisEnemyPosition;// Enemy右侧绝对坐标
|
||||
float LMetoEnemyDist = Vector3.Distance(MyselfPosition, L0toEnemy);
|
||||
float RMetoEnemyDist = Vector3.Distance(MyselfPosition, R0toEnemy);
|
||||
Vector3 LEnemyInView = agentCam.WorldToViewportPoint(L0toEnemy);//Enemy左侧于视角中位置
|
||||
Vector3 REnemyInView = agentCam.WorldToViewportPoint(R0toEnemy);//Enemy右侧于视角中位置
|
||||
//Debug连线,颜色遵循飞机航行灯基本使用规则,左红右绿尾翼白。
|
||||
//Debug.DrawRay(thisEnemyPosition, EnemytoMe, Color.white);//Enemy和自机连线,指向自机的向量
|
||||
//Debug.DrawRay(thisEnemyPosition, Vertical, Color.white);//垂直向上的向量
|
||||
//Debug.DrawRay(thisEnemyPosition, LHorizontal, Color.red);// 垂直于Vc与Vertical向量所组成的面的,且指向<-左侧的小向量<- Enemy
|
||||
//Debug.DrawRay(thisEnemyPosition, RHorizontal, Color.green);// 垂直于Vc与Vertical向量所组成的面的,且指向->右侧的小向量Enemy ->
|
||||
//Debug.DrawRay(MyselfPosition, LMetoEnemy, Color.red);//自机与左侧边界连线<- Enemy
|
||||
//Debug.DrawRay(MyselfPosition, RMetoEnemy, Color.green);//自机与右侧边界连线Enemy ->
|
||||
//Debug.Log("EnemyObj" + EnemyIndex + "Position:" + thisEnemyPosition);
|
||||
|
||||
|
||||
//左侧于可见范围内--<--<--<--<--<--<--<--<--<--<--<--<--<--<
|
||||
if (LEnemyInView.x >= (thisEnemyWidth - attentionRange / 2) && LEnemyInView.x <= (thisEnemyWidth + attentionRange / 2) && LEnemyInView.z > 0)
|
||||
{
|
||||
//射出Raycast
|
||||
Ray LRay = new Ray(MyselfPosition, LMetoEnemy);
|
||||
RaycastHit LHit;
|
||||
if (showEnemySensor)
|
||||
{
|
||||
Debug.DrawRay(LRay.origin, LRay.direction * LMetoEnemyDist, Color.white);//自机与左侧边界连线<- Enemy
|
||||
}
|
||||
//Ray Hit Something
|
||||
if (Physics.Raycast(MyselfPosition, LMetoEnemy, out LHit, LMetoEnemyDist))
|
||||
{
|
||||
//Ray Hit Enemy
|
||||
//并且当射线射到的Enemy的距离与当前判断Enemy的距离差小于该Enemy半径时
|
||||
//既该射线所射到的Enemy为当前Enemy而不是别的时
|
||||
if (LHit.collider.tag == EnemyTagName && System.Math.Abs(LHit.distance - thisEnemyDistance) <= thisEnemyWidth)
|
||||
{
|
||||
if (showEnemySensor)
|
||||
{
|
||||
Debug.DrawRay(LRay.origin, LRay.direction * LHit.distance, Color.red);//自机与所击中物体的连线
|
||||
}
|
||||
thisLDistanceList.Add(LHit.distance);
|
||||
//Debug.Log("Hit Tag = " + LHit.collider.tag);
|
||||
//Debug.Log(LDistance[EnemyIndex-1]);
|
||||
//Debug.Log("ADD_LIST_ENEMYL");
|
||||
}
|
||||
//当射线所hit的不是enemy或hit的enemy不是当前所判断的enemy
|
||||
else
|
||||
{
|
||||
if (showEnemySensor)
|
||||
{
|
||||
Debug.DrawRay(LRay.origin, LRay.direction * LHit.distance, Color.cyan);//自机与所击中物体的连线
|
||||
}
|
||||
thisLDistanceList.Add(MaxDistance);
|
||||
//Debug.LogWarning("Hit Tag = " + LHit.collider.tag);
|
||||
//Debug.LogWarning(LDistance[EnemyIndex-1]);
|
||||
//Debug.Log("ADD_LIST_ENEMYL");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
thisLDistanceList.Add(MaxDistance);
|
||||
Debug.LogError("LRAY HIT NOTHING, Check Code!");
|
||||
}
|
||||
}
|
||||
//左侧不在可见范围内时--<--<--<--<--<--<--<--<--<--<--<--<--<--<
|
||||
else
|
||||
{
|
||||
thisLDistanceList.Add(MaxDistance);
|
||||
//Debug.LogError("NoVisual");
|
||||
//Debug.LogError(LDistance[EnemyIndex-1]);
|
||||
//Debug.Log("ADD_LIST_ENEMYL_ELSE");
|
||||
}
|
||||
|
||||
//右侧于可见范围内-->-->-->-->-->-->-->-->-->-->-->-->-->-->
|
||||
if (REnemyInView.x >= (thisEnemyWidth - attentionRange / 2) && REnemyInView.x <= (thisEnemyWidth + attentionRange / 2) && REnemyInView.z > 0)
|
||||
{
|
||||
//射出Raycast
|
||||
Ray RRay = new Ray(MyselfPosition, RMetoEnemy);
|
||||
RaycastHit RHit;
|
||||
if (showEnemySensor)
|
||||
{
|
||||
Debug.DrawRay(RRay.origin, RRay.direction * RMetoEnemyDist, Color.white);//自机与左侧边界连线<- Enemy
|
||||
}
|
||||
//Ray Hit Something
|
||||
if (Physics.Raycast(MyselfPosition, RMetoEnemy, out RHit, RMetoEnemyDist))
|
||||
{
|
||||
//Ray Hit Enemy
|
||||
//并且当射线射到的Enemy的距离与当前判断Enemy的距离差小于该Enemy半径时
|
||||
//既该射线所射到的Enemy为当前Enemy而不是别的时
|
||||
if (RHit.collider.tag == EnemyTagName && System.Math.Abs(RHit.distance - thisEnemyDistance) <= thisEnemyWidth)
|
||||
{
|
||||
if (showEnemySensor)
|
||||
{
|
||||
Debug.DrawRay(RRay.origin, RRay.direction * RHit.distance, Color.red);//自机与所击中物体的连线
|
||||
}
|
||||
thisRDistanceList.Add(RHit.distance);
|
||||
//Debug.Log("Hit Tag = " + LHit.collider.tag);
|
||||
//Debug.Log(LDistance[EnemyIndex-1]);
|
||||
//Debug.Log("ADD_LIST_ENEMYR");
|
||||
}
|
||||
//当射线所hit的不是enemy或hit的enemy不是当前所判断的enemy
|
||||
else
|
||||
{
|
||||
if (showEnemySensor)
|
||||
{
|
||||
Debug.DrawRay(RRay.origin, RRay.direction * RHit.distance, Color.cyan);//自机与所击中物体的连线
|
||||
}
|
||||
thisRDistanceList.Add(MaxDistance);
|
||||
//Debug.LogWarning("Hit Tag = " + LHit.collider.tag);
|
||||
//Debug.LogWarning(LDistance[EnemyIndex-1]);
|
||||
//Debug.Log("ADD_LIST_ENEMYR");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
thisLDistanceList.Add(MaxDistance);
|
||||
Debug.LogError("RRAY HIT NOTHING, Check Code!");
|
||||
}
|
||||
}
|
||||
//右侧不在可见范围内时-->-->-->-->-->-->-->-->-->-->-->-->-->-->
|
||||
else
|
||||
{
|
||||
thisRDistanceList.Add(MaxDistance);
|
||||
//Debug.LogError("NoVisual");
|
||||
//Debug.LogError(LDistance[EnemyIndex-1]);
|
||||
//Debug.Log("ADD_LIST_ENEMYR_ELSE");
|
||||
}
|
||||
}
|
||||
//Debug.LogWarning("EnemyIndex" + EnemyIndex);
|
||||
//Debug.LogWarning(thisLDistanceList.Count);
|
||||
//Debug.LogWarning(MinEnemyIndex);
|
||||
//Debug.Log("thisLDistanceList" + thisLDistanceList.Count);
|
||||
//Debug.Log("thisRDistanceList" + thisRDistanceList.Count);
|
||||
|
||||
//检查最近Enemy
|
||||
if (System.Math.Min(thisLDistanceList[EnemyIndex], thisRDistanceList[EnemyIndex]) < MinEnemyDis)
|
||||
{
|
||||
//Debug.Log("EnemyIndex" + EnemyIndex);
|
||||
MinEnemyDis = System.Math.Min(thisLDistanceList[EnemyIndex], thisRDistanceList[EnemyIndex]);
|
||||
MinEnemyIndex = EnemyIndex;
|
||||
}
|
||||
}
|
||||
// 获取最近敌人的准确位置信息
|
||||
MinEnemyInfo[0] = (float)MinEnemyIndex;
|
||||
if(MinEnemyInfo[0] <= 0)
|
||||
{
|
||||
MinEnemyInfo[1] = MaxDistance;
|
||||
MinEnemyInfo[2] = MaxDistance;
|
||||
}
|
||||
else
|
||||
{
|
||||
MinEnemyInfo[1] = EnemyGameObjs[MinEnemyIndex].transform.position.x;
|
||||
MinEnemyInfo[2] = EnemyGameObjs[MinEnemyIndex].transform.position.z;
|
||||
}
|
||||
return (EnemyIndex, thisLDistanceList, thisRDistanceList,MinEnemyInfo);
|
||||
}
|
||||
|
||||
|
||||
// 全局Ray探测处理
|
||||
(float[], float[]) eyeSensorRay(int rayNum, Camera agentCam, float viewDistance)
|
||||
{
|
||||
//初始化result Array
|
||||
float[] thisRayTagResult = new float[rayNum];
|
||||
float[] thisRayDisResult = new float[rayNum];
|
||||
|
||||
//于视角范围内一帧射出rayNum条射线
|
||||
for (int a = 0; a <= rayNum - 1; a = a + 1)
|
||||
{
|
||||
//射线射出
|
||||
Vector3 point = new Vector3(a * agentCam.pixelWidth / (rayNum - 1), agentCam.pixelHeight / 2, 0);//发射位置
|
||||
Ray ray = agentCam.ScreenPointToRay(point);
|
||||
RaycastHit hit;
|
||||
if (showEyeSensor)
|
||||
{
|
||||
Debug.DrawRay(ray.origin, ray.direction * viewDistance, Color.black);
|
||||
}
|
||||
//若在viewDistance范围内有碰撞
|
||||
if (Physics.Raycast(ray, out hit, viewDistance))
|
||||
{
|
||||
thisRayTagResult[a] = tagToInt(hit.collider.tag);
|
||||
thisRayDisResult[a] = hit.distance;
|
||||
if (showEyeSensor)
|
||||
{
|
||||
Debug.DrawRay(ray.origin, ray.direction * hit.distance, Color.yellow);
|
||||
}
|
||||
//输出log
|
||||
//Debug.Log(rayTagResult[a]);
|
||||
//Debug.Log(tagToInt(hit.collider.tag));
|
||||
|
||||
}
|
||||
//若在viewDistance范围无碰撞
|
||||
else
|
||||
{
|
||||
thisRayTagResult[a] = -1f;
|
||||
thisRayDisResult[a] = -1f;
|
||||
//输出log
|
||||
//Debug.Log(0);
|
||||
//Debug.Log(0);
|
||||
}
|
||||
}
|
||||
return (thisRayTagResult, thisRayDisResult);
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
rayTagResult = new float[rayNum];
|
||||
rayDisResult = new float[rayNum];
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
(rayTagResult, rayDisResult) = eyeSensorRay(rayNum, agentCam, viewDistance);
|
||||
(allEnemyNum, enemyLDisList, enemyRDisList, focusEnemyInfo) = enemySensorRay(myself, agentCam, attentionRange);
|
||||
transform.gameObject.GetComponent<UIController>().updateRemainEnemy(allEnemyNum);
|
||||
/*
|
||||
Debug.LogWarning("rayNum :" + rayNum);
|
||||
for (int i =0;i < rayNum; i++)
|
||||
{
|
||||
Debug.Log("rayTagResult" + rayTagResult[i] + "rayDisResult"+ rayDisResult[i]);
|
||||
}
|
||||
Debug.LogWarning("EnemyNum Include Me:" + allEnemyNum);
|
||||
for(int i = 0; i < allEnemyNum; i++)
|
||||
{
|
||||
Debug.Log("enemyLDisList" + enemyLDisList[i] + "enemyRDisList" + enemyRDisList[i]);
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 787273829b64d4d4b8237ea7316f59ce
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,55 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class RewardChanger : MonoBehaviour
|
||||
{
|
||||
public GameObject Agent;
|
||||
|
||||
public Button nonRBT;
|
||||
public Button shootRBT;
|
||||
public Button shootWithoutReadyRBT;
|
||||
public Button hitRBT;
|
||||
public Button killRBT;
|
||||
public Button winRBT;
|
||||
public Button loseRBT;
|
||||
|
||||
public InputField nonRInputField;
|
||||
public InputField shootRInputField;
|
||||
public InputField shootWithoutReadyRInputField;
|
||||
public InputField hitRInputField;
|
||||
public InputField killRInputField;
|
||||
public InputField winRInputField;
|
||||
public InputField loseRInputField;
|
||||
|
||||
|
||||
public void nonRBTPresses(){
|
||||
string reward = nonRInputField.GetComponent<InputField>().text;
|
||||
Agent.GetComponent<AgentWithGun>().nonReward = float.Parse(reward);
|
||||
}
|
||||
public void shootRBTPresses(){
|
||||
string reward = shootRInputField.GetComponent<InputField>().text;
|
||||
Agent.GetComponent<AgentWithGun>().shootReward = float.Parse(reward);
|
||||
}
|
||||
public void shootWithoutReadyRBTPresses(){
|
||||
string reward = shootWithoutReadyRInputField.GetComponent<InputField>().text;
|
||||
Agent.GetComponent<AgentWithGun>().shootWithoutReadyReward = float.Parse(reward);
|
||||
}
|
||||
public void hitRBTPresses(){
|
||||
string reward = hitRInputField.GetComponent<InputField>().text;
|
||||
Agent.GetComponent<AgentWithGun>().hitReward = float.Parse(reward);
|
||||
}
|
||||
public void killRBTPresses(){
|
||||
string reward = killRInputField.GetComponent<InputField>().text;
|
||||
Agent.GetComponent<AgentWithGun>().killReward = float.Parse(reward);
|
||||
}
|
||||
public void winRBTPresses(){
|
||||
string reward = winRInputField.GetComponent<InputField>().text;
|
||||
Agent.GetComponent<AgentWithGun>().winReward = float.Parse(reward);
|
||||
}
|
||||
public void loseRBTPresses(){
|
||||
string reward = loseRInputField.GetComponent<InputField>().text;
|
||||
Agent.GetComponent<AgentWithGun>().loseReward = float.Parse(reward);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e23822cccaf08084ab5cde559ca068e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,120 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class RewardsChange : MonoBehaviour
|
||||
{
|
||||
public GameObject DataTransfer;
|
||||
|
||||
public GameObject nonRInputOBJ;
|
||||
public GameObject shootRInputOBJ;
|
||||
public GameObject shootWithoutReadyRInputOBJ;
|
||||
public GameObject hitRInputOBJ;
|
||||
public GameObject killRInputOBJ;
|
||||
public GameObject winRInputOBJ;
|
||||
public GameObject loseRInputOBJ;
|
||||
|
||||
|
||||
public Text nonRInputText;
|
||||
public Text shootRInputText;
|
||||
public Text shootWithoutReadyRInputText;
|
||||
public Text hitRInputText;
|
||||
public Text killRInputText;
|
||||
public Text winRInputText;
|
||||
public Text loseRInputText;
|
||||
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
}
|
||||
// Update is called once per frame
|
||||
|
||||
public void nonRValueChanged()
|
||||
{
|
||||
if (nonRInputOBJ.GetComponent<InputField>().text == "")
|
||||
{
|
||||
nonRInputText.color = Color.gray;
|
||||
DataTransfer.GetComponent<StartSeneData>().nonReward = DataTransfer.GetComponent<StartSeneData>().nonRewardDefault;
|
||||
}
|
||||
else
|
||||
{
|
||||
nonRInputText.color = Color.yellow;
|
||||
DataTransfer.GetComponent<StartSeneData>().nonReward = float.Parse(nonRInputOBJ.GetComponent<InputField>().text);
|
||||
}
|
||||
}
|
||||
public void shootRValueChanged()
|
||||
{
|
||||
if (shootRInputOBJ.GetComponent<InputField>().text == "")
|
||||
{
|
||||
shootRInputText.color = Color.gray;
|
||||
DataTransfer.GetComponent<StartSeneData>().shootReward = DataTransfer.GetComponent<StartSeneData>().shootRewardDefault;
|
||||
}
|
||||
else {
|
||||
shootRInputText.color = Color.yellow;
|
||||
DataTransfer.GetComponent<StartSeneData>().shootReward = float.Parse(shootRInputOBJ.GetComponent<InputField>().text);
|
||||
}
|
||||
}
|
||||
public void shootWOReadyRValueChanged()
|
||||
{
|
||||
if(shootWithoutReadyRInputOBJ.GetComponent<InputField>().text == "")
|
||||
{
|
||||
shootWithoutReadyRInputText.color = Color.gray;
|
||||
DataTransfer.GetComponent<StartSeneData>().shootWithoutReadyReward = DataTransfer.GetComponent<StartSeneData>().shootWithoutReadyRewardDefault;
|
||||
}
|
||||
else{
|
||||
shootWithoutReadyRInputText.color = Color.yellow;
|
||||
DataTransfer.GetComponent<StartSeneData>().shootWithoutReadyReward = float.Parse(shootWithoutReadyRInputOBJ.GetComponent<InputField>().text);
|
||||
}
|
||||
}
|
||||
public void hitRValueChanged()
|
||||
{
|
||||
if(hitRInputOBJ.GetComponent<InputField>().text == "")
|
||||
{
|
||||
hitRInputText.color = Color.gray;
|
||||
DataTransfer.GetComponent<StartSeneData>().hitReward = DataTransfer.GetComponent<StartSeneData>().hitRewardDefault;
|
||||
}
|
||||
else{
|
||||
hitRInputText.color = Color.yellow;
|
||||
DataTransfer.GetComponent<StartSeneData>().hitReward = float.Parse(hitRInputOBJ.GetComponent<InputField>().text);
|
||||
}
|
||||
}
|
||||
public void winRValueChanged()
|
||||
{
|
||||
if(winRInputOBJ.GetComponent<InputField>().text == "")
|
||||
{
|
||||
winRInputText.color = Color.gray;
|
||||
DataTransfer.GetComponent<StartSeneData>().winReward = DataTransfer.GetComponent<StartSeneData>().winRewardDefault;
|
||||
}
|
||||
else{
|
||||
winRInputText.color = Color.yellow;
|
||||
DataTransfer.GetComponent<StartSeneData>().winReward = float.Parse(winRInputOBJ.GetComponent<InputField>().text);
|
||||
}
|
||||
}
|
||||
public void loseRValueChanged()
|
||||
{
|
||||
if(loseRInputOBJ.GetComponent<InputField>().text == "")
|
||||
{
|
||||
loseRInputText.color = Color.gray;
|
||||
DataTransfer.GetComponent<StartSeneData>().loseReward = DataTransfer.GetComponent<StartSeneData>().loseRewardDefault;
|
||||
}
|
||||
else{
|
||||
loseRInputText.color = Color.yellow;
|
||||
DataTransfer.GetComponent<StartSeneData>().loseReward = float.Parse(loseRInputOBJ.GetComponent<InputField>().text);
|
||||
}
|
||||
}
|
||||
public void killRValueChanged()
|
||||
{
|
||||
if(killRInputOBJ.GetComponent<InputField>().text == "")
|
||||
{
|
||||
killRInputText.color = Color.gray;
|
||||
DataTransfer.GetComponent<StartSeneData>().killReward = DataTransfer.GetComponent<StartSeneData>().killRewardDefault;
|
||||
}
|
||||
else{
|
||||
killRInputText.color = Color.yellow;
|
||||
DataTransfer.GetComponent<StartSeneData>().killReward = float.Parse(killRInputOBJ.GetComponent<InputField>().text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44fd351436d221849991bd84d37d8109
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class SaveWeightsButton : MonoBehaviour
|
||||
{
|
||||
public GameObject Agent;
|
||||
public void onButtonClicked()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 065aae6979c750b4fa0a0be5dbd8d2c7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class SceneChange : MonoBehaviour
|
||||
{
|
||||
public GameObject DataTransfer;
|
||||
public Text errorText;
|
||||
// Start is called before the first frame update
|
||||
bool checkLoadDirFormat()
|
||||
{
|
||||
string thisLoadDirDate = DataTransfer.GetComponent<StartSeneData>().LoadDirDate;
|
||||
string thisLoadDirTime = DataTransfer.GetComponent<StartSeneData>().LoadDirTime;
|
||||
int LoadDirDateLength = thisLoadDirDate.Length;
|
||||
int LoadDirTimeLength = thisLoadDirTime.Length;
|
||||
if ((LoadDirDateLength == 8 && LoadDirTimeLength == 6) || (thisLoadDirDate == "0" && thisLoadDirTime == "0"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void onStartClick()
|
||||
{
|
||||
if (checkLoadDirFormat())
|
||||
{
|
||||
errorText.text = "Loading next Scene...";
|
||||
errorText.color = Color.green;
|
||||
SceneManager.LoadScene("InGame");
|
||||
}
|
||||
else
|
||||
{
|
||||
errorText.text = "Dir Format error";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73ae3df5f5faba1428ab2529c043b7ae
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class StartSeneData : MonoBehaviour
|
||||
{
|
||||
public string LoadDirDate = "0";
|
||||
public string LoadDirTime = "0";
|
||||
[Header("Default Rewards")]
|
||||
public float nonRewardDefault = -0.05f;
|
||||
public float shootRewardDefault = -0.06f;
|
||||
public float shootWithoutReadyRewardDefault = -0.06f;
|
||||
public float hitRewardDefault = 5.0f;
|
||||
public float killRewardDefault = 10.0f;
|
||||
public float winRewardDefault = 20.0f;
|
||||
public float loseRewardDefault = -10.0f;
|
||||
|
||||
[Header("Rewards for Transfer")]
|
||||
public float nonReward;
|
||||
public float shootReward;
|
||||
public float shootWithoutReadyReward;
|
||||
public float hitReward;
|
||||
public float killReward;
|
||||
public float winReward;
|
||||
public float loseReward;
|
||||
|
||||
[Header("Decision Period")]
|
||||
public int DecisionPeriod = 1;
|
||||
public bool ActionsBetweenDecisions = true;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
nonReward = nonRewardDefault;
|
||||
shootReward = shootRewardDefault;
|
||||
shootWithoutReadyReward = shootWithoutReadyRewardDefault;
|
||||
hitReward = hitRewardDefault;
|
||||
killReward = killRewardDefault;
|
||||
winReward = winRewardDefault;
|
||||
loseReward = loseRewardDefault;
|
||||
}
|
||||
// Update is called once per frame
|
||||
void Awake()
|
||||
{
|
||||
DontDestroyOnLoad(transform.gameObject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 52ba7faaa6129cf418f26c5933d4ea0e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,204 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XCharts.Runtime;
|
||||
|
||||
/*????UI*/
|
||||
|
||||
public class UIController : MonoBehaviour
|
||||
{
|
||||
|
||||
[Header("Key Viewer")]
|
||||
public int updateStep = 600;
|
||||
public Text upText;
|
||||
public Text downText;
|
||||
public Text leftText;
|
||||
public Text rightText;
|
||||
public Text shootText;
|
||||
public Text MouseText;
|
||||
public Text remainTimeText;
|
||||
public Text remainEnemyText;
|
||||
public Image mouseVisualizationBG;
|
||||
public Image mouseVisualizationMeter;
|
||||
|
||||
[Header("Reward Viewer")]
|
||||
public Text nonRewardText;
|
||||
public Text shootRewardText;
|
||||
public Text shootWithoutReadyRewardText;
|
||||
public Text hitRewardText;
|
||||
public Text winRewardText;
|
||||
public Text loseRewardText;
|
||||
public Text killRewardText;
|
||||
|
||||
|
||||
[Header("X_Charts")]
|
||||
public GameObject realTimeRewardChartOBJ;
|
||||
public GameObject realTimeKeyCounterChartOBJ;
|
||||
public GameObject EPTotalRewardsChartOBJ;
|
||||
|
||||
[Header("Other Para")]
|
||||
public float mouseMaxMovement = 15;
|
||||
|
||||
private LineChart realTimeRewardChart;
|
||||
private BarChart realTimeKeyCounterChart;
|
||||
private LineChart EPTotalRewardsChart;
|
||||
|
||||
//-----------RewardViewer--------
|
||||
public void updateRewardViewer(float nonReward, float shootReward, float shootWithoutReadyReward, float hitReward, float winReward, float loseReward, float killReward)
|
||||
{
|
||||
nonRewardText.text = Convert.ToString(nonReward);
|
||||
shootRewardText.text = Convert.ToString(shootReward);
|
||||
shootWithoutReadyRewardText.text = Convert.ToString(shootWithoutReadyReward);
|
||||
hitRewardText.text = Convert.ToString(hitReward);
|
||||
winRewardText.text = Convert.ToString(winReward);
|
||||
loseRewardText.text = Convert.ToString(loseReward);
|
||||
killRewardText.text = Convert.ToString(killReward);
|
||||
}
|
||||
|
||||
//------------RemainTime----------
|
||||
public void updateRemainTime(int remainTime)
|
||||
{
|
||||
remainTimeText.text = Convert.ToString(remainTime);
|
||||
}
|
||||
//------------RemainEnemy---------
|
||||
public void updateRemainEnemy(int enemyNum)
|
||||
{
|
||||
remainEnemyText.text = Convert.ToString(enemyNum);
|
||||
}
|
||||
|
||||
//------------Key Viewer----------
|
||||
public void updateWASDKeyViewer(int kW,int kS,int kA,int kD)
|
||||
{
|
||||
if (kW == 1)
|
||||
{
|
||||
upText.color = Color.red;
|
||||
}
|
||||
else
|
||||
{
|
||||
upText.color = Color.black;
|
||||
}
|
||||
if (kS == 1)
|
||||
{
|
||||
downText.color = Color.red;
|
||||
}
|
||||
else
|
||||
{
|
||||
downText.color = Color.black;
|
||||
}
|
||||
if(kA == 1)
|
||||
{
|
||||
leftText.color = Color.red;
|
||||
}
|
||||
else
|
||||
{
|
||||
leftText.color = Color.black;
|
||||
}
|
||||
if( kD == 1)
|
||||
{
|
||||
rightText.color = Color.red;
|
||||
}
|
||||
else
|
||||
{
|
||||
rightText.color = Color.black;
|
||||
}
|
||||
}
|
||||
public void updateShootKeyViewer(int shoot,bool isGunReady)
|
||||
{
|
||||
if(shoot != 0 && isGunReady == true)
|
||||
{
|
||||
shootText.color = Color.red;
|
||||
}
|
||||
else if(shoot != 0 && isGunReady == false)
|
||||
{
|
||||
shootText.color = Color.yellow;
|
||||
}
|
||||
else
|
||||
{
|
||||
shootText.color = Color.black;
|
||||
}
|
||||
}
|
||||
public void updateMouseMovementViewer(float Mouse_x)
|
||||
{
|
||||
MouseText.text = Mouse_x.ToString();
|
||||
float mouseBGWidth = mouseVisualizationBG.GetComponent<RectTransform>().sizeDelta.x;
|
||||
float mouseBGPosX = mouseVisualizationBG.GetComponent<RectTransform>().position.x;
|
||||
float mouseBGPosY = mouseVisualizationBG.GetComponent<RectTransform>().position.y;
|
||||
|
||||
float mouseMeterWidth = mouseBGWidth * Mouse_x / mouseMaxMovement;
|
||||
float mouseMeterPositionOffset = mouseMeterWidth / 2;
|
||||
mouseVisualizationMeter.rectTransform.sizeDelta = new Vector2(Math.Abs(mouseMeterWidth),mouseVisualizationMeter.GetComponent<RectTransform>().sizeDelta.y);
|
||||
mouseVisualizationMeter.rectTransform.position = new Vector3(mouseBGPosX + mouseMeterPositionOffset, mouseBGPosY, 0);
|
||||
}
|
||||
|
||||
// ------------X Chart------------
|
||||
// Initialize Chart UI
|
||||
public void iniChart()
|
||||
{
|
||||
realTimeRewardChart = realTimeRewardChartOBJ.GetComponent<LineChart>();
|
||||
resetStepChart();
|
||||
realTimeKeyCounterChart = realTimeKeyCounterChartOBJ.GetComponent<BarChart>();
|
||||
resetCounterChat();
|
||||
|
||||
EPTotalRewardsChart = EPTotalRewardsChartOBJ.GetComponent<LineChart>();
|
||||
resetEPChart();
|
||||
}
|
||||
|
||||
// Resert------------
|
||||
// reset setp by setp update Chart
|
||||
public void resetStepChart()
|
||||
{
|
||||
realTimeRewardChart.RemoveData();
|
||||
realTimeRewardChart.AddSerie<Line>("RealTimeRewardChart");
|
||||
}
|
||||
// reset keyCounter Chart
|
||||
public void resetCounterChat()
|
||||
{
|
||||
realTimeKeyCounterChart.RemoveData();
|
||||
realTimeKeyCounterChart.AddSerie<Bar>("RealTimeKeyCounterChart");
|
||||
realTimeKeyCounterChart.AddXAxisData("W");
|
||||
realTimeKeyCounterChart.AddXAxisData("S");
|
||||
realTimeKeyCounterChart.AddXAxisData("A");
|
||||
realTimeKeyCounterChart.AddXAxisData("D");
|
||||
realTimeKeyCounterChart.AddXAxisData("Pew");
|
||||
realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "W");
|
||||
realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "S");
|
||||
realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "A");
|
||||
realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "D");
|
||||
realTimeKeyCounterChart.AddData("RealTimeKeyCounterChart", 0, "Pew");
|
||||
}
|
||||
// reset EP by EP update Chart
|
||||
public void resetEPChart()
|
||||
{
|
||||
EPTotalRewardsChart.RemoveData();
|
||||
EPTotalRewardsChart.AddSerie<Line>("EPTotalRewardsChart");
|
||||
}
|
||||
|
||||
// Update------------
|
||||
// update setp by setp update Type Chart
|
||||
public void stepUpdateChart(int step, float reward)
|
||||
{
|
||||
if(step % updateStep == 0)
|
||||
{
|
||||
resetStepChart();
|
||||
}
|
||||
realTimeRewardChart.AddXAxisData(Convert.ToString(step));
|
||||
realTimeRewardChart.AddData(0,reward);
|
||||
}
|
||||
// update KeyCounter chart
|
||||
public void updateKeyCounterChart(int kWCount, int kSCount, int kACount, int kDCount,int shootCount)
|
||||
{
|
||||
realTimeKeyCounterChart.UpdateData(0, 0, kWCount);
|
||||
realTimeKeyCounterChart.UpdateData(0, 1, kSCount);
|
||||
realTimeKeyCounterChart.UpdateData(0, 2, kACount);
|
||||
realTimeKeyCounterChart.UpdateData(0, 3, kDCount);
|
||||
realTimeKeyCounterChart.UpdateData(0, 4, shootCount);
|
||||
}
|
||||
// update EP by EP update Type Chart
|
||||
public void epUpdateChart(int EP,float totalReward)
|
||||
{
|
||||
EPTotalRewardsChart.AddXAxisData(Convert.ToString(EP));
|
||||
EPTotalRewardsChart.AddData(0,totalReward);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 89d83d123322a5c4ba6afb3c86403371
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user