@@ -26,34 +26,89 @@ function newSquare()
2626 return square
2727end
2828
29+ function sign (x )
30+ if x > 0 then
31+ return 1
32+ elseif x < 0 then
33+ return - 1
34+ else
35+ return 0
36+ end
37+ end
38+
39+ function move_something ()
40+ -- Find a rect very close to the edge
41+ -- of visible objects and move it.
42+ x = circle .x + circle .r
43+ y = circle .y + circle .r
44+
45+ for _ , sq in pairs (squares ) do
46+ local dx = sq .x + sq .width / 2 - x
47+ local dy = sq .y + sq .height / 2 - y
48+ distance = dx * dx + dy * dy
49+
50+ if 5000 < distance and distance < 8000 then
51+ local sq_dx = love .math .random (- 10 , 10 )
52+ local sq_dy = love .math .random (- 10 , 10 )
53+ sq .x , sq .y = world :move (sq , sq .x + sq_dx , sq .y + sq_dy )
54+ movesound :setPosition (sign (dx ), sign (dy ))
55+ movesound :play ()
56+ return true
57+ end
58+ end
59+
60+ return false
61+ end
62+
63+ ---- ----------------
64+ -- love callbacks --
65+ ---- ----------------
2966function love .load ()
67+ -- hide mouse cursor
68+ love .mouse .setVisible (false )
69+
70+ ambient = love .audio .newSource (" res/scary.ogg" , " stream" )
71+ ambient :setLooping (true )
72+ ambient :setVolume (0.1 )
73+ ambient :play ()
74+
75+ movesound = love .audio .newSource (" res/movement.ogg" , " static" )
76+ next_move_in = love .math .random (10 , 20 )
77+
78+ light = love .graphics .newShader (" light.glsl" )
3079 world = bump .newWorld ()
3180 squares = {}
32- circle = {x = 0 , y = 0 , dx = 0 , dy = 0 , speed = 100 , r = 10 , color = {1 , 0 , 1 , 1 }}
81+ circle = {x = 0 , y = 0 , dx = 0 , dy = 0 , speed = 100 , r = 10 , color = {1 , 1 , 1 , 1 }}
3382 world :add (circle , circle .x , circle .y , circle .r * 2 , circle .r * 2 )
3483
3584 for i = 1 , 100 do
3685 local square = newSquare ()
3786 table.insert (squares , square )
3887 world :add (square , square .x , square .y , square .width , square .height )
3988 end
89+
90+ light :send (" sourcePos" , {circle .x + circle .r , circle .y + circle .r })
4091end
4192
4293function love .draw ()
94+ love .graphics .setShader (light )
95+
4396 for _ , sq in pairs (squares ) do
44- with_color ( sq . color , love .graphics .rectangle ,
45- " fill" , sq .x , sq .y , sq .width , sq .height )
97+ love .graphics .setColor ( sq . color )
98+ love . graphics . rectangle ( " fill" , sq .x , sq .y , sq .width , sq .height )
4699 end
47100
48101 -- draw the circle to the right/bottom of x, y,
49- -- since bump uses these coordinates for collision
50- -- checks
51- with_color (circle .color , love .graphics .arc , " fill" ,
52- circle .x + circle .r , circle .y + circle .r , circle .r , 0 , math.pi * 2 )
102+ -- since bump uses these coordinates for collision checks
103+ love .graphics .setColor (circle .color )
104+ local x , y = circle .x + circle .r , circle .y + circle .r
105+ love .graphics .arc (" fill" , x , y , circle .r , 0 , math.pi * 2 )
106+
107+ love .graphics .setShader ()
53108end
54109
55110function love .update (dt )
56- -- handle keyboard input
111+ -- handle circle movement
57112 if love .keyboard .isDown (" a" , " left" ) then
58113 circle .dx = - circle .speed
59114 elseif love .keyboard .isDown (" d" , " right" ) then
@@ -72,6 +127,27 @@ function love.update(dt)
72127
73128 local x = circle .x + circle .dx * dt
74129 local y = circle .y + circle .dy * dt
75- -- circle.x, circle.y = world:move(circle, x - circle.r, y - circle.r)
76- circle .x , circle .y = world :move (circle , x , y )
130+
131+ if x ~= circle .x or y ~= circle .y then
132+ circle .x , circle .y = world :move (circle , x , y )
133+ -- update the light shader source position
134+ light :send (" sourcePos" , {circle .x + circle .r , circle .y + circle .r })
135+ end
136+
137+ -- handle randomly moving squares
138+ next_move_in = next_move_in - dt
139+ if next_move_in <= 0 then
140+ if move_something () then
141+ next_move_in = love .math .random (10 , 20 )
142+ else
143+ next_move_in = 0
144+ end
145+ end
146+ end
147+
148+ function love .keypressed (key , scancode , isrepeat )
149+ -- exit on Escape
150+ if key == " escape" then
151+ love .event .quit ()
152+ end
77153end
0 commit comments