forked from andmatand/dial-a-pirate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
75 lines (64 loc) · 1.7 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function love.load()
back = {
image = love.graphics.newImage('assets/back.png'),
position = {x = 0, y = 0},
offset = {x = 480, y = 480},
r = 0
}
front = {
image = love.graphics.newImage('assets/front.png'),
position = {x = 0, y = 0},
offset = {x = 480, y = 480},
r = 0
}
adjust_scale()
end
function adjust_scale()
local screenWidth, screenHeight = love.graphics.getDimensions()
if screenWidth < screenHeight then
scale = screenWidth / back.image:getWidth()
else
scale = screenHeight / back.image:getHeight()
end
back.position.x = screenWidth / 2
back.position.y = screenHeight / 2
front.position.x = screenWidth / 2
front.position.y = screenHeight / 2
end
function love.resize()
adjust_scale()
end
function love.mousepressed(x, y, button)
if button == 1 then
drag = {
initialAngle = getAngle(x, y),
initialDiscRotation = back.r
}
end
end
function love.mousereleased(x, y, button)
if button == 1 then
drag = nil
end
end
function getAngle(clickX, clickY)
local x = clickX - back.position.x
local y = clickY - back.position.y
return math.atan2(y, x)
end
function love.mousemoved(x, y)
if love.mouse.isDown(1) and drag then
local currentAngle = getAngle(x, y)
back.r = drag.initialDiscRotation + currentAngle - drag.initialAngle
end
end
function love.draw()
for _, disc in pairs({back, front}) do
love.graphics.draw(
disc.image,
disc.position.x, disc.position.y,
disc.r,
scale, scale,
disc.offset.x, disc.offset.y)
end
end