using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FiringMechanism : MonoBehaviour
{
/*
* The purpose of this script is to instantiate blocks from the position of the gameobject "Firing Mechanism".
* It is ENTIRELY possible to do this from the player script but due to NGUI's strange problem with positioning (of my lack of coding prowess),
* it's much better to have a seperate script handle the firing of blocks.
*/
public List<GameObject> playerBlocks; //The blocks that the player can shoot.
public GameObject currentBlock;
private GameObject playerObject;
private GameObject gameController; //the game controller object
private GameObject firingMechanismObject; // self reference
private GameObject gamePanel; // the game panel object
private GameObject redBlock;
private GameObject blueBlock;
private GameObject greenBlock;
private GameObject yellowBlock;
private GameObject purpleBlock;
private GameObject blackBlock;
private GameObject whiteBlock;
private GameObject cyanBlock;
void Awake()
{
firingMechanismObject = GameObject.Find("FiringMechanism");
playerObject = GameObject.Find("Player");
gamePanel = GameObject.Find("GamePanel");
currentBlock
= new GameObject
(); currentBlock.AddComponent<Rigidbody2D>();
currentBlock.AddComponent<BoxCollider2D>();
}
// Use this for initialization
void Start ()
{
InitFireList();
}
// Update is called once per frame
void Update ()
{
Debug.Log(firingMechanismObject.transform.position);
}
void InitFireList()
{
//Load the block prefabs
redBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireRedBlock") as GameObject;
greenBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireGreenBlock") as GameObject;
blueBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireBlueBlock") as GameObject;
yellowBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireYellowBlock") as GameObject;
purpleBlock = Resources.Load("GameObjects/PlayerFireBlocks/FirePurpleBlock") as GameObject;
blackBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireBlackBlock") as GameObject;
whiteBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireWhiteBlock") as GameObject;
cyanBlock = Resources.Load("GameObjects/PlayerFireBlocks/FireCyanBlock") as GameObject;
//add the loaded blocks to the playerBlock list.
playerBlocks.Add(redBlock);
playerBlocks.Add(blueBlock);
playerBlocks.Add(greenBlock);
playerBlocks.Add(yellowBlock);
playerBlocks.Add(purpleBlock);
playerBlocks.Add(blackBlock);
playerBlocks.Add(whiteBlock);
playerBlocks.Add(cyanBlock);
currentBlock = playerBlocks [Random.Range(0, playerBlocks.Count)]; //Changes the currently selected block to a random element inside the Blocks array
}
public void FireBlock()
{
//Fires the currentBlock upward from the Firing Mechanism.
//GameObject block = (GameObject) Instantiate(currentBlock, playerObject.transform.position, playerObject.transform.rotation);
NGUITools.AddChild(gamePanel, currentBlock);
currentBlock.transform.position = firingMechanismObject.transform.position;
currentBlock.rigidbody2D.AddForce(Vector2.up);
currentBlock = playerBlocks [Random.Range(0, playerBlocks.Count)]; //Changes the currently selected block to a random element inside the Blocks array
Debug.Log(currentBlock.transform.position);
}
}