tModLoaderI wonder how to make a check collision between blocks and NPCs?

Jefferson Luis Bernardo

Terrarian
Code:
``````using Terraria;
using Terraria.ID;
using Terraria.ObjectData;
using Microsoft.Xna.Framework;

namespace Castleroid.NPCs
{
public class gosma : ModNPC
{
int segundos = 0;
public override void SetDefaults()
{
npc.name = "Gosma";
npc.displayName = "Gosma";
npc.width = 18;
npc.height = 40;
npc.damage = 14;
npc.defense = 6;
npc.lifeMax = 200;
npc.soundHit = 7;
npc.soundKilled = 5;
npc.noTileCollide = false;//colisões com pisos
npc.value = 60f;
npc.knockBackResist = 0f;//1.0f sem resistencia - 0.0000... quanto mais zeros mais resitente
}

public override float CanSpawn(NPCSpawnInfo spawnInfo)
{
return spawnInfo.spawnTileY < Main.rockLayer && !Main.dayTime ? 0.5f : 0f;
}

public override void AI()
{
npc.collideX = true;
npc.collideY = true;

float vel_x = npc.position.X;
float vel_y = npc.position.Y;
segundos++;
}
if(segundos > 3){
//vel_x += 16;
vel_y -= 64;//plano cartesiano o ponto de horigem é diferente - 4 blocos altura
segundos = 0;
}
string s =  seconds.ToString();// converte variavel segundos para o formato de texto
npc.position = new Vector2(vel_x,vel_y);
Main.NewText(s, 255, 240, 20, false);// Texto com posicao x

}

}
}``````

The npc jumps every 3 seconds, but it does not collide with blocks during the jump

You use 2 variables to manage counting? Pretty wasteful, but it works, I guess...
Also, you set the position, not the velocity. Change "npc.position = blah" to "npc.velocity = blah"
Setting the position will ignore tilecollisions because you can set the position inside of a block. Terraria handles tile collisions by checking that position plus the velocity it currently has, and checks if it collides that frame. If it does, it stops the NPC.

``````            if(contador > 60){//60 numero de fps do jogo 60quadros por segundo