Files
CalamityModPublic/TileEntities/TEBaseTurret.cs
T
DominicKarma42 a8b15f290e Another load of Arsenal things. See description.
Updated Draedon's logbook GUI to be a bit more advanced.

Added Dubious Plating and Mysterious Circuitry. Currently not used in recipes yet.

Added Gauss Pistol

Several misc changes/fixes pertaining to Arsenal content.

Added a BaseTurret tile entity and Draedon Turret which derives from this.

Added a Draedon Energy field tile entity.

Renamed the item generator internally. Its name used to use "Dreadon" instead of "Draedon".

 Updated worldgen schematics to use the field generator and turret.

Added a hologram tile entity that can be interacted with like a typical town NPC. Placeholder text is used currently.
2020-06-21 03:07:51 -05:00

133 lines
5.7 KiB
C#

using CalamityMod.Projectiles.Enemy;
using CalamityMod.Tiles;
using Microsoft.Xna.Framework;
using System;
using System.IO;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace CalamityMod.TileEntities
{
public class TEBaseTurret : ModTileEntity
{
public int Time;
public int Direction;
public float Rotation;
public virtual int TileType => ModContent.TileType<DraedonTurretTile>();
public virtual int ShootWaitTime => 15;
public virtual int ShootRate => 60;
public virtual int ProjectileDamage => 20;
public virtual float ShootSpeed => 5f;
public virtual float ShootSpawnOffset => 32f;
public virtual float ProjectileKnockback => 3f;
public virtual float MaxAngleOffestShootPrompt => MathHelper.ToRadians(35f);
public virtual int ProjectileShot => ModContent.ProjectileType<DreadonLaser>();
public virtual Vector2 ShootCoordsOffset => new Vector2(22f + 4f * Direction, -2f);
public virtual float TargetDistanceCheck => 600f;
public void SyncTile()
{
NetMessage.SendData(MessageID.TileEntitySharing, -1, -1, null, ID, Position.X, Position.Y);
}
public virtual int AttemptToLocatePlayer(Vector2 shootTargetCoords)
{
int playerIndex = -1;
for (int i = 0; i < Main.player.Length; i++)
{
// Ignore dead/inactive players. They are irrelevant since we're supposed to target a player.
if (!Main.player[i].active || Main.player[i].dead)
continue;
if (Main.player[i].Distance(shootTargetCoords) < TargetDistanceCheck)
{
playerIndex = i;
break;
}
}
return playerIndex;
}
public virtual void AttemptToFireAtPlayer(int playerIndex, Vector2 shootTargetCoords)
{
Player player = Main.player[playerIndex];
float angleToTarget = (player.Center - shootTargetCoords).ToRotation();
float idealAngle = angleToTarget + (Direction == -1).ToInt() * MathHelper.Pi;
// Adjust the rotation before waiting, and then firing.
if (Time % ShootRate < ShootRate - ShootWaitTime)
{
AimTowardsPlayer(playerIndex, shootTargetCoords, angleToTarget, ref idealAngle);
SyncTile(); // The stupid rotation/direction doesn't sync otherwise. Don't ask me why.
}
// If at the end of the shoot cycle, and the angle offset isn't too far away, fire at the player.
// Think of the angle offset as how close the turret is to aiming 100% perfectly at the player.
if (Time % ShootRate == ShootRate - 1 && Math.Abs(MathHelper.WrapAngle(idealAngle - Rotation)) < MaxAngleOffestShootPrompt)
{
ShootProjectile(shootTargetCoords);
SyncTile();
}
}
public virtual void AimTowardsPlayer(int playerIndex, Vector2 shootTargetCoords, float angleToTarget, ref float idealAngle)
{
Player player = Main.player[playerIndex];
int idealDirection = (player.Center.X - shootTargetCoords.X > 0).ToDirectionInt();
// Adjust the direction if the turret should do so, along with the angle.
if (Direction != idealDirection)
{
Direction = idealDirection;
idealAngle = angleToTarget + (Direction == -1).ToInt() * MathHelper.Pi;
Rotation = Rotation.AngleLerp(idealAngle, 0.4f); // Get a head start in rotating towards the ideal angle.
}
Rotation = Rotation.AngleLerp(idealAngle, 0.1f);
}
public virtual void ShootProjectile(Vector2 shootCoords)
{
float adjustedAngle = Rotation - (Direction == -1).ToInt() * MathHelper.Pi;
Vector2 velocity = adjustedAngle.ToRotationVector2() * ShootSpeed;
Projectile.NewProjectileDirect(shootCoords + adjustedAngle.ToRotationVector2() * ShootSpawnOffset, velocity, ProjectileShot, ProjectileDamage, ProjectileKnockback);
}
public override bool ValidTile(int i, int j)
{
Tile tile = Main.tile[i, j];
return tile.active() && tile.type == TileType;
}
public override void Update()
{
Vector2 positionWorldCoords = Position.ToVector2() * 16f;
Vector2 shootTargetCoords = positionWorldCoords + ShootCoordsOffset;
int playerIndex = AttemptToLocatePlayer(shootTargetCoords);
if (playerIndex != -1)
{
AttemptToFireAtPlayer(playerIndex, shootTargetCoords);
Time++;
}
else
{
Rotation = Rotation.AngleLerp(0f, 0.25f);
}
}
public override int Hook_AfterPlacement(int i, int j, int type, int style, int direction)
{
if (Main.netMode == NetmodeID.MultiplayerClient)
{
NetMessage.SendTileSquare(Main.myPlayer, i, j, 3);
NetMessage.SendData(MessageID.TileEntityPlacement, -1, -1, null, i, j, Type, 0f, 0, 0, 0);
return -1;
}
return Place(i, j);
}
public override void NetSend(BinaryWriter writer, bool lightSend)
{
writer.Write(Time);
writer.Write(Direction);
writer.Write(Rotation);
}
public override void NetReceive(BinaryReader reader, bool lightReceive)
{
Time = reader.ReadInt32();
Direction = reader.ReadInt32();
Rotation = reader.ReadSingle();
}
}
}