Friday, August 14, 2015

Making a Lights Out Game in Unity 5 - Part 1


So a while ago I did a few Unreal Engine tutorials, and now I am doing some tutorial to learn Unity 5 from Digital Tutors. This tutorial is about making a simple game of lights out for mobile devices using C#.

The screenshot above shows the simple set up of the game so far. I made a cube and attached a couple of scrips and made a prefab out of the cube game object. The scripts that I wrote so far are at the bottom of this entry.

There is also a simple XML file that I made that makes it possible to "create" a level by typing out which cubes should be in the "on" or "off" position. This should come in had later when I need to make more levels.

I have only finished 4 of the tutorial episodes so far, I will post more stuff as I'm done. That's all for now, thanks for reading!

lightSwitch.cs

using UnityEngine;
using System.Collections;

public class lightSwitch : MonoBehaviour {

public bool isOn = false;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

public void change(){
if (isOn) {
isOn=false;
this.transform.localEulerAngles = new Vector3 (0,45,0);
}else{
isOn=true;
this.transform.localEulerAngles = Vector3.zero;
}
}
}

turnManager.cs

using UnityEngine;
using System.Collections;

public class turnManager : MonoBehaviour {

// Use this for initialization
void Start () {
int count = 1;

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
GameObject tmpGb = Instantiate (Resources.Load ("Cube", typeof(GameObject))) as GameObject;
tmpGb.transform.position = new Vector3 (j * 1.5f - 3, i * -1.5f + 3, 0);
tmpGb.name = count.ToString ();
count++;
}
}
this.gameObject.GetComponent<levelHandler> ().loadLevel (1);
}

// Update is called once per frame
void Update () {
if (Input.GetMouseButtonUp (0)) {
RaycastHit hit = new RaycastHit ();
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100)) {
makeMove (int.Parse(hit.collider.gameObject.name));
}
}
}

void makeMove(int name){
turn (name);
turn (name + 5);
turn (name - 5);
if (name % 5 != 0) {
turn (name + 1);
}
if (name % 5 != 1) {
turn (name - 1);
}
}

void turn(int name) {
if (name < 1 || name > 25)
return;
GameObject turnObj = GameObject.Find (name.ToString ()).gameObject;
turnObj.GetComponent<lightSwitch> ().change ();
}

}

levelHandler.cs

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

public class levelHandler : MonoBehaviour {

XmlDocument levelDoc;
XmlNodeList levelList;
List<string> levelArray;

// Use this for initialization
void Start () {
levelArray = new List<string> ();
levelDoc = new XmlDocument();
TextAsset xmlfile = Resources.Load ("levels", typeof(TextAsset)) as TextAsset;
levelDoc.LoadXml (xmlfile.text);
levelList = levelDoc.GetElementsByTagName("level");

foreach (XmlNode leveldata in levelList) {
XmlNodeList levelinfo = leveldata.ChildNodes;
Debug.Log (levelList.Count);
foreach (XmlNode data in levelinfo) {
Debug.Log(data.Name);
if(data.Name == "setup"){
Debug.Log(data.InnerText);
levelArray.Add(data.InnerText);
}
}
}
}

// Update is called once per frame
void Update () {

}

public void loadLevel(int nr){

string[] levString = levelArray [nr - 1].Split(',');

foreach (string brick in levString){
GameObject.Find(brick).GetComponent<lightSwitch>().change();
}
}
}

1 comment:

  1. What kind of component did you use for the lights in the scene.
    Fellow student assigned with a similar project.

    ReplyDelete