Untitled

 avatar
unknown
plain_text
a year ago
3.2 kB
13
Indexable
using System.Collections;
using System.Collections.Generic;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using UnityEngine;
//playeragent without status 
public class PlayerAgent : Agent
{
    public enum PenaltyStyle
    {
        Reward,
        Termination
    }

    public int lives;
    public int playerscore;
    private Rigidbody2D rb2d;
    private SpriteRenderer sr;

    public GameManager manager;
    public PenaltyStyle penaltyStyle;
    public float dmg;
    public int phaseReached;
    public Player _playerScript;
    public PlayerStatus status;
    public UIManager _uiManager;

    // Start is called before the first frame update
    void Start()
    {
        lives = _playerScript.lives;
        playerscore = _uiManager.score;
        phaseReached = manager.phase;

        Application.runInBackground = true;

        rb2d = GetComponent<Rigidbody2D>();
        sr = GetComponent<SpriteRenderer>();
        Debug.Log("player agent has been called");
        _playerScript = GetComponent<Player>(); // Ensure this is correctly assigned

    }
    public override void OnEpisodeBegin()
    {
        // When comparing agent configurations, it's best to have performance meausures that aren't 
        // swayed by the reward setup used. You can use Academy.Instance.StatsRecorder.Add() to add
        // extra data to TensorBoard.
        if (phaseReached != -1)
        {
            Academy.Instance.StatsRecorder.Add("Phase Reached", phaseReached);
            Academy.Instance.StatsRecorder.Add("player Score", playerscore);
        }

        dmg = 0;
        manager.newGame();
    }




     public void HandlePhaseAdvance()
     {
           Debug.LogError("phase" + phaseReached);
           AddReward(1.0f);
    }

    public void HandleDamage()
    {
        dmg++;

        if (penaltyStyle == PenaltyStyle.Reward)
        {
            AddReward(dmg);
        }
        else
        {
            EndEpisode();
        }
    }



    public override void CollectObservations(VectorSensor sensor)
    {
        sensor.AddObservation(transform.position.x);
        sensor.AddObservation(transform.position.y);
        sensor.AddObservation(transform.rotation);
    }

    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        int action_chosen = actionBuffers.DiscreteActions[0];
       // Debug.Log("Action chosen = " + action_chosen + ", " + actionBuffers.DiscreteActions[1]);
      //  Debug.LogError("player score: " + playerscore);
      //  Debug.LogError("player lives: " + lives);
       // Debug.LogError("phase reached " + phaseReached);
        int rotate = actionBuffers.DiscreteActions[0];
        int strafe = actionBuffers.DiscreteActions[1];
        _playerScript.Movement_V1(rotate, strafe);
    }

    public override void Heuristic(in ActionBuffers actionsOut)
    {
        var actions = actionsOut.DiscreteActions;
        //Debug.Log("actions: " + actions[0] + " " + actions[1]);
    }

    private void Update()
    {
        //playerscore++;
    }
}

Editor is loading...
Leave a Comment