Bento-Quirino / DJD2D3D

GNU General Public License v3.0
2 stars 0 forks source link

Códigos #18

Open Carrtz opened 5 days ago

Carrtz commented 5 days ago

Dialogo:

using System; using System.Collections; using TMPro; using UnityEngine;

public class TypeTextAnimation : MonoBehaviour {

public Action TypeFinished;

public float typeDelay = 0.05f;
public TextMeshProUGUI textObject;

public string fullText;

Coroutine coroutine;

void Start() {

}

public void StartTyping() {
    coroutine = StartCoroutine(TypeText());
}

IEnumerator TypeText() {

    textObject.text = fullText;
    textObject.maxVisibleCharacters = 0;
    for(int i = 0; i <= textObject.text.Length; i++) {
        textObject.maxVisibleCharacters = i;
        yield return new WaitForSeconds(typeDelay);
    }

    TypeFinished?.Invoke();

}

public void Skip() {

    StopCoroutine(coroutine);
    textObject.maxVisibleCharacters = textObject.text.Length;

}

} using UnityEngine;

public enum STATE { DISABLED, WAITING, TYPING }

public class DialogueSystem : MonoBehaviour {

public DialogueData dialogueData;

int currentText = 0;
bool finished = false;

TypeTextAnimation typeText;
DialogueUI dialogueUI;

STATE state;

void Awake() {

    typeText = FindObjectOfType<TypeTextAnimation>();
    dialogueUI = FindObjectOfType<DialogueUI>();

    typeText.TypeFinished = OnTypeFinishe;

}

void Start() {
    state = STATE.DISABLED;
}

void Update() {

    if(state == STATE.DISABLED) return;

    switch(state) {
        case STATE.WAITING:
            Waiting();
            break;
        case STATE.TYPING:
            Typing();
            break;
    }

}

public void Next() {

    if(currentText == 0) {
        dialogueUI.Enable();
    }

    dialogueUI.SetName(dialogueData.talkScript[currentText].name);

    typeText.fullText = dialogueData.talkScript[currentText++].text;

    if(currentText == dialogueData.talkScript.Count) finished = true;

    typeText.StartTyping();
    state = STATE.TYPING;

}

void OnTypeFinishe() {
    state = STATE.WAITING;
}

void Waiting() {

    if(Input.GetKeyDown(KeyCode.Return)) {

        if(!finished) {
            Next();
        } else {
            dialogueUI.Disable();
            state = STATE.DISABLED;
            currentText = 0;
            finished = false;
        }

    }

}

void Typing() {

    if(Input.GetKeyDown(KeyCode.Return)) {
        typeText.Skip();
        state = STATE.WAITING;
    }

}

} using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro;

public class DialogueUI : MonoBehaviour {

Image background;
TextMeshProUGUI nameText;
TextMeshProUGUI talkText;

public float speed = 10f;
bool open = false;

void Awake() {
    background = transform.GetChild(0).GetComponent<Image>();
    nameText   = transform.GetChild(1).GetComponent<TextMeshProUGUI>();
    talkText   = transform.GetChild(2).GetComponent<TextMeshProUGUI>();
}

void Start() {

}

void Update() {
    if(open) {
        background.fillAmount = Mathf.Lerp(background.fillAmount, 1, speed * Time.deltaTime);
    } else {
        background.fillAmount = Mathf.Lerp(background.fillAmount, 0, speed * Time.deltaTime);
    }
}

public void SetName(string name) {
    nameText.text = name;
}

public void Enable() {
    background.fillAmount = 0;
    open = true;
}

public void Disable() {
    open = false;
    nameText.text = "";
    talkText.text = "";
}

} Movimentação:

using UnityEngine;

public class Player : MonoBehaviour {

SpriteRenderer spriteRenderer;
Rigidbody2D rb; // Adiciona uma referência ao Rigidbody2D

public Sprite idle;
public Sprite running;

public Transform npc;

DialogueSystem dialogueSystem;

public float speed = 10f;
public float jumpForce = 5f; // Força do pulo

private bool isGrounded; // Verifica se o jogador está no chão
private Vector2 velocity = Vector2.zero;

private void Awake()
{
    dialogueSystem = FindObjectOfType<DialogueSystem>();
    spriteRenderer = GetComponent<SpriteRenderer>();
    rb = GetComponent<Rigidbody2D>(); // Obtém o Rigidbody2D
}

void Update()
{
    float input = Input.GetAxisRaw("Horizontal");

    if (input < 0)
        spriteRenderer.flipX = true;
    if (input > 0)
        spriteRenderer.flipX = false;

    velocity = speed * input * Vector2.right;

    if (velocity.sqrMagnitude > 0)
    {
        spriteRenderer.sprite = running;
    }
    else
    {
        spriteRenderer.sprite = idle;
    }

    transform.position += (Vector3)velocity * Time.deltaTime;

    // Verifica se o jogador está próximo do NPC
    if (Mathf.Abs(transform.position.x - npc.position.x) < 2.0f)
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            dialogueSystem.Next();
        }
    }

    // Verifica se o jogador está no chão e se a tecla de pulo é pressionada
    if (isGrounded && Input.GetKeyDown(KeyCode.Space))
    {
        Jump();
    }
}

private void Jump()
{
    rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); // Aplica a força do pulo
    isGrounded = false; // Define que o jogador não está mais no chão
}

private void OnCollisionEnter2D(Collision2D collision)
{
    // Verifica se o jogador colidiu com o chão
    if (collision.gameObject.CompareTag("Ground"))
    {
        isGrounded = true; // Define que o jogador está no chão
    }
}

}

Soco: using UnityEngine;

public class PlayerPunch : MonoBehaviour { private SpriteRenderer spriteRenderer; // Referência ao SpriteRenderer

void Start()
{

    spriteRenderer = GetComponent<SpriteRenderer>();
}

void Update()
{

    if (Input.GetMouseButtonDown(0))
    {
        Debug.Log("Botão esquerdo do mouse pressionado!");
        ChangeColor(); // Chama o método que muda a cor
    }
}

void ChangeColor()
{

    spriteRenderer.color = new Color(Random.value, Random.value, Random.value);
}

}