First commit, did some stuff
This commit is contained in:
commit
0729efb0e3
35
clock.lua
Normal file
35
clock.lua
Normal file
@ -0,0 +1,35 @@
|
||||
local MAX_SECONDS = 60 * 60 * 12
|
||||
|
||||
local Hand = {}
|
||||
Hand.__index = Hand
|
||||
|
||||
function Hand:new(length, color, sec_per_turn, ticks_per_turn)
|
||||
local h = {
|
||||
length = (length or 10),
|
||||
color = (color or {1,1,1}),
|
||||
sec_per_turn = (sec_per_turn or 1),
|
||||
ticks_per_turn = ticks_per_turn
|
||||
}
|
||||
return setmetatable(h, self)
|
||||
end
|
||||
|
||||
local function handTransform(sec_elapsed)
|
||||
local turns = sec_elapsed / self.sec_per_turn
|
||||
if self.ticks_per_turn then
|
||||
turns = math.floor(turns * self.ticks_per_turn) / self.ticks_per_turn
|
||||
end
|
||||
return love.math.newTransform(0, 0, 2 * math.pi * turns)
|
||||
end
|
||||
|
||||
function Hand:draw(sec_elapsed)
|
||||
local turns = sec_elapsed / self.sec_per_turn
|
||||
if self.ticks_per_turn then
|
||||
turns = math.floor(turns * self.ticks_per_turn) / self.ticks_per_turn
|
||||
end
|
||||
love.graphics.push("all")
|
||||
love.graphics.applyTransform(handTransform(sec_elapsed))
|
||||
love.graphics.setColor(unpack(self.color))
|
||||
love.graphics.rectangle("fill", -10, 10, 20, -self.length)
|
||||
love.graphics.pop()
|
||||
end
|
||||
|
77
main.lua
Normal file
77
main.lua
Normal file
@ -0,0 +1,77 @@
|
||||
local MAX_SECONDS = 60 * 60 * 12
|
||||
|
||||
local Hand = {}
|
||||
Hand.__index = Hand
|
||||
|
||||
function Hand:new(length, color, sec_per_turn, ticks_per_turn)
|
||||
local h = {
|
||||
length = (length or 10),
|
||||
color = (color or {1,1,1}),
|
||||
sec_per_turn = (sec_per_turn or 1),
|
||||
ticks_per_turn = ticks_per_turn
|
||||
}
|
||||
return setmetatable(h, self)
|
||||
end
|
||||
|
||||
function Hand:draw(sec_elapsed)
|
||||
local turns = sec_elapsed / self.sec_per_turn
|
||||
if self.ticks_per_turn then
|
||||
turns = math.floor(turns * self.ticks_per_turn) / self.ticks_per_turn
|
||||
end
|
||||
love.graphics.push("all")
|
||||
love.graphics.setColor(unpack(self.color))
|
||||
love.graphics.rotate(2 * math.pi * turns)
|
||||
love.graphics.rectangle("fill", -10, 10, 20, -self.length)
|
||||
love.graphics.pop()
|
||||
end
|
||||
|
||||
local clock = {rawseconds = 0, speedup = 120}
|
||||
|
||||
function clock:advance(dt)
|
||||
self.rawseconds = self.rawseconds + dt * self.speedup
|
||||
if self.rawseconds >= MAX_SECONDS then
|
||||
self.rawseconds = self.rawseconds - MAX_SECONDS
|
||||
end
|
||||
end
|
||||
|
||||
function clock:getHour()
|
||||
local h = math.floor(self.rawseconds / 60 / 60 % 12)
|
||||
if h == 0 then
|
||||
h = 12
|
||||
end
|
||||
return h
|
||||
end
|
||||
|
||||
function clock:getMinute()
|
||||
return math.floor(self.rawseconds / 60 % 60)
|
||||
end
|
||||
|
||||
function clock:getSecond()
|
||||
return math.floor(self.rawseconds % 60)
|
||||
end
|
||||
|
||||
function clock:fmt()
|
||||
return string.format("%2d:%02d:%02d", self:getHour(), self:getMinute(), self:getSecond())
|
||||
end
|
||||
|
||||
local secondhand = Hand:new(300, {0.5,0.5,0}, 60, 60)
|
||||
local minutehand = Hand:new(240, {1,0,0}, 60*60)
|
||||
local hourhand = Hand:new(180, {0,0.5,0.5}, 60*60*12)
|
||||
|
||||
function love.load()
|
||||
love.graphics.setBackgroundColor(0,0,0)
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
clock:advance(dt)
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
love.graphics.print(clock:fmt())
|
||||
love.graphics.print(math.floor(clock.rawseconds), 10, 50)
|
||||
|
||||
love.graphics.translate(love.graphics.getWidth()/2, love.graphics.getHeight()/2)
|
||||
secondhand:draw(clock.rawseconds)
|
||||
minutehand:draw(clock.rawseconds)
|
||||
hourhand:draw(clock.rawseconds)
|
||||
end
|
Loading…
Reference in New Issue
Block a user