Builder.cs
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
public class Builder : MonoBehaviour
{
//this class is meant to handle building an item from a menu, such as a unit, upgrade, component, or something else
//can also be used to show
//position value to use for guiProgressBar
public Vector3 buildGuiLocation;
private Camera mainCam;
private int guiLayerZ = -3;
public float beginTimeOfBuild = 0.0f; //this is used to show how much time is elapsed for current build***
public float timeNeededToFinishBuild = 0.0f;
public bool buildStarted = false;
public BuildQueue currentBuildQueue;
//gui
public GUITexture progressBar;
private Texture progressBarBlueTex;
public float progressBarInitWidth;
public GUITexture progressBarBackground;
private Texture progressBarYellowTex;
public TextMesh labelOfBuilder;
//cancel button
//hook into UIToolkit and instantiate a button to cancel the current builder's task and destroy the builder
private UIButton cancelBuildButton;
public float elapsedTime = 0.0f;
public float curTime = 0;
//action
private GameObject actionObj;
public bool isResearchItem = false;
//debug
//-----------------------------------------------------
public GameObject debugObj; //assigned in inspector
public DebugGuiText debugGText;
//========================================================
void Awake()
{
mainCam = GameObject.Find("CamMainPivot/CameraFocus/Main Camera").camera;
progressBar = transform.Find("builderProgressBar").guiTexture;
progressBarBlueTex = Resources.Load("BuilderProgressBlue") as Texture;
progressBarYellowTex = Resources.Load("BuilderProgressYellow") as Texture;
labelOfBuilder = transform.Find("BuilderText").GetComponent("TextMesh") as TextMesh;
//set progress bar texture here
//if research item, make it blue
if(isResearchItem)
{
progressBar.texture = progressBarBlueTex;
}
else
{
progressBar.texture = progressBarYellowTex;
}
//if actual construction, then make it yellow
progressBarInitWidth = progressBar.pixelInset.width;
progressBarBackground = transform.Find("builderProgressBarBackground").guiTexture;
//=================================================================================
debugObj = (GameObject)GameObject.Find("Debug-gText");
debugGText = (DebugGuiText)debugObj.gameObject.GetComponent("DebugGuiText");
debugGText.permDebug("done with awake() ");
}
void Start ()
{
//debug output
debugGText.permDebug("start() of` builder"); //this line works
//=================================================================================
cancelBuildButton = UIButton.create("DeleteIcon2x.png","DeleteDownIcon2x.png",(int)(buildGuiLocation.x+progressBarInitWids-a32),(int)(Screen.height-buildGuiLocation.y));
}
// Update is called once per frame
void Update ()
{
debugGText.resetDebug();
debugGText.setDebug("pos: (" + transform.position.x + "," + transform.position.y + ")");
debugGText.setDebug("localPos: (" + transform.localPosition.x + "," + transform.localPosition.y + ")");
curTime = Time.time;
//new guiPos updated by build queue
if(buildStarted)
{
debugGText.setDebug("buildStarted");
elapsedTime = (Time.time - beginTimeOfBuild);
if((Time.time - beginTimeOfBuild) >= timeNeededToFinishBuild)
{
actionPerformed();
removeThisBuilderFromList();
}
else
{
updateBuilderGui();
}
}
}
public void setUpObj(Vector2 loc,GameObject actionO,bool b, BuildQueue bq)
{
debugGText.permDebug("setting up builder");//console complains about null reference for 'debugGText'
buildGuiLocation = new Vector3(loc.x,loc.y,guiLayerZ);
currentBuildQueue = bq;
actionObj = actionO; //what obj to instantiate or call a method on
isResearchItem = b; //tells if item being built is research item or not
transform.position = Vector3.zero;
debugGText.permDebug("pos(after): (" + transform.position.x + "," + transform.position.y + ") and localPos: (" + transform.localPosition.x + "," + transform.localPosition.y + ")");
transform.localPosition.Set(0,0,0);
debugGText.permDebug("done setting up builder");
}
// Use this for initialization
public void startBuild(float n)//n represents the time needed to finish build
{
//debug
debugGText.permDebug("starting build");
timeNeededToFinishBuild = n;
//debugGText.permDebug("added time into builder");
beginTimeOfBuild = Time.time;
//debugGText.permDebug("set beginning time of 'Time.time'");
buildStarted = true;
//debug
//debugGText.permDebug("done starting build");
//debugGText.permDebug("done with start build method");
}
private void updateBuilderGui()
{
float percentage = (Time.time - beginTimeOfBuild) / timeNeededToFinishBuild;
progressBar.pixelInset = new Rect(buildGuiLocation.x,buildGuiLocation.y,(percentage * progressBarInitWidth),190);
}
public void updateGuiPos(Vector2 v)
{
buildGuiLocation = v;
updateButtonPos();
updateTextPos();
}
public void removeThisBuilderFromList()
{
//calls remove function on a builder list
//3 lists, one for each "tab" of scroll list menu vert
currentBuildQueue.removeObjFromQueue(this);
}
void OnDestroy()
{
Destroy(progressBar);
Destroy(progressBarBackground);
cancelBuildButton.destroy();
}
private void actionPerformed()
{
if(isResearchItem)
{
// actionObj.performAction();//if research item, then perform the research
//make a research item interface, then impliment this interface to make new research objs
//each research obj must have a performAction() method
}
else
{
//currentBuildQueue.shipComponentOfQueue
}
}
private void updateButtonPos()
{
//update button position here
//take into account hiding and unhiding, if not done so already
cancelBuildButton.position = new Vector3(buildGuiLocation.x,buildGuiLocation.y,buildGuiLocation.z);
}
private void updateTextPos()
{
Vector3 worldLocation = mainCam.ScreenToWorldPoint(buildGuiLocation);
labelOfBuilder.transform.position = worldLocation;
}
public void testNullRefBug(float n)
{
this.debugGText.permDebug("testing null ref bug");
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------