Just realized my last example didnt work at all, so I went ahead and actually fired up love and made this example for you. Left-click anywhere on the screen to add a fish.
Code: Select all
-- Fish ---------------------------------
-----------------------------------------
Fish = Fish or {} -- Declare fish class
Fish_mt = {} -- Declare a metatable to handle it
Fish_mt.__index = Fish -- Tie our metatable to our class
-- Constructor for a specifically colored fish
function Fish.new(fill_mode, xf, yf, R2, r, g, b)
-- Create a metatable for our object
local metatable = {
fill_mode = fill_mode,
xf = xf,
yf = yf,
R2 = R2,
r = r,
g = g,
b = b
}
return setmetatable(metatable, Fish_mt)
end
-- Constructor for a random colored fish
function Fish.newRandom(fill_mode, xf, yf, R2)
-- Assign random RGB values and then
-- call specific color constructor
local r = math.random(0, 255)
local g = math.random(0, 255)
local b = math.random(0, 255)
local a = math.random(0, 255)
return Fish.new(fill_mode, xf, yf, R2, r, g, b)
end
-- Draws the fish when called in draw()
function Fish:draw()
local fill_mode = self.fill_mode
local xf = self.xf
local yf = self.yf
local R2 = self.R2
local fish_debug = self.debug
local r = self.r
local g = self.g
local b = self.b
-- Store our current color so we don't lose it.
local color = love.graphics.getColor()
-- Paint with the colors defined by our object
love.graphics.setColor(r, g, b)
-- Paint the fish.
love.graphics.circle(fill_mode, xf, yf, R2);
love.graphics.circle(fill_mode, xf - 20, yf - 10, R2 / 2);
love.graphics.line(xf - R2, yf + R2 / 2, xf - 2, yf + 2);
love.graphics.triangle(fill_mode, xf - 5, yf - R2 + 2, xf + 10, yf - R2 + 2, xf + R2, yf - R2 - 22);
love.graphics.triangle(fill_mode, xf + R2, yf, xf + R2 + 40, yf, xf + R2 + 50, yf + R2 + 12);
love.graphics.triangle(fill_mode, xf + R2, yf, xf + R2 + 40, yf, xf + R2 + 50, yf - R2 - 12);
-- Return our colors back to what they were before
love.graphics.setColor(color)
if lovedebug == true then
love.graphics.draw("Fish Model 1\nSam\nX = " .. xf .. " Y = " .. yf, xf - R2, yf + R2 + 18);
end
end
-- LÖVE ---------------------------------
-----------------------------------------
function load()
-- Declare a table to hold our new fish collection.
ABunchOfFish = {}
-- Create a new fish to start with in the middle, and
-- then add it to our fish table.
local fish1 = Fish.newRandom(love.draw_fill, 400, 300, math.random(20,100))
table.insert(ABunchOfFish, fish1)
end
-- Callback for mouseclick events
function mousepressed(x, y, button)
-- See if the user clicked the lrft
-- mouse button.
if button == love.mouse_left then
-- If they did, create a new fish with a random
-- size at the mouse-click location and add it
-- to our collection to be displayed in the draw()
-- callback
local fish = Fish.newRandom(love.draw_fill, x, y, math.random(20,100))
table.insert(ABunchOfFish, fish)
end
end
function draw()
-- Loop through our table of fish and
-- draw all of them.
for i, v in ipairs(ABunchOfFish) do
v:draw()
end
end