Lionblaster /src/com/graphics.lua

Language Lua Lines 121
MD5 Hash d28860a33f94a01d6bd7e384a9f400e5
Repository https://github.com/zorggn/Lionblaster.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
-- the entity's graphics data
-- could probably tear this apart more, but for now, this will suffice
-- and yes, this can only draw images (and image regions) for now, no meshes.

local static = {}

-- creates initial hierarchy structure for entity
static.init = function(self)
	-- this always exists as a fallback, and is always overwritten when a set happens, and the tables differ.
	current = {
		-- dimensions
		w = 1,
		h = 1,
		-- offsets (translation)
		ox = 0,
		oy = 0,
		-- orientation (rotation) and centre offsets
		phi = 0,
		cx = 0,
		cy = 0,
		-- scale
		sx = 1.0,
		sy = 1.0,
		-- flip
		fv = false,
		fh = false,
		fd = false,
		-- image or atlas/tilemap, with an optional quad
		--image = nil
		-- quad = nil
		-- 3D rotation, not implemented
		--[[
			r = {
				-- horiz., vert., diag. flip; z-axis rotation; x, y shearing; x, y mindpoint scale multiplier
				hf,vf,df,phi,kx,ky,kw,kh = false,false,false,0.0,0.0,0.0,1.0,1.0
			}
		--]]
	}

	-- these are the separate graphical states for an entity
	state = {}

	-- string:number pairs for easier handling
	stateEnums = {['default'] = 1}

	-- which graphics state we are in
	currentState = 1

	state[1] = {

		-- graphics data - state fallback
		graphics = current, -- if we edit a state's graphics, we want the fallback to be the last setting, so if it isn't do graphics.current = g.

		-- animation data
		animation = {
			frameCount = 1, -- how many frames are in this set; 1 makes it static, so framedelay, and loop properities are moot in that case.
			currentFrame = 1, -- which frame are we on
			frameDelay = 0.0, -- how long, in seconds, should a frame stay in place (granularity up to the defined frameRate, not the tickRate)
			biDirectional = false, -- if true, do the animation in reverse as well.
			isLooping = true, -- if false, it one-shots through the frames
			loopStart = 1,
			loopEnd = 1,
			resetOnSwitch = true, -- if true, it will reset currentFrame to 1, else it will be left alone.
		},

		-- the frames of a state
		frame = {},

		frame[1] = {
			graphics = current,
			frameDelay = 0.0,
		}
	}
end

-- switch the graphics state
static.switchState = function(self,state,frame)
	local state = state
	if type(state) ~= 'number' then
		state = self.stateEnums[state]
		assert(state,"Error: no graphics state found with that name.")
	end
	assert(self.state[state].animation.frameCount >= frame,"Error: specified frame doesn't exist in the target animation")
	self.currentState = state
	self.state[state].animation.currentFrame = frame
end

-- toString method
static.toString = function(self)
	local t = {}
	table.insert(t,'hierarchy:\n')

	table.insert(t,'parent: ')
	table.insert(t,self.parent or 'nil')
	table.insert(t,'\n')
	table.insert(t,'children:')
	if #self.children then
		table.insert(t,' nil')
	else
		table.insert(t,'\n')
		for i,v in ipairs(self.children) do
			table.insert(t,'  ')
			table.insert(t,i)
			table.insert(t,'. ')
			table.insert(t,v)
		end
	end
	return table.concat(t,'')
end

-- factory
local graphics = function()
	return setmetatable(
		{},
		{
			__index = static,
		}
	)
end

return graphics
Back to Top