浏览代码

grid sensor agent ready

/hh-develop-water-balloon-fight
HH 4 年前
当前提交
54fb8fd6
共有 6 个文件被更改,包括 1254 次插入195 次删除
  1. 334
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBallAgent.cs
  2. 105
      Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBallGameController.cs
  3. 2
      Project/Assets/ML-Agents/Examples/SharedAssets/Materials/AgentBlue.mat
  4. 1001
      Project/Assets/ML-Agents/Examples/FPS_Game/Prefabs/DodgeballAgent.prefab
  5. 7
      Project/Assets/ML-Agents/Examples/FPS_Game/Prefabs/DodgeballAgent.prefab.meta

334
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBallAgent.cs


public bool UseVectorObs;
private DodgeBallGameController m_GameController;
private Vector3 m_StartingPos;
private Quaternion m_StartingRot;
private Rigidbody m_AgentRb;
[Header("HIT EFFECTS")] public ParticleSystem HitByParticles;

[Header("HIT DAMAGE")] public int NumberOfTimesPlayerCanBeHit = 5;
public int HitPointsRemaining; //how many more times can we be hit
public bool m_Initialized;
//PLAYER STATE TO OBSERVE
// Start is called before the first frame update

// m_Cam = Camera.main;
m_AgentRb = GetComponent<Rigidbody>();
input = GetComponent<FPSAgentInput>();
m_GameController = FindObjectOfType<DodgeBallGameController>();
m_StartingPos = transform.position;
m_StartingRot = transform.rotation;
m_Initialized = true;
}

m_CubeMovement = GetComponent<AgentCubeMovement>();
// m_Cam = Camera.main;
m_AgentRb = GetComponent<Rigidbody>();
input = GetComponent<FPSAgentInput>();
m_GameController = FindObjectOfType<DodgeBallGameController>();
if (!m_Initialized)
{
Initialize();
}
ResetAgent();
}
public void ResetAgent()
{
transform.position = m_StartingPos;
// transform.rotation = m_StartingRot;
transform.rotation = Quaternion.Euler(new Vector3(0f, Random.Range(0, 360)));
// Unfreeze();
// Unpoison();
// Unsatiate();
// m_Shoot = false;
// m_AgentRb.velocity = Vector3.zero;
// myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
// transform.position = new Vector3(Random.Range(-m_MyArea.range, m_MyArea.range),
// 2f, Random.Range(-m_MyArea.range, m_MyArea.range))
// + area.transform.position;
transform.rotation = Quaternion.Euler(new Vector3(0f, Random.Range(0, 360)));
m_AgentRb.velocity = Vector3.zero;
m_AgentRb.angularVelocity = Vector3.zero;
// SetResetParameters();
}
void SetActiveBalls(int numOfBalls)

BallUIList[i].gameObject.SetActive(active);
i++;
}
// for (int i = 0; i < numOfBalls; i++)
// {
// var active = i < numOfBalls;
// ActiveBallsList[i].gameObject.SetActive();
// }
}
public override void CollectObservations(VectorSensor sensor)

// var localVelocity = transform.InverseTransformDirection(m_AgentRb.velocity);
// sensor.AddObservation(localVelocity.x);
// sensor.AddObservation(localVelocity.z);
// sensor.AddObservation(m_Frozen);
// sensor.AddObservation(m_ShootInput);
sensor.AddObservation(StepCount / (float)MaxStep); //Help with credit assign?
sensor.AddObservation(currentNumberOfBalls/4); //Held DBs Normalized
sensor.AddObservation(HitPointsRemaining/NumberOfTimesPlayerCanBeHit); //Remaining Hit Points Normalized
// // var localVelocity = transform.InverseTransformDirection(m_AgentRb.velocity);
// // sensor.AddObservation(localVelocity.x);
// // sensor.AddObservation(localVelocity.z);
// // sensor.AddObservation(m_Frozen);
// // sensor.AddObservation(m_ShootInput);
// else if (useVectorFrozenFlag)
// {
// sensor.AddObservation(m_Frozen);

