IT rekvalifikace s garancí práce. Seniorní programátoři vydělávají až 160 000 Kč/měsíc a rekvalifikace je prvním krokem. Zjisti, jak na to!
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Lekce 23 - Unity (C#) Android: Konec hry

V minulé lekci, Unity (C#) Android: Návrat do menu, jsem se zabýval možností kdy se může hráč ještě před začátkem hry vrátit do menu.

Dnešní tutoriál už naplní mnou dlouho dávaný slib, kdy jsem říkal, že udělám konec hry. Zároveň mi přišlo jako dobrý nápad vytvořit tlačítko, které je volně škálovatelné, tedy bez deformace nebo ztráty kvality.

Video

Nový PlayerMoveScript

Upravený PlayerMoveScript přináší detekci smrti.

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

public class PlayerMoveScript : MonoBehaviour
{
    public LayerMask touchControlLayerMask;

    public GameObject StartGameButtonPanel;

    public Text countOfFireText;

    float flapAmount = 10;
    public float speed = 150;

    bool android;

    bool started = false;

    private bool paused = false;
    private Quaternion pausedRotation;

    public GameObject pauseIndicator;

    Animator anim;

    private bool didFlap = false;

    private GameObject flame;

    private float fireLength = 2;
    private float currentFlameTime = 0;
    private bool currentlyFlaming = false;

    private int countOfFires = 5;

    private bool died = false;

    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();

        flame = transform.GetChild(0).gameObject;

        if (Application.platform == RuntimePlatform.Android)
            android = true;
        else
            android = false;
    }

    // Update is called once per frame
    void Update ()
    {
        if (died)
        {
            Vector3 velDied = rigidbody2D.velocity;
            velDied.x = 0;
            rigidbody2D.velocity = velDied;
            return;
        }



        if (currentlyFlaming)
        {
            if (currentFlameTime <= 0)
            {
                currentFlameTime = 0;
                DeactivateFire();
            }
            else
            {
                currentFlameTime -= Time.deltaTime;
            }
        }


        if (Input.GetKeyDown(KeyCode.Escape))
        {
            paused = !paused;
            if (paused)
            {
                Time.timeScale = 0;
                //pauseIndicator.enabled = true;
                pauseIndicator.SetActive(true);
                pausedRotation = transform.rotation;
            }
            else
            {
                Time.timeScale = 1;
                //pauseIndicator.enabled = false;
                pauseIndicator.SetActive(false);
            }
        }

        if (paused)
        {

            if (!android)
            {
                if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
                {
                    didFlap = true;
                }
            }
            else
            {
                if (Input.touches.Length > 0)
                {
                    didFlap = true;
                }
            }

            transform.rotation = pausedRotation;
            return;
        }

        if (!started)
        {
            /*
            if (!android)
            {
                if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
                {
                    StartGame();
                }
            }
            else
            {
                if (Input.touches.Length > 0)
                {
                    StartGame();
                }
            }*/

            return;
        }


        Vector3 vel = rigidbody2D.velocity;


        if (didFlap)
        {
            didFlap = false;
            vel = Flap(vel);
        }

        if (!android)
        {
            if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
            {
                if(RaycastingDragon(Input.mousePosition))
                    ActivateFire();


                vel = Flap(vel);
            }
        }
        else
        {
            if (Input.touches.Length > 0)
            {
                vel = Flap(vel);
            }
        }

        vel.x = speed * Time.deltaTime;

        rigidbody2D.velocity = vel;


        RotateMe(vel.y);
    }

    private bool RaycastingDragon(Vector3 mousePos)
    {
        Ray r = Camera.main.ScreenPointToRay(mousePos);
        RaycastHit2D hit = Physics2D.Raycast(r.origin, r.direction, Mathf.Infinity, touchControlLayerMask);

        if (hit.collider == null)
        {
            return false;
        }
        return true;
    }

    public void StartGame()
    {
        started = true;
        rigidbody2D.gravityScale = 2.5f;

        Vector3 vel = rigidbody2D.velocity;
        vel = Flap(vel);

        rigidbody2D.velocity = vel;


        print("Called start game 1");

        StartGameButtonPanel.SetActive(false);
    }

    public void StartGame2()
    {
        started = true;
        rigidbody2D.gravityScale = 2.5f;

        Vector3 vel = rigidbody2D.velocity;
        vel = Flap(vel);

        rigidbody2D.velocity = vel;

        print("Called start game 2");

        StartGameButtonPanel.SetActive(false);
    }

    public void RestartGame()
    {
        Application.LoadLevel(Application.loadedLevel);
    }

    public void BackToMenu()
    {
        Application.LoadLevel("menu_4");
    }

    void RotateMe(float y)
    {
        // -20, 10
        // -70, 0

        // 0 -70

        if(y >= 0)
        {
            transform.rotation = Quaternion.Euler(0, 0, 0);
        }
        else
        {
            float angle = Mathf.Lerp(0, -70, -y);
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), 0.05f);
        }
    }

    Vector3 Flap(Vector3 v)
    {
        anim.SetTrigger("DoFlap");
        v.y = flapAmount;
        return v;
    }

    void ActivateFire()
    {
        if (countOfFires <= 0)
            return;

        countOfFires--;
        countOfFireText.text = "Fire count avalible: " + countOfFires;
        currentlyFlaming = true;
        flame.SetActive(true);
        currentFlameTime = fireLength;
    }

    void DeactivateFire()
    {
        currentlyFlaming = false;
        flame.SetActive(false);
    }

    public void Died()
    {
        died = true;
    }
}

Upravený HealthScript

using UnityEngine;
using System.Collections;

public class HealthScript : MonoBehaviour {

    int health = 100;
    float initSize;

    bool dead = false;

    public Animator PlayerAnimator;
    private PlayerMoveScript playerMoveScript;

    public GameObject buttonsPanel;

    // Use this for initialization
    void Start ()
    {

        playerMoveScript = PlayerAnimator.GetComponent<PlayerMoveScript>();

        initSize = transform.localScale.x;
        health = 100;
        RefreshScore();
    }

    public int Hit(int damage)
    {
        if (dead)
            return 0;


        health -= damage;
        RefreshScore();

        if (health <= 0)
            Die();

        return health;
    }

    void Die()
    {
        PlayerAnimator.SetTrigger("DiedTrigger");
        dead = true;
        playerMoveScript.Died();
        buttonsPanel.SetActive(true);
    }

    void RefreshScore()
    {
        //transform.GetChild(0).guiText.text = "Health: " + health;

        Vector3 scale = transform.localScale;
        scale.x = initSize / 100 * health;
        transform.localScale = scale;
    }
}

Jak hra vypadá nyní?

Přidaná tlačítka - Zdrojákoviště Unity - 2D hry

Problémy?

Pokud máte nějaké otázky, neváhejte se zeptat v komentářích nebo mi napsat do zpráv.

V příští lekci, Unity (C#) Android: Publikování na Google Play, se mrkneme na proces publikování hry na Google play.


 

Předchozí článek
Unity (C#) Android: Návrat do menu
Všechny články v sekci
Zdrojákoviště Unity - 2D hry
Přeskočit článek
(nedoporučujeme)
Unity (C#) Android: Publikování na Google Play
Článek pro vás napsal vratislavino
Avatar
Uživatelské hodnocení:
2 hlasů
Autor se věnuje programování v C#, především pak ve vývojovém prostředí Unity3D. Má asi pětileté zkušenosti s programováním a momentálně pracuje na připravované hře Azulgar: Beyond The Frontiers.
Aktivity