Requires love 11.5.
Put files in a directory then run love path/to/directory
dir
main.lua
classic.lua
-- | |
-- classic | |
-- | |
-- Copyright (c) 2014, rxi | |
-- | |
-- This module is free software; you can redistribute it and/or modify it under | |
-- the terms of the MIT license. See LICENSE for details. | |
-- | |
local Object = {} | |
Object.__index = Object | |
function Object:new() | |
end | |
function Object:extend() | |
local cls = {} | |
for k, v in pairs(self) do | |
if k:find("__") == 1 then | |
cls[k] = v | |
end | |
end | |
cls.__index = cls | |
cls.super = self | |
setmetatable(cls, self) | |
return cls | |
end | |
function Object:implement(...) | |
for _, cls in pairs({...}) do | |
for k, v in pairs(cls) do | |
if self[k] == nil and type(v) == "function" then | |
self[k] = v | |
end | |
end | |
end | |
end | |
function Object:is(T) | |
local mt = getmetatable(self) | |
while mt do | |
if mt == T then | |
return true | |
end | |
mt = getmetatable(mt) | |
end | |
return false | |
end | |
function Object:__tostring() | |
return "Object" | |
end | |
function Object:__call(...) | |
local obj = setmetatable({}, self) | |
obj:new(...) | |
return obj | |
end | |
return Object |
Object = require 'classic' | |
function love.load() | |
circle1 = circle(400,300,50) | |
end | |
function love.draw() | |
Circle2:drawShaking(0.2) | |
end | |
function love.update() | |
end | |
function love.mousepressed(x,y,btn,istouch) | |
Circle2:mousePressed(x,y,btn,istouch, function() | |
print('Circle2 was pressed') | |
end) | |
end | |
local circle = Object:extend() | |
--Creates a circle with position x,y and radius | |
-- | |
-- | |
---@param x number | |
---@param y number | |
---@param radius number | |
---@param r number | |
---@param g number | |
---@param b number | |
--**creation_time** Will have a creation_time | |
function circle:new(x,y,radius, r,g,b) | |
self.x = x or 0 | |
self.y = y or 0 | |
self.radius = radius or 1 | |
self.r = r or 1 | |
self.g = r or 1 | |
self.b = r or 1 | |
self.creation_time = love.timer.getTime() | |
end | |
function circle:getCreationTime() | |
return self.creation_time | |
end | |
function circle:draw() | |
love.graphics.circle("fill", self.x, self.y, self.radius) | |
-- love.graphics.reset() | |
end | |
-- see: https://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly | |
-- how to create a shaking circle | |
-- | |
-- r = R * sqrt(random()) | |
-- | |
-- theta = random() *2 * pi | |
-- | |
-- x = centerX + r * cos(theta) | |
-- | |
-- y = centerY + r * sin(theta) | |
---@param _shake_radius number magnitude of shaking [0..1] / default 10% of circle radius | |
---@return nil | |
function circle:drawShaking(_shake_radius) | |
local shake_radius = (_shake_radius*self.radius) or (.1*self.radius) | |
local pi = 3.14159 | |
local r = shake_radius * math.sqrt(love.math.random()) | |
local theta = love.math.random() *2*pi | |
local new_x = self.x + r * math.cos(theta) | |
local new_y = self.y + r * math.sin(theta) | |
love.graphics.circle("fill", new_x, new_y, self.radius) | |
end | |
function circle:update(dt) | |
end | |
-- Put this function inside love.mousepressed(x,y,btn,istouch) | |
function circle:mousePressed(x,y, btn, istouch, callback) | |
if btn == 1 then | |
-- For a circle button at 20, 50 coordinates with 40 radius: | |
if math.sqrt((self.x-x)^2+(self.y-y)^2) <= self.radius then | |
--do your stuff because the button was pressed | |
callback() | |
end | |
end | |
end |