-
Notifications
You must be signed in to change notification settings - Fork 1
/
touch.lua
131 lines (95 loc) · 3.27 KB
/
touch.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
----------------------------------------------------------------------------------------------------
-- Code related to touch interactions (from Codea, not yet updated for Love2D)
----------------------------------------------------------------------------------------------------
-- toggle booleans if clicked in areas where button / sliders are
function love.mousepressed(x, y, button)
-- print(x)
-- print(y)
--[[ fun times
if shotInProgress == true then
explode(x,y)
end
]] --
-- rectangle on bottom-right to detect pressing shoot button
if shotInProgress == false and (x > (WIDTH - 100) and y > HEIGHT - 100) then
playerPressedShootButton()
end
if shotInProgress == false then
clickNearShip = nearShip(x, y, currentPlayer())
if clickNearShip == 'force' then
mouseXinitial = x
draggingType = 'force'
dragging = true
love.mouse.setCursor(dragCursor)
elseif clickNearShip == 'angle' then
mouseXinitial = x
draggingType = 'angle'
dragging = true
love.mouse.setCursor(dragCursor)
end
end
end
-- returns 'force', 'angle', 'no'
function nearShip(x, y, playerN)
forceOffset = 100 * playerN.force / 5
distance = math.pow(math.pow((playerN.x - x), 2) + math.pow((playerN.y - y), 2), 0.5)
if distance < clamp(forceOffset, 10, 90) then
return 'force'
elseif distance < 100 then
return 'angle'
else
return 'no'
end
end
-- short circuit the love.update function with boolean
function love.mousereleased(x, y, button)
dragging = false
love.mouse.setCursor()
end
-- if the mouse is being dragged after clicking, update the values of force or angle
-- uses distance from initial click for smoothly
function love.update(dt)
if dragging then
mouseXcurrent = love.mouse.getX()
if mouseXinitial ~= mouseXcurrent then
diff = mouseXcurrent - mouseXinitial
if draggingType == 'angle' then
if turn == 1 then
player1.angle = getAngle(player1, diff)
elseif turn == 2 then
player2.angle = getAngle(player2, diff)
end
elseif draggingType == 'force' then
if turn == 1 then
player1.force = getForce(player1, diff)
elseif turn == 2 then
player2.force = getForce(player2, diff)
end
end
love.graphics.setCanvas(canvas)
drawUI()
love.graphics.setCanvas()
end
end
-- if shotInProgress == false then
-- if keyUp then
-- player1.force = player1.force * 1.01
-- end
-- end
end
function getForce(playerN, diff)
return clamp(playerN.force + clamp(math.pow(diff / 1000, 3), -0.02, 0.02), 0, 5)
end
function getAngle(playerN, diff)
angle = playerN.angle + clamp(math.pow(diff / 200, 3), -1, 1)
if angle > 360 then
angle = angle - 360
elseif angle < 0 then
angle = angle + 360
end
return angle
end
-- make the input value never go below min or above max
function clamp(value, min, max)
return math.max(math.min(value, max), min)
end