Code: Select all
Planet = {
bgColour = {0,0,0},
landColour = {0,0,0},
size = 100,
landPoints = {
}
} -- the class table
function Planet:new()
self = setmetatable({}, self)
self.__index = self -- failed table lookups on the instances should fallback to the class table, to get methods
self.bgColour = {love.math.random(0,255), love.math.random(0,255), love.math.random(0,255)}
self.landColour = {love.math.random(0,255), love.math.random(0,255), love.math.random(0,255)}
self.size = 100
self.landPoints = {}
-- create the terrain
for j=1,love.math.random(1,10) do
tempPoints = {}
-- start point
table.insert(tempPoints, love.math.random(-self.size, self.size))
table.insert(tempPoints, love.math.random(-self.size, self.size))
-- each continent has a set of points for drawing
for i=1,love.math.random(5,8) do
lastPoint = {tempPoints[tableLength(tempPoints)-1], tempPoints[tableLength(tempPoints)]}
sizeCoef = 2
table.insert(tempPoints, love.math.random(lastPoint[1]-(self.size/sizeCoef), lastPoint[1]+(self.size/sizeCoef)))
table.insert(tempPoints, love.math.random(lastPoint[2]-(self.size/sizeCoef), lastPoint[2]+(self.size/sizeCoef)))
end
table.insert(self.landPoints, tempPoints)
end
return self
end
function Planet:getSize()
return self.size
end
function Planet:draw(centre)
-- Draw the planet (table centre as {x=number, y=number})
love.graphics.push()
love.graphics.translate(centre.x, centre.y)
love.graphics.setColor(self.bgColour)
love.graphics.circle("fill", 0,0, self.size)
love.graphics.setColor(self.landColour)
love.graphics.stencil(function () love.graphics.circle("fill", 0,0, self.size) end , "replace", 1, false)
love.graphics.setStencilTest("greater", 0)
for ind, points in pairs(self.landPoints) do
love.graphics.polygon("fill", points)
end
love.graphics.setStencilTest()
love.graphics.pop()
end
Code: Select all
planet = Planet:new()
Code: Select all
print(planet:getSize())
Code: Select all
Attempt to call method getSize (a nil value)