Commits

Carl Ådahl committed 7c4ccf7

Property system in Actor, made drawPriority and updatePriority properties.
Simplified implementation of attach() in Actor.
Some minor refactoring and cleanup.

Comments (0)

Files changed (8)

 function Actor.new(t, x, y)
 	local self = {}
 
+	local properties = 
+	{ 
+		drawPriority = { value = 0, onSet = world.setDrawListDirty },
+		updatePriority = { value = 0, onSet = world.setUpdateListDirty }
+	}
+
+	setmetatable(self, 
+	{
+		__newindex = function(tbl, key, val)
+			local property = properties[key]
+			if property then
+				local oldValue = property.value
+				property.value = val
+				property.onSet(oldValue, val)
+			else
+				rawset(tbl, key, val)
+			end
+		end,
+	
+		__index = function(tbl, key)
+			local property = properties[key]
+			if property then
+				return property.value
+			else
+				return rawget(tbl, key)
+			end
+		end
+	})
+
 	-- Initialize the actor.
 	function self.init()
+
 		self.uid = nextUid()
 		self.type = t 
 		self.dt = 0
+		self.states = {}
 		self.drawPriority = 0
-		self.updatePriority = 0
-		self.states = {}
-		self.setDrawPriority(0)
 		self.sfx = {}
+
 	end
 
 	-- Attach another actor to this one, making it stay positioned the same, relatively.
 	-- Optionally supply an x,y offset that will be kept.
 	function self.attach(other, offsetx, offsety)
-		if offsetx and offsety then
-			other.getX = function() return self.getX() + offsetx end
-			other.getY = function() return self.getY() + offsety end
-		else
-			other.getX = self.getX
-			other.getY = self.getY
-		end
+		other.getX = function() return self.getX() + (offsetx or 0) end
+		other.getY = function() return self.getY() + (offsety or 0) end
 		return other
 	end
 
-	function self.setDrawPriority(dp)
-		self.drawPriority = dp
-		world.setDrawListDirty()
-	end
-
-	function self.getDrawPriority()
-		return self.drawPriority
-	end
-
-	function self.setUpdatePriority(up)
-		self.updatePriority = up
-		world.setUpdateListDirty()
-	end
-
 	-- doFor(time, func, ...)	
 	--	Repeat the specified function every frame for a certain amount of time.	
 	function self.doFor(time, func, ...)

src/background.lua

 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ]]
+local lg = love.graphics
+
 Background = {}
 
-local img = love.graphics.newImage("img/background.png")
+local img = lg.newImage("img/background.png")
 
 function Background.new(x,y)
 
 	local s = Actor.new("background")
 	local scrol = 0
-	local logo = love.graphics.newImage("img/logo.png")
-	local badges = love.graphics.newImage("img/badges.png")
-	local lillfonten2 = love.graphics.newFont("fonts/heavywei.ttf", 20*1.3333)
-	local fonten = love.graphics.newFont("fonts/heavywei.ttf", 52)
-	local storfonten = love.graphics.newFont("fonts/heavywei.ttf", 105*1.3333)
-	local jattefonten = love.graphics.newFont("fonts/heavywei.ttf", 200*1.3333)
-	local ball = love.graphics.newImage("img/threeballs.png")
+	local logo = lg.newImage("img/logo.png")
+	local badges = lg.newImage("img/badges.png")
+	local lillfonten2 = lg.newFont("fonts/heavywei.ttf", 20*1.3333)
+	local fonten = lg.newFont("fonts/heavywei.ttf", 52)
+	local storfonten = lg.newFont("fonts/heavywei.ttf", 105*1.3333)
+	local jattefonten = lg.newFont("fonts/heavywei.ttf", 200*1.3333)
+	local ball = lg.newImage("img/threeballs.png")
 
 	img:setWrap("repeat", "repeat")
 
 	end
 
 	local function drawSurface()
-		local lg = love.graphics
 		lg.pushMaterial()
 		lg.setBlendMode("alpha")
 		lg.setColor(255,255,255,255)
