Currently, I have the horizontal sprite sheet of an NPC object (1 frame) with the
others being the same but different color palettes next to the original one. In my custom NPC Lua file, I can't seem to have it randomize the starting color and rotate simultaneously.
I have tried once or twice just randomizing the color (without the rotation) in the onDraw function. I used a random value to determine where the image should source from (in terms of pixels). However, either the NPCs were stuck with one color at a time or the NPC is flashing like a Disco shell.
I did get the rotation to work (thanks to SetaYoshi's wormhole) in terms of creating a sprite. The problem is that I can't try the same with the color randomization. When I override the sprite sheet, it stretches to the object's size (32x32) instead of sourcing the sprite from a particular position. Besides that, I am unable to come up with where I should put my randomizer in (especially the sourceX part), as well as some other details between the sprite initialization and the onDraw function. Putting the parts in either places don't do anything.
Here is the NPC Lua code in question:
Code: Select all
local npcManager = require("npcManager")
local npcutils = require("npcs/npcutils")
local rng = require("rng")
-- Settings
-- Harmtypes
local iniNPC = function(v)
local config = NPC.config[v.id]
if not v.data.ini then
v.data.ini = true
v.data.sprite = Sprite.box{
x = v.x,
y = v.y,
width = 32,
height = 32,
texture = Graphics.sprites.npc[v.id].img,
rotation = 0,
align = Sprite.align.CENTRE,
frames = npcutils.getTotalFramesByFramestyle(v)
}
end
end
function myNPC.onTickEndNPC(v)
iniNPC(v)
v.data.sprite:rotate(12 * v.direction)
-- NPC behaviors
end
function myNPC.onDrawNPC(v)
if v:mem(0x12A, FIELD_WORD) <= 0 then
return end
local config = NPC.config[v.id]
local p = -45
if config.foreground then p = -15 end
v.data.sprite.x = v.x + v.width*0.5 + config.gfxoffsetx
v.data.sprite.y = v.y + v.height*0.5 + config.gfxoffsety
v.data.sprite:draw{priority = p - 0.1, sceneCoords = true, frame = v.animationFrame + 1}
npcutils.hideNPC(v)
end
return myNPC;
And the lines/parts I probably needed to add:
Code: Select all
local smallballs = Graphics.loadImageResolved("smallballs.png")
Graphics.sprites.npc[752].img = Graphics.loadImage("smallballs.png")
local state = rng.randomInt(0, 7)
sourceX = 32 * state
Many thanks to someone who can help. And I am definitely keeping this useful stuff for future uses.