nickmattinson / ZXYZ

Top-Down 2D Dungeon Crawler
1 stars 0 forks source link

PlayerAttack #14

Open nickmattinson opened 3 months ago

nickmattinson commented 3 months ago
git fetch origin
git checkout 14-playerattack
nickmattinson commented 2 months ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AOECollisionManager : MonoBehaviour
{

    [SerializeField] float damageAmount;
    [SerializeField] private float duration;
    private Player player;
    // Start is called before the first frame update
    void Start()
    {
        player = FindObjectOfType<Player>();
        Destroy(gameObject, duration);
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log($"[{this.name}] collided with [{other.name}]");
        if (other.CompareTag("Enemy"))
        {
            Enemy enemy = other.GetComponent<Enemy>();
            if (enemy != null)
            {
                enemy.TakeDamage(player);
            }
        }
    }
}
nickmattinson commented 2 months ago
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using TMPro;
using UnityEngine.AI;

public class Player : Entity
{
    public StateManager stateManager;
    public int score;  // should be private
    public string username;  // should be private
    private float aoeAttackTimer;
    [SerializeField] private float aoeAttackCooldown;  //set in unity inspector
    private TextMeshProUGUI scoreText;
    [SerializeField] private GameObject AOEAttack;
    private float attackDuration = 0.3f;

    //[SerializeField] LineRenderer lineRenderer; // Reference to LineRenderer component

    public new void Awake(){
        // used for initial setup that
        // doesn't rely on other objects
        // or components being initialized.

        // get rid of the Clone reference    
        this.name = this.name.Replace("(Clone)","").Trim();

        SetLevel(1);
        SetAttack(3);
        SetHealth(200);
        SetDefense(2);
        username = "Unknown player";
        //Debug.Log($"[{this.name}] {this} ____ AWAKE.");

    }

    public new void Start(){
        // used for initial setup that
        // does rely on other objects
        // or components being initialized.

        stateManager = FindObjectOfType<StateManager>();
        this.transform.position = new Vector2(0f,90f);
        Debug.Log($"[{this.name}] {this} ____ STARTED.");

        AOEAttack.SetActive(true);

    }

    void Update()
    {
        if(aoeAttackTimer > 0){
            aoeAttackTimer -= Time.deltaTime;
        }
        if (Input.GetMouseButtonDown(0))
        {
            // Convert mouse position to world space in 2D
            Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            // Perform a raycast in 2D from the mouse position
            RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector2.zero);

            if (hit.collider != null)
            {
                Enemy enemy = hit.collider.GetComponent<Enemy>();
                if (enemy != null)
                {
                    // Deal damage to the enemy
                    Debug.Log($"Player's attack is {this.GetAttack()}");
                    enemy.TakeDamage(this);
                }

                TutorialEnemy tutorialEnemy = hit.collider.GetComponent<TutorialEnemy>();
                if (tutorialEnemy != null)
                {
                    // Deal damage to the enemy
                    Debug.Log($"Player's attack is {this.GetAttack()}");
                    tutorialEnemy.TakeDamage(this);
                }
            }
        } else if (Input.GetMouseButtonDown(1) && aoeAttackTimer <= 0){
                Debug.Log($"[{this.name}] attacking with AOE Attack.");
                aoeAttackTimer = aoeAttackCooldown;
                // Instantiate the rectangular sprite at the mouse position
                Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                mousePosition.z = 0f; // Ensure the z-coordinate is appropriate for 2D space

                GameObject aoeAttackInstance = Instantiate(AOEAttack, mousePosition, Quaternion.identity);
                //StartCoroutine(DestroyAOEAttackAfterDelay(aoeAttackInstance));
        }
    }

    private IEnumerator DestroyAOEAttackAfterDelay(GameObject aoeAttackInstance)
    {
        // Adjust the duration as needed
        yield return new WaitForSeconds(attackDuration); 
        Destroy(aoeAttackInstance);
    }

    public void ActivatePowerUp(string powerUp)
    {
        switch(powerUp) 
        {
            case "AttackUp":
                // code block
                AttackUp(2);  // max of xx
                break;
            case "HealthUp":
                // code block
                HealthUp(20); // max of xxx
                break;
            case "DefenseUp":
                // code block
                DefenseUp(2);  // max of xxx
                break;
            default:
                // code block
                break;
        }
    }

    protected override void Die()
    {
        Debug.Log($"Player {username} has died. Score: {score}");
        //scoreText.text = score.ToString();
        stateManager.loadGameOver();
    }

    public void SetScore(int score){
        this.score = score;
    }

    public int GetScore(){
        return this.score;
    }

    public void SetUsername(string username){
        this.username = username;
    }

    public string GetUsername(){
        return username;
    }

    public override string ToString()
    {
        return $"{username}, Level: {this.GetLevel()}, Health: {this.GetHealth()}, Defense: {this.GetDefense()}, Attack: {this.GetAttack()}";
    }
}