-
Notifications
You must be signed in to change notification settings - Fork 6
en Basic PRT System
PRT is an abbreviation for 'Particle', used to distinguish it from other particle systems.
Compared to the original Dust system, the PRT system provided by InnoVault offers greater customization options, allowing developers to implement more complex particle effects more efficiently.
BasePRT is the base class for PRT entities. Inherit from it to create various special particle effects.
The implementation and development process of PRT entities is similar to that of bullet patterns.
Each PRT entity is an independent instance with its own field space, and they do not interfere with each other.
Below is a simple particle example from InnoVaultExample
// Our first PRT particle, pretty cool right? It is generated in VaultSword,
// so grab the sword and check out the effect.
internal class ExamplePRT : BasePRT
{
// The Texture property doesn't need to be overridden, as BasePRT has an automatic loading mechanism.
// It automatically loads a .png file with the same name in the same directory.
// This is similar to how ModProjectile works.
// So, let's prepare a .png file called "ExamplePRT", which is an image with the same name as the class.
// public override string Texture => base.Texture;
// Override this function, it will be called once when the particle is generated.
// PRT entities are independent instances, so the settings in this function
// can also be applied to each instance individually, similar to ModProjectile.SetDefaults.
public override void SetProperty() {
// PRTDrawMode determines which rendering mode the instance will be batched into.
// This sets the color blending mode for the particle's rendering.
// Here, we set it to additive blending mode. The effect brought by this field is real-time,
// and it will batch all PRT instances in each draw call.
PRTDrawMode = PRTDrawModeEnum.AdditiveBlend;
SetLifetime = true; // Set this to true, and the particle will be subject to a lifetime limit.
Lifetime = Main.rand.Next(220, 360); // Lifetime of 220 to 360 ticks.
}
public override void AI() {
// A simple AI that randomly changes direction.
if (--ai[0] <= 0) {
Velocity = Velocity.RotatedByRandom(0.3f);
ai[0] = Main.rand.Next(10);
}
// Adjust the rotation according to the movement direction.
Rotation += Velocity.X * 0.02f;
//// Relative position change
// Position += Main.LocalPlayer.velocity;
// A fun brightness transformation.
Color.A = (byte)(1 - LifetimeCompletion * 255f);
// Apply a fading effect near the end of its life.
if (LifetimeCompletion > 0.8f) {
Color *= 0.9f;
}
}
// Override this drawing function. If you want to customize the drawing, return false here,
// and the default drawing will not be applied.
public override bool PreDraw(SpriteBatch spriteBatch) => true;
}Next, let's explain the implementation of this class sentence by sentence.
- SetProperty
This function is called once when a PRT entity is created. At this point, the particle's ID has already been assigned, and it will be added to the entity collection for updates. You can use this function to initialize some data for this instance, similar to the ModProjectile.SetDefaults function.
- PRTDrawMode
In line 13, the code PRTDrawMode = PRTDrawModeEnum.AdditiveBlend; sets the particle's drawing mode, which determines how the particle is rendered using blending modes.
PRTDrawMode affects which collection the instance of this particle will be assigned to. This allocation happens every frame, meaning you can change the instance's PRTDrawMode value at any time and observe its real-time changes.
- AI
This function is used for logical updates. Here, you write code to control the specific behavior of the particle.
- PreDraw
This drawing function updates before the default drawing occurs. If you return true, the particle will perform the default drawing behavior, which is useful in most cases.
However, you can choose to return false, which will disable the subsequent default drawing behavior, allowing you to write code in this function for a higher degree of custom drawing.
- PostDraw
This drawing function updates after the default drawing, and it will run regardless of what PreDraw returns. In most cases, we do not use this function.
This class is responsible for most of the logical updates in the PRT system and is the core of the PRT system.
- NewParticle
We typically use this function to generate particles, similar to ModProjectile.NewProjectile, in conjunction with PRTLoader.GetParticleID<T> to obtain the internal ID value of the target particle.
Here is an example of usage from InnoVaultExample.
PRTLoader.NewParticle(
PRTLoader.GetParticleID<ExamplePRT>()// Get our particle ID for generation
, player.Center// Generated on the player's position
, Main.rand.NextFloat(MathHelper.TwoPi).ToRotationVector2() * 6// A random direction
, Color.White, Main.rand.NextFloat(1, 2));