m_InputV = act[0];
m_InputH = act[1];
m_Rotate = act[2];
m_ShootInput = act[3];
m_ThrowInput = act[3];
m_DashInput = act[4];
//HANDLE ROTATION
Vector3 moveDir = input.Cam.transform.TransformDirection(new Vector3(m_InputH, 0, m_InputV));
moveDir.y = 0;
// m_CubeMovement.RunOnGround(m_AgentRb, m_Cam.transform.TransformDirection(new Vector3(0, 0, m_InputV)));
// m_CubeMovement.RunOnGround(m_AgentRb, moveDir);
//HANDLE XZ MOVEMENT
Vector3 moveDir = Vector3.zero;
// moveDir = input.Cam.transform.TransformDirection(new Vector3(m_InputH, 0, m_InputV));
// moveDir.y = 0;
moveDir = transform.TransformDirection(new Vector3(m_InputH, 0, m_InputV));
// if (m_InputH != 0)
// {
// if (leftStrafe)
// {
// m_CubeMovement.Strafe(transform.right * -1);
// leftStrafe = false;
// }
// if (rightStrafe)
// {
// m_CubeMovement.Strafe(transform.right * 1);
// rightStrafe = false;
// }
//
// m_CubeMovement.Strafe(transform.right * m_InputH);
// }
if (AgentShield && act[6] > 0)
{
AgentShield.ActivateShield(true);
}
else
{
AgentShield.ActivateShield(false);
}
// if (AgentShield && act[6] > 0)
// {
// AgentShield.ActivateShield(true);
// }
// else
// {
// AgentShield.ActivateShield(false);
// }
// if (m_ShootInput > 0 && currentNumberOfBalls > 0)

// }
if (input.shootPressed)
//HANDLE THROWING
if (m_ThrowInput > 0)
if (act[4] > 0 && m_CubeMovement.groundCheck.isGrounded)
{
m_CubeMovement.Jump();
}
if (act[5] > 0)
// if (act[4] > 0 && m_CubeMovement.groundCheck.isGrounded)
// {
// m_CubeMovement.Jump();
// }
//HANDLE DASH MOVEMENT
if (m_DashInput > 0)
if (m_AgentRb.velocity.sqrMagnitude > 25f) // slow it down
{
m_AgentRb.velocity *= 0.95f;
}
// if (m_AgentRb.velocity.sqrMagnitude > 25f) // slow it down
// {
// m_AgentRb.velocity *= 0.95f;
// }
}
public void ThrowTheBall()

public bool leftStrafe;
public bool rightStrafe;
void Update()
{
// m_InputH = Input.GetKeyDown(KeyCode.K) ? 1 : Input.GetKeyDown(KeyCode.J) ? -1 : 0; //inputH
if (Input.GetKeyDown(KeyCode.K))
{
rightStrafe = true;
}
if (Input.GetKeyDown(KeyCode.J))
{
leftStrafe = true;
}
}
// void Update()
// {
// // m_InputH = Input.GetKeyDown(KeyCode.K) ? 1 : Input.GetKeyDown(KeyCode.J) ? -1 : 0; //inputH
// if (Input.GetKeyDown(KeyCode.K))
// {
// rightStrafe = true;
// }
//
// if (Input.GetKeyDown(KeyCode.J))
// {
// leftStrafe = true;
// }
// }
// void FixedUpdate()
// {

