// Obtain a projectile from ProjectileFactory and shoots it // Shoot is here instead of CmdShoot is such that Raycast would work on the local player who shoots it // If it's a normal projectile, nothing really happens, CmdShoot is called. // If it's a lightning projectile, finalPosition is calculated and passed into CmdShoot. public void Shoot(Vector2 shootDirection, ElementType elementType, int elementLevel) { //Earth type expand when "shoot" if (elementType == ElementType.Earth) { if (currentEarthProjectileSpawner) { currentEarthProjectileSpawner.GetComponent <EarthProjectileSpawner>().startExpand(); } return; } Rigidbody2D projectile = projectileFactory.getProjectileFromType(elementType, elementLevel); float maxDistance = projectile.GetComponent <ProjectileController>().maxDistance; int layerMask = LayerMask.GetMask("Enemy", "Wall"); RaycastHit2D hit = Physics2D.Raycast(transform.position, shootDirection, Mathf.Infinity, layerMask); Vector2 finalPosition; if (hit.distance <= maxDistance) { finalPosition = hit.transform.position; } else { finalPosition = new Vector2(transform.position.x, transform.position.y) + shootDirection.normalized * maxDistance; } CmdShoot(shootDirection, elementType, elementLevel, finalPosition); depleteEnergy(elementType, elementLevel); }
[UnityTest] /*1 Projectile to enemeyBat: enemyBat Hp=7? true;*/ public IEnumerator ProjectileType2() { NetworkServer.Listen(7777); GameObject gm = Instantiate(Resources.Load("Tests/GameManager")) as GameObject; ProjectileFactory projectileFactory = gm.GetComponent <ProjectileFactory> (); PlayerController.ElementType element = PlayerController.ElementType.Fire; int level = 2; Rigidbody2D projectile = projectileFactory.getProjectileFromType(element, level); yield return(new WaitForSeconds(1)); GameObject projectile2 = Instantiate(Resources.Load("Tests/FireballProjectile2")) as GameObject; Assert.AreEqual(projectile2.GetComponent <ProjectileController>().projectileDamage, projectile.gameObject.GetComponent <ProjectileController>().projectileDamage); }
public void createProjectilePattern(string pattern, Vector3 startPosition, Vector2 shootDirection, bool belongToPlayer, GameObject shooter) { // Fire, Water Vector2 noDirection = new Vector2(1.0f, 0.0f); if (pattern == "basicFireball") { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Fire, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg; Vector2 velocity = shootDirection.normalized * cloneGameObject.GetComponent <ProjectileController>().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocityAndRotation(velocity, rotation); NetworkServer.Spawn(cloneGameObject); } if (pattern == "weakFireball") { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Fire, 0); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg; Vector2 velocity = shootDirection.normalized * cloneGameObject.GetComponent <ProjectileController>().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocityAndRotation(velocity, rotation); NetworkServer.Spawn(cloneGameObject); } if (pattern == "shoot5FireballSpread30Degree") { for (int i = 0; i < 5; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Fire, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg - 15 + 30 * i / 5; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot3FireballSpread30Degree2") { for (int i = 0; i < 3; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Fire, 2); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg - 15 + 30 * i / 3; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot8Fireball360DegreeSpread") { for (int i = 0; i < 8; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Fire, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg + 45 * i; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot18lv2Fireball360DegreeSpread") { for (int i = 0; i < 18; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Fire, 2); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg + 20 * i; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "basicWaterball") { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg; Vector2 velocity = shootDirection.normalized * cloneGameObject.GetComponent <ProjectileController>().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocityAndRotation(velocity, rotation); NetworkServer.Spawn(cloneGameObject); } if (pattern == "weakWaterball") { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 0); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg; Vector2 velocity = shootDirection.normalized * cloneGameObject.GetComponent <ProjectileController>().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocityAndRotation(velocity, rotation); NetworkServer.Spawn(cloneGameObject); } if (pattern == "shoot5lv3Waterball360DegreeSpread") { for (int i = 0; i < 5; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 3); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg + 72 * i; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot4Waterball360DegreeSpread0") { for (int i = 0; i < 4; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(noDirection.y, noDirection.x) * Mathf.Rad2Deg + 360 / 4 * i + 90 / 8 * 0; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot4Waterball360DegreeSpread1") { for (int i = 0; i < 4; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(noDirection.y, noDirection.x) * Mathf.Rad2Deg + 360 / 4 * i + 90 / 8 * 1; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot4Waterball360DegreeSpread2") { for (int i = 0; i < 4; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(noDirection.y, noDirection.x) * Mathf.Rad2Deg + 360 / 4 * i + 90 / 8 * 2; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot4Waterball360DegreeSpread3") { for (int i = 0; i < 4; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(noDirection.y, noDirection.x) * Mathf.Rad2Deg + 360 / 4 * i + 90 / 8 * 3; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot4Waterball360DegreeSpread4") { for (int i = 0; i < 4; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(noDirection.y, noDirection.x) * Mathf.Rad2Deg + 360 / 4 * i + 90 / 8 * 4; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot4Waterball360DegreeSpread5") { for (int i = 0; i < 4; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(noDirection.y, noDirection.x) * Mathf.Rad2Deg + 360 / 4 * i + 90 / 8 * 5; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot4Waterball360DegreeSpread6") { for (int i = 0; i < 4; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(noDirection.y, noDirection.x) * Mathf.Rad2Deg + 360 / 4 * i + 90 / 8 * 6; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "shoot4Waterball360DegreeSpread7") { for (int i = 0; i < 4; i++) { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Water, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } float rotation = Mathf.Atan2(noDirection.y, noDirection.x) * Mathf.Rad2Deg + 360 / 4 * i + 90 / 8 * 7; cloneGameObject.GetComponent <ProjectileController> ().setRotation(rotation); //float vx = cloneGameObject.transform.right*projectile.GetComponent<ProjectileController> ().projectileSpeed; //float vy = cloneGameObject.transform.up*projectile.GetComponent<ProjectileController> ().projectileSpeed; Vector2 velocity = new Vector2(Mathf.Cos(rotation * Mathf.Deg2Rad), Mathf.Sin(rotation * Mathf.Deg2Rad)).normalized *cloneGameObject.GetComponent <ProjectileController> ().projectileSpeed; cloneGameObject.GetComponent <ProjectileController> ().setVelocity(velocity); NetworkServer.Spawn(cloneGameObject); } } if (pattern == "basicLightningball") { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Lightning, 0); Rigidbody2D clone; float maxDistance = projectile.GetComponent <ProjectileController> ().maxDistance; int layerMask = LayerMask.GetMask("Player", "Wall"); //changed Player and wall RaycastHit2D hit = Physics2D.Raycast(startPosition, shootDirection, Mathf.Infinity, layerMask); // change transform to startPostion Vector2 finalPosition; if (hit.distance <= maxDistance) { finalPosition = hit.transform.position; } else { finalPosition = new Vector2(startPosition.x, startPosition.y) + shootDirection.normalized * maxDistance; } clone = Instantiate(projectile, finalPosition, transform.rotation) as Rigidbody2D; GameObject lightning = Instantiate(lightningEffect, transform.position, transform.rotation); lightning.GetComponent <LightningBoltScript> ().StartPosition = startPosition; lightning.GetComponent <LightningBoltScript> ().EndPosition = finalPosition; NetworkServer.Spawn(lightning); GameObject cloneGameObject = clone.gameObject; NetworkServer.Spawn(cloneGameObject); if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } } if (pattern == "basicRockball") { Rigidbody2D projectile = projectileFactory.getProjectileFromType(PlayerController.ElementType.Earth, 1); Rigidbody2D clone; clone = Instantiate(projectile, startPosition, transform.rotation) as Rigidbody2D; GameObject cloneGameObject = clone.gameObject; if (belongToPlayer) { cloneGameObject.GetComponent <ProjectileController> ().belongsToPlayer(); } cloneGameObject.GetComponent <EarthProjectileSpawner> ().shooter = shooter; NetworkServer.Spawn(cloneGameObject); } }