Creating a Simple 3D Game with XNA/Adding NPCs

Creating Other Fish
The simplest way to create other fish is to copy the model file and modify the local textures to it. In my case, I created a new folder in the 'Content'/'Models' folder called 'OtherFish', and modified the textures so that all of the textures are shades of orange instead of blue. Ensure the new texture files are the same name as the previous ones, and that the 'Content Processor' is set to 'SkinnedModelProcessor', and it will work.

Next up, create a new array of your usermade 'ModelRenderer' type called OtherFishes alongside your other class variables.

And initialise them by placing the following 'for' statement inside your LoadContent method alongside the others.

In the same way as the original fish we created, we need to place a call to each of these fishes draw and update methods. Place in the following statements, this in alongside the 'FishMen' Update method call;

And this alongside the it's Draw call.

This covers the routine aspects to displaying the fish.

Moving the Fish
Create a new method called 'ResetNPCFish' as below;

And place a call to it inside your initialisation routine as below.

If you run the game as it is, you should see 16 fish initialised swim past the controllable fish, and little else.



To ensure that the fish swim indefinitely, add the following changes to the 'OtherFishes's update code, which will check the fishes position on the screen, and restart it if needed.

Run it now, and it will run indefinitely.

Hitting the Fish
Next up, we will place in code which will default the fish to displaying a frown, then when it is bumped into by the player, will change the fishes frowns upside down. Add the following line at the bottom of the 'ResetNPCFish' method.

To check a collision between the player and the enemy, create the following two new methods.

And the following alongside the other pieces of update code.

Play it now, and you should see the fish change when you touch them.