Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
* Implemented ProjectilesAsConsecutiveHittingAndCooldown setting (untested)
* Added assurance of projectiles hitting at least 1 tile in OnTileCollide
* Restructured some projectile code
  • Loading branch information
hamstar0 committed Jun 1, 2019
1 parent 699e91b commit 13b29b9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 70 deletions.
5 changes: 4 additions & 1 deletion Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DestructibleTilesConfigData : ConfigurationDataBase {
public IDictionary<string, int> ProjectileTileDamageDefaults = new Dictionary<string, int>();
public IDictionary<string, int> ProjectileTileDamageOverrides = new Dictionary<string, int>();
public IDictionary<string, int> ProjectilesAsExplosivesAndRadius = new Dictionary<string, int>();
//public IDictionary<string, int[]> ProjectilesAsConsecutiveHittingAndCooldown = new Dictionary<string, int[]>();
public IDictionary<string, int> ProjectilesAsConsecutiveHittingAndCooldown = new Dictionary<string, int>();
//public IDictionary<string, float> ProjectilesAsPhysicsObjectsAndMaxVelocity = new Dictionary<string, float>();

public IDictionary<string, float> TileDamageScaleOverrides = new Dictionary<string, float>();
Expand Down Expand Up @@ -81,6 +81,9 @@ public void SetDefaults() {
this.ProjectileTileDamageDefaults["Terraria.517"] = 65; //Bouncy Grenade
this.ProjectileTileDamageDefaults["Terraria.588"] = 30; //Happy Grenade

this.ProjectilesAsConsecutiveHittingAndCooldown["Terraria."+ProjectileID.MolotovFire] = 60 * 2;
this.ProjectilesAsConsecutiveHittingAndCooldown["Terraria."+ProjectileID.MolotovFire2] = 60 * 2;
this.ProjectilesAsConsecutiveHittingAndCooldown["Terraria."+ProjectileID.MolotovFire3] = 60 * 2;
}

public void SetProjectileDefaults() {
Expand Down
1 change: 1 addition & 0 deletions DestructibleTiles.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Compile Include="Helpers\TileHelpers\TileFinderHelpers.cs" />
<Compile Include="Helpers\TileHelpers\TileHelpers.cs" />
<Compile Include="Helpers\TileHelpers\TileIdentityHelpers.cs" />
<Compile Include="MyProjectile_Data.cs" />
<Compile Include="Overlay.cs" />
<Compile Include="TileData\TileData.cs" />
<Compile Include="TileData\TileDataManager_Update.cs" />
Expand Down
11 changes: 6 additions & 5 deletions Helpers/CollisionHelpers/CollisionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace DestructibleTiles.Helpers.CollisionHelpers {
public class CollisionHelpers {
public static float MeasureWorldDistanceToTile( Vector2 position, Vector2 direction, float maxDistance, List<Tuple<int, int>> ignoredTiles = null ) {
public static float MeasureWorldDistanceToTile( Vector2 position, Vector2 direction, float maxDistance, out bool notFound, List<Tuple<int, int>> ignoredTiles = null ) {
int fromTileX = (int)position.X / 16;
int fromTileY = (int)position.Y / 16;
Vector2 from = position + direction * maxDistance;
Expand All @@ -16,12 +16,13 @@ public static float MeasureWorldDistanceToTile( Vector2 position, Vector2 direct
Tuple<int, int> toTile;
float dist;

if( !Collision.TupleHitLine( fromTileX, fromTileY, toTileX, toTileY, 0, 0, ignoredTiles ?? new List<Tuple<int, int>>(), out toTile ) ) {
dist = new Vector2( Math.Abs( fromTileX - toTile.Item1 ), Math.Abs( fromTileY - toTile.Item2 ) ).Length() * 16f;
} else if( toTile.Item1 == toTileX && toTile.Item2 == toTileY ) {
ignoredTiles = ignoredTiles ?? new List<Tuple<int, int>>();
notFound = Collision.TupleHitLine( fromTileX, fromTileY, toTileX, toTileY, 0, 0, ignoredTiles, out toTile );

if( notFound && toTile.Item1 == toTileX && toTile.Item2 == toTileY ) {
dist = maxDistance;
} else {
dist = new Vector2( Math.Abs(fromTileX - toTile.Item1), Math.Abs(fromTileY - toTile.Item2) ).Length() * 16f;
dist = new Vector2( Math.Abs( fromTileX - toTile.Item1 ), Math.Abs( fromTileY - toTile.Item2 ) ).Length() * 16f;
}

return dist;
Expand Down
102 changes: 38 additions & 64 deletions MyProjectile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DestructibleTiles.Helpers.CollisionHelpers;
using DestructibleTiles.Helpers.TileHelpers;
using HamstarHelpers.Helpers.DebugHelpers;
Expand All @@ -19,47 +18,7 @@ partial class DestructibleTilesProjectile : GlobalProjectile {

////////////////

public static IDictionary<string, Tuple<int, int>> GetExplosives() {
var projectiles = new Dictionary<string, Tuple<int, int>>();

for( int i = 0; i < Main.projectileTexture.Length; i++ ) {
var proj = new Projectile();
Main.projectile[0] = proj;

try {
proj.SetDefaults( i );

if( proj.aiStyle == 16 ) {
int damage = proj.damage;

proj.position = new Vector2( 3000, 1000 );
proj.owner = Main.myPlayer;
proj.hostile = true;

proj.timeLeft = 3;
proj.VanillaAI();

int radius = ( proj.width + proj.height ) / 4;
damage = damage > proj.damage ? damage : proj.damage;

string projName = ProjectileIdentityHelpers.GetProperUniqueId( i );
projectiles[projName] = Tuple.Create( radius, damage );
}
} catch {
continue;
}
}

Main.projectile[0] = new Projectile();

return projectiles;
}



////////////////

public override bool PreAI( Projectile projectile ) {
public override void AI( Projectile projectile ) {
if( projectile.aiStyle == 84 ) {
Vector2 projPos = projectile.Center + projectile.velocity * projectile.localAI[1];
Point? tilePosNull = TileFinderHelpers.GetNearestSolidTile( projPos, 32, false, false );
Expand All @@ -73,16 +32,14 @@ public override bool PreAI( Projectile projectile ) {
int damage = DestructibleTilesProjectile.ComputeProjectileDamage( projectile );

if( DestructibleTilesProjectile.HitTile( damage, tilePos.X, tilePos.Y, 1 ) ) {
projectile.localAI[1] = CollisionHelpers.MeasureWorldDistanceToTile( projectile.Center, projectile.velocity, 2400f );

bool _;
projectile.localAI[1] = CollisionHelpers.MeasureWorldDistanceToTile( projectile.Center, projectile.velocity, 2400f, out _ );
}
//var pos1 = tilePos.ToVector2() * 16f;
//var pos2 = new Vector2( pos1.X + 16, pos1.Y + 16 );
//Dust.QuickBox( pos1, pos2, 0, Color.Red, d => { } );
}
}

return base.PreAI( projectile );
}


Expand Down Expand Up @@ -113,36 +70,53 @@ public override void Kill( Projectile projectile, int timeLeft ) {

public override bool OnTileCollide( Projectile projectile, Vector2 oldVelocity ) {
var mymod = DestructibleTilesMod.Instance;
string projName = ProjectileIdentityHelpers.GetProperUniqueId( projectile.type );

if( mymod.Config.ProjectilesAsExplosivesAndRadius.ContainsKey( projName ) ) {
return base.OnTileCollide( projectile, oldVelocity );
}

//float avg = (projectile.width + projectile.height) / 2;
//bool isOblong = Math.Abs( 1 - (projectile.width / avg) ) > 0.2f;

string timerName = "PTH_" + projectile.whoAmI;
bool isConsecutive = Timers.GetTimerTickDuration( timerName ) > 0;

Timers.SetTimer( timerName, 2, () => false );

if( !isConsecutive ) {
string projName = ProjectileIdentityHelpers.GetProperUniqueId( projectile.type );
bool isExplosive = mymod.Config.ProjectilesAsExplosivesAndRadius.ContainsKey( projName );
if( isConsecutive ) {
if( mymod.Config.ProjectilesAsConsecutiveHittingAndCooldown.ContainsKey( projName ) ) {
string repeatTimerName = timerName + "_repeat";
int cooldown = mymod.Config.ProjectilesAsConsecutiveHittingAndCooldown[projName];

if( !isExplosive ) {
var rect = new Rectangle( (int)projectile.position.X, (int)projectile.position.Y, projectile.width, projectile.height );
rect.X += (int)oldVelocity.X;
rect.Y += (int)oldVelocity.Y;
if( Timers.GetTimerTickDuration(repeatTimerName) <= 0 ) {
Timers.SetTimer( repeatTimerName, cooldown, () => false );
isConsecutive = false;
}
}
}

if( !isConsecutive ) {
var rect = new Rectangle( (int)projectile.position.X, (int)projectile.position.Y, projectile.width, projectile.height );
rect.X += (int)oldVelocity.X;
rect.Y += (int)oldVelocity.Y;

bool onlySometimesRespects;
bool respectsPlatforms = Helpers.ProjectileHelpers.ProjectileHelpers.VanillaProjectileRespectsPlatforms( projectile, out onlySometimesRespects )
&& !onlySometimesRespects;
bool onlySometimesRespects;
bool respectsPlatforms = Helpers.ProjectileHelpers.ProjectileHelpers.VanillaProjectileRespectsPlatforms( projectile, out onlySometimesRespects )
&& !onlySometimesRespects;

IDictionary<int, int> hits = Helpers.TileHelpers.TileFinderHelpers.GetSolidTilesInWorldRectangle( rect, respectsPlatforms, false );
int damage = DestructibleTilesProjectile.ComputeProjectileDamage( projectile );
int damage = DestructibleTilesProjectile.ComputeProjectileDamage( projectile );

if( mymod.Config.DebugModeInfo ) {
Main.NewText( "RECTANGLE - " + projectile.Name + " hits #" + hits.Count + " tiles" );
IDictionary<int, int> hits = Helpers.TileHelpers.TileFinderHelpers.GetSolidTilesInWorldRectangle( rect, respectsPlatforms, false );
if( hits.Count == 0 ) {
Point? point = TileFinderHelpers.GetNearestSolidTile( projectile.Center, 32, respectsPlatforms, false );
if( point.HasValue ) {
hits[ point.Value.X ] = point.Value.Y;
}
}

DestructibleTilesProjectile.HitTilesInSet( damage, hits );
if( mymod.Config.DebugModeInfo ) {
Main.NewText( "RECTANGLE - " + projectile.Name + " hits #" + hits.Count + " tiles" );
}

DestructibleTilesProjectile.HitTilesInSet( damage, hits );
}

return base.OnTileCollide( projectile, oldVelocity );
Expand Down
52 changes: 52 additions & 0 deletions MyProjectile_Data.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DestructibleTiles.Helpers.CollisionHelpers;
using DestructibleTiles.Helpers.TileHelpers;
using HamstarHelpers.Helpers.DebugHelpers;
using HamstarHelpers.Helpers.ProjectileHelpers;
using HamstarHelpers.Services.Timers;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ModLoader;


namespace DestructibleTiles {
partial class DestructibleTilesProjectile : GlobalProjectile {
public static IDictionary<string, Tuple<int, int>> GetExplosives() {
var projectiles = new Dictionary<string, Tuple<int, int>>();

for( int i = 0; i < Main.projectileTexture.Length; i++ ) {
var proj = new Projectile();
Main.projectile[0] = proj;

try {
proj.SetDefaults( i );

if( proj.aiStyle == 16 ) {
int damage = proj.damage;

proj.position = new Vector2( 3000, 1000 );
proj.owner = Main.myPlayer;
proj.hostile = true;

proj.timeLeft = 3;
proj.VanillaAI();

int radius = ( proj.width + proj.height ) / 4;
damage = damage > proj.damage ? damage : proj.damage;

string projName = ProjectileIdentityHelpers.GetProperUniqueId( i );
projectiles[projName] = Tuple.Create( radius, damage );
}
} catch {
continue;
}
}

Main.projectile[0] = new Projectile();

return projectiles;
}
}
}

0 comments on commit 13b29b9

Please sign in to comment.