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;
public int t = 0;
//========================================================
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.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");
t = 10;
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.permDebug("pos: (" + transform.position.x + "," + transform.position.y + ")");
debugGText.permDebug("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");
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
BuildQueue.cs
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BuildQueue : MonoBehaviour {
//used to handle builder objects and implimented using a Queue or a List<> collection.
//maybe use c# queue implimentated class?
List<Builder> buildQueue = new List<Builder>(4);
//position on gui obj
private GUITexture sideBarGuiBottom;
//debug=========================================================================
public GameObject debugObj;
DebugGuiText debugGText;
//=============================================================================
int widthOfElementsInQueue = 30;
//what ship component does this queue belong to? Call functions and etc to handle job completion and where to instantiate built ship/obj
public GameObject shipComponentOfQueue;
public GameObject shipOfComponent;
//shipOfComponent can reference a ship that has no components if the ship has base construction abilities
//type of queue
//used to organize when showing multiple build queues for selected ships all at once
//use for position x, hiding and unhiding sidebar
private Vector2 positionOfQueue;
// Use this for initialization
public GameObject bObj;
public Builder b;
public GameObject testNull; //assign in inspector
void Awake()
{
sideBarGuiBottom = GameObject.Find("SideBarBottom").GetComponent("GUITexture") as GUITexture;
positionOfQueue = new Vector2(sideBarGuiBottom.pixelInset.x,sideBarGuiBottom.pixelInset.y);
debugObj = (GameObject)GameObject.Find("Debug-gText");
debugGText = (DebugGuiText)debugObj.gameObject.GetComponent("DebugGuiText");
}
void Start ()
{
//debug instantiation of a builder====================================================================
debugGText.permDebug("creating builder");
bObj = Instantiate(Resources.Load("BuilderPrefab"),Vector3.zero,new Quaternion(0,0,0,0) ) as GameObject;
b = bObj.GetComponent("Builder") as Builder;
b.setUpObj(positionOfQueue,new GameObject("dummyForBuilder"),false,this);
b.startBuild(15f);
debugGText.permDebug("going to add to queue now");//line works here
addToQueue(b); //using this for action as dummy obj
debugGText.permDebug("done creating builder");
//==========================================================================================*==========
}
// Update is called once per frame
void Update ()
{
if(buildQueue.Count > 0)
{
//update all elements location
for(int i = 0;i<buildQueue.Count;i++)
{
if(buildQueue[i] is Builder)
{
Builder b = buildQueue[i];
b.updateGuiPos(new Vector2(positionOfQueue.x,positionOfQueue.y + (widthOfElementsInQueue * i) ));
}
}
}
}
public bool addToQueue(Builder b)
{
if(buildQueue.Count<4)
{
buildQueue.Add(b);
return true;
}
return false;
}
public bool removeObjFromQueue(Builder b)
{
int idx = buildQueue.IndexOf(b);
if(idx >= 0)
{
Builder temp = buildQueue[idx];
buildQueue.RemoveAt(idx);
Destroy(temp);
return true;
}
return false;
}
public void dequeue()
{
//remove first element
buildQueue.RemoveAt(0);
}
public void hideQueue()
{
positionOfQueue = new Vector2(positionOfQueue.x+200,positionOfQueue.y);
}
public void showQueue()
{
positionOfQueue = new Vector2(positionOfQueue.x-200,positionOfQueue.y);
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------