public void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Enemy")) { // Disable the collider so we can't hit another enemy if (type != 4) { myTr.collider2D.enabled = false; // laser can shoot through enemies ! } //Debug.Log("Bullet collided with an enemy", gameObject); other.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); // Call the DestroyObject function if (type != 4) { StartCoroutine(DestroyObject()); } } if (other.CompareTag("Ground")) { //Debug.Log("Player Bullet collided with the ground", gameObject); // If type = laser if (type == 4) { return; // laser can shoot through walls ! } // If type = bomb, deploy napalm ! if (type == 5) { impactClone = impactPool.Spawn(); impactClone.transform.position = myTr.position; ImpactScript impactCloneScript = impactClone.GetComponent <ImpactScript>() as ImpactScript; //var impactCloneScript : ImpactScript = impactClone.GetComponent<ImpactScript>() as ImpactScript; impactCloneScript.bombImpact = true; StartCoroutine(BombLandingDamage()); StartCoroutine(DestroyObject()); return; } impactClone = impactPool.Spawn(); impactClone.transform.position = myTr.position; // Call the DestroyObject function StartCoroutine(DestroyObject()); } }
// "LaunchProjectile()" is called by animation Event in 'Enemy 5 Fire Animation' public IEnumerator LaunchProjectile() { // check if the object is already active - this is because this function is called by animation event ! if (gameObject.activeInHierarchy == false) { yield break; // FIX CS return null; } canFire = false; // the object is no more allowed to fire (this is a unique shot) bulletClone = weaponPool.Spawn(); bulletClone.transform.position = new Vector3(myTr.position.x + 0.06f * myTr.right.x, myTr.position.y + 0.16f * myTr.up.y, myTr.position.z); enemyWeaponScript = bulletClone.GetComponent <EnemyWeaponScript>() as EnemyWeaponScript; enemyWeaponScript.direction = new Vector2(myTr.right.x, myTr.up.y); audio.clip = bulletSound; audio.Play(); yield return(null); myAnimator.SetBool("Firing_Bool", false); yield return(new WaitForSeconds(1.3f)); firing = false; myRb.isKinematic = false; }
public IEnumerator ApplyDamage(int damage) { // Ensure that object receiving damage is not sleeping (and therefore out of screen) if (asleep == true) { yield break; // FIX CS return null; } hp = hp - 1; if (hp > 0) { StartCoroutine(DamageBlink()); } else { audio.Play(); myTr.collider2D.enabled = false; myTr.renderer.enabled = false; //REGULAR METHOD : INSTANTIATE (unoptimised) //explosionClone = Instantiate(explosion, Vector3 (myTr.position.x, myTr.position.y, myTr.position.z), Quaternion.identity); // Pooling Method : grab object from "ObjectPooler"'s gameObjects list explosionClone = explosionPool.Spawn(); explosionClone.transform.position = myTr.position; // search for "GiveCoins" value in player preferences if (PlayerPrefs.HasKey("Give coins") == false || PlayerPrefs.GetInt("Give coins") == 1) { giveCoins = false; // we use the value 2 for true, and one for false } else if (PlayerPrefs.GetInt("Give coins") == 2) { giveCoins = true; } if (giveCoins == true) { coinPos = myTr.position; for (i = 0; i < scoreValue / 10; i++) { //REGULAR METHOD : INSTANTIATE (unoptimised) //coinClone = Instantiate(coin, coinPos, Quaternion.identity); // Pooling Method : grab object from "ObjectPooler"'s gameObjects list coinClone = coinPool.Spawn(); coinClone.transform.position = coinPos; yield return(null); } } playerScript.UpdateScore(scoreValue); myTr.parent.SendMessage("SubstractChild", gameObject, SendMessageOptions.DontRequireReceiver); StartCoroutine(DestroyObject()); } }
public IEnumerator EventProcess() { transform.collider2D.enabled = false; if (setCheckpoint == true) { mainScript.checkpointPos = /*transform.position.x*/ checkpointPosition; } if (setMusic == true) { StartCoroutine(mainScript.MusicPlay(music, musicLoop, musicWaitForClipEnd, musicDelay)); } else // if (setMusic == false) if (stopMusic == true) { StartCoroutine(mainScript.MusicStop(musicFadeOut)); } if (setText == true) { uiTextMesh.text = text; uiTextGO.transform.localPosition = new Vector3(textPosition.x, textPosition.y, uiTextGO.transform.localPosition.z); uiTextMesh.characterSize = textSize; uiTextMesh.color = textColor; // (Text duration : if set to 0, text duration is null and text will stay displayed) if (textDuration > 0) { yield return(new WaitForSeconds(textDuration)); uiTextMesh.text = ""; } } if (setLevel == true) { if (setCheckpoint == true) { PlayerPrefs.SetFloat("Spawn position", checkpointPosition); } mainScript.level = levelValue; // Change level value in "MainScript" StartCoroutine(mainScript.SetLevel()); } if (setUpgrade == true) { GameObject upgradeClone = upgradePool.Spawn(); upgradeClone.transform.position = new Vector3(transform.position.x + upgradePosition.x, transform.position.y + upgradePosition.y, upgradeClone.transform.localPosition.z); } }
// This function is called each time a child is destroyed, to check for combo public void SubstractChild(GameObject go) { childrenNumber = childrenNumber - 1; if (childrenNumber == 0) { Debug.Log("Well Done ! you made a combo", gameObject); GameObject upgradeClone = upgradePool.Spawn(); upgradeClone.transform.position = go.transform.position; } }
public IEnumerator BombLandingDamage() { // Already setup in variables declaration : //bombNapalmMaxCount = 9; napalmLifeTime = 3.0f; napalmStartDelay = 0.0f; //bombNapalmBelow = transform.TransformDirection (0.03f,-1.0f,0.0f); napalmPosX = myTr.position.x; napalmPreviousHeight = myTr.position.y; yield return(null); // Deploy a range of napalm for (i = 0; i < bombNapalmMaxCount; i++) { if (i > 0) { if (i % 2 == 1) // if "i" is even, place to the left and change start and die delays { napalmPosX = napalmPosX + i * 0.06f; napalmStartDelay = napalmStartDelay + 0.1f; napalmLifeTime = napalmLifeTime * 0.95f; } else { napalmPosX = napalmPosX - i * 0.06f; } // else if "i" is odd, place to the right } RaycastHit2D hit = Physics2D.Raycast(new Vector2(napalmPosX, napalmPreviousHeight + 0.32f), -Vector2.up, Mathf.Infinity, groundLayerMask.value); if (hit /*!= null*/) { //Debug.DrawRay (Vector2(napalmPosX, napalmPreviousHeight + 0.32), -Vector2.up, Color.green); napalmPosX = hit.point.x; napalmClone = napalmPool.Spawn(); napalmClone.transform.position = new Vector3(hit.point.x, hit.point.y, napalmClone.transform.position.z); playerNapalmScript = napalmClone.GetComponent <PlayerNapalmScript>() as PlayerNapalmScript; playerNapalmScript.startDelay = napalmStartDelay; playerNapalmScript.lifeTime = napalmLifeTime; } //else Debug.DrawRay (Vector2(napalmPosX, napalmPreviousHeight + 0.32), -Vector2.up, Color.red); } yield return(null); }
public void LaunchProjectile() { //REGULAR METHOD : INSTANTIATE bullet (unoptimised) //bulletClone = Instantiate(bullet, bulletPos, Quaternion.identity); // Pooling Method : grab a bullet from "ObjectPooler"'s gameObjects list // Prepare the bullet position bulletPos = myTr.TransformPoint(-0.1f, 0.0f, 0.0f); // Prepare the bullet direction bulletDir = new Vector3(-1.0f, 0.0f, 0.0f); bulletClone = weaponPool.Spawn(); // Spawn from pool bulletClone.transform.position = bulletPos; enemyWeaponScript = bulletClone.GetComponent <EnemyWeaponScript>() as EnemyWeaponScript; enemyWeaponScript.direction = (Vector2)bulletDir; // Prepare the bullet position bulletPos = myTr.TransformPoint(-0.1f, 0.0f, 0.0f); // Prepare the bullet direction bulletDir = new Vector3(-1.0f, 0.3f, 0.0f); bulletClone = weaponPool.Spawn(); bulletClone.transform.position = bulletPos; enemyWeaponScript = bulletClone.GetComponent <EnemyWeaponScript>() as EnemyWeaponScript; enemyWeaponScript.direction = (Vector2)bulletDir; // Prepare the bullet position bulletPos = myTr.TransformPoint(-0.1f, 0.0f, 0.0f); // Prepare the bullet direction bulletDir = new Vector3(-1.0f, -0.3f, 0.0f); bulletClone = weaponPool.Spawn(); bulletClone.transform.position = bulletPos; enemyWeaponScript = bulletClone.GetComponent <EnemyWeaponScript>() as EnemyWeaponScript; enemyWeaponScript.direction = (Vector2)bulletDir; audio.clip = bulletSound; audio.Play(); }
// This function is called by enemies' scripts public IEnumerator ApplyDamage(int damage) // "damage" value (refers to int "hp", health) isn't used for player in the project { // If the player is "invicible", ignore the damages by aborting the function if (invincible == true) { yield break; //return null; // FIX CS 13 01 2015 VERIFIER !! } Debug.Log("Player received damage !", gameObject); myTr.collider2D.enabled = false; myTr.renderer.enabled = false; myExhaustTr.renderer.enabled = false; canMove = false; camScrollEnabled = false; mainScript.showDebugGUI = false; // hide the debug GUI (if it is displayed) mainScript.audio.Stop(); // Create a big explosion ! for (int i = 0; i < 6; i++) { explosionClone = explosionPool.Spawn(); float randomXPos = UnityEngine.Random.Range(-0.04f, 0.04f); float randomYPos = UnityEngine.Random.Range(-0.04f, 0.04f); explosionClone.transform.position = myTr.position; /* * var tmpPos = myTr.position; * tmpPos.x = myTr.position.x + randomXPos; * tmpPos.y = myTr.position.y + randomYPos; * myTr.position = tmpPos; */ myTr.position = new Vector3(myTr.position.x + randomXPos, myTr.position.y + randomYPos, myTr.position.z); //explosionClone.SetActive(true); audio.clip = deathSound; audio.Play(); yield return(new WaitForSeconds(audio.clip.length)); } lifes = lifes - 1; yield return(new WaitForSeconds(2.0f)); // Launch "Die()" function located in the main script StartCoroutine(mainScript.Die()); }
public IEnumerator ApplyDamage(int damage) { // Ensure that object receiving damage is not sleeping (and therefore out of screen) if (asleep == true) { yield break; //return null; // FIX CS 13 01 2015 VERIFIER !! } hp = hp - 1; if (hp > 0) { StartCoroutine(DamageBlink()); audio.clip = armorSound; audio.Play(); // Raise the difficulty for our boss fight ! if (hp < 20) { actionDelayMax = shootDelayMax = 60.0f; } if (hp < 10) { actionDelayMax = shootDelayMax = 35.0f; } } else { audio.Play(); myTr.collider2D.enabled = false; yield return(null); asleep = true; // Stop all coroutines relative to the audio in "MainScript" mainScript.StopCoroutine("MusicPlay"); mainScript.StopCoroutine("MusicStop"); yield return(null); mainScript.audio.Stop(); StartCoroutine(mainScript.MusicPlay(victorySound, false, false, 0.0f)); // music, musicLoop, musicWaitForClipEnd, musicDelay playerScript.UpdateScore(scoreValue); //REGULAR METHOD : INSTANTIATE (unoptimised) //explosionClone = Instantiate(explosion, Vector3 (myTr.position.x, myTr.position.y, myTr.position.z), Quaternion.identity); // Pooling Method : grab object from "ObjectPooler"'s gameObjects list // Create a big explosion ! for (int i = 0; i < 20; i++) { explosionClone = explosionPool.Spawn(); float randomXPos = UnityEngine.Random.Range(-0.08f, 0.08f); float randomYPos = UnityEngine.Random.Range(-0.08f, 0.08f); explosionClone.transform.position = new Vector3(myTr.position.x + randomXPos, myTr.position.y + randomYPos, explosionClone.transform.position.z); audio.clip = deathSound; audio.Play(); yield return(new WaitForSeconds(audio.clip.length)); } var tmpColor = mySpriteRdr.color; tmpColor.a = 0.0f; mySpriteRdr.color = tmpColor; // search for "GiveCoins" value in player preferences if (PlayerPrefs.HasKey("Give coins") == false || PlayerPrefs.GetInt("Give coins") == 1) { giveCoins = false; // we use the value 2 for true, and one for false } else if (PlayerPrefs.GetInt("Give coins") == 2) { giveCoins = true; } if (giveCoins == true) { coinPos = myTr.position; for (i = 0; i < scoreValue * 0.1f; i++) { //REGULAR METHOD : INSTANTIATE (unoptimised) //coinClone = Instantiate(coin, coinPos, Quaternion.identity); // Pooling Method : grab object from "ObjectPooler"'s gameObjects list coinClone = coinPool.Spawn(); coinClone.transform.position = coinPos; yield return(null); } } yield return(new WaitForSeconds(2.0f)); StartCoroutine(DestroyObject()); } }
public IEnumerator LaunchProjectile(bool buttonKeptDown) { // If canShoot is already false, then abort the function if (canShoot == false || upgrading == true || renderer.isVisible == false) { yield break; //return null; // FIX CS 13 01 2015 VERIFIER !! } // Set canShoot to false until we process the function and the 'shootDelay' canShoot = false; // If player has bomb call "DropBomb()" function if (hasBomb == true && canDropBomb == true) { StartCoroutine(DropBomb()); } // If we don't use auto-fire ("Fire2") button and simple fire button ("space" or "Fire1") is kept pressed, // then slow down the fire rate by adding an additional delay ! // This is meant to encourage intensive button press. if (buttonKeptDown == true && (Input.GetKey("space") || Input.GetButton("Fire1"))) { yield return(new WaitForSeconds(shootDelay)); } if (bulletCount < bulletCountLimiter) { //REGULAR METHOD : INSTANTIATE bullet (unoptimised) //bulletClone = Instantiate(bullet, Vector3 (myTr.position.x+0.2, myTr.position.y-0.005, myTr.position.z), Quaternion.identity); // SMARTPOOL SYSTEM if (weaponLevel == 1) { // Pooling Method : grab a bullet from "ObjectPooler"'s gameObjects list bulletClone = weaponPool.Spawn(); bulletClone.transform.position = new Vector3(myTr.position.x + 0.2f, myTr.position.y - 0.005f, myTr.position.z); playerWeaponScript = bulletClone.GetComponent <PlayerWeaponScript>() as PlayerWeaponScript; playerWeaponScript.type = 1; // horizontal bullet playerWeaponScript.direction = new Vector2(1.0f, 0.0f); // Set the direction of the bullet bulletCount = bulletCount + 1; } else if (weaponLevel == 2) { // Pooling Method : grab a bullet from "ObjectPooler"'s gameObjects list bulletClone = weaponPool.Spawn(); bulletClone.transform.position = new Vector3(myTr.position.x + 0.2f, myTr.position.y - 0.005f, myTr.position.z); playerWeaponScript = bulletClone.GetComponent <PlayerWeaponScript>() as PlayerWeaponScript; playerWeaponScript.type = 1; // horizontal bullet playerWeaponScript.direction = new Vector2(1.0f, 0.0f); bulletClone = weaponPool.Spawn(); bulletClone.transform.position = new Vector3(myTr.position.x + 0.2f, myTr.position.y - 0.005f, myTr.position.z); playerWeaponScript = bulletClone.GetComponent <PlayerWeaponScript>() as PlayerWeaponScript; playerWeaponScript.type = 2; // diagonal bullet playerWeaponScript.direction = new Vector2(1.0f, 0.5f); bulletCount = bulletCount + 2; } else if (weaponLevel == 3) { // Pooling Method : grab a bullet from "ObjectPooler"'s gameObjects list bulletClone = weaponPool.Spawn(); bulletClone.transform.position = new Vector3(myTr.position.x + 0.2f, myTr.position.y - 0.005f, myTr.position.z); playerWeaponScript = bulletClone.GetComponent <PlayerWeaponScript>() as PlayerWeaponScript; playerWeaponScript.type = 4; // horizontal bullet playerWeaponScript.direction = new Vector2(1.0f, 0.0f); bulletCount = bulletCount + 1; } // Play bullet sound audio.clip = bulletSound; audio.Play(); } // Wait for weapon shoot delay yield return(new WaitForSeconds(shootDelay)); // shoot delay is now finished, set back "canShoot" to true, so we can fire again canShoot = true; }
public IEnumerator LaunchProjectile() { firing = true; // Let the script be aware that enemy is already firing if (shootDelayTimer > shootDelayMax) //shoot { if (mySpriteRdr.sprite == spriteVertical) { // Align the bullet direction to the object UP direction bulletDir = myTr.up; // Prepare the bullet position bulletPos = myTr.TransformPoint(0.0f, 0.2f, 0.0f); //Debug.Log("Enemy 4 (turret) - myTr.up : " + myTr.up, gameObject); //REGULAR METHOD : INSTANTIATE bullet (unoptimised) //bulletClone = Instantiate(bullet, bulletPos, Quaternion.identity); // Pooling Method : grab a bullet from "ObjectPooler"'s gameObjects list bulletClone = weaponPool.Spawn(); bulletClone.transform.position = bulletPos; enemyWeaponScript = bulletClone.GetComponent <EnemyWeaponScript>() as EnemyWeaponScript; enemyWeaponScript.direction = (Vector2)bulletDir; mySpriteRdr.sprite = spriteVerticalFire; } else if (mySpriteRdr.sprite == spriteHorizontal) { // Align the bullet direction to the object right direction - taking into account the sprite horizontal direction. bulletDir = myTr.right * -mySpriteTr.localScale.x; // Prepare the bullet position bulletPos = myTr.TransformPoint(-0.1f * mySpriteTr.localScale.x, 0.11f, 0.0f); //REGULAR METHOD : INSTANTIATE bullet (unoptimised) //bulletClone = Instantiate(bullet, bulletPos, Quaternion.identity); // Pooling Method : grab a bullet from "ObjectPooler"'s gameObjects list bulletClone = weaponPool.Spawn(); bulletClone.transform.position = bulletPos; enemyWeaponScript = bulletClone.GetComponent <EnemyWeaponScript>() as EnemyWeaponScript; enemyWeaponScript.direction = (Vector2)bulletDir; mySpriteRdr.sprite = spriteHorizontalFire; } else if (mySpriteRdr.sprite == spriteDiagonal) { bulletDir = myTr.right * -mySpriteTr.localScale.x + myTr.up; bulletPos = myTr.TransformPoint(-0.07f * mySpriteTr.localScale.x, 0.17f, 0.0f); //REGULAR METHOD : INSTANTIATE bullet (unoptimised) //bulletClone = Instantiate(bullet, bulletPos, Quaternion.identity); // Pooling Method : grab a bullet from "ObjectPooler"'s gameObjects list bulletClone = weaponPool.Spawn(); bulletClone.transform.position = bulletPos; enemyWeaponScript = bulletClone.GetComponent <EnemyWeaponScript>() as EnemyWeaponScript; enemyWeaponScript.direction = (Vector2)bulletDir; mySpriteRdr.sprite = spriteDiagonalFire; } audio.clip = bulletSound; audio.Play(); yield return(new WaitForSeconds(0.1f)); shootDelayTimer = 0.0f; // Reset the shoot delay count firing = false; // Enemy has finished firing } }