Files
2025-01-17 13:10:42 +01:00

113 lines
3.2 KiB
C#

using System;
using Unity.VisualScripting;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.Experimental.GlobalIllumination;
using UnityEngine.SceneManagement;
public class Droid : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
private bool jumpKeyWasPressed = false;
private float horizontalInput;
private Rigidbody rigidbodyComponent;
private bool isGrounded = false;
private int energy = 0;
private int rota = 0;
[SerializeField] private GameObject fusee;
private UIManager UIManager;
private int life = 1;
private int nbrfusee = 1;
void Start()
{
UIManager = FindAnyObjectByType<UIManager>();
rigidbodyComponent = GetComponent<Rigidbody>();
StartCoroutine(UIManager.LifeUp(life, nbrfusee));
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
jumpKeyWasPressed = true;
}
horizontalInput = Input.GetAxis("Horizontal");
if (Input.GetKeyDown(KeyCode.E))
{
if (nbrfusee > 0)
{
GameObject ball = Instantiate(fusee, transform.position, new Quaternion(90, 90, 0, 0));
ball.GetComponent<Rigidbody>().linearVelocity =
new Vector3(20, 0, 0);
nbrfusee--;
StartCoroutine(UIManager.LifeUp(life, nbrfusee));
}
}
}
private void FixedUpdate(){
int speed = 5;
if (energy >= 5)
speed = 10;
if (jumpKeyWasPressed && isGrounded)
{
rigidbodyComponent.AddForce(Vector3.up * speed, ForceMode.VelocityChange);
jumpKeyWasPressed = false;
}
rigidbodyComponent.linearVelocity = new Vector3(horizontalInput * speed, rigidbodyComponent.linearVelocity.y, 0);
}
private void OnCollisionStay(Collision collision){
isGrounded = true;
}
private void OnCollisionExit(Collision collision){
isGrounded = false;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.layer == 7)
{
rota += 180;
Transform[] transforms = gameObject.GetComponentsInChildren<Transform>();
transforms[3].rotation = Quaternion.Euler(0, 0, rota);
}
if (collision.gameObject.layer == 10)
{
life -=2;
StartCoroutine(UIManager.LifeUp(life, nbrfusee));
if (life <= 0)
{
StartCoroutine(UIManager.GameOverSequence());
}
}
if (collision.gameObject.layer == 11)
{
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == 3)
{
Destroy(other.gameObject);
energy++;
life++;
nbrfusee++;
StartCoroutine(UIManager.LifeUp(life, nbrfusee));
}
if (other.tag == "exit" ){
SceneManager.LoadScene(1);
}
if (other.tag == "GameOver")
{
StartCoroutine(UIManager.GameOverSequence());
}
}
}