V3.4.1 整理script位置
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 860d67fa6b1173e4f8c8f84a289c7cfd
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,407 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class AgentController : MonoBehaviour
|
||||
{
|
||||
public GameObject parameterContainerObj;
|
||||
public GameObject environmentObj;
|
||||
public GameObject enemyContainerObj;
|
||||
public GameObject sceneBlockContainerObj;
|
||||
public GameObject environmentUIControlObj;
|
||||
public GameObject targetControllerObj;
|
||||
public GameObject HUDObj;
|
||||
public Camera fpsCam;
|
||||
|
||||
[Header("GetAxis() Simulate")]
|
||||
public float moveSpeed = 9.0f;
|
||||
|
||||
public float vX = 0f;
|
||||
public float vZ = 0f;
|
||||
public Vector3 nowMovement;
|
||||
public float acceleration = 0.9f; // 加速度
|
||||
public float mouseXSensitivity = 100;
|
||||
public float mouseYSensitivity = 200;
|
||||
public float yRotation = 0.1f;//定义一个浮点类型的量,记录‘围绕’X轴旋转的角度
|
||||
|
||||
private List<float> spinRecord = new List<float>();
|
||||
private bool lockMouse;
|
||||
private float damage;
|
||||
private float fireRate;
|
||||
private bool lockCameraX;
|
||||
private bool lockCameraY;
|
||||
|
||||
// environment
|
||||
private float lastShootTime = 0.0f;
|
||||
|
||||
private int enemyKillCount = 0;
|
||||
private Vector3 killEnemyPosition;
|
||||
public bool defaultTPCamera = true;
|
||||
|
||||
[System.NonSerialized] public 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;
|
||||
private ParameterContainer paramContainer;
|
||||
private SceneBlockContainer blockContainer;
|
||||
private TargetController targetCon;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// initialize scripts
|
||||
paramContainer = parameterContainerObj.GetComponent<ParameterContainer>();
|
||||
blockContainer = sceneBlockContainerObj.GetComponent<SceneBlockContainer>();
|
||||
targetCon = targetControllerObj.GetComponent<TargetController>();
|
||||
raySensors = GetComponent<RaySensors>();
|
||||
playerController = this.transform.GetComponent<CharacterController>();
|
||||
|
||||
// initialize Environment parameters
|
||||
lockMouse = paramContainer.lockMouse;
|
||||
damage = paramContainer.damage;
|
||||
fireRate = paramContainer.fireRate;
|
||||
lockCameraX = paramContainer.lockCameraX;
|
||||
lockCameraY = paramContainer.lockCameraY;
|
||||
|
||||
// initialize remainTime
|
||||
// this agent's tag
|
||||
myTag = gameObject.tag;
|
||||
}
|
||||
|
||||
#region Agent Move Control
|
||||
|
||||
public void MoveAgent(int vertical, int horizontal)
|
||||
{
|
||||
// Vector3 nowMovement;
|
||||
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;
|
||||
}
|
||||
}
|
||||
nowMovement = (transform.forward * vZ + transform.right * vX);
|
||||
//PlayerController下的.Move为实现物体运动的函数
|
||||
//Move()括号内放入一个Vector3类型的量,本例中为Player_Move
|
||||
if (nowMovement.magnitude > moveSpeed)
|
||||
{
|
||||
nowMovement = nowMovement.normalized * moveSpeed;
|
||||
}
|
||||
playerController.Move(nowMovement * Time.deltaTime);
|
||||
// update Key Viewer
|
||||
}
|
||||
|
||||
#endregion Agent Move Control
|
||||
|
||||
#region Camera Control
|
||||
|
||||
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轴为中心旋转的
|
||||
transform.Rotate(Vector3.up * Mouse_X);
|
||||
//Vector3.up相当于Vector3(0,1,0),CameraRotation.Rotate(Vector3.up * Mouse_X)相当于使CameraRotation对象绕y轴旋转Mouse_X个单位
|
||||
//即相机左右旋转时,是以Y轴为中心旋转的,此时Mouse_X控制着值的大小
|
||||
|
||||
//相机在上下旋转移动时,相机方向不会随着移动,类似于低头和抬头,左右移动时,相机方向会随着向左向右移动,类似于向左向右看
|
||||
//所以在控制相机向左向右旋转时,要保证和父物体一起转动
|
||||
fpsCam.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度
|
||||
//且绕轴旋转的坐标轴是父节点本地坐标系的坐标轴
|
||||
}
|
||||
|
||||
#endregion Camera Control
|
||||
|
||||
#region Reward Functions
|
||||
|
||||
// ballistic 射击弹道处理,并返回获得reward
|
||||
private float Ballistic(int shootState)
|
||||
{
|
||||
Vector3 point = new Vector3(fpsCam.pixelWidth / 2, fpsCam.pixelHeight / 2, 0);//发射位置
|
||||
Ray ray = fpsCam.ScreenPointToRay(point);
|
||||
RaycastHit hit;
|
||||
// Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue);
|
||||
//按下鼠标左键
|
||||
if (shootState != 0 && gunReadyToggle == true)
|
||||
{
|
||||
lastShootTime = Time.time;
|
||||
if (Physics.Raycast(ray, out hit, 100))
|
||||
{
|
||||
if (hit.collider.tag != myTag && hit.collider.tag != "Wall" && hit.collider.tag != "Untagged")
|
||||
{
|
||||
// kill enemy
|
||||
GameObject gotHitObj = hit.transform.gameObject;//获取受到Ray撞击的对象
|
||||
gotHitObj.GetComponent<States>().ReactToHit(damage, gameObject);
|
||||
shootState = 0;
|
||||
return targetCon.HitEnemyReward(gotHitObj.transform.position);
|
||||
}
|
||||
}
|
||||
if (targetCon.targetTypeInt == (int)Targets.Attack)
|
||||
{
|
||||
// while if attack mode
|
||||
float targetDis = Vector3.Distance(blockContainer.nowBlock.transform.position, 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.nowBlock.transform.position) <= blockContainer.nowBlock.firebasesAreaDiameter / 2)
|
||||
{
|
||||
// im shooting at target but didn't hit enemy
|
||||
// Debug.DrawRay(ray.origin, viewPoint-ray.origin, Color.blue);
|
||||
return paramContainer.shootTargetAreaReward;
|
||||
}
|
||||
}
|
||||
}
|
||||
shootState = 0;
|
||||
return paramContainer.shootReward;
|
||||
}
|
||||
else if (shootState != 0 && gunReadyToggle == false)
|
||||
{
|
||||
// shoot without ready
|
||||
shootState = 0;
|
||||
return paramContainer.shootWithoutReadyReward;
|
||||
}
|
||||
else
|
||||
{
|
||||
// do not shoot
|
||||
shootState = 0;
|
||||
return paramContainer.nonReward;
|
||||
}
|
||||
}
|
||||
|
||||
private float FacingReward()
|
||||
{
|
||||
float nowReward = 0;
|
||||
bool isFacingtoEnemy = false;
|
||||
float enemyFacingDistance = 0f;
|
||||
Ray ray = fpsCam.ScreenPointToRay(new Vector3(fpsCam.pixelWidth / 2, fpsCam.pixelHeight / 2, 0));
|
||||
if (targetCon.targetTypeInt == (int)Targets.Free)
|
||||
{
|
||||
//free mode
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(ray, out hit, 100))
|
||||
{
|
||||
// facing to an enemy
|
||||
if (hit.collider.tag != myTag && hit.collider.tag != "Wall")
|
||||
{
|
||||
nowReward = paramContainer.facingReward;
|
||||
isFacingtoEnemy = true;
|
||||
}
|
||||
}
|
||||
if (raySensors.inViewEnemies.Count > 0 && !isFacingtoEnemy)
|
||||
{
|
||||
// have enemy in view
|
||||
List<float> projectionDis = new List<float>();
|
||||
foreach (GameObject theEnemy in raySensors.inViewEnemies)
|
||||
{
|
||||
// for each enemy in view
|
||||
Vector3 projection = Vector3.Project(theEnemy.transform.position - transform.position, (ray.direction * 10));
|
||||
Vector3 verticalToRay = transform.position + projection - theEnemy.transform.position;
|
||||
projectionDis.Add(verticalToRay.magnitude);
|
||||
// Debug.Log("enemy!" + verticalToRay.magnitude);
|
||||
// Debug.DrawRay(transform.position, (ray.direction * 100), Color.cyan);
|
||||
// Debug.DrawRay(transform.position, theEnemy.transform.position - transform.position, Color.yellow);
|
||||
// Debug.DrawRay(transform.position, projection, Color.blue);
|
||||
// Debug.DrawRay(theEnemy.transform.position, verticalToRay, Color.magenta);
|
||||
}
|
||||
enemyFacingDistance = projectionDis.Min();
|
||||
if (enemyFacingDistance <= lastEnemyFacingDistance)
|
||||
{
|
||||
// closing to enemy
|
||||
nowReward = 1 / MathF.Sqrt(paramContainer.facingInviewEnemyDisCOEF * enemyFacingDistance + 0.00001f);
|
||||
}
|
||||
else
|
||||
{
|
||||
nowReward = 0;
|
||||
}
|
||||
// enemy in view Reward
|
||||
lastEnemyFacingDistance = enemyFacingDistance;
|
||||
if (nowReward >= paramContainer.facingReward) nowReward = paramContainer.facingReward; // limit
|
||||
if (nowReward <= -paramContainer.facingReward) nowReward = -paramContainer.facingReward; // limit
|
||||
// Debug.Log("ninimum = " + nowReward);
|
||||
}
|
||||
}
|
||||
else if (targetCon.targetTypeInt == (int)Targets.Attack)
|
||||
{
|
||||
// attack mode
|
||||
// Target to Agent distance
|
||||
float targetDis = Vector3.Distance(blockContainer.nowBlock.transform.position, transform.position);
|
||||
// center of screen between target's distance
|
||||
float camCenterToTarget = Vector3.Distance(ray.origin + (ray.direction * targetDis), blockContainer.nowBlock.transform.position);
|
||||
if (targetDis <= raySensors.viewDistance)
|
||||
{
|
||||
// Debug.DrawRay(new Vector3(0,0,0), viewPoint, Color.red);
|
||||
// while center of screen between target's distance is lower than firebasesAreaDiameter
|
||||
// while facing to target
|
||||
if (camCenterToTarget <= blockContainer.nowBlock.firebasesAreaDiameter / 2)
|
||||
{
|
||||
// Debug.DrawRay(ray.origin, viewPoint-ray.origin, Color.blue);
|
||||
|
||||
nowReward = paramContainer.facingReward;
|
||||
}
|
||||
else
|
||||
{
|
||||
// while not facing to target
|
||||
nowReward = (lastTargetFacingDistance - camCenterToTarget) * paramContainer.facingTargetReward;
|
||||
}
|
||||
}
|
||||
// update lastTargetFacingDistance
|
||||
lastTargetFacingDistance = camCenterToTarget;
|
||||
}
|
||||
return nowReward;
|
||||
}
|
||||
|
||||
public float RewardCalculate(float sceneReward, float mouseX, float movement, int shootState)
|
||||
{
|
||||
float epreward = 0f;
|
||||
// 击杀reward判断
|
||||
if (enemyKillCount > 0)
|
||||
{
|
||||
for (int i = 0; i < enemyKillCount; i++)
|
||||
{
|
||||
// get
|
||||
epreward += targetCon.KillReward(killEnemyPosition);
|
||||
}
|
||||
enemyKillCount = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
enemyKillCount = 0;
|
||||
}
|
||||
// 射击动作reward判断
|
||||
epreward += Ballistic(shootState) + sceneReward;
|
||||
// facing reward
|
||||
epreward += FacingReward();
|
||||
// Penalty
|
||||
// spin penalty
|
||||
spinRecord.Add(mouseX);
|
||||
if (spinRecord.Count >= paramContainer.spinRecordMax)
|
||||
{
|
||||
spinRecord.RemoveAt(0);
|
||||
}
|
||||
float spinPenaltyReward = Math.Abs(spinRecord.ToArray().Sum() * paramContainer.spinPenalty);
|
||||
if (spinPenaltyReward >= paramContainer.spinPenaltyThreshold)
|
||||
{
|
||||
epreward -= spinPenaltyReward;
|
||||
}
|
||||
else
|
||||
{
|
||||
epreward -= Math.Abs(mouseX) * paramContainer.mousePenalty;
|
||||
}
|
||||
// move penalty
|
||||
if (movement != 0)
|
||||
{
|
||||
epreward -= paramContainer.movePenalty;
|
||||
}
|
||||
return epreward;
|
||||
}
|
||||
|
||||
#endregion Reward Functions
|
||||
|
||||
// GotKill 获得击杀时用于被呼出
|
||||
public void KillRecord(Vector3 killEnemyPosition)
|
||||
{
|
||||
enemyKillCount += 1;
|
||||
this.killEnemyPosition = killEnemyPosition;
|
||||
}
|
||||
|
||||
public void UpdateLockMouse()
|
||||
{
|
||||
// lock mouse based on paramContainer lockMouse
|
||||
if (lockMouse)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateGunState()
|
||||
{
|
||||
// update gun state
|
||||
if ((Time.time - lastShootTime) >= fireRate)
|
||||
{
|
||||
gunReadyToggle = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
gunReadyToggle = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a899ce3efe4b8384a8cc2e43cc761b71
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,82 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Enemy : MonoBehaviour
|
||||
{
|
||||
//扇形角度
|
||||
//[SerializeField] private float angle = 80f;
|
||||
//扇形半径
|
||||
//[SerializeField] private float radius = 3.5f;
|
||||
//物体B
|
||||
[SerializeField] private Transform b;
|
||||
|
||||
private bool flag;
|
||||
/// <summary>
|
||||
/// 判断target是否在扇形区域内
|
||||
/// </summary>
|
||||
/// <param name="sectorAngle">扇形角度</param>
|
||||
/// <param name="sectorRadius">扇形半径</param>
|
||||
/// <param name="attacker">攻击者的transform信息</param>
|
||||
/// <param name="target">目标</param>
|
||||
/// <returns>目标target在扇形区域内返回true 否则返回false</returns>
|
||||
|
||||
private void Start()
|
||||
{
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
//detactDeath();
|
||||
//flag = IsInRange(angle, radius, transform, b);
|
||||
}
|
||||
|
||||
public bool IsInRange(float sectorAngle, float sectorRadius, Transform attacker, Transform target)
|
||||
{
|
||||
//攻击者位置指向目标位置的向量
|
||||
Vector3 direction = target.position - attacker.position;
|
||||
//点乘积结果
|
||||
float dot = Vector3.Dot(direction.normalized, transform.forward);
|
||||
//反余弦计算角度
|
||||
float offsetAngle = Mathf.Acos(dot) * Mathf.Rad2Deg;
|
||||
return offsetAngle < sectorAngle * .5f && direction.magnitude < sectorRadius;
|
||||
}
|
||||
|
||||
/*
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Handles.color = flag ? Color.cyan : Color.red;
|
||||
|
||||
float x = radius * Mathf.Sin(angle / 2f * Mathf.Deg2Rad);
|
||||
float y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
|
||||
Vector3 a = new Vector3(transform.position.x - x, transform.position.y, transform.position.z + y);
|
||||
Vector3 b = new Vector3(transform.position.x + x, transform.position.y, transform.position.z + y);
|
||||
|
||||
Handles.DrawLine(transform.position, a);
|
||||
Handles.DrawLine(transform.position, b);
|
||||
|
||||
float half = angle / 2;
|
||||
for (int i = 0; i < half; i++)
|
||||
{
|
||||
x = radius * Mathf.Sin((half - i) * Mathf.Deg2Rad);
|
||||
y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
|
||||
a = new Vector3(transform.position.x - x, transform.position.y, transform.position.z + y);
|
||||
x = radius * Mathf.Sin((half - i - 1) * Mathf.Deg2Rad);
|
||||
y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
|
||||
b = new Vector3(transform.position.x - x, transform.position.y, transform.position.z + y);
|
||||
|
||||
Handles.DrawLine(a, b);
|
||||
}
|
||||
for (int i = 0; i < half; i++)
|
||||
{
|
||||
x = radius * Mathf.Sin((half - i) * Mathf.Deg2Rad);
|
||||
y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
|
||||
a = new Vector3(transform.position.x + x, transform.position.y, transform.position.z + y);
|
||||
x = radius * Mathf.Sin((half - i - 1) * Mathf.Deg2Rad);
|
||||
y = Mathf.Sqrt(Mathf.Pow(radius, 2f) - Mathf.Pow(x, 2f));
|
||||
b = new Vector3(transform.position.x + x, transform.position.y, transform.position.z + y);
|
||||
|
||||
Handles.DrawLine(a, b);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8cded1019e262a04e8d9ccb536d1ff20
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,65 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyContainer : MonoBehaviour
|
||||
{
|
||||
public GameObject enemyPrefab;
|
||||
public GameObject environmentObj;
|
||||
public GameObject targetControllerObj;
|
||||
|
||||
private TargetController targetCon;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
targetCon = targetControllerObj.GetComponent<TargetController>();
|
||||
}
|
||||
|
||||
// initialize enemy by random
|
||||
public void RandomInitEnemys(int EnemyNum)
|
||||
{
|
||||
Debug.Log("RandomInitEnemys");
|
||||
for (int i = 0; i < EnemyNum; i++)
|
||||
{
|
||||
float randX = UnityEngine.Random.Range(targetCon.minEnemyAreaX, targetCon.maxEnemyAreaX);
|
||||
float randZ = UnityEngine.Random.Range(targetCon.minEnemyAreaZ, targetCon.maxEnemyAreaZ);
|
||||
int enemyY = 1;
|
||||
InitEnemyAtHere(new Vector3(randX, enemyY, randZ));
|
||||
}
|
||||
}
|
||||
|
||||
// initialize enemy by random but not in block area
|
||||
public void RandomInitEnemysExcept(int enemyNum, Vector3 blockPosition, float sceneSize)
|
||||
{
|
||||
float randX = 0f;
|
||||
float randZ = 0f;
|
||||
for (int i = 0; i < enemyNum; i++)
|
||||
{
|
||||
randX = UnityEngine.Random.Range(targetCon.minEnemyAreaX, targetCon.maxEnemyAreaX);
|
||||
randZ = UnityEngine.Random.Range(targetCon.minEnemyAreaZ, targetCon.maxEnemyAreaZ);
|
||||
while (Vector3.Distance(blockPosition, new Vector3(randX, 0f, randZ)) < sceneSize / 2)
|
||||
{
|
||||
// while in scene area then respawn
|
||||
Debug.Log("spawn enemy in area, re:roll");
|
||||
randX = UnityEngine.Random.Range(targetCon.minEnemyAreaX, targetCon.maxEnemyAreaX);
|
||||
randZ = UnityEngine.Random.Range(targetCon.minEnemyAreaZ, targetCon.maxEnemyAreaZ);
|
||||
}
|
||||
|
||||
int enemyY = 1;
|
||||
InitEnemyAtHere(new Vector3(randX, enemyY, randZ));
|
||||
}
|
||||
}
|
||||
|
||||
// initialize enemy to position
|
||||
public void InitEnemyAtHere(Vector3 position)
|
||||
{
|
||||
Instantiate(enemyPrefab, position + environmentObj.transform.position, Quaternion.identity, this.transform);
|
||||
}
|
||||
|
||||
// destroyEnemy delete enemyContainer's all enemy
|
||||
public void DestroyAllEnemys()
|
||||
{
|
||||
foreach (Transform childObj in this.transform)
|
||||
{
|
||||
childObj.GetComponent<States>().DestroyMe();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0350ec966da42b44699fee7309a89ac7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class HPBar : MonoBehaviour
|
||||
{
|
||||
private GameObject thisObj;
|
||||
private GameObject backgroundObj;
|
||||
private GameObject gaugeImgOBJ;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
thisObj = transform.parent.gameObject;
|
||||
backgroundObj = transform.GetChild(0).gameObject;
|
||||
gaugeImgOBJ = backgroundObj.transform.GetChild(0).gameObject;
|
||||
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);
|
||||
}
|
||||
|
||||
private 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 = thisObj.GetComponent<States>().maxHP;
|
||||
float nowHP = thisObj.GetComponent<States>().GetnowHP();
|
||||
gaugeImgOBJ.GetComponent<Image>().fillAmount = nowHP / maxHP;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 925561829acf6c94097de75bf186b561
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,272 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.MLAgents;
|
||||
using Unity.MLAgents.Actuators;
|
||||
using Unity.MLAgents.Sensors;
|
||||
using UnityEngine;
|
||||
|
||||
public class MLAgentsCustomController : Agent
|
||||
{
|
||||
public GameObject paramContainerObj;
|
||||
public GameObject targetControllerObj;
|
||||
public GameObject environmentUIObj;
|
||||
public GameObject sideChannelObj;
|
||||
public GameObject hudUIObj;
|
||||
|
||||
[Header("Env")]
|
||||
public bool oneHotRayTag = true;
|
||||
|
||||
// script
|
||||
private AgentController agentController;
|
||||
|
||||
private ParameterContainer paramContainer;
|
||||
private TargetController targetController;
|
||||
private EnvironmentUIControl envUIController;
|
||||
private HUDController hudController;
|
||||
private TargetUIController targetUIController;
|
||||
private RaySensors raySensors;
|
||||
private MessageBoxController messageBoxController;
|
||||
private AimBotSideChannelController sideChannelController;
|
||||
|
||||
// observation
|
||||
private float[] myObserve = new float[4];
|
||||
|
||||
private float[] rayTagResult;
|
||||
private float[] rayTagResultOnehot;
|
||||
private float[] rayDisResult;
|
||||
private float[] targetStates;
|
||||
private float remainTime;
|
||||
private float inAreaState;
|
||||
|
||||
private int finishedState;
|
||||
private int step = 0;
|
||||
private int EP = 0;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
agentController = transform.GetComponent<AgentController>();
|
||||
raySensors = transform.GetComponent<RaySensors>();
|
||||
paramContainer = paramContainerObj.GetComponent<ParameterContainer>();
|
||||
targetController = targetControllerObj.GetComponent<TargetController>();
|
||||
envUIController = environmentUIObj.GetComponent<EnvironmentUIControl>();
|
||||
hudController = hudUIObj.GetComponent<HUDController>();
|
||||
targetUIController = hudUIObj.GetComponent<TargetUIController>();
|
||||
messageBoxController = hudUIObj.GetComponent<MessageBoxController>();
|
||||
sideChannelController = sideChannelObj.GetComponent<AimBotSideChannelController>();
|
||||
}
|
||||
|
||||
#region On episode begin function
|
||||
|
||||
public override void OnEpisodeBegin()
|
||||
{
|
||||
step = 0;
|
||||
agentController.UpdateLockMouse();
|
||||
paramContainer.ResetTimeBonusReward();
|
||||
if (paramContainer.gameMode == 0)
|
||||
{
|
||||
// train mode
|
||||
Debug.Log("MLAgentCustomController.OnEpisodeBegin: train mode start");
|
||||
targetController.RollNewScene();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("MLAgentCustomController.OnEpisodeBegin: play mode start");
|
||||
// play mode
|
||||
targetController.PlayInitialize();
|
||||
// reset target UI
|
||||
targetUIController.ClearGamePressed();
|
||||
}
|
||||
|
||||
// give default Reward to Reward value will be used.
|
||||
if (hudController.chartOn)
|
||||
{
|
||||
envUIController.InitChart();
|
||||
}
|
||||
raySensors.UpdateRayInfo(); // update raycast
|
||||
}
|
||||
|
||||
#endregion On episode begin function
|
||||
|
||||
#region Observation sensor function
|
||||
|
||||
public override void CollectObservations(VectorSensor sensor)
|
||||
{
|
||||
//List<float> enemyLDisList = RaySensors.enemyLDisList;// All Enemy Lside Distances
|
||||
//List<float> enemyRDisList = RaySensors.enemyRDisList;// All Enemy Rside Distances
|
||||
/**myObserve[0] = transform.localPosition.x / raySensors.viewDistance;
|
||||
myObserve[1] = transform.localPosition.y / raySensors.viewDistance;
|
||||
myObserve[2] = transform.localPosition.z / raySensors.viewDistance;
|
||||
myObserve[3] = transform.eulerAngles.y / 360f;**/
|
||||
myObserve[0] = transform.localPosition.x;
|
||||
myObserve[1] = transform.localPosition.y;
|
||||
myObserve[2] = transform.localPosition.z;
|
||||
myObserve[3] = transform.eulerAngles.y / 36f;
|
||||
rayTagResult = raySensors.rayTagResult;// 探测用RayTag结果 float[](raySensorNum,1)
|
||||
rayTagResultOnehot = raySensors.rayTagResultOneHot.ToArray(); // 探测用RayTagonehot结果 List<int>[](raySensorNum*Tags,1)
|
||||
rayDisResult = raySensors.rayDisResult; // 探测用RayDis结果 float[](raySensorNum,1)
|
||||
targetStates = targetController.targetState; // (6) targettype, target x,y,z, firebasesAreaDiameter
|
||||
remainTime = targetController.leftTime;
|
||||
inAreaState = targetController.GetInAreaState();
|
||||
agentController.UpdateGunState();
|
||||
//float[] focusEnemyObserve = RaySensors.focusEnemyInfo;// 最近的Enemy情报 float[](3,1) MinEnemyIndex,x,z
|
||||
|
||||
//sensor.AddObservation(allEnemyNum); // 敌人数量 int
|
||||
sensor.AddObservation(targetStates);// (6) targettype, target x,y,z, firebasesAreaDiameter
|
||||
sensor.AddObservation(inAreaState); // (1)
|
||||
sensor.AddObservation(remainTime); // (1)
|
||||
sensor.AddObservation(agentController.gunReadyToggle); // (1) save gun is ready?
|
||||
sensor.AddObservation(myObserve); // (4)自机位置xyz+朝向 float[](4,1)
|
||||
if (oneHotRayTag)
|
||||
{
|
||||
sensor.AddObservation(rayTagResultOnehot); // 探测用RayTag结果 float[](raySensorNum,1)
|
||||
}
|
||||
else
|
||||
{
|
||||
sensor.AddObservation(rayTagResult);
|
||||
}
|
||||
sensor.AddObservation(rayDisResult); // 探测用RayDis结果 float[](raySensorNum,1)
|
||||
envUIController.UpdateStateText(targetStates, inAreaState, remainTime, agentController.gunReadyToggle, myObserve, rayTagResultOnehot, rayDisResult);
|
||||
/*foreach(float aaa in rayDisResult)
|
||||
{
|
||||
Debug.Log(aaa);
|
||||
}
|
||||
Debug.LogWarning("------------");*/
|
||||
//sensor.AddObservation(focusEnemyObserve); // 最近的Enemy情报 float[](3,1) MinEnemyIndex,x,z
|
||||
//sensor.AddObservation(raySensorNum); // raySensor数量 int
|
||||
//sensor.AddObservation(remainTime); // RemainTime int
|
||||
}
|
||||
|
||||
#endregion Observation sensor function
|
||||
|
||||
#region Action received function
|
||||
|
||||
public override void OnActionReceived(ActionBuffers actionBuffers)
|
||||
{
|
||||
//获取输入
|
||||
int vertical = actionBuffers.DiscreteActions[0];
|
||||
int horizontal = actionBuffers.DiscreteActions[1];
|
||||
int mouseShoot = actionBuffers.DiscreteActions[2];
|
||||
float Mouse_X = actionBuffers.ContinuousActions[0];
|
||||
if (vertical == 2) vertical = -1;
|
||||
if (horizontal == 2) horizontal = -1;
|
||||
|
||||
//应用输入
|
||||
agentController.CameraControl(Mouse_X, 0);
|
||||
agentController.MoveAgent(vertical, horizontal);
|
||||
raySensors.UpdateRayInfo(); // update raycast
|
||||
|
||||
//判断结束
|
||||
float sceneReward = 0f;
|
||||
float endReward = 0f;
|
||||
(finishedState, sceneReward, endReward) = targetController.CheckOverAndRewards();
|
||||
float nowRoundReward = agentController.RewardCalculate(sceneReward + endReward, Mouse_X, Math.Abs(vertical) + Math.Abs(horizontal), mouseShoot);
|
||||
if (hudController.chartOn)
|
||||
{
|
||||
envUIController.UpdateChart(nowRoundReward);
|
||||
}
|
||||
else
|
||||
{
|
||||
envUIController.RemoveChart();
|
||||
}
|
||||
//Debug.Log("reward = " + nowRoundReward);
|
||||
if (finishedState != (int)TargetController.EndType.Running)
|
||||
{
|
||||
// Win or lose Finished
|
||||
Debug.Log("Finish reward = " + nowRoundReward);
|
||||
EP += 1;
|
||||
string targetString = Enum.GetName(typeof(Targets), targetController.targetTypeInt);
|
||||
switch (finishedState)
|
||||
{
|
||||
case (int)TargetController.EndType.Win:
|
||||
sideChannelController.SendSideChannelMessage("Result",targetString+ "|Win");
|
||||
messageBoxController.PushMessage(
|
||||
new List<string> { "Game Win" },
|
||||
new List<string> { "green" });
|
||||
break;
|
||||
|
||||
case (int)TargetController.EndType.Lose:
|
||||
sideChannelController.SendSideChannelMessage("Result", targetString + "|Lose");
|
||||
messageBoxController.PushMessage(
|
||||
new List<string> { "Game Lose" },
|
||||
new List<string> { "red" });
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.LogWarning("TypeError");
|
||||
break;
|
||||
}
|
||||
SetReward(nowRoundReward);
|
||||
EndEpisode();
|
||||
}
|
||||
else
|
||||
{
|
||||
// game not over yet
|
||||
step += 1;
|
||||
}
|
||||
SetReward(nowRoundReward);
|
||||
}
|
||||
|
||||
#endregion Action received function
|
||||
|
||||
#region Heuristic function
|
||||
|
||||
public override void Heuristic(in ActionBuffers actionsOut)
|
||||
{
|
||||
//-------------------BUILD
|
||||
ActionSegment<float> continuousActions = actionsOut.ContinuousActions;
|
||||
ActionSegment<int> discreteActions = actionsOut.DiscreteActions;
|
||||
|
||||
if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
|
||||
{
|
||||
discreteActions[0] = 1;
|
||||
}
|
||||
else if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W))
|
||||
{
|
||||
discreteActions[0] = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
discreteActions[0] = 0;
|
||||
}
|
||||
if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
|
||||
{
|
||||
discreteActions[1] = 1;
|
||||
}
|
||||
else if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
|
||||
{
|
||||
discreteActions[1] = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
discreteActions[1] = 0;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
// Debug.Log("mousebuttonhit");
|
||||
discreteActions[2] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
discreteActions[2] = 0;
|
||||
}
|
||||
//^^^^^^^^^^^^^^^^^^^^^discrete-Control^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvcontinuous-Controlvvvvvvvvvvvvvvvvvvvvvv
|
||||
float Mouse_X = Input.GetAxis("Mouse X") * agentController.mouseXSensitivity * Time.deltaTime;
|
||||
//float Mouse_Y = Input.GetAxis("Mouse Y") * agentController.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^^^^^^^^^^^^^^^^^^^^^^
|
||||
}
|
||||
|
||||
#endregion Heuristic function
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 427cc67643922b245b572721bea0462d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Onehot
|
||||
{
|
||||
private List<string> tags = new List<string>();
|
||||
public List<List<float>> onehot = new List<List<float>>();
|
||||
private float totalNum;
|
||||
|
||||
public void Initialize(List<string> inputTags)
|
||||
{
|
||||
tags = inputTags;
|
||||
totalNum = tags.Count;
|
||||
for (int i = 0; i < totalNum; i++)
|
||||
{
|
||||
List<float> oneHot = new List<float>();
|
||||
for (int j = 0; j < totalNum; j++) oneHot.Add(0f);
|
||||
oneHot[i] = 1f;
|
||||
onehot.Add(oneHot);
|
||||
}
|
||||
}
|
||||
|
||||
public List<float> Encoder(string name = null)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
List<float> allZeroOnehot = new List<float>();
|
||||
for (int j = 0; j < totalNum; j++) allZeroOnehot.Add(0);
|
||||
return allZeroOnehot;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
return onehot[tags.IndexOf(name)];
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
List<float> allZeroOnehot = new List<float>();
|
||||
for (int j = 0; j < totalNum; j++) allZeroOnehot.Add(0);
|
||||
return allZeroOnehot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Decoder(List<float> oneHot)
|
||||
{
|
||||
return tags[onehot.IndexOf(oneHot)];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cdb75d03525930d4caaefab4eaaf6e8a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,226 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ParameterContainer : MonoBehaviour
|
||||
{
|
||||
public GameObject targetConObj;
|
||||
public GameObject blockConObj;
|
||||
public GameObject agentObj;
|
||||
public GameObject hudObj;
|
||||
private TargetController targetCon;
|
||||
private SceneBlockContainer blockCont;
|
||||
private StartSeneData startSceneData;
|
||||
private MessageBoxController messageCon;
|
||||
private float agentDistance;
|
||||
private int agentInArea;
|
||||
|
||||
[Header("Env")]
|
||||
public bool lockMouse = false;
|
||||
|
||||
public float damage = 50; // damage to enemy
|
||||
public float fireRate = 0.5f;
|
||||
public int timeLimit = 30;
|
||||
public bool lockCameraX = false;
|
||||
public bool lockCameraY = true;
|
||||
public bool spawnAgentInAllMap = true;
|
||||
public int spinRecordMax = 40;
|
||||
public float spinPenaltyThreshold = 10;
|
||||
public float facingInviewEnemyDisCOEF = 0.5f;
|
||||
|
||||
[Header("Dynamic Defaut Rewards")]
|
||||
//[Tooltip("Hit Enemy reward")]
|
||||
//public float hitRewardDefault = 60.0f;
|
||||
[Tooltip("Free mode Hit Enemy reward")]
|
||||
public float hitTargetRewardDefault = 25f;
|
||||
|
||||
//[Tooltip("Enemy down reward")]
|
||||
//public float killRewardDefault = 60.0f;
|
||||
[Tooltip("Enemy down in area Reward")]
|
||||
public float killTargetEnemyRewardDefault = 25f;
|
||||
|
||||
[Tooltip("stay in firebasesArea reward")]
|
||||
public float inAreaRewardDefault = 12f;
|
||||
|
||||
[Tooltip("free left time bonus reward. ALLR + leftTime * r")]
|
||||
public float freeTimeBonusPerSec = 1.0f;
|
||||
|
||||
[Tooltip("target left time bonus reward. ALLR + leftTime * r")]
|
||||
public float targetTimeBonusPerSec = 0.5f;
|
||||
|
||||
[Tooltip("in area left time bonus reward. ALLR + leftTime * r")]
|
||||
public float areaTimeBonusPerSec = 0.2f;
|
||||
|
||||
[Tooltip("distance reward reward = r*(1-(nowDis/startDis))")]
|
||||
public float distanceReward = 50.0f;
|
||||
|
||||
[Tooltip("facing to Target distance reward reward = r*(1-(nowDis/startDis))")]
|
||||
public float facingTargetReward = 10.0f;
|
||||
|
||||
[Space(10)]
|
||||
[Tooltip("Goto Win reward")]
|
||||
public float goWinRewardDefault = 999f;
|
||||
|
||||
[Tooltip("Attack Win reward")]
|
||||
public float attackWinRewardDefault = 999f;
|
||||
|
||||
[Tooltip("Defence Win reward")]
|
||||
public float defenceWinRewardDefault = 999f;
|
||||
|
||||
[Tooltip("free Win reward")]
|
||||
public float freeWinRewardDefault = 999f;
|
||||
|
||||
[Header("Static Rewards")]
|
||||
[Tooltip("Nothing happened reward")]
|
||||
public float nonReward = -1f;
|
||||
|
||||
[Tooltip("Episode Lose reward")]
|
||||
public float loseReward = -999f;
|
||||
|
||||
[Tooltip("Agent Do shoot action reward")]
|
||||
public float shootReward = -0.5f;
|
||||
|
||||
[Tooltip("Hit Not target Enemy reward")]
|
||||
public float hitNonTargetReward = -5f;
|
||||
|
||||
[Tooltip("Not Target Enemy down reward")]
|
||||
public float killNonTargetReward = -5f;
|
||||
|
||||
[Tooltip("Agent Do shoot action but gun is not read")]
|
||||
public float shootWithoutReadyReward = -1.15f;
|
||||
|
||||
[Tooltip("Kill bonus reward stack to nothing happend reward")]
|
||||
public float killBonusReward = 0.0f;
|
||||
|
||||
[Tooltip("Facing to enemy's reward")]
|
||||
public float facingReward = 5f;
|
||||
|
||||
[Tooltip("Shoot at target area but didn't hit enemy")]
|
||||
public float shootTargetAreaReward = 10f;
|
||||
|
||||
[Header("Penalty Rewards")]
|
||||
[Tooltip("move Penalty Reward")]
|
||||
public float movePenalty = 0.1f;
|
||||
|
||||
[Tooltip("spiiiiiiin Panalty Reward")]
|
||||
public float spinPenalty = 0.08f;
|
||||
|
||||
[Tooltip("while move mouse a little bit's penalty")]
|
||||
public float mousePenalty = 0.06f;
|
||||
|
||||
[Header("Dynamic Rewards")]
|
||||
[Tooltip("Free mode Hit Enemy reward")]
|
||||
public float hitTargetReward = 60.0f;
|
||||
|
||||
[Tooltip("Enemy down in area Reward")]
|
||||
public float killTargetEnemyReward = 80.0f;
|
||||
|
||||
[Tooltip("stay in firebasesArea reward")]
|
||||
public float inAreaReward = 5.0f;
|
||||
|
||||
[Space(10)]
|
||||
[Tooltip("go Win reward")]
|
||||
public float goWinReward = 50.0f;
|
||||
|
||||
[Tooltip("attack Win reward")]
|
||||
public float attackWinReward = 50.0f;
|
||||
|
||||
[Tooltip("defence Win reward")]
|
||||
public float defenceWinReward = 50.0f;
|
||||
|
||||
[Tooltip("free Win reward")]
|
||||
public float freeWinReward = 50.0f;
|
||||
|
||||
[Tooltip("Scene Prefab Set")]
|
||||
public SceneBlocksSet scenePrefabSet;
|
||||
|
||||
private float targetTimeBonus = 0f;
|
||||
private float areaTimeBonus = 0f;
|
||||
private float freeTimeBonus = 0f;
|
||||
private float targetInAreaTime = 0f;
|
||||
private float lastFrameTime = 0f;
|
||||
|
||||
[System.NonSerialized] public int gameMode; // 0 = trainning mode, 1 = play mode
|
||||
[System.NonSerialized] public float attackProb = 0f;
|
||||
[System.NonSerialized] public List<float> attackLevelProbs = new List<float>();
|
||||
[System.NonSerialized] public float gotoProb = 0f;
|
||||
[System.NonSerialized] public List<float> gotoLevelProbs = new List<float>();
|
||||
[System.NonSerialized] public float defenceProb = 0f;
|
||||
[System.NonSerialized] public List<float> defenceLevelProbs = new List<float>();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
targetCon = targetConObj.GetComponent<TargetController>();
|
||||
blockCont = blockConObj.GetComponent<SceneBlockContainer>();
|
||||
messageCon = hudObj.GetComponent<MessageBoxController>();
|
||||
areaTimeBonus = areaTimeBonusPerSec * timeLimit;
|
||||
freeTimeBonus = freeTimeBonusPerSec * timeLimit;
|
||||
targetTimeBonus = targetTimeBonusPerSec * timeLimit;
|
||||
try
|
||||
{
|
||||
// try get start scene data
|
||||
startSceneData = GameObject.Find("StartSceneDataTransfer").GetComponent<StartSeneData>();
|
||||
messageCon.PushMessage(
|
||||
new List<string> { "ParameterContainer:", "StartSceneDataTransfer found!" },
|
||||
new List<string> { "green", "white" });
|
||||
}
|
||||
catch
|
||||
{
|
||||
// if not found, find dummy StartSeneData
|
||||
startSceneData = GameObject.Find("StartSceneDataTransferDummy").GetComponent<StartSeneData>();
|
||||
messageCon.PushMessage(
|
||||
new List<string> { "ParameterContainer:", "StartSceneDataTransfer not found!Use Dummy." },
|
||||
new List<string> { "orange" });
|
||||
}
|
||||
gameMode = startSceneData.gameMode;
|
||||
attackProb = startSceneData.attackProb;
|
||||
attackLevelProbs = startSceneData.attackLevelProbs;
|
||||
gotoProb = startSceneData.gotoProb;
|
||||
gotoLevelProbs = startSceneData.gotoLevelProbs;
|
||||
defenceProb = startSceneData.defenceProb;
|
||||
defenceLevelProbs = startSceneData.defenceLevelProbs;
|
||||
scenePrefabSet = startSceneData.scenePrefabSet;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// get target distance and in area
|
||||
if (targetCon.targetTypeInt is (int)Targets.Go or (int)Targets.Attack)
|
||||
{
|
||||
(agentDistance, agentInArea) = blockCont.GetAgentTargetDistanceAndInside(agentObj.transform.position);
|
||||
// attack goto or defence target
|
||||
if (agentInArea == 1)
|
||||
{
|
||||
// agent out of area
|
||||
targetInAreaTime += Time.time - lastFrameTime;
|
||||
}
|
||||
areaTimeBonus = areaTimeBonusPerSec * (targetCon.leftTime + targetInAreaTime);
|
||||
freeTimeBonus = freeTimeBonusPerSec * targetCon.leftTime;
|
||||
targetTimeBonus = targetTimeBonusPerSec * targetCon.leftTime;
|
||||
lastFrameTime = Time.time;
|
||||
}
|
||||
else
|
||||
{
|
||||
// free target
|
||||
areaTimeBonus = areaTimeBonusPerSec * targetCon.leftTime;
|
||||
freeTimeBonus = freeTimeBonusPerSec * targetCon.leftTime;
|
||||
targetTimeBonus = targetTimeBonusPerSec * targetCon.leftTime;
|
||||
}
|
||||
|
||||
hitTargetReward = hitTargetRewardDefault + targetTimeBonus;
|
||||
killTargetEnemyReward = killTargetEnemyRewardDefault + targetTimeBonus;
|
||||
inAreaReward = inAreaRewardDefault + areaTimeBonus;
|
||||
|
||||
// Win Rewards
|
||||
goWinReward = goWinRewardDefault;
|
||||
attackWinReward = attackWinRewardDefault;
|
||||
defenceWinReward = defenceWinRewardDefault;
|
||||
freeWinReward = freeWinRewardDefault;
|
||||
}
|
||||
|
||||
public void ResetTimeBonusReward()
|
||||
{
|
||||
areaTimeBonus = areaTimeBonusPerSec * timeLimit;
|
||||
freeTimeBonus = freeTimeBonusPerSec * timeLimit;
|
||||
targetInAreaTime = 0f;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cab5ee46e8af1134b840e91077e7592b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: -1100
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,200 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/*该scrip用于创建复数条ray于视角内,并探测被ray射到的物体*/
|
||||
|
||||
public class RaySensors : MonoBehaviour
|
||||
{
|
||||
public Camera agentCam;
|
||||
public Camera TPSCam;
|
||||
public Material lineMeterial;
|
||||
public GameObject rayInfoPrefab;
|
||||
public GameObject agentCanvas;
|
||||
|
||||
[SerializeField, Range(0, 500)] public float viewDistance = 100; // how long the ray can detect
|
||||
|
||||
//[SerializeField, Range(0, 1)] public float totalRange = 1f; // Total view range Max = 1
|
||||
[SerializeField, Range(0, 1)] public float focusRange = 0.15f; // center focus range
|
||||
|
||||
public int halfOuterRayNum = 7; // >=2
|
||||
public int focusRayNum = 5; // >= 1 and must be odd num!
|
||||
|
||||
[Header("InGameLineSetting")]
|
||||
public bool showInGameRay = true;
|
||||
|
||||
public bool showDebugRay = true;
|
||||
public bool showInGameRayInfo = true;
|
||||
public float lineWidth = 0.05f;
|
||||
|
||||
[Header("RayCastResult")]
|
||||
public float[] rayTagResult;
|
||||
|
||||
public List<float> rayTagResultOneHot;
|
||||
public float[] rayDisResult;
|
||||
|
||||
[System.NonSerialized] public int totalRayNum;
|
||||
private string myTag = "";
|
||||
private GameObject[] linesOBJ;
|
||||
private GameObject[] rayInfoOBJ;
|
||||
private LineRenderer[] lineRenderers;
|
||||
private RayInfoUI[] rayInfoUIs;
|
||||
public List<GameObject> inViewEnemies = new List<GameObject>();
|
||||
private List<string> tags = new List<string> { "Wall", "Enemy" };
|
||||
private Onehot oneHotTags = new Onehot();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
myTag = gameObject.tag;
|
||||
totalRayNum = halfOuterRayNum * 2 + focusRayNum;
|
||||
rayTagResult = new float[totalRayNum];
|
||||
rayDisResult = new float[totalRayNum];
|
||||
linesOBJ = new GameObject[totalRayNum];
|
||||
lineRenderers = new LineRenderer[totalRayNum];
|
||||
rayInfoOBJ = new GameObject[totalRayNum];
|
||||
rayInfoUIs = new RayInfoUI[totalRayNum];
|
||||
oneHotTags.Initialize(tags);
|
||||
for (int i = 0; i < totalRayNum; i++)
|
||||
{
|
||||
linesOBJ[i] = new GameObject();
|
||||
linesOBJ[i].name = "rayCastLine-" + Convert.ToString(i);
|
||||
linesOBJ[i].transform.parent = agentCam.transform;
|
||||
linesOBJ[i].AddComponent<LineRenderer>();
|
||||
lineRenderers[i] = linesOBJ[i].GetComponent<LineRenderer>();
|
||||
lineRenderers[i].material = lineMeterial;
|
||||
|
||||
rayInfoOBJ[i] = (GameObject)Instantiate(rayInfoPrefab);
|
||||
rayInfoOBJ[i].transform.SetParent(agentCanvas.transform, false);
|
||||
rayInfoOBJ[i].name = "rayInfo-" + Convert.ToString(i);
|
||||
rayInfoUIs[i] = rayInfoOBJ[i].GetComponent<RayInfoUI>();
|
||||
}
|
||||
}
|
||||
|
||||
public int TagToInt(string tag)
|
||||
{
|
||||
switch (tag)
|
||||
{
|
||||
case "Wall":
|
||||
return 1;
|
||||
|
||||
default:
|
||||
if (tag != myTag)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void SingleRaycastUpdate(Ray ray, LineRenderer lineRenderer, RayInfoUI rayInfoUI, out float rayTagResult, out float rayDisResult)
|
||||
{
|
||||
// get Raycast hit infomation and return Tag and distance
|
||||
RaycastHit nowHit;
|
||||
Color rayColor = Color.cyan;
|
||||
float lineLength = viewDistance;
|
||||
string rayInfoText = "";
|
||||
Vector3 rayInfoPosition;
|
||||
if (Physics.Raycast(ray, out nowHit, viewDistance)) // 若在viewDistance范围内有碰撞
|
||||
{
|
||||
rayInfoText = nowHit.collider.tag;
|
||||
rayTagResult = TagToInt(rayInfoText);
|
||||
rayTagResultOneHot.AddRange(oneHotTags.Encoder(rayInfoText));
|
||||
rayDisResult = nowHit.distance;
|
||||
lineLength = rayDisResult;
|
||||
rayInfoText += "\n" + Convert.ToString(rayDisResult);
|
||||
// rayDisResult = rayDisResult / viewDistance; // Normalization!
|
||||
// 输出log
|
||||
switch (rayTagResult)
|
||||
{
|
||||
case 1:// Wall
|
||||
rayColor = Color.white;
|
||||
break;
|
||||
|
||||
case 2: // Enemy
|
||||
rayColor = Color.red;
|
||||
inViewEnemies.Add(nowHit.transform.gameObject);
|
||||
break;
|
||||
|
||||
case -1: // Hit Nothing
|
||||
rayColor = Color.gray;
|
||||
break;
|
||||
|
||||
default: // default,got wrong
|
||||
rayColor = Color.cyan;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else // 若在viewDistance范围无碰撞
|
||||
{
|
||||
rayTagResultOneHot.AddRange(oneHotTags.Encoder());
|
||||
rayTagResult = -1f;
|
||||
rayDisResult = -1f;
|
||||
//输出log
|
||||
//Debug.Log(0);
|
||||
//Debug.Log(0);
|
||||
}
|
||||
rayInfoPosition = ray.origin + (ray.direction * lineLength);
|
||||
if (showInGameRay)
|
||||
{
|
||||
DrawLine(ray, lineLength, lineRenderer, rayColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
TurnOffLine(lineRenderer, rayColor);
|
||||
}
|
||||
// drawRay in game
|
||||
if (showInGameRayInfo) rayInfoUI.UpdateInfo(rayInfoText, rayInfoPosition, rayColor, TPSCam);
|
||||
// Show log
|
||||
if (showDebugRay) Debug.DrawRay(ray.origin, ray.direction * viewDistance, rayColor); // drawRay in debug
|
||||
// Debug.Log(ray.origin + ray.direction);
|
||||
// Debug.Log(rayTagResult);
|
||||
// Debug.Log(tagToInt(nowHit.collider.tag));
|
||||
}
|
||||
|
||||
private void DrawLine(Ray ray, float lineLength, LineRenderer lineRenderer, Color lineColor)
|
||||
{
|
||||
lineRenderer.startColor = lineColor;
|
||||
lineRenderer.endColor = lineColor;
|
||||
lineRenderer.startWidth = lineWidth;
|
||||
lineRenderer.endWidth = lineWidth;
|
||||
lineRenderer.SetPosition(0, ray.origin);
|
||||
lineRenderer.SetPosition(1, ray.origin + (ray.direction * lineLength));
|
||||
}
|
||||
|
||||
private void TurnOffLine(LineRenderer lineRenderer, Color lineColor)
|
||||
{
|
||||
lineRenderer.startColor = lineColor;
|
||||
lineRenderer.endColor = lineColor;
|
||||
lineRenderer.startWidth = 0f;
|
||||
lineRenderer.endWidth = 0f;
|
||||
lineRenderer.SetPosition(0, new Vector3(0, 0, 0));
|
||||
lineRenderer.SetPosition(1, new Vector3(0, 0, 0));
|
||||
}
|
||||
|
||||
public void UpdateRayInfo()
|
||||
{
|
||||
float focusLEdge = agentCam.pixelWidth * (1 - focusRange) / 2;
|
||||
float focusREdge = agentCam.pixelWidth * (1 + focusRange) / 2;
|
||||
float camPixelHeight = agentCam.pixelHeight;
|
||||
inViewEnemies.Clear();
|
||||
rayTagResultOneHot.Clear();
|
||||
for (int i = 0; i < halfOuterRayNum; i++) // create left outside rays; 0 ~ focusLeftEdge
|
||||
{
|
||||
Vector3 point = new Vector3(i * focusLEdge / (halfOuterRayNum - 1), camPixelHeight / 2, 0);
|
||||
Ray nowRay = agentCam.ScreenPointToRay(point);
|
||||
SingleRaycastUpdate(nowRay, lineRenderers[i], rayInfoUIs[i], out rayTagResult[i], out rayDisResult[i]);
|
||||
}
|
||||
for (int i = 0; i < halfOuterRayNum; i++) // create right outside rays; focusRightEdge ~ MaxPixelHeight
|
||||
{
|
||||
Vector3 point = new Vector3(focusREdge + (i * focusLEdge / (halfOuterRayNum - 1)), camPixelHeight / 2, 0);
|
||||
Ray nowRay = agentCam.ScreenPointToRay(point);
|
||||
SingleRaycastUpdate(nowRay, lineRenderers[halfOuterRayNum + i], rayInfoUIs[halfOuterRayNum + i], out rayTagResult[halfOuterRayNum + i], out rayDisResult[halfOuterRayNum + i]);
|
||||
}
|
||||
for (int i = 0; i < focusRayNum; i++) // create center focus rays; focusLeftEdge ~ focusLeftEdge
|
||||
{
|
||||
Vector3 point = new Vector3(focusLEdge + ((i + 1) * (focusREdge - focusLEdge) / (focusRayNum + 1)), camPixelHeight / 2, 0);
|
||||
Ray nowRay = agentCam.ScreenPointToRay(point);
|
||||
SingleRaycastUpdate(nowRay, lineRenderers[halfOuterRayNum * 2 + i], rayInfoUIs[halfOuterRayNum * 2 + i], out rayTagResult[halfOuterRayNum * 2 + i], out rayDisResult[halfOuterRayNum * 2 + 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,214 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class SceneBlock : MonoBehaviour
|
||||
{
|
||||
public GameObject fireBasesAreaObj;
|
||||
public GameObject enemyContainerObj;
|
||||
public float group1InareaNum = 0;
|
||||
public float group2InareaNum = 0;
|
||||
public float belongRatio = 0;
|
||||
public float blockSize = 10f;
|
||||
public Vector3 firebasesAreaPosition;
|
||||
public float firebasesAreaScale;
|
||||
public float firebasesAreaDiameter;
|
||||
|
||||
public enum SceneType
|
||||
{ Go, Attack, Defence }
|
||||
|
||||
public SceneType sceneType;
|
||||
public bool isDestroyed = false;
|
||||
|
||||
// firebase state
|
||||
public string group1Tag = "Player";
|
||||
|
||||
public string group2Tag = "Enemy";
|
||||
public float belongMaxPoint = 10f;
|
||||
public float firebasesBelong; // -10 mean's belon to group2Tag 10 = belon to group1Tag
|
||||
public float addPointInterval = 0.1f; // add point every addPointInterval scecond
|
||||
public float stayTimeNeeded = 5f; // howmany time to stay at area to get this area 0->10
|
||||
|
||||
private float addPointEachInterval; // add point in each interval
|
||||
private float intervalStart;
|
||||
private GameObject environmentObj;
|
||||
public GameObject[] group1Objs;
|
||||
public GameObject[] group2Objs;
|
||||
|
||||
// Start is called before the first frame update
|
||||
private void Start()
|
||||
{
|
||||
intervalStart = Time.time;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
if (Time.time - intervalStart >= addPointInterval)
|
||||
{
|
||||
// update belong ratio every addPointInterval scecond
|
||||
group1InareaNum = GetInAreaNumber(group1Tag);
|
||||
group2InareaNum = GetInAreaNumber(group2Tag);
|
||||
belongRatio = group1InareaNum - group2InareaNum;
|
||||
if (belongRatio > 0)
|
||||
{
|
||||
// group 1 have the advantage!
|
||||
firebasesBelong += addPointEachInterval;
|
||||
if (firebasesBelong >= belongMaxPoint)
|
||||
{
|
||||
firebasesBelong = belongMaxPoint;
|
||||
}
|
||||
}
|
||||
else if (belongRatio < 0)
|
||||
{
|
||||
// group 2 have the advantage!
|
||||
firebasesBelong -= addPointEachInterval;
|
||||
if (firebasesBelong <= -belongMaxPoint)
|
||||
{
|
||||
firebasesBelong = -belongMaxPoint;
|
||||
}
|
||||
}
|
||||
intervalStart = Time.time;
|
||||
}
|
||||
}
|
||||
|
||||
//Initialize this scene block should be excuted after enemy created
|
||||
public void InitBlock(GameObject envObj)
|
||||
{
|
||||
//Buffer all Player or enemy obj int this environment to list
|
||||
environmentObj = envObj;
|
||||
GameObject[] allGroup1Objs = GameObject.FindGameObjectsWithTag(group1Tag);
|
||||
GameObject[] allGroup2Objs = GameObject.FindGameObjectsWithTag(group2Tag);
|
||||
List<GameObject> group1ObjsList = new List<GameObject>();
|
||||
List<GameObject> group2ObjsList = new List<GameObject>();
|
||||
foreach (GameObject obj in allGroup1Objs)
|
||||
{
|
||||
if (obj.transform.root.gameObject == envObj && !obj.GetComponent<States>().isDead)
|
||||
{
|
||||
group1ObjsList.Add(obj);
|
||||
}
|
||||
}
|
||||
foreach (GameObject obj in allGroup2Objs)
|
||||
{
|
||||
if (obj.transform.root.gameObject == envObj && !obj.GetComponent<States>().isDead)
|
||||
{
|
||||
group2ObjsList.Add(obj);
|
||||
}
|
||||
}
|
||||
group1Objs = group1ObjsList.ToArray();
|
||||
group2Objs = group2ObjsList.ToArray();
|
||||
|
||||
// if firebasesAreaObj is null, find sub object by name
|
||||
if (fireBasesAreaObj == null)
|
||||
{
|
||||
// Debug.Log("SceneBlock.Start: fireBasesAreaObj not found, get it by name");
|
||||
fireBasesAreaObj = transform.Find("FirebasesArea").gameObject;
|
||||
}
|
||||
// if enemyContainerObj is null, find them by name
|
||||
if (enemyContainerObj == null)
|
||||
{
|
||||
// Debug.Log("SceneBlock.Start: enemyContainerObj not found, get it by name");
|
||||
enemyContainerObj = transform.Find("EnemyContainer").gameObject;
|
||||
}
|
||||
firebasesAreaPosition = transform.position + fireBasesAreaObj.transform.position;
|
||||
firebasesAreaScale = fireBasesAreaObj.transform.localScale.x;
|
||||
firebasesAreaDiameter = firebasesAreaScale * blockSize;
|
||||
firebasesBelong = -belongMaxPoint;
|
||||
addPointEachInterval = belongMaxPoint / (stayTimeNeeded / addPointInterval);
|
||||
intervalStart = Time.time;
|
||||
}
|
||||
|
||||
//check game over 0=notover 1=win
|
||||
public int CheckOver()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// get in area player number by tag
|
||||
public float GetInAreaNumber(string tag)
|
||||
{
|
||||
float inAreaNum = 0;
|
||||
float dist = 0f;
|
||||
float isInarea = 0f;
|
||||
int index = 0;
|
||||
if (tag == group1Tag)
|
||||
{
|
||||
foreach (GameObject obj in group1Objs)
|
||||
{
|
||||
// if object is dead then delete it from list
|
||||
if (obj == null)
|
||||
{
|
||||
// delete null object from list
|
||||
List<GameObject> group1ObjsList = new List<GameObject>(group1Objs);
|
||||
group1ObjsList.RemoveAt(index);
|
||||
group1Objs = group1ObjsList.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
(dist, isInarea) = GetDistInArea(obj.transform.position);
|
||||
if (isInarea != 0f)
|
||||
{
|
||||
inAreaNum += 1;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
else if (tag == group2Tag)
|
||||
{
|
||||
foreach (GameObject obj in group2Objs)
|
||||
{
|
||||
// if object is dead then delete it from list
|
||||
if (obj == null)
|
||||
{
|
||||
// delete null object from list
|
||||
List<GameObject> group2ObjsList = new List<GameObject>(group2Objs);
|
||||
group2ObjsList.RemoveAt(index);
|
||||
group2Objs = group2ObjsList.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
(dist, isInarea) = GetDistInArea(obj.transform.position);
|
||||
if (isInarea != 0f)
|
||||
{
|
||||
inAreaNum += 1;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("SceneBlock.getinAreaNumber:TagError");
|
||||
}
|
||||
return inAreaNum;
|
||||
}
|
||||
|
||||
// get this position and target's distance and is in firebase area
|
||||
public (float, int) GetDistInArea(Vector3 position)
|
||||
{
|
||||
position.y = fireBasesAreaObj.transform.position.y;
|
||||
float dist = Vector3.Distance(position, fireBasesAreaObj.transform.position) - (firebasesAreaDiameter / 2);
|
||||
int isinarea = 0;
|
||||
if (dist <= 0)
|
||||
{
|
||||
dist = 0f;
|
||||
isinarea = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
isinarea = 0;
|
||||
}
|
||||
return (dist, isinarea);
|
||||
}
|
||||
|
||||
//destroy this block
|
||||
public void DestroyMe()
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
foreach (Transform childObj in enemyContainerObj.transform)
|
||||
{
|
||||
childObj.GetComponent<States>().DestroyMe();
|
||||
}
|
||||
isDestroyed = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e1ae09061637cf4ead72321c221f783
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,70 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SceneBlockContainer : MonoBehaviour
|
||||
{
|
||||
public float sceneSize = 10f;
|
||||
public GameObject environmentObj;
|
||||
public GameObject parameterContainerObj;
|
||||
public GameObject hudObj;
|
||||
|
||||
// public GameObject[] attackBlockPrefabs = new GameObject[1];
|
||||
// public GameObject[] goBlockPrefabs = new GameObject[1];
|
||||
// public GameObject[] defencePrefabs = new GameObject[1];
|
||||
|
||||
public SceneBlock nowBlock;
|
||||
private GameObject nowBlockObj;
|
||||
private ParameterContainer paramCon;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
paramCon = parameterContainerObj.GetComponent<ParameterContainer>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a specified block at a specified position.
|
||||
/// 在指定位置创建指定类型的场景块。
|
||||
/// </summary>
|
||||
/// <param name="targetType">The target type.</param>
|
||||
/// <param name="level">The level.</param>
|
||||
/// <param name="blockType">The block type.</param>
|
||||
/// <param name="blockPosition">The block position.</param>
|
||||
/// <param name="tag1">Tag 1 (optional, default is "Player").</param>
|
||||
/// <param name="tag2">Tag 2 (optional, default is "Enemy").</param>
|
||||
public void CreateNewBlock(Targets targetType, int level, int blockType, Vector3 blockPosition, string tag1 = "Player", string tag2 = "Enemy")
|
||||
{
|
||||
// check if nowBlock is deleted
|
||||
if (nowBlockObj != null)
|
||||
{
|
||||
// delete nowBlock
|
||||
// Debug.LogWarning("SceneBlockContainer.CreateNewBlock: Block not clear!");
|
||||
DestroyBlock();
|
||||
}
|
||||
// choose target type
|
||||
nowBlockObj = Instantiate(paramCon.scenePrefabSet.GetPrefab(level, blockType, targetType), blockPosition + environmentObj.transform.position, Quaternion.identity, transform);
|
||||
nowBlock = nowBlockObj.GetComponent<SceneBlock>();
|
||||
nowBlock.group1Tag = tag1;
|
||||
nowBlock.group2Tag = tag2;
|
||||
sceneSize = nowBlock.blockSize;
|
||||
}
|
||||
|
||||
// delete nowBlock
|
||||
public void DestroyBlock()
|
||||
{
|
||||
if (nowBlock != null)
|
||||
{
|
||||
nowBlock.DestroyMe();
|
||||
}
|
||||
nowBlockObj = null;
|
||||
nowBlock = null;
|
||||
}
|
||||
|
||||
public (float, int) GetAgentTargetDistanceAndInside(Vector3 agentPosition)
|
||||
{
|
||||
return nowBlock.GetDistInArea(agentPosition);
|
||||
}
|
||||
|
||||
public void InitializeBlock(GameObject envObj)
|
||||
{
|
||||
nowBlock.InitBlock(envObj);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94687c2b580b85d4a8ced43c8cfc7bf2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 904366f9a1b2c0644bb656ed4643ca4a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,36 @@
|
||||
using Unity.MLAgents;
|
||||
using Unity.MLAgents.SideChannels;
|
||||
using UnityEngine;
|
||||
|
||||
public class AimBotSideChannelController : MonoBehaviour
|
||||
{
|
||||
public AimbotSideChannel aimbotSideChannel;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
// We create the Side Channel
|
||||
aimbotSideChannel = new AimbotSideChannel();
|
||||
|
||||
// When a Debug.Log message is created, we send it to the stringChannel
|
||||
Application.logMessageReceived += aimbotSideChannel.SendDebugStatementToPython;
|
||||
|
||||
// The channel must be registered with the SideChannelManager class
|
||||
SideChannelManager.RegisterSideChannel(aimbotSideChannel);
|
||||
}
|
||||
|
||||
// Side Channel
|
||||
public void OnDestroy()
|
||||
{
|
||||
// De-register the Debug.Log callback
|
||||
Application.logMessageReceived -= aimbotSideChannel.SendDebugStatementToPython;
|
||||
if (Academy.IsInitialized)
|
||||
{
|
||||
SideChannelManager.UnregisterSideChannel(aimbotSideChannel);
|
||||
}
|
||||
}
|
||||
|
||||
public void SendSideChannelMessage(string MessageType, string message)
|
||||
{
|
||||
Debug.LogWarning(MessageType + "|" + message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9585e6d628809d14ba93cb74005c3514
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using Unity.MLAgents.SideChannels;
|
||||
using UnityEngine;
|
||||
|
||||
public class AimbotSideChannel : SideChannel
|
||||
{
|
||||
public AimbotSideChannel()
|
||||
{
|
||||
ChannelId = new Guid("8bbfb62a-99b4-457c-879d-b78b69066b5e");
|
||||
}
|
||||
|
||||
protected override void OnMessageReceived(IncomingMessage msg)
|
||||
{
|
||||
var receivedString = msg.ReadString();
|
||||
Debug.Log("From Python : " + receivedString);
|
||||
}
|
||||
|
||||
public void SendDebugStatementToPython(string logString, string stackTrace, LogType type)
|
||||
{
|
||||
if (type == LogType.Warning)
|
||||
{
|
||||
var stringToSend = "Warning|" + logString;
|
||||
using (var msgOut = new OutgoingMessage())
|
||||
{
|
||||
msgOut.WriteString(stringToSend);
|
||||
QueueMessageToSend(msgOut);
|
||||
}
|
||||
}
|
||||
if (type == LogType.Error)
|
||||
{
|
||||
var stringToSend = "Error|" + logString;
|
||||
using (var msgOut = new OutgoingMessage())
|
||||
{
|
||||
msgOut.WriteString(stringToSend);
|
||||
QueueMessageToSend(msgOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6836d727b536dd54e893311318779b9a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,59 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class States : MonoBehaviour
|
||||
{
|
||||
public bool isDead = false;
|
||||
public float maxHP = 100;
|
||||
private float myHP = 100;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
myHP = maxHP;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
}
|
||||
|
||||
private void DetactDeath()
|
||||
{
|
||||
if (myHP <= 0)
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
isDead = true;
|
||||
}
|
||||
}
|
||||
|
||||
// while got hit
|
||||
public void ReactToHit(float Damage, GameObject damageSource)
|
||||
{
|
||||
myHP -= Damage;
|
||||
Debug.Log("HP:" + myHP);
|
||||
if (myHP <= 0)
|
||||
{
|
||||
if (damageSource.tag == "Player")
|
||||
{
|
||||
damageSource.GetComponent<AgentController>().KillRecord(transform.position);
|
||||
Destroy(this.gameObject);
|
||||
isDead = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
isDead = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get my hp from other script
|
||||
public float GetnowHP()
|
||||
{
|
||||
return myHP;
|
||||
}
|
||||
|
||||
public void DestroyMe()
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
isDead = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b25389b3cd1e7084d81fa752823ef210
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,682 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 92a93d5d33962bc44b9fd2368358d47e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user