浏览代码

cooldowns/shockwave feature of big agent

/asymm-envs
Andrew Cohen 5 年前
当前提交
b6a621e9
共有 3 个文件被更改,包括 514 次插入475 次删除
  1. 858
      Project/Assets/ML-Agents/Examples/CubeWars/Prefabs/CubeWarArea.prefab
  2. 90
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/LargeCubeAgent.cs
  3. 41
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/SmallCubeAgent.cs

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

90
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/LargeCubeAgent.cs


CubeWarArea m_MyArea;
bool m_Dead;
bool m_Shoot;
float m_ShootTime;
bool m_Shockwave;
float m_ShockwaveTime;
Rigidbody m_AgentRb;
float m_LaserLength;
float m_HitPoints;

public Material weakMaterial;
public Material deadMaterial;
public GameObject myLaser;
public GameObject shockwave;
public override void Initialize()

public void MoveAgent(float[] act)
{
m_Shoot = false;
m_Shockwave = false;
var dirToGo = Vector3.zero;
var rotateDir = Vector3.zero;

var shootCommand = false;
var shockwaveCommand = false;
var shockwaveAxis = (int)act[4];
switch (forwardAxis)
{

shootCommand = true;
break;
}
switch (shockwaveAxis)
{
case 1:
shockwaveCommand = true;
break;
}
m_Shoot = true;
dirToGo *= 0.5f;
m_AgentRb.velocity *= 0.75f;
if (Time.time > m_ShootTime + 1f)
{
m_Shoot = true;
dirToGo *= 0.5f;
m_AgentRb.velocity *= 0.75f;
m_ShootTime = Time.time;
}
else
if (shockwaveCommand)
transform.Rotate(rotateDir, Time.fixedDeltaTime * turnSpeed);
if (Time.time > m_ShockwaveTime + 3f)
{
m_Shockwave = true;
dirToGo *= 0.5f;
m_AgentRb.velocity *= 0.75f;
m_ShockwaveTime = Time.time;
}
transform.Rotate(rotateDir, Time.fixedDeltaTime * turnSpeed);
m_AgentRb.AddForce(dirToGo * moveSpeed, ForceMode.VelocityChange);
}

}
float checkTime = Time.time;
var rayDir = 125.0f * myTransform.forward;
var rayDir = 120.0f * myTransform.forward;
if (Physics.SphereCast(transform.position, 7f, rayDir, out hit, 125f))
if (Physics.SphereCast(transform.position, 7f, rayDir, out hit, 120f))
hit.collider.gameObject.GetComponent<SmallCubeAgent>().HitAgent();
hit.collider.gameObject.GetComponent<SmallCubeAgent>().HitAgent(.35f);
}
else if (hit.collider.gameObject.CompareTag("StrongLargeAgent") || hit.collider.gameObject.CompareTag("WeakLargeAgent"))
{

}
else
else if (checkTime > m_ShootTime + .5f)
if (m_Shockwave)
{
var myTransform = transform;
shockwave.transform.localScale = new Vector3(1f, 1f, 1f);
RaycastHit hit;
int casts = 16;
float angleRotation = 360f / (float)casts;
for (int i = 0; i < casts; i++)
{
var rayDir = Quaternion.AngleAxis(angleRotation * i, Vector3.up) * myTransform.forward * 10f;
Debug.DrawRay(myTransform.position, rayDir, Color.green, 0f, true);
if (Physics.SphereCast(transform.position, 3f, rayDir, out hit, 7f))
{
if (hit.collider.gameObject.CompareTag("StrongSmallAgent") || hit.collider.gameObject.CompareTag("WeakSmallAgent"))
{
hit.collider.gameObject.GetComponent<SmallCubeAgent>().HitAgent(1f);
}
}
}
}
else if (checkTime > m_ShockwaveTime + 1.5f)
{
shockwave.transform.localScale = new Vector3(0f, 0f, 0f);
}
public void HitAgent()
public void HitAgent(float damage)
m_HitPoints -= .005f;
m_HitPoints -= damage;
HealthStatus();
}
}

if (m_HitPoints < 1f && !m_Dead)
{
m_HitPoints += .1f;
m_HitPoints = Mathf.Min(m_HitPoints + .1f, 1f);
HealthStatus();
}

public override float[] Heuristic()
{
var action = new float[4];
var action = new float[5];
if (Input.GetKey(KeyCode.D))
{
action[2] = 2f;

action[0] = 1f;
}
if (Input.GetKey(KeyCode.E))
{
action[1] = 1f;
}
if (Input.GetKey(KeyCode.Q))
{
action[1] = 2f;
}
if (Input.GetKey(KeyCode.A))
{
action[2] = 1f;

action[0] = 2f;
}
action[3] = Input.GetKey(KeyCode.Space) ? 1.0f : 0.0f;
action[4] = Input.GetKey(KeyCode.O) ? 1.0f : 0.0f;
return action;
}

HealthStatus();
m_Dead = false;
m_Shoot = false;
m_ShootTime = -1f;
m_ShockwaveTime = -3f;
shockwave.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;

41
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/SmallCubeAgent.cs


LargeCubeAgent m_LargeAgent;
bool m_Dead;
bool m_Shoot;
float m_ShootTime;
Rigidbody m_AgentRb;
float m_LaserLength;
float m_HitPoints;

}
if (shootCommand)
{
m_Shoot = true;
if (Time.time > m_ShootTime + .5f)
{
m_Shoot = true;
dirToGo *= 0.5f;
m_AgentRb.velocity *= 0.9f;
m_ShootTime = Time.time;
}
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;
//}
if (m_Shoot)
{

Debug.DrawRay(myTransform.position, rayDir, Color.red, 0f, true);
RaycastHit hit;
if (Physics.SphereCast(transform.position, 2f, rayDir, out hit, 25f))
if (Physics.SphereCast(transform.position, 2f, rayDir, out hit, 28f))
{
if (hit.collider.gameObject.CompareTag("StrongSmallAgent") || hit.collider.gameObject.CompareTag("WeakSmallAgent"))
{

{
hit.collider.gameObject.GetComponent<LargeCubeAgent>().HitAgent();
hit.collider.gameObject.GetComponent<LargeCubeAgent>().HitAgent(.05f);
else
else if (Time.time > m_ShootTime + .25f)
public void HitAgent()
public void HitAgent(float damage)
m_HitPoints -= .05f;
m_HitPoints -= damage;
HealthStatus();
}
}

if (m_HitPoints < 1f)
{
m_HitPoints += .01f;
m_HitPoints = Mathf.Min(m_HitPoints + .01f, 1f);
m_HitPoints = Mathf.Min(m_HitPoints + .1f, 1f);
HealthStatus();
}
}

{
action[0] = 1f;
}
if (Input.GetKey(KeyCode.E))
{
action[1] = 1f;
}
if (Input.GetKey(KeyCode.Q))
{
action[1] = 2f;
}
if (Input.GetKey(KeyCode.A))
{
action[2] = 1f;

HealthStatus();
m_Dead = false;
m_Shoot = false;
m_ShootTime = -.5f;
float smallRange = 30f * m_MyArea.range;
float smallRange = 50f * m_MyArea.range;
transform.position = new Vector3(Random.Range(-smallRange, smallRange),
2f,Random.Range(-smallRange, smallRange))
+ area.transform.position;

正在加载...
取消
保存