Skip to content

Instantly share code, notes, and snippets.

@jherico
Created January 23, 2016 07:14
Show Gist options
  • Save jherico/c52f4c20a4c33e8a83e0 to your computer and use it in GitHub Desktop.
Save jherico/c52f4c20a4c33e8a83e0 to your computer and use it in GitHub Desktop.
(function() {
PulseEntity.prototype = {
colors: [ "#F00", "#0F0", "#00F" ],
// Return the initial state.
initialState: function(stateWrapper) {
return {
// 64 bit absolute UTC timestamp of the current time
startTime: stateWrapper.now,
currentColor: 0
};
},
// how often (in seconds) should the update function be called ideally.
desiredFrequency: function() {
return 1;
},
update: function(stateWrapper) {
// 64 bit absolute UTC timestamp of the last time this function was
// successfully called on any host
var lastExecution = stateWrapper.lastExecution
// A replacement for the entity ID. The domain server gives each
// executor of the script it's own token and uses the token on incoming
// edit packets to do conflict resolution
var entityToken = stateWrapper.entityToken
var state = stateWrapper.state;
// Determine the interval between the start time and the current time
var interval = stateWrapper.now - state.startTime;
// Convert to seconds
interval = interval / 1000;
// Cycle between a size of 0.1 and 2.1 meters
var desiredSize = Math.sin((interval % 60) / 60.0 * Math.PI * 2.0) + 1.1;
Entities.editEntity(entityToken, {
dimensions: {
x: desiredSize,
y: desiredSize,
z: desiredSize,
},
color: colors[state.currentColor]
});
state.currentColor = (state.currentColor + 1) % colors.length;
}
};
return new PulseEntity();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment