Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CreateWorld : MonoBehaviour {
- public int pituus = 10; //maailman pituus
- public int leveys = 10; //maailman leveys
- float korkeus = 1; //mille korkeudelle laatat asetetaan
- static int[,] Gridi; //varsinaisen maailman koordinaatit, vasemman alakulman indeksi 0,0
- public GameObject laatta; // Pelilaatan perusmalli
- int playerBlueX, playerBlueY;
- public GameObject PlayerBlueprefab;
- int player1Score = 1;
- int gameStatus = 1; // tällä voidaan jatkossa lopettaa pelaajien ohjaaminen tarvittaessa
- GameObject blueTail1, blueTail2, blueTail3;
- GameObject PlayerBlue;
- Color32 empty = new Color32(127, 127, 127, 255); //Peruslaatan väri
- Color32 border = new Color32(64, 64, 64, 255); //Reuna/Estelaatan väri
- Color32 colorBlue = new Color32(0, 102, 204, 255); //Sinisen pelaajan laatan väri
- Color32 colorBlueTail1 = new Color32(0, 128, 255, 255); //sinisen pelaajan "häntä"
- Color32 colorBlueTail2 = new Color32(51, 153, 255, 255);
- Color32 colorBlueTail3 = new Color32(102, 178, 255, 255);
- // suunnat: 1=ylös 2=oikea 3=alas 4=vasen
- // peliruudun statukset:
- // 0=reunaruutu, ei voi liikkua niihin
- // 1=koskematon, valkea ruutu
- // 2=PlayerBlue
- // 3=PlayerRed
- // 4=PlayerBlueTail //näille tail-statuksille käyttöä vain jos hännässä eri toiminta kuin player-laatoissa
- // 5=PlayerRedTail
- // Use this for initialization
- void Start () {
- Gridi = new int[pituus, leveys];
- GridDefinition(); // määrittää maailman ruudukkona, joka sisältää nollia ja ykkösiä
- CreatePlates(); // luo laatat gridin perusteella
- playerBlueX = Random.Range(1, 9);
- playerBlueY = Random.Range(1, 9);
- PlayerBlue = Instantiate(PlayerBlueprefab, new Vector3(playerBlueX * 10, korkeus +3, playerBlueY * 10), Quaternion.identity);
- GameObject.Find(playerBlueX + "-" + playerBlueY).GetComponent<Renderer>().material.SetColor("_Color", colorBlue);
- Gridi[playerBlueX, playerBlueY] = 2;
- //luodaan väliaikaiset viittaukset "häntä" objekteihin myöhempää käyttöä varten.
- string blueTail1NameTemp = (playerBlueX + "-" + playerBlueY);
- blueTail1 = GameObject.Find(blueTail1NameTemp);
- blueTail2 = GameObject.Find(blueTail1NameTemp);
- }
- void GridDefinition() // luo taulukon/pelilaudan, kooltaan pituus X leveys (i,j)
- {
- for (int i = 0; i < pituus; i++)
- {
- for (int j = 0; j < leveys; j++)
- {
- if (i == 0 || j == 0 || i == pituus - 1 || j == leveys - 1) // reunimmaisiin laitetaan arvoksi nolla, muihin yksi
- {
- Gridi[i, j] = 0;
- }
- else
- {
- int temp = Random.Range(1, 20);
- if (temp >= 2)
- {
- Gridi[i, j] = 1;
- }
- else
- {
- Gridi[i, j] = 0;
- }
- }
- }
- }
- }
- void CreatePlates()
- {
- GameObject laattaInstance;
- for (int i = 0; i < pituus; i++)
- {
- for (int j = 0; j < leveys; j++)
- {
- if (Gridi[i, j] == 1)
- {
- laattaInstance = Instantiate(laatta, new Vector3(i * 10, korkeus, j * 10), Quaternion.Euler(0, 0, 0)) as GameObject;
- laattaInstance.GetComponent<Renderer>().material.SetColor("_Color", empty);
- laattaInstance.name = i + "-" + j; //antaa laatalle nimen joka koostuu sen koordinaateista
- }
- else
- {
- laattaInstance = Instantiate(laatta, new Vector3(i * 10, korkeus, j * 10), Quaternion.Euler(0, 0, 0)) as GameObject;
- laattaInstance.name = i + "-" + j;
- laattaInstance.GetComponent<Renderer>().material.SetColor("_Color", border);
- }
- }
- }
- }
- private void Update()
- {
- if ((Input.GetKeyDown("up")) && (gameStatus == 1))
- {
- MovePlayerBlue(1);
- }
- if ((Input.GetKeyDown("down")) && (gameStatus == 1))
- {
- MovePlayerBlue(3);
- }
- if ((Input.GetKeyDown("left")) && (gameStatus == 1))
- {
- MovePlayerBlue(4);
- }
- if ((Input.GetKeyDown("right")) && (gameStatus == 1))
- {
- MovePlayerBlue(2);
- }
- }
- void MovePlayerBlue(int suunta)
- {
- //siirretään viittausta häntälaatassa eteenpäin
- blueTail3 = blueTail2;
- blueTail2 = blueTail1;
- //tallenna ennen liikettä alla olleen laatan nimi
- string blueTail1Name = (playerBlueX + "-" + playerBlueY);
- Gridi[playerBlueX, playerBlueY] = 2;
- //luodaan viittaus edelliseen laattaan
- blueTail1 = GameObject.Find(blueTail1Name);
- switch (suunta) // tehdään liike suunnan perusteella
- {
- case 1:
- //tarkistetaan voiko liikkua YLÖS, vai onko tukossa
- if (Gridi[playerBlueX, playerBlueY + 1] != 0 && Gridi[playerBlueX, playerBlueY + 1] != 3)
- { //liike
- PlayerBlue.gameObject.transform.Translate(Vector3.forward * 10f, Space.World);
- playerBlueY++;
- if (Gridi[playerBlueX, playerBlueY] == 1)
- {
- player1Score++;
- }
- }
- break;
- case 2:
- //tarkistetaan voiko liikkua OIKEALLE, vai onko tukossa
- if (Gridi[playerBlueX + 1, playerBlueY] != 0 && Gridi[playerBlueX + 1, playerBlueY] != 3)
- {
- PlayerBlue.gameObject.transform.Translate(Vector3.right * 10f, Space.World);
- playerBlueX++;
- if (Gridi[playerBlueX, playerBlueY] == 1)
- {
- player1Score++;
- }
- }
- break;
- case 3:
- //tarkistetaan voiko liikkua ALAS, vai onko tukossa
- if (Gridi[playerBlueX, playerBlueY - 1] != 0 && Gridi[playerBlueX, playerBlueY - 1] != 3)
- {
- PlayerBlue.gameObject.transform.Translate(Vector3.back * 10f, Space.World);
- playerBlueY--;
- if (Gridi[playerBlueX, playerBlueY] == 1)
- {
- player1Score++;
- }
- }
- break;
- case 4:
- //tarkistetaan voiko liikkua VASEMMALLE, vai onko tukossa
- if (Gridi[playerBlueX - 1, playerBlueY] != 0 && Gridi[playerBlueX - 1, playerBlueY] != 3)
- {
- PlayerBlue.gameObject.transform.Translate(Vector3.left * 10f, Space.World);
- playerBlueX--;
- if (Gridi[playerBlueX, playerBlueY] == 1)
- {
- player1Score++;
- }
- }
- break;
- }
- GameObject.Find(playerBlueX + "-" + playerBlueY).GetComponent<Renderer>().material.SetColor("_Color", colorBlue);
- Gridi[playerBlueX, playerBlueY] = 2;
- blueTail3.GetComponent<Renderer>().material.SetColor("_Color", colorBlueTail3);
- blueTail2.GetComponent<Renderer>().material.SetColor("_Color", colorBlueTail2);
- blueTail1.GetComponent<Renderer>().material.SetColor("_Color", colorBlueTail1);
- return;
- }
- private void OnGUI()
- {
- GUI.skin.label.fontSize = 20;
- GUI.Label(new Rect(10, 10, 200, 50), "Blue Score: " + player1Score);
- GUI.Label(new Rect(200, 10, 200, 50), "Red Score: " + 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement