BulletMLLib.Bullet BulletMLLib.IBulletManager.CreateBullet(BulletMLLib.Bullet source, bool top) { // This is where we create an instance // of our bullet implementation (BulletObject) // Create and return empty BulletObject return new BulletObject(this); }
BulletMLLib.Bullet BulletMLLib.IBulletManager.SetBulletProperties(BulletMLLib.Bullet bullet) { // This is where we set a bullet's properties // and effectively instantiate it in Unity // Check bullet bank for bullet information BulletInfo info; // If we can't find the information if(bullet.Label == null || !bulletBank.bank.TryGetValue(bullet.Label, out info)) { // Use default bullet //Debug.Log("no information on bank. using default bullet info"); bulletBank.bank.TryGetValue("default", out info); } // Get game object from pool // Check which type of bullet we are instantiating GameObject go = null; switch(info.shape) { // Bullet has a circle collider case BulletShape.CIRCLE: go = ObjectPool.Spawn("EnemyCircleBullet"); // Adjust collider size CircleCollider2D cCol = go.GetComponent<CircleCollider2D>(); cCol.radius = Mathf.Max (info.sprite.bounds.size.x, info.sprite.bounds.size.y)/2; break; // Bullet has a box collider case BulletShape.SQUARE: go = ObjectPool.Spawn("EnemySquareBullet"); // Adjust collider size BoxCollider2D bCol = go.GetComponent<BoxCollider2D>(); bCol.size = info.sprite.bounds.size; break; } // Set game object as active go.SetActive(true); // Assign game object position to source bullet poistion go.transform.position = new Vector2(bullet.X, bullet.Y); // Assign sprite based on bullet information SpriteRenderer renderer = go.GetComponent<SpriteRenderer>(); renderer.sprite = info.sprite; // Get BulletScript component from GameObject BulletScript bs = go.GetComponent<BulletScript>(); // Instantiate BulletObject inside BulletScript bs.bullet = bullet as BulletObject; // Assign atributes bs.bullet.Scale = scale; bs.bullet.TimeSpeed = timeSpeed; bs.bullet.holder = go; // Return this BulletObject return bs.bullet; }
void BulletMLLib.IBulletManager.RemoveBullet(BulletMLLib.Bullet deadBullet) { // A "Vanish" tag was called from within a bullet // The bullet will be destroyed. In that case, we // need to "destroy" the GameObject holding this bullet //Debug.Log ("Removing bullet"); // Cast general bullet to our bullet implementation (BulletObject) BulletObject b = deadBullet as BulletObject; // Send notification to pool manager to despawn // the game object that was holding this bullet ObjectPool.Despawn(b.holder); }
/// <summary> /// Look what kind if bullet it is and how to retrieve the game object from it /// </summary> /// <param name="sourceBullet"></param> /// <returns></returns> public GameObject GetGameObjectFromBullet(BulletMLLib.Bullet sourceBullet) { GameObject requestSource = null; if (sourceBullet is TopBullet) { var parent = ((TopBullet)sourceBullet).Parent; if (parent != null) { requestSource = parent.gameObject; } } else if (sourceBullet is BulletObject) { requestSource = ((BulletObject)sourceBullet).Parent; } return requestSource; }
/// <summary> /// Trigger anything from its name /// </summary> /// <param name="source"></param> /// <param name="name"></param> public void Trigger(BulletMLLib.Bullet source, string name) { GameObject sourceObject = GetGameObjectFromBullet(source); if (OnTrigger != null) { OnTrigger(sourceObject, name); } }
/// <summary> /// Clean bullet /// </summary> /// <param name="deadBullet"></param> public void RemoveBullet(BulletMLLib.Bullet deadBullet) { if (OnBulletDestroyed != null) { OnBulletDestroyed(deadBullet); return; } if (deadBullet is BulletObject) { BulletObject b = (BulletObject)deadBullet; if (b != null && b.Parent != null && b.Parent != null) { Destroy(b.Parent); } } }
/// <summary> /// Player current position for the bullet /// </summary> /// <param name="sourceBullet"></param> /// <returns></returns> public Vector2 PlayerPosition(BulletMLLib.Bullet sourceBullet) { if (GetPlayerPosition != null) { // Find the parent GameObject requestSource = GetGameObjectFromBullet(sourceBullet); // Call the handler if (requestSource == null) { Debug.LogError("Unable to get the parent from " + sourceBullet); } return GetPlayerPosition(requestSource); } else if (player != null) { return player.transform.position; } else { return Vector2.zero; } }
/// <summary> /// New bullet creation /// </summary> /// <remarks>Not the bullet spawn, just the container creation, we don't know the bullet name here</remarks> /// <returns></returns> public BulletMLLib.Bullet CreateBullet(BulletMLLib.Bullet source, bool top) { // Try to get the parent GameObject gameObject = null; BulletSourceScript emitter = null; if (source is TopBullet) { emitter = ((TopBullet)source).Parent; if (emitter != null) { gameObject = emitter.gameObject; } } else if (source is BulletObject) { gameObject = ((BulletObject)source).Parent; } // Create a top bullet (weird case) if (top) { return new TopBullet(this, emitter); } else { // Create a bullet BulletObject bullet = null; if (OnBulletCreated != null) { bullet = OnBulletCreated(gameObject); } else { bullet = new BulletObject(this, gameObject); } bullet.OnBulletSpawned += BulletSpawnedHandler; return bullet; } }
/// <summary> /// New bullet creation /// </summary> /// <param name="bulletManager"></param> public BulletObject(BulletMLLib.IBulletManager bulletManager, GameObject parent) : base(bulletManager) { this.Parent = parent; }
/// <summary> /// New top bullet /// </summary> /// <param name="bulletManager"></param> public TopBullet(BulletMLLib.IBulletManager bulletManager, BulletSourceScript parent) : base(bulletManager) { this.position = Vector2.zero; this.Parent = parent; }
Vector2 BulletMLLib.IBulletManager.PlayerPosition(BulletMLLib.Bullet targettedBullet) { if (aimTarget == null) return new Vector2 (0, 0); return aimTarget.transform.position; }
void BulletMLLib.IBulletManager.Trigger(BulletMLLib.Bullet source, string name) { // We probably won't use this tag... // If needed, we can implement this later }