Advertisement
JonneOpettaja

CreateWorld

Feb 17th, 2019
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CreateWorld : MonoBehaviour {
  6.  
  7.     public int pituus = 10; //maailman pituus
  8.     public int leveys = 10; //maailman leveys
  9.     float korkeus = 1;  //mille korkeudelle laatat asetetaan
  10.     static int[,] Gridi;                   //varsinaisen maailman koordinaatit, vasemman alakulman indeksi 0,0
  11.     public GameObject laatta;       // Pelilaatan perusmalli
  12.     int playerBlueX, playerBlueY;
  13.     public GameObject PlayerBlueprefab;
  14.     int player1Score = 1;
  15.     int gameStatus = 1;         // tällä voidaan jatkossa lopettaa pelaajien ohjaaminen tarvittaessa
  16.     GameObject blueTail1, blueTail2, blueTail3;
  17.     GameObject PlayerBlue;
  18.  
  19.     Color32 empty = new Color32(127, 127, 127, 255);    //Peruslaatan väri
  20.     Color32 border = new Color32(64, 64, 64, 255);      //Reuna/Estelaatan väri
  21.     Color32 colorBlue = new Color32(0, 102, 204, 255);  //Sinisen pelaajan laatan väri
  22.     Color32 colorBlueTail1 = new Color32(0, 128, 255, 255); //sinisen pelaajan "häntä"
  23.     Color32 colorBlueTail2 = new Color32(51, 153, 255, 255);
  24.     Color32 colorBlueTail3 = new Color32(102, 178, 255, 255);
  25.  
  26.     // suunnat: 1=ylös 2=oikea 3=alas 4=vasen
  27.     // peliruudun statukset:
  28.     // 0=reunaruutu, ei voi liikkua niihin
  29.     // 1=koskematon, valkea ruutu
  30.     // 2=PlayerBlue
  31.     // 3=PlayerRed
  32.     // 4=PlayerBlueTail   //näille tail-statuksille käyttöä vain jos hännässä eri toiminta kuin player-laatoissa
  33.     // 5=PlayerRedTail
  34.  
  35.     // Use this for initialization
  36.     void Start () {
  37.         Gridi = new int[pituus, leveys];
  38.         GridDefinition(); // määrittää maailman ruudukkona, joka sisältää nollia ja ykkösiä
  39.         CreatePlates();      // luo laatat gridin perusteella
  40.         playerBlueX = Random.Range(1, 9);
  41.         playerBlueY = Random.Range(1, 9);
  42.         PlayerBlue = Instantiate(PlayerBlueprefab, new Vector3(playerBlueX * 10, korkeus +3, playerBlueY * 10), Quaternion.identity);
  43.         GameObject.Find(playerBlueX + "-" + playerBlueY).GetComponent<Renderer>().material.SetColor("_Color", colorBlue);
  44.         Gridi[playerBlueX, playerBlueY] = 2;
  45.  
  46.         //luodaan väliaikaiset viittaukset "häntä" objekteihin myöhempää käyttöä varten.
  47.         string blueTail1NameTemp = (playerBlueX + "-" + playerBlueY);
  48.         blueTail1 = GameObject.Find(blueTail1NameTemp);
  49.         blueTail2 = GameObject.Find(blueTail1NameTemp);
  50.     }
  51.  
  52.     void GridDefinition() // luo taulukon/pelilaudan, kooltaan pituus X leveys (i,j)
  53.     {
  54.         for (int i = 0; i < pituus; i++)
  55.         {
  56.             for (int j = 0; j < leveys; j++)
  57.             {
  58.                 if (i == 0 || j == 0 || i == pituus - 1 || j == leveys - 1) // reunimmaisiin laitetaan arvoksi nolla, muihin yksi
  59.                 {
  60.                     Gridi[i, j] = 0;
  61.                 }
  62.                 else
  63.                 {
  64.                     int temp = Random.Range(1, 20);
  65.                     if (temp >= 2)
  66.                     {
  67.                         Gridi[i, j] = 1;
  68.                     }
  69.                     else
  70.                     {
  71.                         Gridi[i, j] = 0;
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     void CreatePlates()
  79.     {
  80.         GameObject laattaInstance;
  81.         for (int i = 0; i < pituus; i++)
  82.         {
  83.             for (int j = 0; j < leveys; j++)
  84.             {
  85.  
  86.                 if (Gridi[i, j] == 1)
  87.                 {
  88.                     laattaInstance = Instantiate(laatta, new Vector3(i * 10, korkeus, j * 10), Quaternion.Euler(0, 0, 0)) as GameObject;
  89.                     laattaInstance.GetComponent<Renderer>().material.SetColor("_Color", empty);
  90.                     laattaInstance.name = i + "-" + j; //antaa laatalle nimen joka koostuu sen koordinaateista
  91.                 }
  92.                 else
  93.                 {
  94.                     laattaInstance = Instantiate(laatta, new Vector3(i * 10, korkeus, j * 10), Quaternion.Euler(0, 0, 0)) as GameObject;
  95.                     laattaInstance.name = i + "-" + j;
  96.                     laattaInstance.GetComponent<Renderer>().material.SetColor("_Color", border);
  97.                 }
  98.  
  99.             }
  100.         }
  101.     }
  102.  
  103.     private void Update()
  104.     {
  105.         if ((Input.GetKeyDown("up")) && (gameStatus == 1))
  106.         {
  107.             MovePlayerBlue(1);
  108.         }
  109.         if ((Input.GetKeyDown("down")) && (gameStatus == 1))
  110.         {
  111.             MovePlayerBlue(3);
  112.         }
  113.         if ((Input.GetKeyDown("left")) && (gameStatus == 1))
  114.         {
  115.             MovePlayerBlue(4);
  116.         }
  117.         if ((Input.GetKeyDown("right")) && (gameStatus == 1))
  118.         {
  119.             MovePlayerBlue(2);
  120.         }
  121.     }
  122.  
  123.     void MovePlayerBlue(int suunta)
  124.     {
  125.         //siirretään viittausta häntälaatassa eteenpäin
  126.         blueTail3 = blueTail2;
  127.         blueTail2 = blueTail1;
  128.  
  129.         //tallenna ennen liikettä alla olleen laatan nimi
  130.         string blueTail1Name = (playerBlueX + "-" + playerBlueY);
  131.         Gridi[playerBlueX, playerBlueY] = 2;
  132.         //luodaan viittaus edelliseen laattaan
  133.         blueTail1 = GameObject.Find(blueTail1Name);
  134.  
  135.         switch (suunta) // tehdään liike suunnan perusteella
  136.         {
  137.             case 1:
  138.                 //tarkistetaan voiko liikkua YLÖS, vai onko tukossa
  139.                 if (Gridi[playerBlueX, playerBlueY + 1] != 0 && Gridi[playerBlueX, playerBlueY + 1] != 3)
  140.                 { //liike
  141.                     PlayerBlue.gameObject.transform.Translate(Vector3.forward * 10f, Space.World);
  142.                     playerBlueY++;
  143.                     if (Gridi[playerBlueX, playerBlueY] == 1)
  144.                     {
  145.                         player1Score++;
  146.                     }
  147.                 }
  148.                 break;
  149.             case 2:
  150.                 //tarkistetaan voiko liikkua OIKEALLE, vai onko tukossa
  151.                 if (Gridi[playerBlueX + 1, playerBlueY] != 0 && Gridi[playerBlueX + 1, playerBlueY] != 3)
  152.                 {
  153.                     PlayerBlue.gameObject.transform.Translate(Vector3.right * 10f, Space.World);
  154.                     playerBlueX++;
  155.                     if (Gridi[playerBlueX, playerBlueY] == 1)
  156.                     {
  157.                         player1Score++;
  158.                     }
  159.                 }
  160.                 break;
  161.             case 3:
  162.                 //tarkistetaan voiko liikkua ALAS, vai onko tukossa
  163.                 if (Gridi[playerBlueX, playerBlueY - 1] != 0 && Gridi[playerBlueX, playerBlueY - 1] != 3)
  164.                 {
  165.                     PlayerBlue.gameObject.transform.Translate(Vector3.back * 10f, Space.World);
  166.                     playerBlueY--;
  167.                     if (Gridi[playerBlueX, playerBlueY] == 1)
  168.                     {
  169.                         player1Score++;
  170.                     }
  171.                 }
  172.                 break;
  173.             case 4:
  174.                 //tarkistetaan voiko liikkua VASEMMALLE, vai onko tukossa
  175.                 if (Gridi[playerBlueX - 1, playerBlueY] != 0 && Gridi[playerBlueX - 1, playerBlueY] != 3)
  176.                 {
  177.                     PlayerBlue.gameObject.transform.Translate(Vector3.left * 10f, Space.World);
  178.                     playerBlueX--;
  179.                     if (Gridi[playerBlueX, playerBlueY] == 1)
  180.                     {
  181.                         player1Score++;
  182.                     }
  183.                 }
  184.                 break;
  185.         }
  186.  
  187.         GameObject.Find(playerBlueX + "-" + playerBlueY).GetComponent<Renderer>().material.SetColor("_Color", colorBlue);
  188.  
  189.         Gridi[playerBlueX, playerBlueY] = 2;
  190.         blueTail3.GetComponent<Renderer>().material.SetColor("_Color", colorBlueTail3);
  191.         blueTail2.GetComponent<Renderer>().material.SetColor("_Color", colorBlueTail2);
  192.         blueTail1.GetComponent<Renderer>().material.SetColor("_Color", colorBlueTail1);
  193.  
  194.         return;
  195.     }
  196.  
  197.     private void OnGUI()
  198.     {
  199.         GUI.skin.label.fontSize = 20;
  200.         GUI.Label(new Rect(10, 10, 200, 50), "Blue Score: " + player1Score);
  201.         GUI.Label(new Rect(200, 10, 200, 50), "Red Score: " + 0);
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement