Projet-IA-Madelaine/Scripts/GameObjectsScripts/PlayerScript.cs
2024-06-12 21:03:42 +02:00

167 lines
4.8 KiB
C#

using System;
using UnityEngine;
using static ScoreHandler;
public class PlayerScript : MonoBehaviour
{
#region PROPERTIES
public bool IsHuman { get; set; } = false;
public string Name { get; private set; }
public int Id { get; private set; }
public Color Color { get; private set; } = new Color(255, 255, 255);
public bool IsGrounded { get; private set; } = false;
public bool IsFlying { get; private set; } = false;
#endregion
#region VARIABLES
private ScoreHandler _scoreHandler;
private PlayersHandler _playersHandler;
public float jumpForce = 8f;
private Rigidbody2D rb;
public float maxVelocity = 8f;
public float minVelocity = -8f;
#endregion
#region EVENTS
public event EventHandler<PrefabTouchedEventArgs> OnObstacleTouched;
public event EventHandler<PrefabTouchedEventArgs> OnBonusTouched;
public class PrefabTouchedEventArgs : EventArgs
{
public GameObject objectTouched;
public int playerId;
public string playerName;
public PrefabTouchedEventArgs(GameObject objectTouched, int playerId, string playerName)
{
this.objectTouched = objectTouched;
this.playerId = playerId;
this.playerName = playerName;
}
}
private void Lose_OnScoreReachingZero(object sender, ScoreEventArgs args)
{
if(args.playerId == Id)
_playersHandler.RemovePlayer(Id);
}
#endregion
#region METHODS
public void MoveVertically(sbyte direction,float custom_DeltaTime)
{
IsFlying=(direction>0);
rb.AddForce(Vector2.up * (direction*jumpForce) * custom_DeltaTime);
}
public void SetName(string name)
{
Name = name;
}
public void SetId(int id)
{
Id = id;
}
public void SetColor(Color color)
{
Color = color;
}
public void Dispose()
{
Destroy(gameObject);
}
#endregion
#region LIFECYCLE
void Awake()
{
rb = GetComponent<Rigidbody2D>();
rb.freezeRotation = true;
_scoreHandler = ScoreHandler.Instance;
_playersHandler = PlayersHandler.Instance;
_scoreHandler.OnScoreReachingZero += Lose_OnScoreReachingZero;
}
private void OnTriggerEnter2D(Collider2D other)
{
// Vérifier si la collision concerne l'objet que vous souhaitez détecter
switch (other.tag)
{
case "Floor":
IsGrounded = true;
break;
case "Laser":
OnObstacleTouched.Invoke(this, new PrefabTouchedEventArgs(other.gameObject, Id, Name));
_scoreHandler.AddToScore(-200, Id);
break;
case "GoodCoin":
if (IsHuman)
other.gameObject.GetComponent<SpriteRenderer>().enabled = false;
OnBonusTouched.Invoke(this, new PrefabTouchedEventArgs(other.gameObject, Id, Name));
_scoreHandler.AddToScore(50, Id);
break;
case "BadCoin":
if (IsHuman)
other.gameObject.GetComponent<SpriteRenderer>().enabled = false;
OnObstacleTouched.Invoke(this, new PrefabTouchedEventArgs(other.gameObject, Id, Name));
_scoreHandler.AddToScore(-50, Id);
break;
case "Missile":
MissileScript missileScript = other.transform.GetComponent<MissileScript>();
if (missileScript.TargetId == Id)
{
OnObstacleTouched.Invoke(this, new PrefabTouchedEventArgs(other.gameObject, Id, Name));
other.GetComponent<MissileScript>().Explode();
_scoreHandler.AddToScore(-500, Id);
}
break;
default:
break;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Floor"))
{
IsGrounded = false;
}
}
void Update()
{
// Récupérer la vélocité actuelle du Rigidbody2D
Vector2 currentVelocity = rb.velocity;
// Limiter la vélocité verticale
float clampedVelocityY = Mathf.Clamp(currentVelocity.y, minVelocity, maxVelocity);
rb.velocity = new Vector2(currentVelocity.x, clampedVelocityY);
// Appliquer une force constante vers le haut ou le bas en fonction de la touche enfoncée
//float verticalForce = Input.GetButton("Jump") ? jumpForce : -jumpForce;
//rb.AddForce(Vector2.up * verticalForce * Time.deltaTime);
// Effectuer le raycast pour vérifier si le joueur est au sol
//RaycastHit2D hit = Physics2D.Raycast(rb.position, Vector2.down, 0.25f);
//IsGrounded = hit.collider != null && hit.transform.CompareTag("Floor");
}
#endregion
}