-
Notifications
You must be signed in to change notification settings - Fork 0
/
nspire-dots.lua
221 lines (187 loc) · 4.8 KB
/
nspire-dots.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
--NspireDots by RandomByte(apps.randombyteqgmail.com; https://github.com/randombyte-developer)
local debug = true
local width, height = 0, 0
local xCenter, yCenter = 0, 0
local window = platform.window
local colors = {
black = {0, 0, 0}
}
local boom = {
startPosX = 60,
fired = false,
x = 0,
y = 0,
d = 30,
r = 15,
speed = 15,
activeSkin = 0,
skins = {
[0] = {
color = {0, 153, 0},
nc = {255, 51, 204}
}
},
hitAngle = -1
}
local dot = {
x = 0,
y = 0,
d = 30,
r = 15,
speed = 0,
speedMin = 2,
speedMax = 4.2,
direction = 0, -- -1 left +1 right
hitAnimate = -1,
spawnBox = {
left = 40,
right = 160,
top = 250,
bottom = 125
},
activeSkin = 0,
skins = {
[0] = {
color = {0, 51, 204}
}
}
}
function boom.reset()
boom.x = boom.startPosX
boom.y = yCenter
boom.fired = false
end
function boom.fire()
boom.fired = true
end
function boom.tick()
if (boom.fired == true) then
boom.x = boom.x + boom.speed
--border
if (boom.x > width) then
boom.reset()
dot.spawn()
c = false
end
--collision check(rect)
if (boom.x + boom.r > dot.x and dot.y - dot.r <= boom.y + boom.r and dot.y + dot.r >= boom.y - boom.r) then
--collision check(circle, Pythagorean theorem)
if (math.sqrt(
((boom.x - dot.x) * (boom.x - dot.x)) +
((boom.y - dot.y) * (boom.y - dot.y))
) <= boom.r + dot.r) then
boom.hitAngle = math.atan2(dot.x - boom.x, dot.y - boom.y)
boom.reset()
dot.spawn()
c = true
end
end
end
end
function boom.draw(gc)
boom.skins[boom.activeSkin].draw(gc)
end
boom.skins[0].draw = function(gc)
gc:clp(c and boom.skins[0].color or boom.skins[0].nc)
gc:fillArc(boom.x-boom.r, boom.y-boom.r, boom.d, boom.d, 0, 360)
end
function dot.spawn()
dot.x = math.random(dot.spawnBox.bottom, dot.spawnBox.top)
dot.y = math.random(dot.spawnBox.left, dot.spawnBox.right)
dot.direction = math.random(0, 1) == 0 and -1 or 1
dot.speed = math.random(dot.speedMin*10, dot.speedMax*10)/10 --to get float values
hitAnimate = -1
end
dot.skins[0].draw = function(gc)
gc:clp(dot.skins[0].color)
gc:fillArc(dot.x-dot.r, dot.y-dot.r, dot.d, dot.d, 0, 360)
end
function dot.tick()
if (dot.direction == -1) then
if (dot.y > dot.spawnBox.left) then
dot.y = dot.y - dot.speed
else
dot.direction = 1
dot.y = dot.y + dot.speed
end
elseif (dot.direction == 1) then
if (dot.y < dot.spawnBox.right) then
dot.y = dot.y + dot.speed
else
dot.direction = -1
dot.y = dot.y + dot.speed
end
end
end
function dot.draw(gc)
dot.skins[dot.activeSkin].draw(gc)
end
function on.construction()
intoGC("cl", function(gc, r, g, b) gc:setColorRGB(r, g, b) end) --color
intoGC("clp", function(gc, color) gc:setColorRGB(unpack(color)) end) --color packed
timer.start(0.01)
end
function on.resize(w, h)
width = w
height = h
xCenter = width/2
yCenter = height/2
gameStart() --restart whole game to ensure that the positions are correct
end
function on.timer()
gameTick()
window:invalidate()
end
function gameStart()
boom.reset()
end
function on.enterKey()
boom.fire()
end
function on.charIn(input)
if (input == "1") then
dot.spawn()
boom.reset()
elseif(input == "d") then
debug = not debug
end
end
function gameTick()
dot.tick()
boom.tick()
end
function on.paint(gc)
if (debug) then
drawDebug(gc)
end
boom.draw(gc)
dot.draw(gc)
end
function drawDebug(gc)
for i = 0, width, 25 do
if (i%50 == 0) then
gc:cl(255, 0, 0)
else
gc:cl(0, 0, 100)
end
gc:drawLine(i, 0, i, height)
end
for i = 0, height, 25 do
if (i%50 == 0) then
gc:cl(255, 0, 0)
else
gc:cl(0, 0, 100)
end
gc:drawLine(0, i, width, i)
end
gc:clp(colors.black)
gc:drawString("speed=".. dot.speed ..";c=".. (c and "true" or "false") ..";hitAngle=".. boom.hitAngle, 10, 5)
gc:cl(255, 0, 255)
gc:drawLine(boom.x, boom.y, dot.x, dot.y)
gc:cl(0, 255, 0)
gc:drawLine(boom.x, boom.y, dot.x, boom.y)
gc:drawLine(dot.x, boom.y, dot.x, dot.y)
end
function intoGC(k, v)
platform.withGC(getmetatable)[k] = v
end