Skip to content

Instantly share code, notes, and snippets.

@wtrebella
Created June 6, 2013 02:42
Show Gist options
  • Save wtrebella/5718952 to your computer and use it in GitHub Desktop.
Save wtrebella/5718952 to your computer and use it in GitHub Desktop.
This will pulsate the float properties of any object added to that so that they are all in time with a specific bpm.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class WTPulsator {
public List<WTPulsatorObject> objects = new List<WTPulsatorObject>();
public float totalTime = 0;
public float bpm;
bool isPulsating = false;
public WTPulsator(float bpm) {
this.bpm = bpm;
StartPulsating();
}
public void StartPulsating() {
isPulsating = true;
}
public void StopPulsating() {
isPulsating = false;
}
public WTPulsator AddPulsateProperty(object targetObject, string propertyName, float valA, float valB, float numBeats = 1) {
WTPulsatorObject po = new WTPulsatorObject(targetObject, propertyName, valA, valB, numBeats);
objects.Add(po);
float allTime = totalTime;
int amt = 0;
while (allTime > 30.0f / bpm * po.numBeats) {
amt++;
allTime -= 30.0f / bpm * po.numBeats;
}
if (amt % 2 == 0) po.isPulsatingUp = true;
else po.isPulsatingUp = false;
po.halfTimer = allTime;
po.Refresh(bpm);
return this;
}
public void RemoveTargetObject(object targetObject) {
for (int i = objects.Count - 1; i >= 0; i--) {
WTPulsatorObject po = objects[i];
if (po.targetObject == targetObject) {
objects.Remove(po);
}
}
}
public void RemoveAllTargetObjects() {
for (int i = objects.Count - 1; i >= 0; i--) {
objects.RemoveAt(objects.Count -1 );
}
}
public void AdjustVals(object targetObject, string propertyName, float valA, float valB) {
for (int i = 0; i < objects.Count; i++) {
WTPulsatorObject po = objects[i];
if (po.targetObject == targetObject && po.propertyName == propertyName) {
po.valA = valA;
po.valB = valB;
break;
}
}
}
public void HandleUpdate() {
if (!isPulsating) return;
totalTime += Time.deltaTime;
for (int i = 0; i < objects.Count; i++) {
WTPulsatorObject po = objects[i];
po.halfTimer += Time.deltaTime;
po.Refresh(bpm);
}
}
}
public class WTPulsatorObject {
public float halfTimer = 0;
public object targetObject;
public float valA;
public float valB;
public float numBeats;
public bool isPulsatingUp = true;
public string propertyName {get; private set;}
private Action<float> setter;
public WTPulsatorObject(System.Object targetObject, string propertyName, float valA, float valB, float numBeats = 1) {
this.targetObject = targetObject;
this.numBeats = numBeats;
this.valA = valA;
this.valB = valB;
this.propertyName = propertyName;
setter = GoTweenUtils.setterForProperty<Action<float>>(targetObject, propertyName);
}
public void Refresh(float bpm) {
if (halfTimer > 30.0f / bpm * numBeats) {
halfTimer -= 30.0f / bpm * numBeats;
isPulsatingUp = !isPulsatingUp;
}
float percentage = halfTimer / (30.0f / bpm * numBeats);
if (!isPulsatingUp) percentage = 1 - percentage;
float newVal = percentage * (valB - valA) + valA;
setter(newVal);
}
}
using UnityEngine;
using System.Collections;
public class WTPulsatorTest : FStage {
WTPulsator pulsator;
public WTPulsatorTest() : base("") {
pulsator = new WTPulsator(120);
for (int i = 0; i < 50; i++) {
FSprite s = new FSprite("circle");
s.scale = Random.Range(0.1f, 1.0f);
s.x = Random.Range(s.width / 2f, Futile.screen.width - s.width / 2f);
s.y = Random.Range(s.height / 2f, Futile.screen.height - s.height / 2f);
s.color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1.0f);
AddChild(s);
pulsator.AddTargetObject(s, "scale", s.scale / 2.0f, s.scale);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment