PageRenderTime 38ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 16ms app.codeStats 0ms

/src/com/graphics.lua

https://github.com/zorggn/Lionblaster
Lua | 121 lines | 83 code | 11 blank | 27 comment | 1 complexity | d28860a33f94a01d6bd7e384a9f400e5 MD5 | raw file
  1-- the entity's graphics data
  2-- could probably tear this apart more, but for now, this will suffice
  3-- and yes, this can only draw images (and image regions) for now, no meshes.
  4
  5local static = {}
  6
  7-- creates initial hierarchy structure for entity
  8static.init = function(self)
  9	-- this always exists as a fallback, and is always overwritten when a set happens, and the tables differ.
 10	current = {
 11		-- dimensions
 12		w = 1,
 13		h = 1,
 14		-- offsets (translation)
 15		ox = 0,
 16		oy = 0,
 17		-- orientation (rotation) and centre offsets
 18		phi = 0,
 19		cx = 0,
 20		cy = 0,
 21		-- scale
 22		sx = 1.0,
 23		sy = 1.0,
 24		-- flip
 25		fv = false,
 26		fh = false,
 27		fd = false,
 28		-- image or atlas/tilemap, with an optional quad
 29		--image = nil
 30		-- quad = nil
 31		-- 3D rotation, not implemented
 32		--[[
 33			r = {
 34				-- horiz., vert., diag. flip; z-axis rotation; x, y shearing; x, y mindpoint scale multiplier
 35				hf,vf,df,phi,kx,ky,kw,kh = false,false,false,0.0,0.0,0.0,1.0,1.0
 36			}
 37		--]]
 38	}
 39
 40	-- these are the separate graphical states for an entity
 41	state = {}
 42
 43	-- string:number pairs for easier handling
 44	stateEnums = {['default'] = 1}
 45
 46	-- which graphics state we are in
 47	currentState = 1
 48
 49	state[1] = {
 50
 51		-- graphics data - state fallback
 52		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.
 53
 54		-- animation data
 55		animation = {
 56			frameCount = 1, -- how many frames are in this set; 1 makes it static, so framedelay, and loop properities are moot in that case.
 57			currentFrame = 1, -- which frame are we on
 58			frameDelay = 0.0, -- how long, in seconds, should a frame stay in place (granularity up to the defined frameRate, not the tickRate)
 59			biDirectional = false, -- if true, do the animation in reverse as well.
 60			isLooping = true, -- if false, it one-shots through the frames
 61			loopStart = 1,
 62			loopEnd = 1,
 63			resetOnSwitch = true, -- if true, it will reset currentFrame to 1, else it will be left alone.
 64		},
 65
 66		-- the frames of a state
 67		frame = {},
 68
 69		frame[1] = {
 70			graphics = current,
 71			frameDelay = 0.0,
 72		}
 73	}
 74end
 75
 76-- switch the graphics state
 77static.switchState = function(self,state,frame)
 78	local state = state
 79	if type(state) ~= 'number' then
 80		state = self.stateEnums[state]
 81		assert(state,"Error: no graphics state found with that name.")
 82	end
 83	assert(self.state[state].animation.frameCount >= frame,"Error: specified frame doesn't exist in the target animation")
 84	self.currentState = state
 85	self.state[state].animation.currentFrame = frame
 86end
 87
 88-- toString method
 89static.toString = function(self)
 90	local t = {}
 91	table.insert(t,'hierarchy:\n')
 92
 93	table.insert(t,'parent: ')
 94	table.insert(t,self.parent or 'nil')
 95	table.insert(t,'\n')
 96	table.insert(t,'children:')
 97	if #self.children then
 98		table.insert(t,' nil')
 99	else
100		table.insert(t,'\n')
101		for i,v in ipairs(self.children) do
102			table.insert(t,'  ')
103			table.insert(t,i)
104			table.insert(t,'. ')
105			table.insert(t,v)
106		end
107	end
108	return table.concat(t,'')
109end
110
111-- factory
112local graphics = function()
113	return setmetatable(
114		{},
115		{
116			__index = static,
117		}
118	)
119end
120
121return graphics