Particles together
Version vom 5. Mai 2020, 18:08 Uhr von TomasM (Diskussion | Beiträge)
///S&&O SS2020 // course: *From Space to Space* with Hannah Pearl - Wilson
//press the mouse to create a particle system
// l or r to change wind direction
import mqtt.*; // import MQTT library
MQTTClient client; // declare class MQTTClient
ArrayList<ParticleSystem> sparksystems; //declare ArrayList of Particle Systems
String mePlayer = "/particles-together/pink/p2/xy";
String otherPlayer = "/particles-together/pink/p1/xy";
int otherMouseX = 0; //for incoming values from other player's mouse xy
int otherMouseY = 0;
void setup() {
client = new MQTTClient(this);
client.connect("mqtt://tomasAxolotl:spielundobjekt@broker.shiftr.io", "tomas"); // "connect to namespace with clientID"
size(1200, 900);
sparksystems = new ArrayList<ParticleSystem>(); //initialize the Particle System class
}
// RECEIVE // .suscribe <3
void clientConnected() {
println("client connected");
client.subscribe(otherPlayer);
}
//transforms the incoming xy string from the other player into x, y coordinates on my canvas
// which are the new coordinates for the particle system origin point
void messageReceived(String topic, byte[] payload) {
// the incoming MQTT "message" is a String: "xValue,yValue"
// to unpack it we use the "split" function:
String incomingPayload = new String(payload);
String[] xy = split(incomingPayload, ',');
println("new message: " + topic + " x: " + xy[0] + " y: " +xy[1]);
otherMouseX = int(xy[0]);
otherMouseY = int(xy[1]);
sparksystems.add(new ParticleSystem(new PVector(otherMouseX, otherMouseY)));
}
//create my particle system at an emmiting point
// .publish to send mouse x and y to coplayer!
void mousePressed(){
sparksystems.add(new ParticleSystem(new PVector(mouseX, mouseY)));
client.publish(mePlayer, mouseX + "," + mouseY); // ("topic", "message")
}
void draw(){
background(0);
// enhanced for loop which can go through arrays which change length
for (ParticleSystem ps : sparksystems){
ps.run();
}
}
void connectionLost() {
println("connection lost");
}