This commit is contained in:
2024-06-12 21:03:42 +02:00
parent 4685d9942b
commit aef3b3ab97
1548 changed files with 5615 additions and 72 deletions

View File

@@ -0,0 +1,21 @@
using Assets.Scripts;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BadCoinScript : MonoBehaviour
{
[SerializeField] float SpawnChance = 1f;
private bool objectEnabled;
private void Awake()
{
objectEnabled = DataBearer.Instance.Rules.MalusCoins && Random.value < SpawnChance;
if (!objectEnabled)
{
Destroy(gameObject);
return;
}
}
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FinishLineScript : MonoBehaviour
{
private GameHandler _gameHandler;
// Start is called before the first frame update
void Awake()
{
_gameHandler = GameHandler.Instance;
_gameHandler.OnAttemptEnding += Destroy_OnAttemptEnding;
}
// Update is called once per frame
void Update()
{
gameObject.transform.position += new Vector3(-_gameHandler.FrameDistance, 0, 0);
}
public void Destroy_OnAttemptEnding(object sender, EventArgs args)
{
Destroy(gameObject);
}
private void OnDestroy()
{
GameHandler.Instance.OnAttemptEnding -= Destroy_OnAttemptEnding;
}
}

View File

@@ -0,0 +1,22 @@
using Assets.Scripts;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoodCoinScript : MonoBehaviour
{
[SerializeField] float SpawningChance = 1f;
private bool objectEnabled;
private void Awake()
{
objectEnabled = DataBearer.Instance.Rules.BonusCoins && Random.value < SpawningChance;
if (!objectEnabled)
{
Destroy(gameObject);
return;
}
}
}

View File

@@ -0,0 +1,47 @@
using Assets.Scripts;
using UnityEngine;
public class CapsulePlacer : MonoBehaviour
{
public Transform objectA;
public Transform objectB;
private bool laserEnabled;
private void Awake()
{
if(objectA == null || objectB == null)
{
laserEnabled = false;
Debug.LogError("Les boules du laser ne sont pas définis.");
}
laserEnabled = DataBearer.Instance.Rules.Lasers;
if (!laserEnabled)
{
Destroy(gameObject);
return;
}
}
private void Update()
{
Vector2 positionA = objectA.position;
Vector2 positionB = objectB.position;
// Calcul de la distance et mise à jour de l'échelle
float distance = Vector2.Distance(positionA, positionB);
Vector3 localScale = transform.localScale;
localScale.y = distance / 2f;
transform.localScale = localScale;
// Mise à jour de la position
transform.position = (positionA + positionB) / 2f;
// Calcul de la direction et de l'angle
Vector2 direction = positionB - positionA;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90f;
// Mise à jour de la rotation
transform.rotation = Quaternion.Euler(0f, 0f, angle);
}
}

View File

@@ -0,0 +1,77 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MissileAlertScript : MonoBehaviour
{
#region PROPERTIES
#endregion
#region VARIABLES
private float blinkInterval = 0.2f;
private bool isBlinking = false;
private SpriteRenderer spriteRenderer;
private Vector3 position;
#endregion
#region EVENTS
#endregion
#region METHODS
private IEnumerator Blink()
{
isBlinking = true;
while (isBlinking)
{
SetSpriteVisible(true);
yield return new WaitForSeconds(blinkInterval);
SetSpriteVisible(false);
yield return new WaitForSeconds(blinkInterval);
}
}
private void SetSpriteVisible(bool isVisible)
{
spriteRenderer.enabled = isVisible;
}
#endregion
#region LIFECYCLE
private void Awake()
{
position = transform.position;
spriteRenderer = GetComponent<SpriteRenderer>();
var mainCamera = Camera.main;
position.x = mainCamera.transform.position.x + mainCamera.orthographicSize * mainCamera.aspect - 1f;
transform.position = position;
SetSpriteVisible(false);
enabled = false;
}
private void OnEnable()
{
if (!isBlinking)
{
StartCoroutine(Blink());
}
}
private void OnDisable()
{
if (isBlinking)
{
StopCoroutine(Blink());
SetSpriteVisible(false); // Assure que le sprite est invisible quand on arr<72>te le clignotement
isBlinking = false;
}
}
#endregion
}

View File

@@ -0,0 +1,82 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MissileScript : MonoBehaviour
{
#region PROPERTIES
public int TargetId { get; set; }
#endregion
#region VARIABLES
[SerializeField] private float speed = 15.0f;
[SerializeField] private GameObject explosion;
private bool renderSprite;
private GameHandler _gameHandler;
#endregion
#region METHODS
public void Explode()
{
StopParticles();
if (renderSprite)
{
var rotation = transform.rotation;
rotation.z = 180;
Instantiate(explosion, transform.position, rotation);
}
Destroy(gameObject);
return;
}
public void SetRenderSprite(bool render)
{
renderSprite = render;
}
public void SetTarget(int id)
{
TargetId = id;
}
public void StopParticles()
{
if (transform.childCount == 0) return;
var emit = transform?.GetChild(0);
emit.parent = null;
emit.GetComponent<ParticleSystem>().Stop(true, ParticleSystemStopBehavior.StopEmitting);
// This finds the particleAnimator associated with the emitter and then
// sets it to automatically delete itself when it runs out of particles
}
#endregion
#region LIFECYCLE
void Start()
{
_gameHandler = GameHandler.Instance;
}
// Update is called once per frame
void Update()
{
if(transform.position.x < -20)
{
StopParticles();
Destroy(gameObject);
return;
}
//Go up because up is the direction of the head of the missile, even if he goes left the player's POV
transform.Translate(Vector3.up * (speed+_gameHandler.MapSpeed) * Time.deltaTime);
}
#endregion
}

View File

@@ -0,0 +1,106 @@
using System.Collections;
using System.Collections.Generic;
using Unity.MLAgents;
using UnityEngine;
public class ObstaclePatternBehaviour : MonoBehaviour
{
[SerializeField] private GameHandler gameHandler;
private float gameObjectLength;
private float gameSpeed;
private GameHandler _gameHandler;
// Start is called before the first frame update
void Awake()
{
_gameHandler = GameHandler.Instance;
}
private void Start()
{
gameObjectLength = ComputeLengthRecursive(0.0f, gameObject.transform) - gameObject.transform.position.x;
}
// Update is called once per frame
void Update()
{
// move the obstacle depending on GameHandler's offset value
gameObject.transform.position += new Vector3(-_gameHandler.FrameDistance, 0, 0);
//kill the obstacle if it reach the GameHandler's dead end value
var position = gameObject.transform.position.x;
var spawnerPosition = LevelSpawner.Instance.transform.position.x;
//Debug.Log("position : " + position+"\n"+"scale : " + gameObjectLength);
if(position < -gameObjectLength - spawnerPosition)
{
//Debug.Log("DESTROY");
MonoBehaviour.Destroy(gameObject);
return;
}
}
private float ComputeLengthRecursive(float max, Transform @object)
{
foreach (Transform child in @object)
{
if(child.position.x > max)
{
max = child.position.x;
}
max = ComputeLengthRecursive(max, child);
}
return max;
}
//private float ComputeLength()
//{
// Debug.Log("ComputeLength");
// CenterAndChildCount centerAndChildCount = new();
// centerAndChildCount = ComputeCenterRecursive(centerAndChildCount, gameObject.transform);
// Vector3 center = centerAndChildCount.center;
// int childCount = centerAndChildCount.childCount;
// center /= childCount; //center is average center of children
// Bounds bounds = new Bounds(center, Vector3.zero);
// bounds = ComputeBoundsRecursive(bounds, gameObject.transform);
// return bounds.size.x;
//}
//private CenterAndChildCount ComputeCenterRecursive(CenterAndChildCount centerAndChildCount, Transform @object)
//{
// foreach (Transform child in @object)
// {
// centerAndChildCount.center += child.gameObject.GetComponent<Renderer>().bounds.center;
// centerAndChildCount.childCount++;
// centerAndChildCount = ComputeCenterRecursive(centerAndChildCount, child);
// }
// return centerAndChildCount;
//}
//private Bounds ComputeBoundsRecursive(Bounds bounds, Transform @object)
//{
// foreach (Transform child in @object)
// {
// ComputeBoundsRecursive(bounds, child);
// bounds.Encapsulate(child.gameObject.GetComponent<Renderer>().bounds);
// }
// return bounds;
//}
//private class CenterAndChildCount
//{
// public Vector3 center = Vector3.zero;
// public int childCount = 0;
//}
}

View File

@@ -0,0 +1,166 @@
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
}

