Skip to content

Commit b1c3168

Browse files
Feature bomber Jira: LK-13 (#7)
* Implemented and Tested Port of Bomber Movement Jira: LK-14 * Implemented and Tested Bomber Port Bomb Spawning Jira: LK-20 * Implemented and Tested Bomber Port Bomb Carrying Jira: LK-15 * Implemented and Tested Bomber Port Bomb Drop Jira: LK-16 * Implemented and Tested Bomber Port Bomb Explosion Jira: LK-17 * Implemented and Tested Bomber Port Bomber can take Damage Jira: LK-19 * Implemented and Tested Bomber Port Bomb can take Damage Jira: LK-18
1 parent 3a253f5 commit b1c3168

File tree

8 files changed

+943
-46
lines changed

8 files changed

+943
-46
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using NUnit.Framework;
4+
using UnityEngine;
5+
using UnityEngine.TestTools;
6+
using UnityEngine.SceneManagement;
7+
8+
namespace Tests
9+
{
10+
public class BomberTest
11+
{
12+
private GameObject Player;
13+
private GameObject Enemy;
14+
private GameObject bomb;
15+
private GameObject[] shrapnel;
16+
17+
[SetUp]
18+
public void SetUp()
19+
{
20+
SceneManager.LoadScene("TestScene", LoadSceneMode.Single);
21+
}
22+
23+
[TearDown]
24+
public void Teardown()
25+
{
26+
SceneManager.UnloadSceneAsync("TestScene");
27+
}
28+
29+
[UnityTest]
30+
public IEnumerator Movement()
31+
{
32+
33+
Enemy = GameObject.FindGameObjectWithTag("Bomber");
34+
Vector2 initialPos = Enemy.GetComponent<Rigidbody2D>().position;
35+
yield return new WaitForSeconds(0.5f);
36+
Assert.Less(Enemy.GetComponent<Rigidbody2D>().position.x, initialPos.x);
37+
}
38+
39+
[UnityTest]
40+
public IEnumerator BombSpawn()
41+
{
42+
43+
Enemy = GameObject.FindGameObjectWithTag("Bomber");
44+
yield return new WaitForSeconds(0.5f);
45+
Assert.IsNotNull(Enemy.transform.GetChild(0));
46+
}
47+
48+
[UnityTest]
49+
public IEnumerator BombCarry()
50+
{
51+
52+
Enemy = GameObject.FindGameObjectWithTag("Bomber");
53+
Vector2 initialPos = Enemy.transform.GetChild(0).position;
54+
yield return new WaitForSeconds(0.5f);
55+
Assert.Less(Enemy.transform.GetChild(0).position.x, initialPos.x);
56+
}
57+
58+
[UnityTest]
59+
public IEnumerator BombDrop()
60+
{
61+
Enemy = GameObject.FindGameObjectWithTag("Bomber");
62+
Vector2 initialPos = Enemy.transform.GetChild(0).position;
63+
yield return new WaitForSeconds(2.0f);
64+
Assert.Less(Enemy.transform.GetChild(0).position.y, initialPos.y);
65+
}
66+
67+
[UnityTest]
68+
public IEnumerator BombExplosion()
69+
{
70+
71+
Enemy = GameObject.FindGameObjectWithTag("Bomber");
72+
Enemy.GetComponent<Rigidbody2D>().position = new Vector2(5, 5);
73+
yield return new WaitForSeconds(1.5f);
74+
shrapnel = GameObject.FindGameObjectsWithTag("Shrapnel");
75+
Assert.IsNotNull(shrapnel);
76+
}
77+
78+
[UnityTest]
79+
public IEnumerator BomberTakeDamage()
80+
{
81+
Enemy = GameObject.FindGameObjectWithTag("Bomber");
82+
float initialHealth = Enemy.GetComponent<Bomber>().getHealth();
83+
Enemy.GetComponent<Bomber>().Damage(2);
84+
yield return new WaitForSeconds(0.1f);
85+
Assert.Less(Enemy.GetComponent<Bomber>().getHealth(), initialHealth);
86+
}
87+
88+
[UnityTest]
89+
public IEnumerator BombTakeDamage()
90+
{
91+
Enemy = GameObject.FindGameObjectWithTag("Bomber");
92+
float initialHealth = Enemy.GetComponentInChildren<Bomb>().getHealth();
93+
Enemy.GetComponentInChildren<Bomb>().Damage(0.5f);
94+
yield return new WaitForSeconds(0.1f);
95+
Assert.Less(Enemy.GetComponentInChildren<Bomb>().getHealth(), initialHealth);
96+
}
97+
}
98+
}

Packages/ie.itcarlow.2d.platformer_enemy_behaviours/PlatformerEnemyBehaviourTests/BomberTest.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class Bomb : MonoBehaviour
6+
{
7+
// Bomb's rigidbody
8+
Rigidbody2D rb;
9+
10+
// Bomber's GameObject
11+
private GameObject bomber;
12+
// Temporary Shrapnel GameObject
13+
private GameObject shrapnel;
14+
// player gameobject
15+
private GameObject player;
16+
// Bomb Health
17+
public float maxHealth = 1.0f;
18+
// Current Health
19+
private float health;
20+
// Controls if the bomb is dropped;
21+
public bool dropped = false;
22+
// shrapnel object
23+
public GameObject ShrapnelPassed;
24+
25+
// Start is called before the first frame update
26+
void Start()
27+
{
28+
rb = gameObject.GetComponent<Rigidbody2D>();
29+
player = GameObject.FindGameObjectWithTag("Player");
30+
health = maxHealth;
31+
rb.velocity = new Vector2(-this.GetComponentInParent<Bomber>().speed, rb.velocity.y);
32+
}
33+
34+
// Update is called once per frame
35+
void Update()
36+
{
37+
if (dropped && rb.gravityScale != 1)
38+
{
39+
rb.gravityScale = 1;
40+
}
41+
42+
}
43+
44+
void OnTriggerEnter2D(Collider2D col)
45+
{
46+
if (col.gameObject.tag == "Ground")
47+
{
48+
rb.velocity = new Vector2(0, 0);
49+
rb.gravityScale = 0;
50+
for (int i = 0; i < 3; i++)
51+
{
52+
shrapnel = Instantiate(ShrapnelPassed, new Vector3(rb.position.x, rb.position.y + i, 0), Quaternion.identity);
53+
if (rb.position.x < player.GetComponent<Rigidbody2D>().position.x)
54+
{
55+
shrapnel.GetComponent<Rigidbody2D>().velocity = new Vector2(this.GetComponentInParent<Bomber>().speed, 0.0f);
56+
shrapnel.GetComponent<Transform>().localScale = new Vector3(shrapnel.GetComponent<Transform>().localScale.x * -1, shrapnel.GetComponent<Transform>().localScale.y, shrapnel.GetComponent<Transform>().localScale.z);
57+
}
58+
else if (rb.position.x > player.GetComponent<Rigidbody2D>().position.x)
59+
{
60+
shrapnel.GetComponent<Rigidbody2D>().velocity = new Vector2(-this.GetComponentInParent<Bomber>().speed, 0.0f);
61+
}
62+
}
63+
Destroy(this.gameObject);
64+
}
65+
else
66+
{
67+
if (col.gameObject.tag == "Player")
68+
{
69+
Destroy(this.gameObject);
70+
71+
}
72+
73+
}
74+
}
75+
76+
public void Damage(float damage)
77+
{
78+
if(health > 0)
79+
{
80+
health -= damage;
81+
}
82+
else
83+
{
84+
Destroy(this.gameObject);
85+
}
86+
}
87+
88+
public float getHealth()
89+
{
90+
return health;
91+
}
92+
}

Packages/ie.itcarlow.2d.platformer_enemy_behaviours/Runtime/Bomb.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class Bomber : MonoBehaviour
6+
{
7+
// Enemy's rigidbody
8+
Rigidbody2D rb;
9+
10+
// Enemy Health
11+
public float maxHealth = 5.0f;
12+
// Enemy movement speed
13+
public float speed = 5.0f;
14+
// Player GameObject
15+
public GameObject player;
16+
// Bomb GameObject
17+
private GameObject bomb;
18+
// Current Health
19+
private float health;
20+
21+
// Bombing Range
22+
public float range = 5;
23+
24+
private bool armed = true;
25+
26+
// Passed in Bomb Object
27+
public GameObject bombPassed;
28+
29+
// Start is called before the first frame update
30+
void Start()
31+
{
32+
rb = gameObject.GetComponent<Rigidbody2D>();
33+
health = maxHealth;
34+
// Movement
35+
rb.velocity = new Vector2(-speed, rb.velocity.y);
36+
bomb = Instantiate(bombPassed, new Vector3(rb.position.x, rb.position.y - 1.5f, 0), Quaternion.identity, transform);
37+
if(!bomb.GetComponent<Bomb>().enabled)
38+
{
39+
bomb.GetComponent<Bomb>().enabled = true;
40+
}
41+
if (!player)
42+
{
43+
player = GameObject.FindGameObjectWithTag("Player");
44+
}
45+
}
46+
47+
// Update is called once per frame
48+
void Update()
49+
{
50+
if (armed)
51+
{
52+
if(player && bomb)
53+
{
54+
if (Vector2.Distance(rb.position, new Vector2(player.transform.position.x, player.transform.position.y)) < range && !bomb.GetComponent<Bomb>().dropped)
55+
{
56+
bomb.GetComponent<Bomb>().dropped = true;
57+
armed = false;
58+
}
59+
}
60+
61+
}
62+
}
63+
64+
public void Damage(float damage)
65+
{
66+
if (health - damage > 0)
67+
{
68+
health -= damage;
69+
}
70+
else
71+
{
72+
health = 0;
73+
Destroy(this.gameObject);
74+
}
75+
}
76+
77+
public float getHealth()
78+
{
79+
return health;
80+
}
81+
}

Packages/ie.itcarlow.2d.platformer_enemy_behaviours/Runtime/Bomber.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)