-		lg.drawq(img, love.graphics.newQuad(0, -scrol, cfg.width, cfg.height, img:getHeight(), img:getHeight()), x, y)
+		lg.drawq(img, lg.newQuad(0, -scrol, cfg.width, cfg.height, img:getHeight(), img:getHeight()), x, y)
 		lg.popMaterial()
 	end
 
 		screen.y = 0 
 		screen.height = cfg.height
 		screen.draw = function()
-			local lg = love.graphics
 			lg.pushMaterial()
 			lg.setColor(230,250,230,80)
 			lg.setFont(fonten)
 		rollin(screen)
 
 		s.waitFor(30)
---		s.waitKey("up")
 		
 		s.enterState("loadingMap")
 	end
 		screen.y = 0
 		screen.height = cfg.height
 		screen.draw = function()
-			local lg = love.graphics
 			lg.pushMaterial()
 			lg.setColor(230,250,230,80)
 			lg.setFont(fonten)
 		screen.y = 0
 		screen.height = cfg.height
 		screen.draw = function()
-			local lg = love.graphics
 			lg.pushMaterial()
 			lg.draw(ball, cfg.width/2 - ball:getWidth()/2, cfg.height/2 - ball:getHeight()/2+screen.y-90, 0, 1, 1, 0, 0)
 			lg.setColor(250,250,240,80)
 		screen.y = 0 
 		screen.height = cfg.height
 		screen.draw = function()
-			local lg = love.graphics
 			lg.pushMaterial()
 			lg.setColor(230,250,230,80)
 			lg.setFont(fonten)
 		s.wait()
 	end
 
-	s.setDrawPriority(-100)
-	s.setUpdatePriority(10)
+	s.drawPriority = -100
+	s.updatePriority = 10
 
 	return s
 end
 	-- Exit: REMOVE
 	-- Desc: Will remove this ball from the map, send it flying+shrinking and then die.
     function s.states.dying(delay)
-		s.setDrawPriority(-2)
+		s.drawPriority = -2
 		blendMode = "alpha"
 
 --		world.spawn(FlareEffect.new(x, y, s))

src/ballshadow.lua

 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ]]
+local LG, LI = love.graphics, love.image
+
 BallShadow = 
 { 	
 }
 
 local function generateShadow()
-	local shadowData = love.image.newImageData(96,96)
+	local shadowData = LI.newImageData(96,96)
 	shadowData:mapPixel(function(x,y,r,g,b,a)
 		x = x - 47.5
 		y = y - 47.5
 		return 255,255,255,math.max(0,math.min(255,255*c))
 	end)
 
-	return love.graphics.newImage(shadowData)
+	return LG.newImage(shadowData)
 end
 
 -- Generate shadow texture.
 
 	-- Initialize the actor.
 	function s.init()
-		s.setDrawPriority(-50)
+		s.drawPriority = -50
 		s.scale = 1
 		-- No state needed
 	end
 	
 	function s.draw()
-		local lg = love.graphics
-		lg.pushMaterial()
-		lg.setBlendMode("alpha")
-		lg.setColorMode("modulate")
+		LG.pushMaterial()
+		LG.setBlendMode("alpha")
+		LG.setColorMode("modulate")
 		local c = 0
 		local a = 160
-		lg.setColor(c,c,c,a)
-		lg.draw(shadow, 
+		LG.setColor(c,c,c,a)
+		LG.draw(shadow, 
 				s.getX(),
 				s.getY(),
 				0,
 				s.scale,
 				shadow:getWidth()/2,
 				shadow:getHeight()/2 )		
-		lg.popMaterial()
+		LG.popMaterial()
 	end
 	
 	s.init()
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ]]
+local LG, LK, LJ = love.graphics, love.keyboard, love.joystick
+
 Cannon = 
 {
 }
 
 local angularSpeed = 0.7
 local maxAngle = 80
-local image = love.graphics.newImage("img/cannon.png")
+local image = LG.newImage("img/cannon.png")
 
 function Cannon.new(x,y)
 	local s = Actor.new("cannon")
 
 	function s.init()
 		s.enterState("waiting")
-		s.setDrawPriority(-1)
+		s.drawPriority = -1
 	end
 	
 	function s.getX()
 	end
 
 	function s.draw()
