-
Notifications
You must be signed in to change notification settings - Fork 0
Creating Custom Projectiles
The first thing you'll want to do is decide what type of projectile you want to create. The three main projectile classes in Broforce are Projectile
, Grenade
, and SachelPack
.
The Projectile
class is what all bullet projectiles inherit from. By default they are deleted on contact but you can make them pierce instead if you want.
Grenades
don't actually inherit from the Projectile class, and they have some other weird properties. They have a color trail, start flashing when they are about to explode, and can be rethrown. All of these properties can be changed however.
SachelPack
inherits from Projectile
, so they are pretty similar, but they have some extra code for sticking to enemies and walls that can be useful.
Creating custom projectiles isn't too difficult. BroMaker provides the following classes that you can inherit from:
BroMakerLib.CustomObjects.Projectiles.CustomProjectile
BroMakerLib.CustomObjects.Projectiles.CustomGrenade
BroMakerLib.CustomObjects.Projectiles.CustomSachelPack
These classes provide some additional utilities to ease the creation of custom projectiles, but using them is not required. You can instead inherit from one of the base game's classes, which may be a good idea if you want to mostly copy an existing projectile with some slight modifications. But keep in mind you may need to add some additional code to handle some of the things that BroMaker's custom classes do. You can consult the source code of CustomProjectile
/ CustomGrenade
to see what all they do if you want to inherit from a specific base game projectile instead.
Your Projectile class should look something like this probably:
using BroMakerLib.CustomObjects.Projectiles;
class YourProjectileClass : CustomProjectile
{
protected override void Awake()
{
base.Awake();
}
}
By default the CustomProjectile class will assume the sprite for your projectile is in a folder called "projectiles" in your bro's folder in BroMaker_Storage. It'll assume the name of your sprite file is "the name of your class.png". You can change these defaults like so:
protected override void Awake()
{
this.spriteFolder = "folderwithmysprites"
this.spriteFileName = "mysprite.png";
base.Awake();
}
CustomProjectile will also automatically set the width, height, and pixel dimensions of your projectile's sprite based on the actual width and height of the sprite file. This is fine for most projectiles, but if you have a projectile with multiple frames then you'll want to ensure you set these yourself, like so:
protected override void Awake()
{
// Assuming your sprite has multiple 16 pixel by 16 pixel frames
this.spriteHeight = 16;
this.spriteWidth = 16;
this.spriteLowerLeftPixel = new Vector2( 0, 16 );
this.spritePixelDimensions = new Vector2( 16, 16 );
base.Awake();
}
spriteHeight
and spriteWidth
can also be set to any value to change the size of your projectile as it appears in-game. They don't have to match the actual width and height of your projectile's sprite file.
protected override void Awake()
{
this.spriteHeight = 20;
this.spriteWidth = 40;
base.Awake();
}
You can view other options that CustomProjectile provides by typing this.
and looking at the different variables available. You'll need to make sure you have your .xml files set up though.
CustomGrenade
and CustomSachelPack
provide all those same options for easily loading sprites.
After you've created your custom projectile class, you'll want to create a prefab of the object that you can continually spawn. So you'll want to have something like this in the Awake method or Start method of your bro, and you should make sure you store the prefab you're creating for use in other methods:
this.yourProjectilePrefab = CustomProjectile.CreatePrefab<YourProjectileClass>();
The prefab created by this method is automatically stored in a cache to avoid having to recreate it in the future. For grenades you simply have to use CustomGrenade
instead of CustomProjectile
Finally when you want to actually spawn the projectile when your character does a specific action, like shooting, using their special, or meleeing, you'll want to do something like this:
this.yourProjectilePrefab.SpawnProjectileLocally( this, x, y, xSpeed, ySpeed, base.playerNum );
The this.yourProjectilePrefab should be replaced with whatever you've called the variable that is storing your CustomProjectile
prefab.
x and y should be replaced with the location you want the projectile to spawn, which is usually an offset of your character's position, for example:
base.X + base.transform.localScale.x * 10f, base.Y + 8f
xSpeed and ySpeed should be replaced by the speed of your object when it spawns, for example:
base.transform.localScale.x * 300f, 100f
If you want to add spread to your projectiles you could do something like this for the ySpeed:
(float)UnityEngine.Random.Range(-20, 20)
If you have questions or need help with creating custom bros, you can join the Free Lives Discord Server and post your questions in the bf-mods channel.