I have a problem letting go of camelCase and underscores. (And never capitalize the first letter of a variable or function) I keep mixing both of them with lowercase for different variables and if it weren't for Sublime Text telling me what I've already typed I'd probably have forced myself into one or the other by now. I'd want to do all lowercase just because that's how Löve's callbacks are named. (keypressed) But then Löve's functions are camelCase. (setBackgroundColor) So I don't know what to use. I should probably standardize before I get too far.
I also like to use tabs or spaces to make some lines of code match up for easy reading if the subsequent lines are similar. Like this behavior callback I created for my Spark enemy to move around a wall:
Code: Select all
-- Causes entity to move along the edge of a wall using the "gridTileSolid" table to determine which tiles are solid.
-- "moving" determines direction. 1 = Clockwise, -1 = Counter-clockwise
function behavior.followWall(self, dt, moving)
moving = moving or 1
if self.isDead or self.dying then return end
local dir = self.movingDirection
if dir == 0 then dir, self.movingDirection = 1, 1 end
if dir == 1 then self.vx, self.vy = -self.speed, 0 -- LEFT
elseif dir == 2 then self.vx, self.vy = 0, -self.speed -- UP
elseif dir == 3 then self.vx, self.vy = self.speed, 0 -- RIGHT
elseif dir == 4 then self.vx, self.vy = 0, self.speed -- DOWN
end
self.distanceMoved = self.distanceMoved + self.speed * dt
local future_x = self.x + self.vx * dt
local future_y = self.y + self.vy * dt
if self.distanceMoved > TILESIZE then
self.distanceMoved = 0
if dir == 1 then future_x = math.floor(self.x / TILESIZE) * TILESIZE
elseif dir == 3 then future_x = math.ceil(self.x / TILESIZE) * TILESIZE
elseif dir == 2 then future_y = math.floor(self.y / TILESIZE) * TILESIZE
elseif dir == 4 then future_y = math.ceil(self.y / TILESIZE) * TILESIZE
end
local b = behavior.returnBlockArea(future_x / TILESIZE, future_y / TILESIZE)
local tl, tc, tr = b[1][1], b[2][1], b[3][1]
local cl, cc, cr = b[1][2], b[2][2], b[3][2]
local bl, bc, br = b[1][3], b[2][3], b[3][3]
if self.moving == 1 then -- Clockwise
if dir == 1 then if not tc then self.movingDirection = 2
elseif tc and cl and not bc then self.movingDirection = 4
elseif tc and cl and bc then self.movingDirection = 3
end
elseif dir == 2 then if not cr then self.movingDirection = 3
elseif cr and tc and not cl then self.movingDirection = 1
elseif cl and tc and cr then self.movingDirection = 4
end
elseif dir == 3 then if not bc then self.movingDirection = 4
elseif bc and cr and not tc then self.movingDirection = 2
elseif tc and cr and bc then self.movingDirection = 1
end
elseif dir == 4 then if not cl then self.movingDirection = 1
elseif cl and bc and not cr then self.movingDirection = 3
elseif cl and bc and cr then self.movingDirection = 2
end
end
else -- Counter-Clockwise
if dir == 1 then if not bc then self.movingDirection = 4
elseif bc and cl and not tc then self.movingDirection = 2
elseif bc and cl and tc then self.movingDirection = 3
end
elseif dir == 2 then if not cl then self.movingDirection = 1
elseif cl and tc and not cr then self.movingDirection = 3
elseif cl and tc and cr then self.movingDirection = 4
end
elseif dir == 3 then if not tc then self.movingDirection = 2
elseif tc and cr and not bc then self.movingDirection = 4
elseif tc and cr and bc then self.movingDirection = 1
end
elseif dir == 4 then if not cr then self.movingDirection = 3
elseif cr and bc and not cl then self.movingDirection = 1
elseif cl and bc and cr then self.movingDirection = 2
end
end
end
end
self.x, self.y = future_x, future_y
gameLevel.collisionMap:remove(self)
gameLevel.collisionMap:add(self, self.x + 2, self.y + 2, self.w, self.h)
end
(For some reason a single line above does not line up in the forum code tags. But it looks right in Sublime.)
I also place underscores before variables that I will need to name properly but only use as a throwaway. Not sure why. (Nothing to do with using just an underscore for returned variables you're not going to use. I also do that. Very smart.)