-		love.graphics.draw(image, x, y, math.rad(angle), 1, 1, image:getWidth()/2, image:getHeight()/2)
+		LG.draw(image, x, y, math.rad(angle), 1, 1, image:getWidth()/2, image:getHeight()/2)
 	end
 	
 	function s.states.loading()
 		s.wait()
 	end
 
+	
+
 	function s.states.aiming(ball)
 
 		-- Ensure the cannon is positioned right, now that we're in gameplay.
 		y = love.graphics.getHeight()
 
+		local function checkShoot()
+
+			-- If player pressed UP, and we're ready, fire the ball.
+			local isUp = LK.isDown("up") or LJ.isDown(0,0)
+		    if isUp and not wasUp then
+		    
+		    	-- Play sound and spawn a flare.
+		    	sfx.shoot()
+				world.spawn(FlareEffect.new(x, y, s))
+
+				-- Send the ball moving, and enter the waiting state.
+		    	ball.enterState("moving", angle)
+		    	s.enterState("waiting", ball)
+		    end
+		    
+			-- Track last key state (annoying)
+		    wasUp = isUp		    
+		end
+
+		-- Check for user input affecting turn angle.
 		local function checkTurn()
-			local stickAxis = love.joystick.getAxis(0,0)
+
+			-- Joystick
+			--
+			local stickAxis = LJ.getAxis(0,0)
 			local deadZone = 0.2
 			local stickValid = math.abs(stickAxis) >= deadZone
 
 
 			if stickValid then
 				if stickAxis < 0 then
-		    	angle = math.max( -maxAngle, angle + stickAxis * angularSpeed * s.dt )
+		    		angle = math.max( -maxAngle, angle + stickAxis * angularSpeed * s.dt )
 				else
-	    		angle = math.min( maxAngle, angle + stickAxis * angularSpeed * s.dt )
-				end					
+	    			angle = math.min( maxAngle, angle + stickAxis * angularSpeed * s.dt )
+				end		
 			end
 
+			-- Keyboard
+			--
 		    if love.keyboard.isDown("left") then
 		    	angle = math.max( -maxAngle, angle - angularSpeed * s.dt )
 		    end
 
 		-- Update loop
 		while s.update() do
-			-- If player pressed UP, and we're ready, fire the ball.
-			local isUp = love.keyboard.isDown("up") or love.joystick.isDown(0,0)
-		    if isUp and not wasUp then
-		    	sfx.shoot()
-				world.spawn(FlareEffect.new(x, y, s))
-		    	ball.enterState("moving", angle)
-		    	s.enterState("waiting", ball)
-		    end
 
-			-- Track last key state (annoying)
-		    wasUp = isUp
+			checkShoot()
+
 
 			checkTurn()
 		end
 		s.enterState("loading", true)
 	end
 
-	s.setUpdatePriority(-2)
+	s.updatePriority = -2
 
     -- Load the map itself.
 	s.lines = love.filesystem.lines("levels.txt")

src/snd/cannon_shot.mp3

Binary file modified.

src/stickeffect.lua

 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ]]
+local LG = love.graphics
+
 StickEffect = {}
 
 local image = love.graphics.newImage("img/stickeffect.png")
 	local alpha = 255
 	
 	function s.init()
-		s.setDrawPriority(-12)
+		s.drawPriority = -12
 		s.enterState("fade")
 	end
 	
 	end
 
 	function s.draw()
-		local lg = love.graphics
 		local scale = math.smoothstep(255,0, alpha)*1.2
-		lg.pushMaterial()
-		lg.setBlendMode("alpha")
-		lg.setColor(255,255,255,alpha)
-		lg.draw(image, x, y, 0, scale, scale, image:getWidth()/2, image:getHeight()/2)	
-		lg.popMaterial()
+		LG.pushMaterial()
+		LG.setBlendMode("alpha")
+		LG.setColor(255,255,255,alpha)
+		LG.draw(image, x, y, 0, scale, scale, image:getWidth()/2, image:getHeight()/2)	
+		LG.popMaterial()
 	end
 
 	function s.states.fade()