-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.lua
More file actions
131 lines (108 loc) · 3.76 KB
/
main.lua
File metadata and controls
131 lines (108 loc) · 3.76 KB
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
if not IMAIN then
livelove = require("livelove")
-- State variables
particles = {}
maxParticles = 500
spawnRate = 10
spawnTimer = 0
mouseX, mouseY = 0, 0
colorCycle = 0
-- Add timer for tracking time since last hotreload!
lastHotreloadTime = 0
IMAIN = true
end
local function hotreload()
-- Reset the hotreload timer.
lastHotreloadTime = 0
end
livelove.postswap = function(f)
hotreload()
end
foo = 0
local function createParticle()
local angle = love.math.random() * math.pi * 2
local distance = love.math.random(50, 150)
local speed = love.math.random(20, 80)
return {
x = mouseX,
y = mouseY,
vx = math.cos(angle) * speed,
vy = math.sin(angle) * speed,
radius = love.math.random(2, 8),
color = {
r = 0.5 + 0.5 * math.sin(colorCycle),
g = 0.5 + 0.5 * math.sin(colorCycle + math.pi * 2/3),
b = 0.5 + 0.5 * math.sin(colorCycle + math.pi * 4/3),
a = 1
},
life = love.math.random(1, 3),
gravity = love.math.random(50, 150)
}
end
function love.update(dt)
livelove.instantupdate()
-- Update hotreload timer
lastHotreloadTime = lastHotreloadTime + dt
-- Get mouse position
mouseX, mouseY = love.mouse.getPosition()
-- Update color cycle!
colorCycle = colorCycle + dt * 0.5
-- Spawn new particles
spawnTimer = spawnTimer + dt
local particlesToSpawn = math.floor(spawnTimer * spawnRate)
if particlesToSpawn > 0 and #particles < maxParticles then
spawnTimer = spawnTimer - particlesToSpawn / spawnRate
for i = 1, math.min(particlesToSpawn, maxParticles - #particles) do
table.insert(particles, createParticle())
end
end
-- Update existing particles
for i = #particles, 1, -1 do
local p = particles[i]
-- Update position
p.x = p.x + p.vx * dt
p.y = p.y + p.vy * dt
-- Apply gravity toward mouse
local dx = mouseX - p.x
local dy = mouseY - p.y
local distSq = dx * dx + dy * dy
local dist = math.sqrt(distSq)
if dist > 0 then
local force = p.gravity / dist
p.vx = p.vx + dx / dist * force * dt
p.vy = p.vy + dy / dist * force * dt
end
-- Apply drag
p.vx = p.vx * 0.98
p.vy = p.vy * 0.98
-- Update life
p.life = p.life - dt
p.color.a = math.min(1, p.life)
-- Remove dead particles
if p.life <= 0 then
table.remove(particles, i)
end
end
foo = foo + 1
end
function love.draw()
-- Clear screen with a dark background
love.graphics.clear(0.1, 0.1, 0.15)
-- Draw particles
for _, p in ipairs(particles) do
love.graphics.setColor(p.color.r, p.color.g, p.color.b, p.color.a)
love.graphics.circle("fill", p.x, p.y, p.radius)
end
-- Draw information text
love.graphics.setColor(1, 1, 1, 0.7)
love.graphics.print("Interactive Particle System - " .. #particles .. " particles", 10, 10)
love.graphics.print("Move your mouse to control the particles", 10, 30)
-- Format and display the time since last hotreload
local seconds = math.floor(lastHotreloadTime % 60)
local minutes = math.floor(lastHotreloadTime / 60) % 60
local hours = math.floor(lastHotreloadTime / 3600)
local timeString = string.format("Time since last hotreload: %02d:%02d:%02d", hours, minutes, seconds)
love.graphics.print(timeString, 10, 50)
-- livelove
livelove.postdraw()
end