View File

@@ -0,0 +1,80 @@
using System;
using UnityEngine;
using UnityEngine.UIElements;
/// <summary>
/// Instanciate a ScorePannel view on the UI. This class only serves as view and does not contain any game data and does not affect the game's work
/// </summary>
public class ScorePannelScript : MonoBehaviour
{
#region PROPERTIES
#endregion
#region VARIABLES
[SerializeField] private VisualTreeAsset scorePannelTemplate; // R<>f<EFBFBD>rence au template UXML du ScorePannel
private VisualElement scoreContainer; // Le VisualElement parent o<> ajouter le ScorePannel
private VisualElement root;
private Label scoreLabel;
private Label playerNameLabel;
private VisualElement scorePanel;
#endregion
#region EVENTS
#endregion
#region ENDPOINTS
public void SetPlayerName(string playerName)
{
playerNameLabel.text = playerName;
}
public void SetScore(float score)
{
scoreLabel.text = score.ToString("0");
}
public void SetColor(Color color)
{
scorePanel.style.backgroundColor = new StyleColor(new Color(color.r, color.g, color.b, 0.43f));
}
public void Dispose()
{
scoreContainer.Remove(root);
Destroy(gameObject);
}
#endregion
#region LIFECYCLE
void OnEnable()
{
Debug.Log("SCORE Instanciatied");
var uiDocument = GetComponentInParent<UIDocument>(); // Obtenir le composant UIDocument attach<63> <20> l'objet
// V<>rifiez que les r<>f<EFBFBD>rences sont assign<67>es
if (uiDocument == null || scorePannelTemplate == null)
{
Debug.LogError("UiDocument ou scorePannelTemplate n'est pas assign<67>.");
return;
}
var rootVisualElement = uiDocument.rootVisualElement;
// V<>rifiez que ScoresContainer existe dans l'arborescence de l'UI
scoreContainer = rootVisualElement.Q<VisualElement>("ScoresContainer");
if (scoreContainer == null)
{
Debug.LogError("ScoresContainer n'a pas <20>t<EFBFBD> trouv<75> dans le UXML.");
return;
}
// Charger et instancier le ScorePannel
root = scorePannelTemplate.CloneTree();
scorePanel = root.Q<VisualElement>("panel");
scoreLabel = root.Q<Label>("ScoreValue");
playerNameLabel = root.Q<Label>("PlayerName");
scoreContainer.Add(root);
root.AddToClassList("scorePanel");
}
#endregion
}