// m_ShootInput = Input.GetKey(KeyCode.Space) ? 1 : 0; //shoot
// }
private Vector2 inputMovement;
private Vector2 rotateMovement;
public float m_ShootInput;
public void OnMovement(InputAction.CallbackContext value)
{
inputMovement = value.ReadValue<Vector2>();
}
// private Vector2 inputMovement;
// private Vector2 rotateMovement;
public float m_ThrowInput;
public float m_DashInput;
public void OnRotate(InputAction.CallbackContext value)
{
rotateMovement = value.ReadValue<Vector2>();
}
public void OnShoot(InputAction.CallbackContext value)
{
// m_ShootInput = value.canceled? 0: value.ReadValue<float>();
// m_ShootInput = 0;
if (value.started)
{
print("started");
}
if (value.performed)
{
print("performed" + Time.frameCount);
}
if (!value.canceled)
{
print("not cancelled" + Time.frameCount + m_ShootInput);
m_ShootInput = value.ReadValue<float>();
// m_ShootInput = value.
}
else
{
m_ShootInput = 0;
print("cancelled" + Time.frameCount + m_ShootInput);
}
}
// public void OnMovement(InputAction.CallbackContext value)
// {
// inputMovement = value.ReadValue<Vector2>();
// }
//
// public void OnRotate(InputAction.CallbackContext value)
// {
// rotateMovement = value.ReadValue<Vector2>();
// }
// public void OnShoot(InputAction.CallbackContext value)
// {
// // m_ShootInput = value.canceled? 0: value.ReadValue<float>();
// // m_ShootInput = 0;
// if (value.started)
// {
// print("started");
// }
// if (value.performed)
// {
// print("performed" + Time.frameCount);
// }
// if (!value.canceled)
// {
// print("not cancelled" + Time.frameCount + m_ThrowInput);
// m_ThrowInput = value.ReadValue<float>();
// // m_ShootInput = value.
// }
// else
// {
// m_ThrowInput = 0;
// print("cancelled" + Time.frameCount + m_ThrowInput);
// }
// }
IEnumerator ShowHitFace()

HitEyes.gameObject.SetActive(false);
}
public void PlayHitFX()
{
ThrowController.impulseSource.GenerateImpulse();
// HitSoundAudioSource.Play();
HitSoundAudioSource.PlayOneShot(BallImpactAudioClip, 1f);
HitSoundAudioSource.PlayOneShot(HurtVoiceAudioClip, 1f);
HitByParticles.Play();
if (AnimateEyes)
{
StartCoroutine(ShowHitFace());
}
}
private void OnCollisionEnter(Collision col)
{
DodgeBall db = col.gameObject.GetComponent<DodgeBall>();

if (db.inPlay) //HIT BY LIVE BALL
{
if (HitPointsRemaining == 1)
{
//RESET ENV
print($"{gameObject.name} Lost.{gameObject.name} was weak:");
//ASSIGN REWARDS
EndEpisode();
}
else
{
HitPointsRemaining--;
//ASSIGN REWARDS
}
m_GameController.PlayerWasHit(this);
// if (HitPointsRemaining == 1)
// {
// //RESET ENV
// print($"{gameObject.name} Lost.{gameObject.name} was weak:");
// //ASSIGN REWARDS
// EndEpisode();
// }
// else
// {
// HitPointsRemaining--;
// //ASSIGN REWARDS
//
// }
ThrowController.impulseSource.GenerateImpulse();
// HitSoundAudioSource.Play();
HitSoundAudioSource.PlayOneShot(BallImpactAudioClip, 1f);
HitSoundAudioSource.PlayOneShot(HurtVoiceAudioClip, 1f);
HitByParticles.Play();
if (AnimateEyes)
{
StartCoroutine(ShowHitFace());
}
db.BallIsInPlay(false);
}
else //TRY TO PICK IT UP

HitSoundAudioSource.PlayOneShot(BallPickupAudioClip, .1f);
//update counter
currentNumberOfBalls++;
SetActiveBalls(currentNumberOfBalls);
//add to our inventory
ActiveBallsQueue.Enqueue(db);
db.BallIsInPlay(true);
db.gameObject.SetActive(false);
// ActiveBallsList.Add(db);
PickUpBall(db);
}
}
// if (col.transform.CompareTag("dodgeBall"))

