OOP changes to EffectMaker

= Conversion of EffectHandler to a class
This commit is contained in:
loplkc loplkc 2022-02-04 21:43:51 -05:00
parent 79202b9034
commit b1a1bb6013
2 changed files with 84 additions and 81 deletions

View file

@ -1,5 +1,5 @@
const TweenService = game.GetService("TweenService");
type effectKeypoint = [number, Color3, Vector3, CFrame, number, Enum.EasingStyle?, Enum.EasingDirection?];
export type effectKeypoint = [time: number, color: Color3, size: Vector3, cframe: CFrame, transparency: number, easingStyle?: Enum.EasingStyle, easingDirection?: Enum.EasingDirection];
type effect = [number, effectKeypoint[], MeshPart, number] // Time since last keypoint, effect keypoints, effect meshPart, effect priority
export interface effectMaker {
@ -13,96 +13,99 @@ export interface effectMaker {
}
export interface effectRunner {
runEffects: (
timeSinceLastFrame: number
) => void;//(timeSinceLastFrame: number, effectsToRun: effect[]) => effect[]
runEffects: (timeSinceLastFrame: number) => void;
}
/*
interface effectHandler extends effectMaker, effectRunner {
EFFECT_FOLDER: Folder;
effectsToRun: effect[]
}
export function makeEffectHandler(effectFolder: Folder) {
return {
meshPartEffect: function(meshPart: MeshPart, material: Enum.Material, effectKeypoints: effectKeypoint[], priority?: number) {
const effectMeshPart = meshPart.Clone();
effectMeshPart.Material = material;
effectMeshPart.Color = effectKeypoints[0][1];
effectMeshPart.Size = effectKeypoints[0][2];
effectMeshPart.CFrame = effectKeypoints[0][3];
effectMeshPart.Transparency = effectKeypoints[0][4];
let effectDuration = 0
effectKeypoints.forEach(effectKeypoint => {
effectDuration += effectKeypoint[0]
*/
class effectHandler implements effectMaker, effectRunner {
constructor(effectFolder: Folder) {
this.EFFECT_FOLDER = effectFolder
}
meshPartEffect(meshPart: MeshPart, material: Enum.Material, effectKeypoints: effectKeypoint[], priority?: number) {
const effectMeshPart = meshPart.Clone();
effectMeshPart.Material = material;
effectMeshPart.Color = effectKeypoints[0][1];
effectMeshPart.Size = effectKeypoints[0][2];
effectMeshPart.CFrame = effectKeypoints[0][3];
effectMeshPart.Transparency = effectKeypoints[0][4];
let effectDuration = 0
effectKeypoints.forEach(effectKeypoint => {
effectDuration += effectKeypoint[0]
});
effectMeshPart.CastShadow = false;
effectMeshPart.CanCollide = false;
effectMeshPart.Anchored = true;
effectMeshPart.Parent = this.EFFECT_FOLDER;
// Insert the effect before the effect that will end after it
const effectsToRun = this.effectsToRun
for (let index = 0; index < effectsToRun.size(); index += 1) {
const effectInArray = effectsToRun[index];
let effectInArrayDuration = -effectInArray[0]
effectInArray[1].forEach(effectKeypoint => {
effectInArrayDuration += effectKeypoint[0]
});
effectMeshPart.CastShadow = false;
effectMeshPart.CanCollide = false;
effectMeshPart.Anchored = true;
effectMeshPart.Parent = this.EFFECT_FOLDER;
// Insert the effect before the effect that will end after it
const effectsToRun = this.effectsToRun
for (let index = 0; index < effectsToRun.size(); index += 1) {
const effectInArray = effectsToRun[index];
let effectInArrayDuration = -effectInArray[0]
effectInArray[1].forEach(effectKeypoint => {
effectInArrayDuration += effectKeypoint[0]
});
if (effectInArrayDuration > effectDuration) {
effectsToRun.insert(index, [0, effectKeypoints, effectMeshPart, priority || 0] as effect);
break
}
if (effectInArrayDuration > effectDuration) {
effectsToRun.insert(index, [0, effectKeypoints, effectMeshPart, priority || 0] as effect);
break
}
effectsToRun.insert(effectsToRun.size(), [0, effectKeypoints, effectMeshPart, priority || 0] as effect);
},
particleEffect: function() {},
runEffects: function(timeSinceLastFrame: number) {
let effectsToRun = this.effectsToRun
for (const effect of effectsToRun) {
// Update the effect time
let nextKeypoint = effect[1][1];
let timeOfNextKeypoint = nextKeypoint[0];
let timeSinceLastKeypoint = effect[0] + timeSinceLastFrame;
while (timeSinceLastKeypoint >= timeOfNextKeypoint) {
if (effect[1][2]) {
timeSinceLastKeypoint -= timeOfNextKeypoint;
effect[1].remove(0);
nextKeypoint = effect[1][1];
timeOfNextKeypoint = nextKeypoint[0];
} else {
effect[2].Destroy()
effectsToRun.remove(0)
continue // Move on if this effect is done
}
}
effect[0] = timeSinceLastKeypoint;
// Get the rest of the variables
const currentKeypoint = effect[1][0];
const easingStyle = currentKeypoint[5];
const meshPart = effect[2];
// Get the alpha
let alpha = 0;
if (easingStyle) {
alpha = TweenService.GetValue(
timeSinceLastKeypoint / timeOfNextKeypoint,
easingStyle,
currentKeypoint[6] || Enum.EasingDirection.InOut,
);
}
effectsToRun.insert(effectsToRun.size(), [0, effectKeypoints, effectMeshPart, priority || 0] as effect);
}
particleEffect() {}
runEffects(timeSinceLastFrame: number) {
let effectsToRun = this.effectsToRun
for (const effect of effectsToRun) {
// Update the effect time
let nextKeypoint = effect[1][1];
let timeOfNextKeypoint = nextKeypoint[0];
let timeSinceLastKeypoint = effect[0] + timeSinceLastFrame;
while (timeSinceLastKeypoint >= timeOfNextKeypoint) {
if (effect[1][2]) {
timeSinceLastKeypoint -= timeOfNextKeypoint;
effect[1].remove(0);
nextKeypoint = effect[1][1];
timeOfNextKeypoint = nextKeypoint[0];
} else {
alpha = timeSinceLastKeypoint / timeOfNextKeypoint;
effect[2].Destroy()
effectsToRun.remove(0)
continue // Move on if this effect is done
}
// Alter the effect
meshPart.Color = currentKeypoint[1].Lerp(nextKeypoint[1], alpha);
meshPart.Position = currentKeypoint[2].Lerp(nextKeypoint[2], alpha);
meshPart.CFrame = currentKeypoint[3].Lerp(nextKeypoint[3], alpha);
meshPart.Transparency = currentKeypoint[4] + (nextKeypoint[4] - currentKeypoint[4]) * alpha;
}
//this = effectsToRun;
},
effect[0] = timeSinceLastKeypoint;
// Get the rest of the variables
const currentKeypoint = effect[1][0];
const easingStyle = currentKeypoint[5];
const meshPart = effect[2];
// Get the alpha
let alpha = 0;
if (easingStyle) {
alpha = TweenService.GetValue(
timeSinceLastKeypoint / timeOfNextKeypoint,
easingStyle,
currentKeypoint[6] || Enum.EasingDirection.InOut,
);
} else {
alpha = timeSinceLastKeypoint / timeOfNextKeypoint;
}
// Alter the effect
meshPart.Color = currentKeypoint[1].Lerp(nextKeypoint[1], alpha);
meshPart.Position = currentKeypoint[2].Lerp(nextKeypoint[2], alpha);
meshPart.CFrame = currentKeypoint[3].Lerp(nextKeypoint[3], alpha);
meshPart.Transparency = currentKeypoint[4] + (nextKeypoint[4] - currentKeypoint[4]) * alpha;
}
//this = effectsToRun;
}
EFFECT_FOLDER: effectFolder,
effectsToRun: [],
} as effectHandler;
EFFECT_FOLDER: Folder;
effectsToRun: effect[] = [];
}
export function makeEffectRunner(effectFolder: Folder): effectRunner {
return new effectHandler(effectFolder);
}
/*
local function horseEffModule(o,typ,a1,a2,a3,a4,a5,a6,a7)

View file

@ -3,7 +3,7 @@ const Players = game.GetService("Players");
const RunService = game.GetService("RunService");
import { bindToServerMessage, messageServer } from "./ClientMessenger";
import { handleGuiInput, drawGui, closeGui } from "./GuiHandler";
import { makeEffectHandler, effectRunner } from "./EffectMaker";
import { makeEffectRunner, effectRunner } from "./EffectMaker";
const LOCALPLAYER = Players.LocalPlayer;
const PLAYERGUI = LOCALPLAYER.WaitForChild("PlayerGui", 1) as PlayerGui;
assert(