Projet-IA-Madelaine/Scripts/PlayerBehaviourScripts/Iterator/IteratorBehaviour.cs
2024-06-12 21:03:42 +02:00

79 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class IteratorBehaviour : PlayerBehaviour
{
SensorsScript sensorsScript;
Rigidbody2D rb;
[SerializeField] private List<float> raycastWeight = new List<float>(){
3f, 5f, 5f, 3f, 12f, 25f, 0f, 25f, 12f, 3f, 5f, 5f, 3f
};
protected override sbyte ChooseDirection()
{
List<SensorData> sensorDataList = sensorsScript.GetObserverRaycast();
float influenceSum = 0f;
// Calculer l'influence totale en parcourant les données de capteurs
for (int i = 0; i < sensorDataList.Count; i++)
{
SensorData data = sensorDataList[i];
float angle = sensorsScript.raycastConfigurations[i].y; // Obtenir l'angle du raycast
//float horizontalDistance = data.distance * Mathf.Cos(Mathf.Deg2Rad * angle); // Composante horizontale de la distance
float DistanceForce = MinFloat(sensorsScript.raycastDistance/data.distance/10,10);
float influence = DistanceForce * raycastWeight[i];
if(data.hitLayer == 7){
influence *= 0.5f;
}
if(i <= 5){
influence *= -1;
}
influenceSum += influence;
//Debug.Log(i +" --- "+DistanceForce);
}
if (influenceSum > 0.25f && rb.velocity.y > 0f)
{
// Si la direction suggérée est la même que la direction actuelle, réduire l'influence
influenceSum -= 1f;
}
// Utiliser l'influence totale pour déterminer la direction
sbyte direction = (sbyte)(influenceSum > 0 ? 1 : -1);
//Debug.Log("influenceSum" + influenceSum);
return direction;
}
float MinFloat(float a,float b){
if(a > b)
return b;
return a;
}
void Start()
{
sensorsScript = GetComponent<SensorsScript>();
rb = GetComponent<Rigidbody2D>();
}
protected override string ChoosePlayerName()
{
return "Itérateur";
}
protected override Color ChoosePlayerColor()
{
return new Color(255, 70, 0);
}
protected override bool ChooseIfPlayerIsHuman()
{
return false;
}
}