void PickUpBall(DodgeBall db)
{
HitSoundAudioSource.PlayOneShot(BallPickupAudioClip, .1f);
//update counter
currentNumberOfBalls++;
SetActiveBalls(currentNumberOfBalls);
//add to our inventory
ActiveBallsQueue.Enqueue(db);
db.BallIsInPlay(true);
db.gameObject.SetActive(false);
// ActiveBallsList.Add(db);
}
// contActionsOut[0] = m_InputV; //inputV
// contActionsOut[2] = m_Rotate; //rotate
// contActionsOut[3] = m_ShootInput; //shoot
// contActionsOut[0] = Input.GetKey(KeyCode.W) ? 1 : Input.GetKey(KeyCode.S) ? -1 : 0; //inputV
// contActionsOut[1] = Input.GetKeyDown(KeyCode.E) ? 1 : Input.GetKeyDown(KeyCode.Q) ? -1 : 0; //inputH
// contActionsOut[2] = Input.GetKey(KeyCode.D) ? 1 : Input.GetKey(KeyCode.A) ? -1 : 0; //rotate
// contActionsOut[3] = Input.GetKey(KeyCode.Space) ? 1 : 0; //shoot
contActionsOut[3] = input.shootInput ? 1 : 0; //shoot
contActionsOut[4] = input.CheckIfInputSinceLastFrame(ref input.jumpInput) ? 1 : 0; //jump
contActionsOut[5] = input.CheckIfInputSinceLastFrame(ref input.dashInput) ? 1 : 0; //jump
// contActionsOut[4] = input.jumpInput ? 1 : 0; //jump
// contActionsOut[5] = input.dashInput ? 1 : 0; //dash
contActionsOut[6] = input.shieldInput ? 1 : 0; //shield
if (input.jumpInput)
{
print($"Agent: Jump: {input.jumpInput} : {Time.frameCount}");
}
// contActionsOut[3] = input.shootInput ? 1 : 0; //shoot
contActionsOut[3] = input.CheckIfInputSinceLastFrame(ref input.shootInput) ? 1 : 0; //jump
contActionsOut[4] = input.CheckIfInputSinceLastFrame(ref input.dashInput) ? 1 : 0; //jump
// contActionsOut[5] = input.CheckIfInputSinceLastFrame(ref input.jumpInput) ? 1 : 0; //jump
// // contActionsOut[4] = input.jumpInput ? 1 : 0; //jump
// // contActionsOut[5] = input.dashInput ? 1 : 0; //dash
// contActionsOut[6] = input.shieldInput ? 1 : 0; //shield
// if (input.jumpInput)
// {
// print($"Agent: Jump: {input.jumpInput} : {Time.frameCount}");
//
// }
// contActionsOut[0] = inputMovement.y;
// contActionsOut[1] = inputMovement.x;

105
Project/Assets/ML-Agents/Examples/FPS_Game/Scripts/DodgeBallGameController.cs


