Drawing bodies

Aus hyperdramatik
Wechseln zu: Navigation, Suche

Drawing bodies.PNG

//////////////////////////////////////////////////////////////////// // S&&O SS2020 // course: *From Space to Space* //////////////////////////////////////////////////////////////////// // this sketch sends your mouse x,y coordinates to a shiftr namespace // and receives other mouse x,y coordinates from the same namespace // code based on example by Joël Gähwiler // https://github.com/256dpi/processing-mqtt ////////////////////////////////////////////////////////////////////

import mqtt.*; // import MQTT library MQTTClient client; // create instance of MQTTClient

//////////////////////////////////////////////////////////////////// ///////////////////// DECLARE GLOBAL VARIABLES ///////////////////// //////////////////////////////////////////////////////////////////// // !!! CHANGE p1 and p2 depending on which player you are !!! // String mePlayer = "/janne_multiplayer/orange/p2"; String otherPlayer = "/janne_multiplayer/orange/p1"; ////////////////////////////////////////////////////////////////////

int otherMouseX = 0; //for incoming values from other player's mouse xy int otherMouseY = 0; int otherPreviousMouseX = 0; //to keep track of last mouse position for drawing line int otherPreviousMouseY = 0;

long timeStamp; //to keep track of time int delayTime = 500/35; //for use to publish messages only every 35 milliseconds!

int mouseClick = 0; //used to toggle between background fade and no-fade PImage layout; PImage drawer; PImage drawer2; boolean showImage = false;

////////////////////////////////////////////////////////////////////

void setup() {

 client = new MQTTClient(this);
 client.connect("mqtt://jannenora:Monkey47@broker.shiftr.io", "works");
 size(800, 550);
 layout = loadImage("drops2.jpeg");
 drawer = loadImage("jannesmall_character.png");
 drawer2 = loadImage("waldgeister.png");

}

//////////////////////////////////////////////////////////////////// // RECEIVE // ////////////////////////////////////////////////////////////////////

void clientConnected() {

 println("client connected");
 client.subscribe(otherPlayer + "/xy");

}

void messageReceived(String topic, byte[] payload) {

 if (topic.equals(otherPlayer + "/xy") == true) {
   // the incoming MQTT "message" is a String: "xValue,yValue"
   // to unpack it we use the "split" function:
   String incomingCoordinates = new String(payload);  
   String[] xy = split(incomingCoordinates, ',');
   println("new message: " + topic + " x: " + xy[0] + " y: " +xy[1]);
   otherMouseX = int(xy[0]);
   otherMouseY = int(xy[1]);
 }

}

//////////////////////////////////////////////////////////////////// // SEND & DRAW my line // ////////////////////////////////////////////////////////////////////

//!only DRAW my line and publish my coordinates when my mouse moves: void mouseMoved() {

 image(drawer, pmouseX, pmouseY, mouseX, mouseY);
 /*stroke(19, 120, 200);  //set line colour R,G,B
 strokeWeight(10);  //line thickness
 fill (356);
 ellipse (pmouseX, pmouseY, mouseX, mouseY);*/
 //the MQTT connection will close if you send more than 25 operations per second!
 //so we implement a delay to SEND my coordinates only every xxx milliseconds:
 if (millis() - timeStamp > delayTime) {
   timeStamp = millis();
   client.publish(mePlayer + "/xy", mouseX + "," + mouseY); // ("topic", "message")
 }

}

//////////////////////////////////////////////////////////////////// // DRAW other line // ////////////////////////////////////////////////////////////////////

void draw() {

 if (mouseClick % 2 == 0) {
   tint(255, 100);  //tint=tönen. gives the image transparency to slowly fade over line
   image(layout, 0, 0);
 }
 image(drawer2, pmouseX, pmouseY, mouseX, mouseY);
 
 /*stroke(10, 160, 230);  //set line colour R,G,B
 strokeWeight(10);  //line thickness
 line(otherPreviousMouseX, otherPreviousMouseY, otherMouseX, otherMouseY); */
 otherPreviousMouseX = otherMouseX;
 otherPreviousMouseY = otherMouseY;    //click mouse to turn on/off fade effect:

}

////////////////////////////////////////////////////////////////////

void mouseClicked() {

 mouseClick++;
 println(mouseClick);
 if (mouseClick % 2 != 0) {
   image(layout, 0, 0);
 }

}

////////////////////////////////////////////////////////////////////

void connectionLost() {

 println("connection lost");

}