Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.2 kB
4
Indexable
Never
using UnityEngine;
using System;
using System.Collections;
public class ballscript : MonoBehaviour
{
    private int xdirection;
    private int ydirection;
    private int foo=1;
    private int bar=1;
    public float spd;
    System.Random rnd = new System.Random();
    void Start()
    {
        while(true)
        {
            xdirection=rnd.Next(-4,5);
            ydirection=rnd.Next(-4,5);
            if(Gcd(Mathf.Abs(xdirection),Mathf.Abs(ydirection))==1)
            {
                break;
            }
        }
        StartCoroutine(Cr());
    }
    int Gcd(int a, int b)
    {
        int r;
        if(a==0^b==0)
        {
            return 1;
        }
        while (b!=0)
        {
            r=a%b;
            a=b;
            b=r;
        }
        return a;
    }
    IEnumerator Cr()
    {
        while(true)
        {
        Move();
        yield return new WaitForSeconds(spd/(Mathf.Abs(Mathf.Abs(xdirection)-Mathf.Abs(ydirection))+1));
        }
    }
    void Move()
    {
        if(Mathf.Abs(transform.position.y)==8&&bar==Mathf.Abs(ydirection))
        {
            ydirection*=-1;
            xdirection-=Convert.ToInt32(rnd.Next(0,Convert.ToInt32(Mathf.Abs(xdirection+2)))==0)*Convert.ToInt32(Mathf.Sign(transform.position.x));
            int gcd=Gcd(Mathf.Abs(xdirection),Mathf.Abs(ydirection));
            ydirection/=gcd;
            xdirection/=gcd;
        }
        if(Mathf.Abs(transform.position.x)==13)
        {
            GameObject player=GameObject.FindWithTag("Player"+Convert.ToInt32(Mathf.Sign(transform.position.x)));
            int playerdirection;
            if(player.GetComponent<playermovement>().direction)
            {
                playerdirection=1;
            }
            else
            {
                playerdirection=-1;
            }
            if(Mathf.Abs(transform.position.y-player.transform.position.y)<=1.5||(Mathf.Abs(transform.position.y-player.transform.position.y)==2.5&&playerdirection==ydirection))
            {
                ydirection+=playerdirection;
                xdirection*=-1;
            }
        }
        if(Mathf.Abs(xdirection)==5)
        {
            xdirection=4*Convert.ToInt32(Mathf.Sign(xdirection));
        }
        if(foo>Mathf.Abs(xdirection))
        {
            foo=Mathf.Abs(xdirection);
        }
        if(bar>Mathf.Abs(ydirection))
        {
            bar=Mathf.Abs(ydirection);
        }
        transform.position= new Vector3(transform.position.x+Convert.ToInt32(foo==Mathf.Abs(xdirection))*Mathf.Sign(xdirection),transform.position.y+Convert.ToInt32(bar==Mathf.Abs(ydirection))*Mathf.Sign(ydirection),transform.position.z);
        if(transform.position.x==15)
        {
            GameObject.Find("GameManager").GetComponent<Gamemanager>().Roundended(Mathf.Sign(transform.position.x)==1);
        }
        if(foo==Mathf.Abs(xdirection))
        {
            foo=1;
        }
        else
        {
            foo++;
        }
        if(bar==Mathf.Abs(ydirection))
        {
            bar=1;
        }
        else
        {
            bar++;
        }     
    }
}