Hitler - RusiaDefense

Programación Orientada a Objetos (Tower defense - kingdom rush)

Moderator: julianmartinez16

User avatar
Vidal18
Posts: 18
Joined: Tue Jan 22, 2019 12:12 pm

Re: Hitler - RusiaDefense

Post by Vidal18 » Thu May 16, 2019 9:50 am

¿que se ha hecho?
se creo la vida y el daño de las torres, se corrigio un bug que ocasionaba que despues de la primera oleada los enemigos aparecieran con la vida baja y solo aguantaban una flecha, se creo la barra(se rediseño por completo), junto a 2 clases que la sostienen, se corrigio un bug que ocasionaba que aparecieran mas enemigos de lo normal, se corrigio un bug que ocasionaba qe las flechas se quedaran en el mapa, y que al comenzar la siguiente oleada, salieran disparadas del suelo hacia los enemigos, se corrigio un bug que hacia que las torres le siguieran disparando a los enemigos cuando morian, se corrigio un bug que ocasionaba que n-1 enemigo de la oleada n estuviera parpadeando mientras caminaba.
la beta oficial esta lista para la presentacion

¿que se va a hacer?
finalmente se desarrollaran sprites propios, de varios enemigos, y de varias torres, de tiles del mapa, de la fflecha y el sistema ui en general, el frontend del proyecto ya entro en juego

¿alguna dificultad?

luego de pasar toda la noche derecho corrigiendo bugs y sin comer, me entro debilidad en el cuerpo y tuve ciertos malestares, espero estr bien para la presentacion

Image

codigos modificados:

Code: Select all

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

public class BarScript : MonoBehaviour
{

    [SerializeField]
    private float fillAmount;

    [SerializeField]
    private Image content;

    public float MaxValue { get; set; }

    public float Value
    {
        set
        {
            //string[] tmp = valueText.text.Split(':');
            //valueText.text = tmp[0] + ": " + value;
            fillAmount = Map(value, 0, MaxValue, 0, 1);
        }
    }

    void Start()
    {
        //Value = 2;
    }

    void Update()
    {
        HandleBar();
    }

    private void HandleBar()
    {
        if (fillAmount != content.fillAmount)
        {
            content.fillAmount = fillAmount;
        }
    }

    public void Reset()
    {
        Value = MaxValue;
        content.fillAmount = 1;
    }

    private float Map(float value, float inMin, float inMax, float outMin, float outMax)
    {
        return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
    }
}

Code: Select all

using System.Collections;
using System.Collections.Generic;
//using UnityEngine;
using System;
using UnityEngine;

[Serializable]
public class Stats
{
    [SerializeField]
    private BarScript bar;

    [SerializeField]
    private float maxVal;

    [SerializeField]
    private float currentVal;

    public float CurrentVal
    {
        get
        {
            return currentVal;
        }

        set
        {

            this.currentVal = value;
            Bar.Value = currentVal;
        }
    }

    public float MaxVal
    {
        get
        {
            return maxVal;
        }

        set
        {
            this.maxVal = value;
            Bar.MaxValue = maxVal;
        }
    }

    public BarScript Bar
    {
        get
        {
            return bar;
        }
    }

