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"); 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 type effect = [number, effectKeypoint[], MeshPart, number] // Time since last keypoint, effect keypoints, effect meshPart, effect priority
export interface effectMaker { export interface effectMaker {
@ -13,96 +13,99 @@ export interface effectMaker {
} }
export interface effectRunner { export interface effectRunner {
runEffects: ( runEffects: (timeSinceLastFrame: number) => void;
timeSinceLastFrame: number
) => void;//(timeSinceLastFrame: number, effectsToRun: effect[]) => effect[]
} }
/*
interface effectHandler extends effectMaker, effectRunner { interface effectHandler extends effectMaker, effectRunner {
EFFECT_FOLDER: Folder; EFFECT_FOLDER: Folder;
effectsToRun: effect[] effectsToRun: effect[]
} }
*/
export function makeEffectHandler(effectFolder: Folder) { class effectHandler implements effectMaker, effectRunner {
return { constructor(effectFolder: Folder) {
meshPartEffect: function(meshPart: MeshPart, material: Enum.Material, effectKeypoints: effectKeypoint[], priority?: number) { this.EFFECT_FOLDER = effectFolder
const effectMeshPart = meshPart.Clone(); }
effectMeshPart.Material = material; meshPartEffect(meshPart: MeshPart, material: Enum.Material, effectKeypoints: effectKeypoint[], priority?: number) {
effectMeshPart.Color = effectKeypoints[0][1]; const effectMeshPart = meshPart.Clone();
effectMeshPart.Size = effectKeypoints[0][2]; effectMeshPart.Material = material;
effectMeshPart.CFrame = effectKeypoints[0][3]; effectMeshPart.Color = effectKeypoints[0][1];
effectMeshPart.Transparency = effectKeypoints[0][4]; effectMeshPart.Size = effectKeypoints[0][2];
let effectDuration = 0 effectMeshPart.CFrame = effectKeypoints[0][3];
effectKeypoints.forEach(effectKeypoint => { effectMeshPart.Transparency = effectKeypoints[0][4];
effectDuration += effectKeypoint[0] 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; if (effectInArrayDuration > effectDuration) {
effectMeshPart.CanCollide = false; effectsToRun.insert(index, [0, effectKeypoints, effectMeshPart, priority || 0] as effect);
effectMeshPart.Anchored = true; break
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
}
} }
effectsToRun.insert(effectsToRun.size(), [0, effectKeypoints, effectMeshPart, priority || 0] as effect); }
}, effectsToRun.insert(effectsToRun.size(), [0, effectKeypoints, effectMeshPart, priority || 0] as effect);
particleEffect: function() {}, }
runEffects: function(timeSinceLastFrame: number) { particleEffect() {}
let effectsToRun = this.effectsToRun runEffects(timeSinceLastFrame: number) {
for (const effect of effectsToRun) { let effectsToRun = this.effectsToRun
// Update the effect time for (const effect of effectsToRun) {
let nextKeypoint = effect[1][1]; // Update the effect time
let timeOfNextKeypoint = nextKeypoint[0]; let nextKeypoint = effect[1][1];
let timeSinceLastKeypoint = effect[0] + timeSinceLastFrame; let timeOfNextKeypoint = nextKeypoint[0];
while (timeSinceLastKeypoint >= timeOfNextKeypoint) { let timeSinceLastKeypoint = effect[0] + timeSinceLastFrame;
if (effect[1][2]) { while (timeSinceLastKeypoint >= timeOfNextKeypoint) {
timeSinceLastKeypoint -= timeOfNextKeypoint; if (effect[1][2]) {
effect[1].remove(0); timeSinceLastKeypoint -= timeOfNextKeypoint;
nextKeypoint = effect[1][1]; effect[1].remove(0);
timeOfNextKeypoint = nextKeypoint[0]; nextKeypoint = effect[1][1];
} else { timeOfNextKeypoint = nextKeypoint[0];
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,
);
} else { } 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, EFFECT_FOLDER: Folder;
effectsToRun: [], effectsToRun: effect[] = [];
} as effectHandler; }
export function makeEffectRunner(effectFolder: Folder): effectRunner {
return new effectHandler(effectFolder);
} }
/* /*
local function horseEffModule(o,typ,a1,a2,a3,a4,a5,a6,a7) 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"); const RunService = game.GetService("RunService");
import { bindToServerMessage, messageServer } from "./ClientMessenger"; import { bindToServerMessage, messageServer } from "./ClientMessenger";
import { handleGuiInput, drawGui, closeGui } from "./GuiHandler"; import { handleGuiInput, drawGui, closeGui } from "./GuiHandler";
import { makeEffectHandler, effectRunner } from "./EffectMaker"; import { makeEffectRunner, effectRunner } from "./EffectMaker";
const LOCALPLAYER = Players.LocalPlayer; const LOCALPLAYER = Players.LocalPlayer;
const PLAYERGUI = LOCALPLAYER.WaitForChild("PlayerGui", 1) as PlayerGui; const PLAYERGUI = LOCALPLAYER.WaitForChild("PlayerGui", 1) as PlayerGui;
assert( assert(