-- v1.3.0

local torch = {}

local redstone = require("redstone")
local npcManager = require("npcManager")

torch.name = "torch"
torch.id = NPC_ID
torch.order = 0.26

torch.onRedPower = function(n, c, power, dir, hitbox)
  if (redstone.is.block(c.id) and dir == 1) or dir == -1 then
    redstone.setEnergy(n, power)
  else
    return true
  end
end

torch.config = npcManager.setNpcSettings({
	id = torch.id,

  width = 32,
  height = 64,

	gfxwidth = 32,
	gfxheight = 64,
	gfxoffsetx = 0,
	gfxoffsety = 0,

	frames = 2,
	framespeed = 8,
	framestyle = 0,
  invisible = false,

  noblockcollision = true,
  notcointransformable = true,
  nogravity = true,
	jumphurt = true,
	nohurt = true,
	noyoshi = true,
  disabledespawn = false,
})

function torch.prime(n)
  local data = n.data

  data.animFrame = data.animFrame or 0
  data.animTimer = data.animTimer or 0

  data.frameX = data.frameX or 0
  data.frameY = data.frameY or 0

  n.height = n.height*0.5
  data.redarea = data.redarea or redstone.basicRedArea(n)
  data.redhitbox = data.redhitbox or redstone.basicRedHitBox(n)
  n.height = n.height*2
end

function torch.onRedTick(n)
  local data = n.data
  data.observ = false

  if data.power == 0 then
    n.height = n.height*0.5
    redstone.updateRedArea(n)
    redstone.updateRedHitBox(n)
    n.height = n.height*2
    redstone.passEnergy{source = n, power = 15, hitbox = data.redhitbox, area = data.redarea}
  end

  if (data.power == 0 and data.powerPrev ~= 0) or (data.power ~= 0 and data.powerPrev == 0) then
    data.observ = true
  end

  if data.power == 0 then
    data.frameY = 0
  else
    data.frameY = 1
  end

  redstone.resetPower(n)
end

torch.onRedDraw = redstone.drawNPC

redstone.register(torch)

return torch