    public void Initialize()
    {
        this.MaxVal = maxVal;
        this.CurrentVal = currentVal;
    }
}

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Monster : MonoBehaviour
{

    [SerializeField]
    private float speed;

    private Stack<Node> path;

    private SpriteRenderer spriteRenderer;

    [SerializeField]
    public Stats health;

    public bool Alive
    {
        get { return health.CurrentVal > 0; }
    }

    public Point GridPosition { get; set; }

    private Vector3 destination;

    private Animator anim;

    public bool IsActive { get; set; }

    private void Awake()
    {
        anim = GetComponent<Animator>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        health.Initialize();
    }

    private void Update()
    {
        Move();
    }

    public void Spawn(int health)
    {
        transform.position = LevelManager.Instance.BluePortal.transform.position;

        this.health.Bar.Reset();
        this.health.MaxVal = health;
        this.health.CurrentVal = this.health.MaxVal;

        StartCoroutine(Scale(new Vector3(0.1f, 0.1f), new Vector3(1, 1), false));

        SetPath(LevelManager.Instance.Path);
    }

    public IEnumerator Scale(Vector3 from, Vector3 to, bool remove)
    {
        //IsActive = false;

        float progress = 0;

        while (progress <= 1)
        {
            transform.localScale = Vector3.Lerp(from, to, progress);
            progress += Time.deltaTime;

            yield return null;
        }

        transform.localScale = to;

        IsActive = true;

        if (remove)
        {
            //Destroy(gameObject);
            Release();
        }
    }

    private void Move()
    {
        if (IsActive)
        {
            transform.position = Vector2.MoveTowards(transform.position, destination, speed * Time.deltaTime);

            if (transform.position == destination)
            {
                if (path != null && path.Count > 0)
                {
                    GridPosition = path.Peek().GridPosition;
                    destination = path.Pop().WorldPosition;
                }
            }
        }


    }

    private void SetPath(Stack<Node> newPath)
    {
        if (newPath != null)
        {
            this.path = newPath;

            GridPosition = path.Peek().GridPosition;
            destination = path.Pop().WorldPosition;
        }
    }

    //EL PREFAB DEL REDPORTAL NECESITA BOX COLLIDER2D Y TRIGGER CHEKEADO
    //TODOS LOS PREFAB DE LOS ENEMIGOS NECESITAN BOX COLLIDER2D Y TRIGGER CHEKEADO, TAMBIEN EL RIGIDBODY2D CON LA GRAVITY SCALE EN 0
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "portal")
        {
            //Debug.Log("Funciona el collider");

            //Empieza a encojerse el enemigo.
            StartCoroutine(Scale(new Vector3(1, 1), new Vector3(0.1f, 0.1f), true));

            //FALTA ANIMACION DEL PORTAL

            GameManager.Instance.Lives--;
        }

        
    }

    public void Release()
    {
        //remove debuffs
        //debuffs.Clear();
        IsActive = false;
        GridPosition = LevelManager.Instance.BlueSpawn;
        GameManager.Instance.RemoveMonster(this);
        GameManager.Instance.Pool.ReleaseObject(gameObject);

    }

    public void TakeDamage(int damage)
    {
        if (IsActive)
        {
            health.CurrentVal -= damage;

            if (health.CurrentVal <= 0)
            {
                GameManager.Instance.Currency += 2;

                anim.SetTrigger("Die");

                IsActive = false;

                Release();

               
            }
        }
    }
}

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Projectile : MonoBehaviour
{
    private Monster target;

    private Tower parent;
    


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        MoveToTarget();
    }

    public void Initialize(Tower parent)
    {
        this.target = parent.Target;
        this.parent = parent;
    }

    private void MoveToTarget()
    {
        if (target != null)
        {
            transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Time.deltaTime * parent.ProjectileSpeed);

            Vector2 dir = target.transform.position - transform.position;
            float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

            transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        }
        if(target != null && !target.Alive || target != null && !target.IsActive)
        {
            GameManager.Instance.Pool.ReleaseObject(gameObject);
           
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {

        if (other.tag == "Monster")
        {
            if (target.gameObject == other.gameObject)
            {
                if (target.gameObject == other.gameObject)
                {
                    target.TakeDamage(parent.Damage);
                    GameManager.Instance.Pool.ReleaseObject(gameObject);
                }
               
            }
            
               
                
            
            
                       
        }

    }
}

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tower : MonoBehaviour
{
    [SerializeField]
    private string projectileType;
    [SerializeField]
    private float projectileSpeed;
    [SerializeField]
    private int damage;

    public float ProjectileSpeed
    {
        get { return projectileSpeed; }
    }

    private SpriteRenderer mySpriteRenderer;

    private Monster target;
    public Monster Target
    {
        get { return target; }
    }

    public int Damage { get => damage; }

    private Queue<Monster> monsters = new Queue<Monster>();

    private bool canAttack = true;

    private float attackTimer;
    [SerializeField]
    private float attackCooldown;
    // Start is called before the first frame update
    void Start()
    {
        mySpriteRenderer = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        Attack();
    }

    public void Select()
    {
        mySpriteRenderer.enabled = !mySpriteRenderer.enabled;
    }

    private void Attack()
    {
        if (!canAttack)
        {
            attackTimer += Time.deltaTime;
            if (attackTimer >= attackCooldown)
            {
                canAttack = true;
                attackTimer = 0;
            }
        }

        if (target == null && monsters.Count > 0 && monsters.Peek().IsActive)
        {
            target = monsters.Dequeue();
        }
        if (target != null && target.IsActive)
        {
            if (canAttack)
            {
                Shoot();
                canAttack = false;
            }
        }

        if (target != null && !target.Alive || target != null && !target.IsActive)
        {
            target = null;
        }     

    }

    private void Shoot()
    {
        Projectile projectile = GameManager.Instance.Pool.GetObject(projectileType).GetComponent<Projectile>();
        projectile.transform.position = transform.position;
        projectile.Initialize(this);
    }

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Monster")
        {
            monsters.Enqueue(other.GetComponent<Monster>());
        } 
    }
    public void OnTriggerExit2D(Collider2D other)
    {
        if (other.tag == "Monster")
        {
            target = null;
        }
    }

}

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Objectpool : MonoBehaviour
{
    [SerializeField]
    private GameObject[] objectPrefabs;

    private List<GameObject> pooledObjects = new List<GameObject>(); 

    public GameObject GetObject(string type)
    {
        foreach (GameObject go in pooledObjects)
        {
            if (go.name == type && !go.activeInHierarchy)
            {
                go.SetActive(true);
                return go;
            }
        }

        for (int i = 0; i < objectPrefabs.Length; i++)
        {
            if (objectPrefabs[i].name == type)
            {
                GameObject newObject = Instantiate(objectPrefabs[i]);
                pooledObjects.Add(newObject);
                newObject.name = type;
                return newObject;
            }
        }

        return null;
    }

    public void ReleaseObject(GameObject gameObject)
    {
        gameObject.SetActive(false);
    }
}
programacion y diseño orientado a objetos :twisted:
black

User avatar
Vidal18
Posts: 18
Joined: Tue Jan 22, 2019 12:12 pm

Re: Hitler - RusiaDefense

Post by Vidal18 » Fri May 31, 2019 10:01 pm

Image
Image
Image
Image
Image
Image
programacion y diseño orientado a objetos :twisted:
black

Post Reply