tModLoader v0.11.8.9
A mod to make and play Terraria mods
ModProjectile.cs
Go to the documentation of this file.
1using Microsoft.Xna.Framework;
2using Microsoft.Xna.Framework.Graphics;
3using System;
4using System.Collections.Generic;
5using System.IO;
6using System.Text.RegularExpressions;
7
8namespace Terraria.ModLoader
9{
13 public class ModProjectile
14 {
15 //add modProjectile property to Terraria.Projectile (internal set)
16 //set modProjectile to null at beginning of Terraria.Projectile.SetDefaults
23 public Projectile projectile {
24 get;
25 internal set;
26 }
27
34 public Mod mod {
35 get;
36 internal set;
37 }
38
45 public string Name {
46 get;
47 internal set;
48 }
49
54 get;
55 internal set;
56 }
57
61 public virtual string Texture => (GetType().Namespace + "." + Name).Replace('.', '/');
65 public virtual string GlowTexture => Texture + "_Glow";
69 public int aiType = 0;
73 public int cooldownSlot = -1;
77 public int drawOffsetX = 0;
81 public int drawOriginOffsetY = 0;
85 public float drawOriginOffsetX = 0f;
90
91 public ModProjectile() {
92 projectile = new Projectile();
93 projectile.modProjectile = this;
94 }
100 public virtual bool Autoload(ref string name) {
101 return mod.Properties.Autoload;
102 }
103
107 public virtual bool CloneNewInstances => false;
108
115 public virtual ModProjectile Clone() => (ModProjectile)MemberwiseClone();
116
123 public virtual ModProjectile NewInstance(Projectile projectileClone) {
124 if (CloneNewInstances) {
125 ModProjectile clone = Clone();
126 clone.projectile = projectileClone;
127 return clone;
128 }
129
130 ModProjectile copy = (ModProjectile)Activator.CreateInstance(GetType());
131 copy.projectile = projectileClone;
132 copy.mod = mod;
133 copy.Name = Name;
134 copy.aiType = aiType;
135 copy.cooldownSlot = cooldownSlot;
136 copy.drawOffsetX = drawOffsetX;
137 copy.drawOriginOffsetY = drawOriginOffsetY;
138 copy.drawOriginOffsetX = drawOriginOffsetX;
139 copy.drawHeldProjInFrontOfHeldItemAndArms = drawHeldProjInFrontOfHeldItemAndArms;
140 return copy;
141 }
142
146 public virtual void SetDefaults() {
147 }
148
152 public virtual void SetStaticDefaults() {
153 }
154
158 public virtual void AutoStaticDefaults() {
159 Main.projectileTexture[projectile.type] = ModContent.GetTexture(Texture);
160 Main.projFrames[projectile.type] = 1;
161 if (projectile.hostile) {
162 Main.projHostile[projectile.type] = true;
163 }
164 if (projectile.aiStyle == 7) {
165 Main.projHook[projectile.type] = true;
166 }
168 DisplayName.SetDefault(Regex.Replace(Name, "([A-Z])", " $1").Trim());
169 }
170
175 public virtual bool PreAI() {
176 return true;
177 }
178
182 public virtual void AI() {
183 }
184
188 public virtual void PostAI() {
189 }
190
194 public virtual void SendExtraAI(BinaryWriter writer) {
195 }
196
200 public virtual void ReceiveExtraAI(BinaryReader reader) {
201 }
202
206 public virtual bool ShouldUpdatePosition() {
207 return true;
208 }
209
216 public virtual bool TileCollideStyle(ref int width, ref int height, ref bool fallThrough) {
217 return true;
218 }
219
224 public virtual bool OnTileCollide(Vector2 oldVelocity) {
225 return true;
226 }
227
231 public virtual bool? CanCutTiles() {
232 return null;
233 }
234
238 public virtual void CutTiles() {
239 }
240
244 public virtual bool PreKill(int timeLeft) {
245 return true;
246 }
247
251 public virtual void Kill(int timeLeft) {
252 }
253
257 public virtual bool CanDamage() {
258 return true;
259 }
260
264 public virtual bool MinionContactDamage() {
265 return false;
266 }
267
272 public virtual void ModifyDamageHitbox(ref Rectangle hitbox) {
273 }
274
279 public virtual bool? CanHitNPC(NPC target) {
280 return null;
281 }
282
291 public virtual void ModifyHitNPC(NPC target, ref int damage, ref float knockback, ref bool crit, ref int hitDirection) {
292 }
293
301 public virtual void OnHitNPC(NPC target, int damage, float knockback, bool crit) {
302 }
303
308 public virtual bool CanHitPvp(Player target) {
309 return true;
310 }
311
318 public virtual void ModifyHitPvp(Player target, ref int damage, ref bool crit) {
319 }
320
327 public virtual void OnHitPvp(Player target, int damage, bool crit) {
328 }
329
334 public virtual bool CanHitPlayer(Player target) {
335 return true;
336 }
337
344 public virtual void ModifyHitPlayer(Player target, ref int damage, ref bool crit) {
345 }
346
353 public virtual void OnHitPlayer(Player target, int damage, bool crit) {
354 }
355
362 public virtual bool? Colliding(Rectangle projHitbox, Rectangle targetHitbox) {
363 return null;
364 }
365
369 public virtual Color? GetAlpha(Color lightColor) {
370 return null;
371 }
372
376 public virtual bool PreDrawExtras(SpriteBatch spriteBatch) {
377 return true;
378 }
379
383 public virtual bool PreDraw(SpriteBatch spriteBatch, Color lightColor) {
384 return true;
385 }
386
390 public virtual void PostDraw(SpriteBatch spriteBatch, Color lightColor) {
391 }
392
396 public virtual bool? CanUseGrapple(Player player) {
397 return null;
398 }
399
403 public virtual bool? SingleGrappleHook(Player player) {
404 return null;
405 }
406
410 public virtual void UseGrapple(Player player, ref int type) {
411 }
412
416 public virtual float GrappleRange() {
417 return 300f;
418 }
419
423 public virtual void NumGrappleHooks(Player player, ref int numHooks) {
424 }
425
429 public virtual void GrappleRetreatSpeed(Player player, ref float speed) {
430 }
431
435 public virtual void GrapplePullSpeed(Player player, ref float speed) {
436 }
437
441 public virtual void GrappleTargetPoint(Player player, ref float grappleX, ref float grappleY) {
442 }
443
447 public virtual void DrawBehind(int index, List<int> drawCacheProjsBehindNPCsAndTiles, List<int> drawCacheProjsBehindNPCs, List<int> drawCacheProjsBehindProjectiles, List<int> drawCacheProjsOverWiresUI) {
448 }
449 }
450}
Manages content added by mods. Liasons between mod content and Terraria's arrays and oversees the Loa...
Definition: ModContent.cs:27
static Texture2D GetTexture(string name)
Gets the texture with the specified name. The name is in the format of "ModFolder/OtherFolders/FileNa...
Definition: ModContent.cs:72
Mod is an abstract class that you will override. It serves as a central place from which the mod's co...
Definition: Mod.cs:25
ModProperties Properties
Definition: Mod.cs:52
This class serves as a place for you to place all your properties and hooks for each projectile....
virtual void ModifyDamageHitbox(ref Rectangle hitbox)
Allows you to change the hitbox used by this projectile for damaging players and NPCs.
virtual bool Autoload(ref string name)
Allows you to automatically load a projectile instead of using Mod.AddProjectile. Return true to allo...
virtual void AI()
Allows you to determine how this projectile behaves. This will only be called if PreAI returns true.
virtual void OnHitPvp(Player target, int damage, bool crit)
Allows you to create special effects when this projectile hits an opponent player.
virtual bool OnTileCollide(Vector2 oldVelocity)
Allows you to determine what happens when this projectile collides with a tile. OldVelocity is the ve...
bool drawHeldProjInFrontOfHeldItemAndArms
If this projectile is held by the player, determines whether it is drawn in front of or behind the pl...
virtual void PostAI()
Allows you to determine how this projectile behaves. This will be called regardless of what PreAI ret...
virtual ? bool CanUseGrapple(Player player)
This code is called whenever the player uses a grappling hook that shoots this type of projectile....
virtual void ModifyHitPvp(Player target, ref int damage, ref bool crit)
Allows you to modify the damage, etc., that this projectile does to an opponent player.
virtual void ModifyHitNPC(NPC target, ref int damage, ref float knockback, ref bool crit, ref int hitDirection)
Allows you to modify the damage, knockback, etc., that this projectile does to an NPC....
virtual void SetDefaults()
Allows you to set all your projectile's properties, such as width, damage, aiStyle,...
virtual bool MinionContactDamage()
Whether or not this minion can damage NPCs by touching them. Returns false by default....
virtual ? bool CanHitNPC(NPC target)
Allows you to determine whether this projectile can hit the given NPC. Return true to allow hitting t...
virtual void UseGrapple(Player player, ref int type)
This code is called whenever the player uses a grappling hook that shoots this type of projectile....
virtual bool PreAI()
Allows you to determine how this projectile behaves. Return false to stop the vanilla AI and the AI h...
virtual void CutTiles()
Code ran when the projectile cuts tiles. Only runs if CanCutTiles() returns true. Useful when program...
virtual bool CanHitPvp(Player target)
Allows you to determine whether this projectile can hit the given opponent player....
virtual void OnHitNPC(NPC target, int damage, float knockback, bool crit)
Allows you to create special effects when this projectile hits an NPC (for example,...
virtual ModProjectile Clone()
Returns a clone of this ModProjectile. Allows you to decide which fields of your ModProjectile class ...
Projectile projectile
The projectile object that this ModProjectile controls.
virtual bool CanDamage()
Whether or not this projectile is capable of killing tiles (such as grass) and damaging NPCs/players....
virtual ? Color GetAlpha(Color lightColor)
Allows you to determine the color and transparency in which this projectile is drawn....
virtual bool TileCollideStyle(ref int width, ref int height, ref bool fallThrough)
Allows you to determine how this projectile interacts with tiles. Width and height determine the proj...
virtual string GlowTexture
The file name of this projectile's glow texture file in the mod loader's file space....
virtual ModProjectile NewInstance(Projectile projectileClone)
Create a new instance of this ModProjectile for a Projectile instance. Called at the end of Projectil...
virtual string Texture
The file name of this projectile's texture file in the mod loader's file space.
int drawOffsetX
How far to the right of its position this projectile should be drawn. Defaults to 0.
virtual void NumGrappleHooks(Player player, ref int numHooks)
How many of this type of grappling hook the given player can latch onto blocks before the hooks start...
int drawOriginOffsetY
The vertical origin offset from the projectile's center when it is drawn. The origin is essentially t...
virtual float GrappleRange()
How far away this grappling hook can travel away from its player before it retracts.
virtual void PostDraw(SpriteBatch spriteBatch, Color lightColor)
Allows you to draw things in front of a projectile. This method is called even if PreDraw returns fal...
virtual void DrawBehind(int index, List< int > drawCacheProjsBehindNPCsAndTiles, List< int > drawCacheProjsBehindNPCs, List< int > drawCacheProjsBehindProjectiles, List< int > drawCacheProjsOverWiresUI)
When used in conjunction with "projectile.hide = true", allows you to specify that this projectile sh...
virtual void OnHitPlayer(Player target, int damage, bool crit)
Allows you to create special effects when this hostile projectile hits a player.
int aiType
Determines which type of vanilla projectile this ModProjectile will copy the behavior (AI) of....
Mod mod
The mod object that this ModProjectile originates from.
float drawOriginOffsetX
The horizontal origin offset from the projectile's center when it is drawn.
int cooldownSlot
Determines which of the player's cooldown counters to use (-1, 0, or 1) when this projectile damages ...
ModTranslation DisplayName
The translations for the display name of this projectile.
virtual void Kill(int timeLeft)
Allows you to control what happens when this projectile is killed (for example, creating dust or maki...
virtual ? bool SingleGrappleHook(Player player)
Whether or not a grappling hook can only have one hook per player in the world at a time....
virtual bool PreDraw(SpriteBatch spriteBatch, Color lightColor)
Allows you to draw things behind this projectile, or to modify the way this projectile is drawn....
virtual void ModifyHitPlayer(Player target, ref int damage, ref bool crit)
Allows you to modify the damage, etc., that this hostile projectile does to a player.
virtual void SetStaticDefaults()
Allows you to set all your projectile's static properties, such as names/translations and the arrays ...
virtual void GrapplePullSpeed(Player player, ref float speed)
The speed at which the grapple pulls the player after hitting something. Defaults to 11,...
virtual bool PreDrawExtras(SpriteBatch spriteBatch)
Allows you to draw things behind this projectile. Returns false to stop the game from drawing extras ...
virtual bool ShouldUpdatePosition()
Whether or not this projectile should update its position based on factors such as its velocity,...
virtual void GrappleRetreatSpeed(Player player, ref float speed)
The speed at which the grapple retreats back to the player after not hitting anything....
virtual void AutoStaticDefaults()
Automatically sets certain static defaults. Override this if you do not want the properties to be set...
virtual void ReceiveExtraAI(BinaryReader reader)
Use this to receive information that was sent in SendExtraAI.
virtual ? bool CanCutTiles()
Return true or false to specify if the projectile can cut tiles, like vines. Return null for vanilla ...
virtual bool CanHitPlayer(Player target)
Allows you to determine whether this hostile projectile can hit the given player. Return false to blo...
virtual bool PreKill(int timeLeft)
Allows you to determine whether the vanilla code for Kill and the Kill hook will be called....
virtual void SendExtraAI(BinaryWriter writer)
If you are storing AI information outside of the projectile.ai array, use this to send that AI inform...
virtual void GrappleTargetPoint(Player player, ref float grappleX, ref float grappleY)
The location that the grappling hook pulls the player to. Defaults to the center of the hook projecti...
string Name
The internal name of this ModProjectile.
virtual ? bool Colliding(Rectangle projHitbox, Rectangle targetHitbox)
Allows you to use custom collision detection between this projectile and a player or NPC that this pr...
virtual bool CloneNewInstances
Whether instances of this ModProjectile are created through a memberwise clone or its constructor....
bool Autoload
Whether or not this mod will autoload content by default. Autoloading content means you do not need t...