public class DodgeBallGameController : MonoBehaviour
{
// public enum CurrentGameMode
// {
// OneVsOne, "2v2", "3v3"
// }
//
// public CurrentGameMode GameMode = CurrentGameMode.1v1;
[Header("PLAYERS")]
public Transform Team0SpawnPos;
public Transform Team1SpawnPos;

public float BallSpawnRadius = 3;
public Transform BallSpawnPosition;
public int NumberOfBallsToSpawn = 10;
public int NumberOfBallsPlayersCanHold = 3;
// public int NumberOfBallsPlayersCanHold = 4;
public int PlayerMaxHitPoints = 5;
// [Serializable]
// public class DodgeBallPlayer

public class AgentInfo
{
public DodgeBallAgent Agent;
public int HitPointsRemaining;
[HideInInspector]
public Vector3 StartingPos;
[HideInInspector]

[HideInInspector]
public Collider Col;
[HideInInspector]
public int TeamID;
public Color Team0Color;
public Color Team1Color;
// public Dictionary<DodgeBallAgent, DodgeBallPlayer> PlayersDict = new Dictionary<DodgeBallAgent, DodgeBallPlayer>();
public Dictionary<DodgeBallAgent, AgentInfo> PlayersDict = new Dictionary<DodgeBallAgent, AgentInfo>();
private int m_ResetTimer;
public int MaxEnvironmentSteps = 5000;

}
void Initialize()
{
//SPAWN DODGE BALLS
for (int i = 0; i < NumberOfBallsToSpawn; i++)
{
GameObject g = Instantiate(BallPrefab, BallSpawnPosition.position + Random.insideUnitSphere * BallSpawnRadius,

g.SetActive(true);
}
//Reset Agents
//INITIALIZE AGENTS
item.StartingPos = item.Agent.transform.position;
item.StartingRot = item.Agent.transform.rotation;
item.Rb = item.Agent.GetComponent<Rigidbody>();
// item.Col = item.Agent.GetComponent<Collider>();
item.Agent.Initialize();
item.HitPointsRemaining = PlayerMaxHitPoints;
item.TeamID = 0;
PlayersDict.Add(item.Agent, item);
item.StartingPos = item.Agent.transform.position;
item.StartingRot = item.Agent.transform.rotation;
item.Rb = item.Agent.GetComponent<Rigidbody>();
item.Agent.Initialize();
item.HitPointsRemaining = PlayerMaxHitPoints;
item.TeamID = 1;
PlayersDict.Add(item.Agent, item);
void PlayerWasHit(DodgeBallAgent agent)
public void PlayerWasHit(DodgeBallAgent agent)
AgentInfo info = PlayersDict[agent];
if (info.HitPointsRemaining == 1)
{
//RESET ENV
print($"{agent.name} Lost.{agent.name} was weak:");
//ASSIGN REWARDS
// EndEpisode();
agent.AddReward(-1f); //you lost penalty
if (info.TeamID == 0)
{
print($"Team 1 Won");
}
else if (info.TeamID == 1)
{
print($"Team 0 Won");
}
ResetScene();
}
else
{
info.HitPointsRemaining--;
//ASSIGN REWARDS
agent.AddReward(-.1f); //small hit penalty
}
}

foreach (var item in Team0Players)
{
item.Agent.EndEpisode();
item.HitPointsRemaining = PlayerMaxHitPoints;
item.HitPointsRemaining = PlayerMaxHitPoints;
//Reset Agents
foreach (var item in Team0Players)
{
ResetAgent(item);
}
foreach (var item in Team1Players)
{
ResetAgent(item);
}
// //Reset Agents
// foreach (var item in Team0Players)
// {
// item.Agent.ResetAgent();
// item.HitPointsRemaining = PlayerMaxHitPoints;
// // ResetAgent(item);
// }
// foreach (var item in Team1Players)
// {
// item.Agent.ResetAgent();
// item.HitPointsRemaining = PlayerMaxHitPoints;
// // ResetAgent(item);
// }
void ResetAgent(AgentInfo item)
{
// var pos = UseRandomAgentPosition ? GetRandomSpawnPos() : item.StartingPos;
// var rot = UseRandomAgentRotation ? GetRandomRot() : item.StartingRot;
item.Agent.transform.SetPositionAndRotation(item.StartingPos, item.StartingRot);
item.Rb.velocity = Vector3.zero;
item.Rb.angularVelocity = Vector3.zero;
item.Agent.gameObject.SetActive(true);
}
// void ResetAgent(AgentInfo item)
// {
// // var pos = UseRandomAgentPosition ? GetRandomSpawnPos() : item.StartingPos;
// // var rot = UseRandomAgentRotation ? GetRandomRot() : item.StartingRot;
// item.Agent.transform.SetPositionAndRotation(item.StartingPos, item.StartingRot);
// item.Rb.velocity = Vector3.zero;
// item.Rb.angularVelocity = Vector3.zero;
// item.Agent.gameObject.SetActive(true);
// }
// // Start is called before the first frame update
// void Awake()

2
Project/Assets/ML-Agents/Examples/SharedAssets/Materials/AgentBlue.mat


- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0.12941177, g: 0.5882353, b: 0.9529412, a: 1}
- _Color: {r: 0, g: 0.51824737, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

1001
Project/Assets/ML-Agents/Examples/FPS_Game/Prefabs/DodgeballAgent.prefab
文件差异内容过多而无法显示
查看文件

7
Project/Assets/ML-Agents/Examples/FPS_Game/Prefabs/DodgeballAgent.prefab.meta


fileFormatVersion: 2
guid: 1c6bcdaf8b76745d8918fc47c464885e
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

部分文件因为文件数量过多而无法显示

正在加载...
取消
保存