Pieces of code used to make this
using UnityEngine;
using System.Collections;
public class groundScript : MonoBehaviour {
public AudioClip SND_breakDefense, SND_offensiveBreach;
public int i = 0; //used to count time /*I also could have used a float and then add Time.Deltatime*/
void FixedUpdate() {
//this is an internal clock, because the fixedupate runs at a fixed amount of frames per second
//this makes sure that only one sound is being played at any given time
if (i >= 1) {
i++;
if (i > 200) i = 0;
}
}
//used to check if an unit collides with one of the stripes on the screen
void OnTriggerEnter2D(Collider2D collider){
if (gameObject.tag == "RightGround" && (collider.gameObject.tag == "MovesRightLancer" | collider.gameObject.tag == "MovesRightSword" | collider.gameObject.tag == "MovesRightAxe")) {
GameObject.Find("units instanciator").GetComponent().rightTrigger = 1; //activate reinforcements from the right
if (SND_breakDefense.isReadyToPlay) playSound();
}
if (gameObject.tag == "LeftGround" && (collider.tag == "MovesLeftLancer" | collider.tag == "MovesLefttSword" | collider.tag == "MovesLeftAxe")) {
GameObject.Find("units instanciator").GetComponent().rightTrigger = 1; //activate reinforcements from the right
if (SND_breakDefense.isReadyToPlay) playSound2();
}
if (gameObject.tag == "RightGround" && (collider.gameObject.tag == "MovesLeftLancer" | collider.gameObject.tag == "MovesLeftSword" | collider.gameObject.tag == "MovesLefttAxe")) {
GameObject.Find("units instanciator").GetComponent().rightTrigger = 0;
}
}
void playSound() {
if (i == 0) { //checks if the clock isn't running
i++; //starts the clock
AudioSource.PlayClipAtPoint(SND_breakDefense, transform.position, 0.4f); //plays we broke their defenses
}
}
void playSound2() {
if (i == 0) { //checks if the clock isn't running
i++; //starts the clock
AudioSource.PlayClipAtPoint(SND_offensiveBreach, transform.position, 0.4f); //plays sound they broke our defenses
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class InstanciateUnit : MonoBehaviour {
public GameObject SoldierLancer, LancerPrefab;
public GameObject SoldierAxe, AxePrefab;
public GameObject SoldierSword, SwordPrefab;
public GameObject City1, City2; //used to store the behavior from both the cities
public float city1MP, city2MP; //manpower from both the cities
public int score, score1, score2, scoreTik; //keeps track of the current score and the total achieved score, score1 and score2 are the limits
public int rightTrigger, leftTrigger, lmTrigger, rmTrigger;
public int random;
public int i1, i2 =400, rangeI1, rangeI2;
public int distanceX = 6; // hardcoded
public int lane =-2; // used to limit the amount of lanes with hardcoded position
public int nPeleton =1; // how many soldiers are allowed in 1 peleton
public int countLanes; // how many lanes the player can fight on
public Text countScore;
public bool hasAlreadyBeenDone = false,hasAlreadyBeenDone1 = false;
public AudioClip SND_tempo;
public int spawnRate = 0;
public int nUpgrades; //keeps tracks how many upgrades the player has
// Use this for initialization
void Start () {
city1MP = City1.GetComponent().manpower;
city2MP = City2.GetComponent().manpower;
}
// Update is called once per frame
void FixedUpdate() {
// if the enemy city has run out of manpower end the game
if (city2MP <= 30) { transform.gameObject.AddComponent(); }
checkScore();
i1++; i2++; //internal clock to count how many frames it has been since last
// city2
if (city2MP > 1) {
// spawn a unit when the timer is bigger then the range
// or when trigger is true and the timer is begger then the modified range
if (i2 >= rangeI2 | (i2 >=20 && rightTrigger ==1)) {
i2 = 0; // reset the timer back to zero
// for each lane
for (int k = -1; k > lane; k-=2) {
// for each peleton
random = Random.Range(0, 3); // used to randomize each peleton, but each peleton has one type of unit
for (int i = 0; i < 3 ; i++) {
if (rightTrigger == 1) { random = Random.Range(0, 3); } // if rightTrigger is true randomize all the units within the peleton
instanciateUniteRight(random, k); // instanciate the random unit on position k
city2MP -= 1; // reduces the enemy manpower
}
}
}
}
Score: "+score +"\n\n\n\n\n\n\n\n\n manpower: " + city2MP;
}
// used to create units and unlock upgrades
void OnGUI() {
if (score >= 50 + 50 * nUpgrades) { // 50 + 50 * nUpgrades
// upgrade button
if ( // if pressed
GUI.Button(
new Rect(
+500,
+40,
250,
30
),
"-" + (50 + 50 * nUpgrades) + " score for better units"
)
) {
score -= 50 + 50 * nUpgrades++;
nPeleton++;
}
}
// as long as the city is not destroyed do this
if (GameObject.FindGameObjectWithTag("City1") != null) {
if (
// spawn red unit
GUI.Button(
new Rect(
+0,
+335,
30,
30
),
"R"
)
) { // for every unit in a peleton spawn a soldier
for (int i= 0; i < nPeleton; i++) {
AxePrefab = Instantiate(SoldierAxe, new Vector3(-distanceX, -1, 0), Quaternion.identity) as GameObject;
AxePrefab.tag = "MovesRightAxe";
}
}
// spawn green unit
if (
GUI.Button(
new Rect(
+30,
+335,
30,
30
),
"G"
)
// for every unit in a peleton spawn a soldier
) {for (int i= 0; i < nPeleton; i++) {
SwordPrefab = Instantiate(SoldierSword, new Vector3(-distanceX, -1, 0), Quaternion.identity) as GameObject;
SwordPrefab.tag = "MovesRightSword";
}
}
// spawn yellow unit
if (
GUI.Button(
new Rect(
+60,
+335,
30,
30
),
"Y"
)
) {
// for every unit in a peleton spawn a soldier
for (int i = 0; i < nPeleton; i++) {
LancerPrefab = Instantiate(SoldierLancer, new Vector3(-distanceX, -1, 0), Quaternion.identity) as GameObject;
LancerPrefab.tag = "MovesRightLancer";
}
}
// for the second lane do the exact same with spawning units, but only one lane lower
if (countLanes >= 1) {
/*repeat the buttons, but on lane two */
}
if (countLanes == 2) {
/*repeat the buttons, but on lane three */
}
}
}
void instanciateUnitRight(int random, float y) {
if (random == 0) {
LancerPrefab = Instantiate(SoldierLancer, new Vector3(distanceX, y, 0), Quaternion.identity) as GameObject;
LancerPrefab.tag = "MovesLeftLancer";
} else if (random == 1) {
SwordPrefab = Instantiate(SoldierSword, new Vector3(distanceX, y, 0), Quaternion.identity) as GameObject;
SwordPrefab.tag = "MovesLeftSword";
} else {
AxePrefab = Instantiate(SoldierAxe, new Vector3(distanceX, y, 0), Quaternion.identity) as GameObject;
AxePrefab.tag = "MovesLeftAxe";
}
}
void checkScore() {
if (scoreTik >= score1 && !hasAlreadyBeenDone) { countLanes = 1; hasAlreadyBeenDone =true;}
if (!hasAlreadyBeenDone1 && scoreTik >= score2) { countLanes = 2; hasAlreadyBeenDone1 = true; }
if (countLanes == 1) { lane = -4; } // if current lane is 1, make sure that the for loops can spawn on 2 lanes
if (countLanes == 2) { lane = -6; } // if current lane is 1, make sure that the for loops can spawn on 2 lanes
if (scoreTik > 25 && spawnRate == 0) { rangeI2 = 250; AudioSource.PlayClipAtPoint(SND_tempo, transform.position, 0.8f); spawnRate++; }
if (scoreTik > 50 && spawnRate == 1) { rangeI2 = 225; AudioSource.PlayClipAtPoint(SND_tempo, transform.position, 0.8f); spawnRate++; }
if (scoreTik > 75 && spawnRate == 2) { rangeI2 = 175; AudioSource.PlayClipAtPoint(SND_tempo, transform.position, 0.8f); spawnRate++; }
if (scoreTik > 150 && spawnRate == 3) { rangeI2 = 150; AudioSource.PlayClipAtPoint(SND_tempo, transform.position, 0.8f); spawnRate++; }
if (scoreTik > 200 && spawnRate == 4) { rangeI2 = 125; AudioSource.PlayClipAtPoint(SND_tempo, transform.position, 0.8f); spawnRate++; }
if (scoreTik > 250 && spawnRate == 5) { rangeI2 = 100; AudioSource.PlayClipAtPoint(SND_tempo, transform.position, 0.8f); spawnRate++; }
if (scoreTik > 350 && spawnRate == 6) { rangeI2 = 80; AudioSource.PlayClipAtPoint(SND_tempo, transform.position, 0.8f); spawnRate++; }
if (scoreTik > 400 && spawnRate == 7) { rangeI2 = 70; AudioSource.PlayClipAtPoint(SND_tempo, transform.position, 0.8f); spawnRate++; }
}
}
/*
Note: This is the same script used for all the three diffrent units, but every so slightly modified
Note2: Units with the tag "MovesRight..." are spawend by the player
Note3: Units with the tag "MovesLeft..." are spawend by the ai
*/
using UnityEngine;
using System.Collections;
public class MovementAxe : MonoBehaviour {
public Rigidbody2D rb; // used to move the player
public int movementDirection; // decides if the player moves right or left
public int speed; // speed
// Use this for initialization
void Start () {
rb = GetComponent();
// the tag of the unit decides if the unit moves left or right
// the tag has been given by "instanciate unit" upon instanciation
if (tag == "MovesRightAxe") movementDirection = 1; else movementDirection = -1;
}
// Update is called once per frame
void FixedUpdate() {
rb.velocity = new Vector3(speed * movementDirection, 0, 0) * Time.deltaTime;
}
// the collision is going to be resolved according to the rock paper scissor rules
void OnCollisionEnter2D(Collision2D coll) {
// this unit comes from the right and collides with an unit from the left do the following /*note: units from the left are spawned by the player*/
if (tag == "MovesRightAxe" && coll.gameObject.tag == "MovesLeftLancer") {
Destroy(this.gameObject);
}
if (tag == "MovesRightAxe" && coll.gameObject.tag == "MovesLeftSword") {
DestroyObject(coll.gameObject);
GameObject.FindGameObjectWithTag("UnitInstanciator").GetComponent().score += 1;
GameObject.FindGameObjectWithTag("UnitInstanciator").GetComponent().scoreTik += 1;
}
if (tag == "MovesRightAxe" && coll.gameObject.tag == "MovesLeftAxe") {
DestroyObject(coll.gameObject);
Destroy(this.gameObject);
GameObject.FindGameObjectWithTag("UnitInstanciator").GetComponent().score += 1;
GameObject.FindGameObjectWithTag("UnitInstanciator").GetComponent().scoreTik += 1;
}
// this unit comes from the left and collides with an unit from the right do the following
if (tag == "MovesLeftAxe" && coll.gameObject.tag == "MovesRightLancer") {
Destroy(this.gameObject);
}
if (tag == "MovesLeftAxe" && coll.gameObject.tag == "MovesRightSword") {
DestroyObject(coll.gameObject);
}
if (tag == "MovesLeftAxe" && coll.gameObject.tag == "MovesRightAxe") {
DestroyObject(coll.gameObject);
Destroy(this.gameObject);
}
// if the unit from the right touches the city end the game
if (tag == "MovesLeftAxe" && coll.gameObject.tag == "City1") {
DestroyObject(coll.gameObject);
transform.gameObject.AddComponent();
} //if the unit from the player touches the enemy base delete the unit
if (tag == "MovesRightAxe" && coll.gameObject.tag == "City2") {
DestroyObject(this.gameObject);
}
}
}