Четверг, 21 Ноября 2024, 21:19

Приветствую Вас Гость

[ Новые сообщения · Игроделы · Правила · Поиск ]
  • Страница 1 из 1
  • 1
Результаты поиска
alewДата: Воскресенье, 16 Февраля 2020, 23:13 | Сообщение # 1 | Тема: Город для игры на Unity
уже был
Сейчас нет на сайте
Цитата drcrack ()
дедом с 50-летним опытом программирования на Си

Ты мне льстишь. ^_^
alewДата: Воскресенье, 16 Февраля 2020, 12:18 | Сообщение # 2 | Тема: Город для игры на Unity
уже был
Сейчас нет на сайте
Повесь на камеру

Код


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

public class createDis : MonoBehaviour
{
     List<int> listInfo = new List<int>();// владелец, дата постройки, количество этажей
    string[] owner  = { "S O N Y",  "SAMSUNG",  "B M W", "COMMERCIAL", "residential 1","residential 2",
        "residential 3", "residential 4","commercial2", "commercial3" } ;
    int[] price = {10,5,23,13,9,7,5,3,6,8 };
    GameObject cursor;
    Vector3 cursorPos;
    GameObject plane;
    enum status { no, move};
    status mouse = status.no;
    Vector2 mt;
    Vector2 screenSize;
    bool drawInfo = false;
    public string nameSel ;
    string[] textInfo = { "owner", "data", "price", "floor" };
    Rect winInfoRect;
    void Start()
    {
        plane = GameObject.CreatePrimitive(PrimitiveType.Quad);
        plane.transform.position = Vector3.zero;
        plane.transform.eulerAngles = new Vector3(90, 0, 0);
        plane.transform.localScale = new Vector3(100, 100,1);
        plane.name = "zemlya";
        Destroy(plane.GetComponent<Collider>());
        Renderer r = plane.GetComponent<MeshRenderer>();
        r.material = new Material(Shader.Find("Standard"));
        r.material.color = new Vector4(0.343f, 0.268f, 0.433f, 1.0f);
        r.material.name = "zemCol";
        createCUBE(plane.transform.position, new Vector3(100, 100, 1), 0.11f);
        
        transform.position = new Vector3(-5.6f, 38.0f, -49.0f);
        transform.eulerAngles = new Vector3(49.0f, 0.0f, 0.0f);
        Camera.main.fieldOfView = 49;
        screenSize = new Vector2(Screen.width, Screen.height);

        cursor = GameObject.CreatePrimitive(PrimitiveType.Cube);
        Destroy(cursor.GetComponent<Collider>());
        Renderer rc = cursor.GetComponent<MeshRenderer>();
        rc.material = new Material(Shader.Find("Standard"));
        rc.material.color = new Vector4(0.99f, 0.0f, 0.07f, 1.0f);
        winInfoRect = new Rect(0, 0, screenSize.x / 5, screenSize.y / 3);
    }
    void Update()
    {
        if(Input.GetMouseButtonDown(0) && mouse == status.no)
        {
            mouse = status.move;
            mt = Input.mousePosition;
        }
        if(Input.GetMouseButtonUp(0) && mouse == status.move)
        {
            mouse = status.no;
        }
        if(mouse == status.move)
        {
            Vector3 pos = transform.position;
            pos.x +=  (mt.x - Input.mousePosition.x)/screenSize.x *100;
            pos.z += (mt.y - Input.mousePosition.y) / screenSize.y * 100;
            transform.position = pos;
            mt = Input.mousePosition;
            drawInfo = false;
        }

        if(mouse == status.no)
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Physics.Raycast(ray, out hit);
            if (hit.collider != null)
            {
                nameSel = hit.collider.gameObject.name;
                drawInfo = true;
                int numShift;
                string n = nameSel.Substring(6, nameSel.Length - 6);
                int.TryParse(n, out numShift);
                textInfo[0] ="owner: " + owner[listInfo[numShift * 10]];
                textInfo[1] = "date: " + listInfo[numShift * 10 + 1];
                textInfo[2] = "floor: " + listInfo[numShift * 10 + 2];
                textInfo[3] = "price: " + listInfo[numShift * 10 + 3];
                Vector3 posCur = new Vector3(listInfo[numShift * 10 + 4], listInfo[numShift * 10 + 5], listInfo[numShift * 10 + 6]);
                Vector3 scalCur = new Vector3(listInfo[numShift * 10 + 7], listInfo[numShift * 10 + 8], listInfo[numShift * 10 + 9]);
                scalCur *= 1.1f;
                cursor.SetActive(true);
                cursor.transform.position = posCur;
                cursor.transform.localScale = scalCur;
            }
            else
            {
                drawInfo = false;
                cursor.SetActive(false);
            }
        }

    }
    private void OnGUI()
    {
        if (drawInfo)
        {
            GUI.Window(1, winInfoRect, WinFunc, "INFO");
        }
    }
    public void WinFunc( int num)
    {
        GUILayout.Button(textInfo[0]);
        GUILayout.Button(textInfo[1]);
        GUILayout.Button(textInfo[2]);
        GUILayout.Button(textInfo[3]);
    }
    void createCUBE(Vector3 pos, Vector3 scal, float qual)
    {
        int namCount = 0;
        //GameObject g;
        int w, l, h;
        bool findFree, next;
        float sX = pos.x - scal.x/2;
        float sZ = pos.z - scal.y / 2;
        int[,] m = new int[(int)scal.x, (int)scal.y];
        m.Initialize();
        int minCell = 7;
        
            for (int y = 0; y < scal.y- minCell; y++)
            for (int x = 0; x < scal.x - minCell; x++)
            {
                int r = Random.Range(1, 10);
                if (r < Random.Range(1, 10 * qual))
                {
                     w = Random.Range(1, 5);
                     l = Random.Range(1, 5);
                     h = Random.Range(1, 10);
                    findFree = true;
                    while (findFree)
                    {
                        next = false;
                        for (int xx = 0; xx < w; xx++)
                        {
                            for (int yy = 0; yy < l; yy++)
                            {
                    if (m[x + xx, y + yy] == 1)
                    {
                    next = true;
                    break;
                    }
                            }
                            if (next) break;
                        }
                        if (next)
                        {
                            x += 1;
                            if (x >= scal.x - minCell)
                            {
                    x = 0;
                    y += 1;
                    if (y >= scal.y - minCell)
                    {
                    y = (int)scal.y;
                    }
                            }
                        }
                        else findFree = false;
                    }
                    for (int xx = 0; xx < w; xx++)
                        for (int yy = 0; yy < l; yy++)
                        {
                            m[x + xx, y + yy] = 1;
                        }
                    GameObject gr = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    gr.transform.position = new Vector3(sX + x, h*0.5f, sZ + y);
                    gr.transform.localScale = new Vector3(w, h, l) ;
                    gr.name = "house_" + namCount.ToString();
                    houseInfo(gr.transform.position, gr.transform.localScale);
                    namCount += 1;
                    x += w + 1;
                    if(x >= scal.x - minCell)
                    {
                        x = 0;
                        y += 2;
                    }
                }
            }
        

    }
    void houseInfo(Vector3 pos, Vector3 scal)
    {
        int r = Random.Range(0, owner.Length);
        listInfo.Add(r); // владелец
        listInfo.Add(Random.Range(1990, 2000)); // дата построики
        listInfo.Add(((int)scal.y)); // сколько этажей
        listInfo.Add((int)scal.x * (int)scal.z * (int)scal.y * price[r]); // стоимость здания
        listInfo.Add((int)pos.x);
        listInfo.Add((int)pos.y);
        listInfo.Add((int)pos.z);
        listInfo.Add((int)scal.x) ;
        listInfo.Add((int)scal.y);
        listInfo.Add((int)scal.z);
    }
}
  • Страница 1 из 1
  • 1
Поиск:

Все права сохранены. GcUp.ru © 2008-2024 Рейтинг