Tonight I got bullets implemented in Whirlygig. You can now press the screen and the heli turns to face the place you pressed and unleashes a torrent of bullets at your finger.

 

This was trickier than it sounds for a number of reasons. First of all, let’s talk about organization. An Entity is a sort of base class for almost any object that exists in the game beyond a simple sprite. Many Entities will share some common attributes such as health, a visible representation (Sprite), a collision shape and more. I create an object that extends Entities called an Actor. An Actor is any Entity that can collide and take damage. Enemies, Players and Neutral parties are all Actors and further Extend the Actor class as needed.

 

So, the Player entity, which extends Actor, needs a place (i.e. a List) to dump all of its shots. But other Actors may also be able to fire shots so this should be implemented at the Actor level, not the Player level. Also, since Entities should be as self-contained as possible this Shot List should exist at the Screen level and Actors should just have a reference to it. This requires that the Screen perform an extra step every time it creates an Actor. That’s mildly inconvenient but I like that method better than the other things I came up. So, the base level object must remember to give any actors it creates a reference to its Shot List if they will be firing shots:

Player.ShotListReference = Shots;

 

 

So, now that I’ve solved the Shot List problem I should be able to use most of the firing code I wrote for Whirlygig Xbox, right? Partially. But firing can only happen if the Actor is in Normal State. Oops. States are not implemented. So, I implement all of the States that an Actor can be in: Normal, Invincible, Dead and Disabled. And then I also need to implement some state management.

 

Still not good to go. Bullets create a Fired Effect when instantiated, leave a particle trail Effect as they move and create a Death Effect when they die. I didn’t have time to port the entire effects system tonight so I simply commented that out for now.

 

After I got all of the base stuff ported over the rest was pretty easy. I worked through a few changes to deal with differences in Phone controls vs Xbox controls but bullets now fire nicely. Here’s the call:

if (GuiManager.Cursor.PrimaryDown)
{
	SetFaceDirection();
	FireMinigun();
}
else
{
	StopFireMinigun();
}

And here’s the FireMinigun() method:

private void FireMinigun()
{
	if (!mGunOverheated && ShotListReference != null)
	{
		if (mReloadTime <= 0 && !mGunOverheated)
		{
			Vector3 firePosition = GetFirePosition();
			float fireAngle = GetFireAngle();
			//ShellEmitter.StartEmit();
			//GunSmokeEmitter.StartEmit(fireAngle);

			Bullet b = WhirlygigWP7.Factories.BulletFactory.CreateNew();
			b.SetUpShot(firePosition, fireAngle, this);

			mReloadTime = (1f / ShotsPerSecond);
			if (mOverheatTime > SecondsToGunOverheat)
			{
				mOverheatTime = SecondsToGunOverheat;
				mGunOverheated = true;
			}
		}

		mOverheatTime += TimeManager.SecondDifference;
	}
	else
	{
		StopFireMinigun();
	}
}

The other main thing I laid out was collision detection. The player now collides with neutral actors and the framework is there for the rest of collision detection. I’m a little worried about this simply because I love the minigun but it puts a lot of bullets on the screen. Enemy bullets need to test against the Player, neutral Actors and the level geometry. Player bullets need to test against enemies and neutral actors. Players and Enemies need to collide against each other, neutral Actors and level geometry. That’s a lot of testing each frame. I’ll have to optimize the heck out of that to keep performance nice.

 

On a positive note, limiting the amount of bullets you could fire didn’t work at first. I fired a LOT of bullets and the phone didn’t struggle at all. But, they weren’t testing for collision against anything. We’ll see how that goes :P