2023-07-09 17:51:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class AgentController : MonoBehaviour
|
|
|
|
|
{
|
2023-10-22 16:54:30 +00:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private GameObject commonParameterContainerObj;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private GameObject environmentObj;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private GameObject enemyContainerObj;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private GameObject sceneBlockContainerObj;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private GameObject environmentUIControlObj;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private GameObject targetControllerObj;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private GameObject HUDObj;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private Camera fpsCam;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
|
|
|
|
|
[Header("GetAxis() Simulate")]
|
|
|
|
|
public float moveSpeed = 9.0f;
|
|
|
|
|
|
|
|
|
|
public float vX = 0f;
|
|
|
|
|
public float vZ = 0f;
|
2023-08-22 17:58:50 +00:00
|
|
|
|
public Vector3 nowMovement;
|
2023-07-11 10:32:28 +00:00
|
|
|
|
public float acceleration = 0.9f; // 加速度
|
2023-07-09 17:51:44 +00:00
|
|
|
|
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;
|
2023-10-22 16:54:30 +00:00
|
|
|
|
private CommonParameterContainer commonPramCon;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
private SceneBlockContainer blockContainer;
|
|
|
|
|
private TargetController targetCon;
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
// initialize scripts
|
2023-10-22 16:54:30 +00:00
|
|
|
|
commonPramCon = commonParameterContainerObj.GetComponent<CommonParameterContainer>();
|
2023-07-09 17:51:44 +00:00
|
|
|
|
blockContainer = sceneBlockContainerObj.GetComponent<SceneBlockContainer>();
|
|
|
|
|
targetCon = targetControllerObj.GetComponent<TargetController>();
|
|
|
|
|
raySensors = GetComponent<RaySensors>();
|
|
|
|
|
playerController = this.transform.GetComponent<CharacterController>();
|
|
|
|
|
|
|
|
|
|
// initialize Environment parameters
|
2023-10-22 16:54:30 +00:00
|
|
|
|
lockMouse = commonPramCon.lockMouse;
|
|
|
|
|
damage = commonPramCon.damage;
|
|
|
|
|
fireRate = commonPramCon.fireRate;
|
|
|
|
|
lockCameraX = commonPramCon.lockCameraX;
|
|
|
|
|
lockCameraY = commonPramCon.lockCameraY;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
|
|
|
|
|
// initialize remainTime
|
|
|
|
|
// this agent's tag
|
|
|
|
|
myTag = gameObject.tag;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 10:47:14 +00:00
|
|
|
|
#region Agent Move Control
|
|
|
|
|
|
2023-07-09 17:51:44 +00:00
|
|
|
|
public void MoveAgent(int vertical, int horizontal)
|
|
|
|
|
{
|
2023-08-22 17:58:50 +00:00
|
|
|
|
// Vector3 nowMovement;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-22 17:58:50 +00:00
|
|
|
|
nowMovement = (transform.forward * vZ + transform.right * vX);
|
2023-07-09 17:51:44 +00:00
|
|
|
|
//PlayerController下的.Move为实现物体运动的函数
|
|
|
|
|
//Move()括号内放入一个Vector3类型的量,本例中为Player_Move
|
2023-08-22 17:58:50 +00:00
|
|
|
|
if (nowMovement.magnitude > moveSpeed)
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
2023-08-22 17:58:50 +00:00
|
|
|
|
nowMovement = nowMovement.normalized * moveSpeed;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
2023-08-22 17:58:50 +00:00
|
|
|
|
playerController.Move(nowMovement * Time.deltaTime);
|
2023-07-09 17:51:44 +00:00
|
|
|
|
// update Key Viewer
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 10:47:14 +00:00
|
|
|
|
#endregion Agent Move Control
|
|
|
|
|
|
|
|
|
|
#region Camera Control
|
|
|
|
|
|
2023-07-09 17:51:44 +00:00
|
|
|
|
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控制着值的大小
|
|
|
|
|
|
|
|
|
|
//相机在上下旋转移动时,相机方向不会随着移动,类似于低头和抬头,左右移动时,相机方向会随着向左向右移动,类似于向左向右看
|
|
|
|
|
//所以在控制相机向左向右旋转时,要保证和父物体一起转动
|
2023-09-07 22:15:24 +00:00
|
|
|
|
fpsCam.transform.localRotation = Quaternion.Euler(yRotation, 0, 0);
|
2023-07-09 17:51:44 +00:00
|
|
|
|
//this.transform指这个CameraRotation的位置,localRotation指的是旋转轴
|
|
|
|
|
//transform.localRotation = Quaternion.Eular(x,y,z)控制旋转的时候,按照X-Y-Z轴的旋转顺规
|
|
|
|
|
//即以围绕X轴旋转x度,围绕Y轴旋转y度,围绕Z轴旋转z度
|
|
|
|
|
//且绕轴旋转的坐标轴是父节点本地坐标系的坐标轴
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 10:47:14 +00:00
|
|
|
|
#endregion Camera Control
|
|
|
|
|
|
|
|
|
|
#region Reward Functions
|
2023-07-09 17:51:44 +00:00
|
|
|
|
|
|
|
|
|
// ballistic 射击弹道处理,并返回获得reward
|
|
|
|
|
private float Ballistic(int shootState)
|
|
|
|
|
{
|
2023-09-07 22:15:24 +00:00
|
|
|
|
Vector3 point = new Vector3(fpsCam.pixelWidth / 2, fpsCam.pixelHeight / 2, 0);//发射位置
|
|
|
|
|
Ray ray = fpsCam.ScreenPointToRay(point);
|
2023-07-09 17:51:44 +00:00
|
|
|
|
RaycastHit hit;
|
2023-10-15 20:02:57 +00:00
|
|
|
|
// Debug.DrawRay(centerRay.origin, centerRay.direction * 100, Color.blue);
|
2023-07-09 17:51:44 +00:00
|
|
|
|
//按下鼠标左键
|
|
|
|
|
if (shootState != 0 && gunReadyToggle == true)
|
|
|
|
|
{
|
|
|
|
|
lastShootTime = Time.time;
|
|
|
|
|
if (Physics.Raycast(ray, out hit, 100))
|
|
|
|
|
{
|
2023-10-12 13:48:02 +00:00
|
|
|
|
if (hit.collider.tag != myTag && hit.collider.tag != "Wall" && hit.collider.tag != "Untagged")
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
|
|
|
|
// kill enemy
|
|
|
|
|
GameObject gotHitObj = hit.transform.gameObject;//获取受到Ray撞击的对象
|
|
|
|
|
gotHitObj.GetComponent<States>().ReactToHit(damage, gameObject);
|
|
|
|
|
shootState = 0;
|
|
|
|
|
return targetCon.HitEnemyReward(gotHitObj.transform.position);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-14 11:13:53 +00:00
|
|
|
|
if (targetCon.targetTypeInt == (int)Targets.Attack)
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
|
|
|
|
// while if attack mode
|
2023-08-22 17:58:50 +00:00
|
|
|
|
float targetDis = Vector3.Distance(blockContainer.nowBlock.transform.position, transform.position);
|
2023-07-09 17:51:44 +00:00
|
|
|
|
if (targetDis <= raySensors.viewDistance)
|
|
|
|
|
{
|
|
|
|
|
// Debug.DrawRay(new Vector3(0,0,0), viewPoint, Color.red);
|
2023-08-22 17:58:50 +00:00
|
|
|
|
if (Vector3.Distance(ray.origin + (ray.direction * targetDis), blockContainer.nowBlock.transform.position) <= blockContainer.nowBlock.firebasesAreaDiameter / 2)
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
|
|
|
|
// im shooting at target but didn't hit enemy
|
2023-10-15 20:02:57 +00:00
|
|
|
|
// Debug.DrawRay(centerRay.origin, viewPoint-centerRay.origin, Color.blue);
|
2023-10-22 16:54:30 +00:00
|
|
|
|
return commonPramCon.shootTargetAreaReward;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
shootState = 0;
|
2023-10-22 16:54:30 +00:00
|
|
|
|
return commonPramCon.shootReward;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
else if (shootState != 0 && gunReadyToggle == false)
|
|
|
|
|
{
|
|
|
|
|
// shoot without ready
|
|
|
|
|
shootState = 0;
|
2023-10-22 16:54:30 +00:00
|
|
|
|
return commonPramCon.shootWithoutReadyReward;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// do not shoot
|
|
|
|
|
shootState = 0;
|
2023-10-22 16:54:30 +00:00
|
|
|
|
return commonPramCon.nonReward;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float FacingReward()
|
|
|
|
|
{
|
2023-08-22 17:58:50 +00:00
|
|
|
|
float nowReward = 0;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
bool isFacingtoEnemy = false;
|
|
|
|
|
float enemyFacingDistance = 0f;
|
2023-10-15 20:02:57 +00:00
|
|
|
|
|
|
|
|
|
Vector3 screenCenter = new Vector3(fpsCam.pixelWidth / 2, fpsCam.pixelHeight / 2, 0);
|
|
|
|
|
Vector3 screenLeft = new Vector3(0, fpsCam.pixelHeight / 2, 0);
|
|
|
|
|
|
|
|
|
|
Ray centerRay = fpsCam.ScreenPointToRay(screenCenter);
|
|
|
|
|
Ray leftRay = fpsCam.ScreenPointToRay(screenLeft);
|
|
|
|
|
|
|
|
|
|
// target fireBaseArea Position, turen y to camera's y
|
|
|
|
|
Vector3 fireBaseArea = blockContainer.nowBlock.fireBasesAreaObj.transform.position;
|
|
|
|
|
fireBaseArea.y = fpsCam.transform.position.y;
|
|
|
|
|
|
|
|
|
|
// my position, turn y to camera's y
|
|
|
|
|
// Debug.DrawRay(fpsCam.transform.position, centerRay.direction * 100, Color.blue);
|
|
|
|
|
Vector3 myposition = transform.position;
|
|
|
|
|
myposition.y = fpsCam.transform.position.y;
|
|
|
|
|
|
|
|
|
|
// Target to Agent distance
|
|
|
|
|
//Debug.DrawLine(fireBaseArea, myposition, Color.red);
|
|
|
|
|
float targetDis = Vector3.Distance(fireBaseArea, myposition);
|
|
|
|
|
|
|
|
|
|
// point in centerRay and leftRay which distance is targetDis from camera center
|
|
|
|
|
Vector3 pointInCenterRay = fpsCam.transform.position + (centerRay.direction * targetDis);
|
|
|
|
|
Vector3 pointInLeftRay = fpsCam.transform.position + (leftRay.direction * targetDis);
|
|
|
|
|
|
|
|
|
|
// center of screen to target's distance
|
|
|
|
|
// Debug.DrawLine(pointInCenterRay, fireBaseArea,Color.green);
|
|
|
|
|
float camCenterToTarget = Vector3.Distance(pointInCenterRay, fireBaseArea);
|
|
|
|
|
|
|
|
|
|
// left of screen to target's distance
|
|
|
|
|
// Debug.DrawLine(pointInLeftRay, pointInCenterRay, Color.yellow);
|
|
|
|
|
float camCenterToViewEdge = Vector3.Distance(pointInLeftRay, pointInCenterRay);
|
|
|
|
|
|
|
|
|
|
switch (targetCon.targetTypeInt)
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
2023-10-15 20:02:57 +00:00
|
|
|
|
case (int)Targets.Free:
|
|
|
|
|
//free mode
|
|
|
|
|
RaycastHit hit;
|
|
|
|
|
if (Physics.Raycast(centerRay, out hit, 100))
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
2023-10-15 20:02:57 +00:00
|
|
|
|
// facing to an enemy
|
|
|
|
|
if (hit.collider.tag != myTag && hit.collider.tag != "Wall")
|
|
|
|
|
{
|
2023-10-22 16:54:30 +00:00
|
|
|
|
nowReward = commonPramCon.facingReward;
|
2023-10-15 20:02:57 +00:00
|
|
|
|
isFacingtoEnemy = true;
|
|
|
|
|
}
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
2023-10-15 20:02:57 +00:00
|
|
|
|
if (raySensors.inViewEnemies.Count > 0 && !isFacingtoEnemy)
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
2023-10-15 20:02:57 +00:00
|
|
|
|
// 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, (centerRay.direction * 10));
|
|
|
|
|
Vector3 verticalToRay = transform.position + projection - theEnemy.transform.position;
|
|
|
|
|
projectionDis.Add(verticalToRay.magnitude);
|
|
|
|
|
// Debug.Log("enemy!" + verticalToRay.magnitude);
|
|
|
|
|
// Debug.DrawRay(transform.position, (centerRay.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
|
2023-10-22 16:54:30 +00:00
|
|
|
|
nowReward = 1 / MathF.Sqrt(commonPramCon.facingInviewEnemyDisCOEF * enemyFacingDistance + 0.00001f);
|
2023-10-15 20:02:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
nowReward = 0;
|
|
|
|
|
}
|
|
|
|
|
// enemy in view Reward
|
|
|
|
|
lastEnemyFacingDistance = enemyFacingDistance;
|
2023-10-22 16:54:30 +00:00
|
|
|
|
if (nowReward >= commonPramCon.facingReward) nowReward = commonPramCon.facingReward; // limit
|
|
|
|
|
if (nowReward <= -commonPramCon.facingReward) nowReward = -commonPramCon.facingReward; // limit
|
2023-10-15 20:02:57 +00:00
|
|
|
|
// Debug.Log("ninimum = " + nowReward);
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
2023-10-15 20:02:57 +00:00
|
|
|
|
break;
|
|
|
|
|
case (int)Targets.Attack:
|
|
|
|
|
// attack mode
|
|
|
|
|
if (targetDis <= raySensors.viewDistance)
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
2023-10-15 20:02:57 +00:00
|
|
|
|
// 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(centerRay.origin, viewPoint-centerRay.origin, Color.blue);
|
2023-10-22 16:54:30 +00:00
|
|
|
|
nowReward = commonPramCon.facingReward;
|
2023-10-15 20:02:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// while not facing to target
|
2023-10-22 16:54:30 +00:00
|
|
|
|
nowReward = (lastTargetFacingDistance - camCenterToTarget) * commonPramCon.facingTargetReward;
|
2023-10-15 20:02:57 +00:00
|
|
|
|
}
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
2023-10-15 20:02:57 +00:00
|
|
|
|
// update lastTargetFacingDistance
|
|
|
|
|
lastTargetFacingDistance = camCenterToTarget;
|
|
|
|
|
break;
|
|
|
|
|
case (int)Targets.Go:
|
|
|
|
|
// goto mode
|
|
|
|
|
if (camCenterToTarget <= camCenterToViewEdge)
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
2023-10-15 20:02:57 +00:00
|
|
|
|
// fireArea is in view
|
2023-10-22 16:54:30 +00:00
|
|
|
|
nowReward = commonPramCon.facingReward;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-15 20:02:57 +00:00
|
|
|
|
nowReward = 0;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
2023-10-15 20:02:57 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Debug.LogError("Wrong target type");
|
|
|
|
|
break;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
2023-08-22 17:58:50 +00:00
|
|
|
|
return nowReward;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2023-10-22 16:54:30 +00:00
|
|
|
|
if (spinRecord.Count >= commonPramCon.spinRecordMax)
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
|
|
|
|
spinRecord.RemoveAt(0);
|
|
|
|
|
}
|
2023-10-22 16:54:30 +00:00
|
|
|
|
float spinPenaltyReward = Math.Abs(spinRecord.ToArray().Sum() * commonPramCon.spinPenalty);
|
|
|
|
|
if (spinPenaltyReward >= commonPramCon.spinPenaltyThreshold)
|
2023-07-09 17:51:44 +00:00
|
|
|
|
{
|
|
|
|
|
epreward -= spinPenaltyReward;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-22 16:54:30 +00:00
|
|
|
|
epreward -= Math.Abs(mouseX) * commonPramCon.mousePenalty;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
// move penalty
|
|
|
|
|
if (movement != 0)
|
|
|
|
|
{
|
2023-10-22 16:54:30 +00:00
|
|
|
|
epreward -= commonPramCon.movePenalty;
|
2023-07-09 17:51:44 +00:00
|
|
|
|
}
|
|
|
|
|
return epreward;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 10:47:14 +00:00
|
|
|
|
#endregion Reward Functions
|
|
|
|
|
|
|
|
|
|
// GotKill 获得击杀时用于被呼出
|
2023-08-22 17:58:50 +00:00
|
|
|
|
public void KillRecord(Vector3 killEnemyPosition)
|
2023-08-15 10:47:14 +00:00
|
|
|
|
{
|
|
|
|
|
enemyKillCount += 1;
|
2023-08-22 17:58:50 +00:00
|
|
|
|
this.killEnemyPosition = killEnemyPosition;
|
2023-08-15 10:47:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-09 17:51:44 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|