1
0
Fork 0
mirror of https://github.com/Acamaeda/The-Modding-Tree.git synced 2024-11-25 01:41:45 +00:00
The-Modding-Tree/js/technical/particleSystem.js

187 lines
5.1 KiB
JavaScript
Raw Normal View History

2021-05-14 05:38:17 +00:00
var particles = {};
2021-05-14 01:40:51 +00:00
var particleID = 0;
var mouseX = 0;
var mouseY = 0;
2021-05-18 01:51:36 +00:00
function makeParticles(data, amount=1, type = "normal") {
2021-05-14 01:40:51 +00:00
for (let x = 0; x < amount; x++) {
2021-05-18 01:51:36 +00:00
let particle = newParticles[type]()
2021-05-14 01:40:51 +00:00
for (thing in data) {
2021-05-14 05:38:17 +00:00
2021-05-14 01:40:51 +00:00
switch(thing) {
2021-05-14 06:17:12 +00:00
case 'onClick': // Functions that should be copied over
2021-05-14 19:53:48 +00:00
case 'onMouseEnter':
case 'onMouseLeave':
2021-05-14 06:17:12 +00:00
case 'update':
2021-05-14 05:38:17 +00:00
particle[thing] = data[thing]
2021-05-14 06:17:12 +00:00
break;
2021-05-14 05:38:17 +00:00
default:
particle[thing]=run(data[thing], data, x)
2021-05-14 01:40:51 +00:00
}
}
2021-05-18 01:27:12 +00:00
if (data.dir === undefined) {
2021-05-14 05:38:17 +00:00
particle.dir = particle.angle
}
particle.dir = particle.dir + (particle.spread * (x- amount/2 + 0.5))
2021-05-18 01:51:36 +00:00
if(particle.offset) {
particle.x += particle.offset * sin(particle.dir)
particle.y += particle.offset * cos(particle.dir) * -1
}
2021-05-14 06:17:12 +00:00
2021-05-14 05:38:17 +00:00
particle.xVel = particle.speed * sin(particle.dir)
particle.yVel = particle.speed * cos(particle.dir) * -1
particle.fadeInTimer = particle.fadeInTime
2021-05-14 05:38:17 +00:00
Vue.set(particles, particle.id, particle)
2021-05-14 01:40:51 +00:00
}
}
2021-05-18 01:51:36 +00:00
// Makes a particle at a random location that stays still until it despawns
function makeShinies(data, amount=1) {
makeParticles(data, amount, "shiny")
}
2021-05-14 01:40:51 +00:00
function updateParticles(diff) {
for (p in particles) {
2021-05-14 06:17:12 +00:00
let particle = particles[p]
particle.time -= diff;
particle.fadeInTimer -= diff;
2021-05-14 06:17:12 +00:00
if (particle["time"] < 0) {
2021-05-14 05:38:17 +00:00
Vue.delete(particles, p);
2021-05-14 01:40:51 +00:00
}
2021-05-14 05:38:17 +00:00
else {
2021-05-14 06:17:12 +00:00
if (particle.update) run(particle.update, particle)
particle.angle += particle.rotation
particle.x += particle.xVel
particle.y += particle.yVel
2021-05-18 20:06:03 +00:00
particle.speed = Math.sqrt(Math.pow(particle.xVel, 2) + Math.pow(particle.yVel, 2))
particle.dir = atan(-particle.xVel/particle.yVel)
2021-05-14 06:17:12 +00:00
particle.yVel += particle.gravity
2021-05-14 05:38:17 +00:00
}
2021-05-14 01:40:51 +00:00
}
}
2021-05-18 20:06:03 +00:00
function setDir(particle, dir) {
particle.dir = dir
particle.xVel = particle.speed * sin(particle.dir)
particle.yVel = particle.speed * cos(particle.dir) * -1
}
function setSpeed(particle, speed) {
particle.speed = speed
particle.xVel = particle.speed * sin(particle.dir)
particle.yVel = particle.speed * cos(particle.dir) * -1
}
2021-05-18 01:51:36 +00:00
const newParticles = {
normal() {
particleID++
return {
time: 3,
id: particleID,
x: mouseX,
y: mouseY,
width: 35,
height: 35,
image: "resources/genericParticle.png",
angle: 0,
spread: 30,
offset: 10,
speed: 15,
xVel: 0,
yVel: 0,
rotation: 0,
gravity: 0,
fadeOutTime: 1,
fadeInTimer: 0,
fadeInTime: 0,
}
},
shiny() {
particleID++
return {
time: 10,
id: particleID,
x: Math.random() * (tmp.other.screenWidth - 100) + 50,
y: Math.random() * (tmp.other.screenHeight - 100) + 50,
width: 50,
height: 50,
image: "resources/genericParticle.png",
angle: 0,
spread: 0,
offset: 0,
speed: 0,
xVel: 0,
yVel: 0,
rotation: 0,
gravity: 0,
fadeOutTime: 1,
fadeInTimer: 0,
fadeInTime: 0.5,
}
},
2021-05-14 01:40:51 +00:00
}
2021-05-18 01:51:36 +00:00
2021-05-14 01:40:51 +00:00
function updateMouse(event) {
mouseX = event.clientX
mouseY = event.clientY
2021-05-14 05:38:17 +00:00
}
function getOpacity(particle) {
if ((particle.time < particle.fadeOutTime) && particle.fadeOutTime)
return particle.time / particle.fadeOutTime
if (particle.fadeInTimer > 0)
return 1 - (particle.fadeInTimer / particle.fadeInTime)
return 1
}
2021-05-14 05:38:17 +00:00
function constructParticleStyle(particle){
2021-05-18 20:06:03 +00:00
let style = {
2021-05-14 05:38:17 +00:00
left: (particle.x - particle.height/2) + 'px',
top: (particle.y - particle.height/2) + 'px',
width: particle.width + 'px',
height: particle.height + 'px',
transform: "rotate(" + particle.angle + "deg)",
opacity: getOpacity(particle),
2021-05-14 06:17:12 +00:00
"pointer-events": (particle.onClick || particle.onHover) ? 'auto' : 'none',
2021-05-14 05:38:17 +00:00
}
2021-05-18 20:06:03 +00:00
if (particle.color) {
style["background-color"] = particle.color
style.mask = "url(#pmask" + particle.id + ")"
style["-webkit-mask-box-image"] = "url(" + particle.image + ")"
}
else
style["background-image"] = "url(" + particle.image + ")"
return style
2021-05-14 06:43:43 +00:00
}
function clearParticles(check) {
if (!check) check = true
2021-05-14 06:43:43 +00:00
for (p in particles) {
if (run(check, particles[p], particles[p])){
Vue.delete(particles, p)
}
}
2021-05-18 20:06:03 +00:00
}
// Trig with degrees
function sin(x) { return Math.sin(x*Math.PI/180)}
function cos(x) {return Math.cos(x*Math.PI/180)}
function tan(x) {return Math.tan(x*Math.PI/180)}
function asin(x) { return Math.asin(x)*180/Math.PI}
function acos(x) { return Math.acos(x)*180/Math.PI}
function atan(x) { return Math.atan(x)*180/Math.PI}