LucLuz
Terrarian
I was making some things for my npc and i noticed that he just don't work when on a platform, my code is checking if he's on the ground before doing some things like jumping and adding friction, so i want to know if there's any way to make platforms count as solid tiles.
Code:
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using System;
namespace LucTerraMod.Content.AI
{
public class ModFighterAI
{
public static float Speed = 1f;
public static float JumpPower = 7f;
public static bool collidingY = false;
public static void RunAI(NPC npc) {
npc.TargetClosest(true);
npc.spriteDirection = npc.direction;
Friction(npc);
Move(npc);
CheckObstacles(npc);
}
private static void Move(NPC npc) {
if (npc.ai[0] != 1f) {
if (Math.Abs(npc.velocity.X) == 0 || Math.Sign(npc.velocity.X) == npc.direction) {
npc.velocity.X = Speed * npc.direction;
}
}
}
private static void CheckObstacles(NPC npc) {
int tileX = (int)((npc.Center.X + npc.direction * npc.width / 2 + npc.direction) / 16);
int tileY = (int)((npc.position.Y + npc.height -1f) / 16f);
bool blockeddown = WorldGen.SolidOrSlopedTile(tileX, tileY);
bool blockedfront = WorldGen.SolidOrSlopedTile(tileX, tileY - 1);
bool blockedup = WorldGen.SolidOrSlopedTile(tileX, tileY -2);
bool holeunder = !WorldGen.SolidOrSlopedTile(tileX, tileY + 1);
collidingY = Collision.SolidCollision(npc.position, npc.width, npc.height + 1);
if (blockedup && collidingY) {
Jump(npc, 1);
} else if (blockedfront && collidingY) {
Jump(npc);
} else if (blockeddown && collidingY) {
npc.velocity.Y += -3.5f;
} else if (holeunder && collidingY) {
Jump(npc, 1.7f);
}
}
private static void Jump(NPC npc, float extraforce = 0f) {
npc.velocity.Y = -(JumpPower + extraforce);
}
private static void Friction(NPC npc) {
if (collidingY) {
npc.velocity.X *= 0.8f;
}
}
}
}