Skip to content

Instantly share code, notes, and snippets.

@mischa
Created October 10, 2016 15:24
Show Gist options
  • Save mischa/39c8d40a8317788f658c6629541f7717 to your computer and use it in GitHub Desktop.
Save mischa/39c8d40a8317788f658c6629541f7717 to your computer and use it in GitHub Desktop.
// This code is an absolute mess :)
import processing.serial.*;
import processing.sound.*;
SoundFile chill;
SoundFile meeting;
SoundFile pewpew;
float redValue = 0; // red value
float greenValue = 0; // green value
float blueValue = 0; // blue value
Serial controllerPort;
float xPosition = 0;
float playerSize = 30;
boolean shooting = false;
// smoothing variables
int numReadings = 15;
float[] readings = new float[numReadings]; // the readings from the analog input
float total = 0; // the running total
float average = 0; // the average
int readIndex = 0;
float speed = 7;
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
float lastShot = -10;
PImage bg;
PImage blackberry;
PImage projectileImage;
boolean inAMeeting = false;
String currentSound = "chill";
PImage meetingImg0;
PImage meetingImg1;
PImage meetingImg2;
ArrayList<PImage> messageImages = new ArrayList<PImage>();
ArrayList<PImage> coworkerImages = new ArrayList<PImage>();
PImage gtd;
public class Projectile {
float xPos;
float yPos;
float size = 15;
boolean used;
Projectile(float x, float y) {
xPos = x;
yPos = y;
used = false;
}
void update() {
yPos -= speed + 10;
float projectileLeft = xPos - size/2;
float projectileRight = xPos + size/2;
float projectileTop = yPos - size/2;
float projectileBottom = yPos + size/2;
int i=0;
while ( i<obstacles.size() && !used) {
Obstacle obstacle = obstacles.get(i);
float obstacleLeft = obstacle.left();
float obstacleRight = obstacle.right();
float obstacleBottom = obstacle.bottom(); // Haxxx
float obstacleTop = obstacle.top();
if (obstacleBottom > 0 && obstacleLeft < projectileRight && obstacleRight > projectileLeft &&
obstacleTop < projectileBottom && obstacleBottom > projectileTop ) {
obstacle.pow();
used = true;
//println("pow");
}
i++;
}
}
void drawProjectile() {
if (!used) {
//fill(0, 255, 0);
image(projectileImage, xPos, yPos, size, size);
}
}
}
public class Obstacle {
float xPos;
float screenY;
boolean cow;
color c = color(255);
float size; // TODO: This may have to be more of a bounds thing
PImage img;
float destroyed = -1;
Obstacle(float y) {
screenY = y;
xPos = random(1.0);
size = 100;
cow = random(1.0) > 0.5;
if (cow) {
img = coworkerImages.get(int(random(coworkerImages.size())));
} else {
img = messageImages.get(int(random(messageImages.size())));
}
}
void drawObstacle() {
imageMode(CENTER);
if (destroyed != 1 && screenY > -size/2 && screenY -size/2 < height) {
float screenX = map(xPos, 0, 1, 0, width);
image(img, screenX, screenY, 100, 100);
} else if(destroyed == 1 && screenY -size/2 < height) {
float screenX = map(xPos, 0, 1, 0, width);
image(gtd, screenX, screenY, 50, 50);
}
}
void scroll() {
screenY = screenY + speed;
}
void playerDidCollide() {
if (cow && millis() - lastMeeting > 300) {
inAMeeting = true;
}
}
void pow() {
if (!cow) {
c = color(0, 0, 255, 255);
destroyed = 1;
}
}
// haxx
float top() {
return screenY - size/2;
}
float bottom() {
return screenY + size/2;
}
float left() {
float screenX = map(xPos, 0, 1, 0, width);
return screenX - size/2;
}
float right() {
float screenX = map(xPos, 0, 1, 0, width);
return screenX + size/2;
}
boolean checkForCollision(float playerY, float playerLeft, float playerRight) {
float screenX = map(xPos, 0, 1, 0, width);
float obstacleLeft = screenX - size/2;
float obstacleRight = screenX + size/2;
boolean yOverlap = ((screenY + size/2) > (playerY + playerSize/2)) && ((screenY - size/2) < (playerY + playerSize/2));
if (yOverlap) {
boolean xOverlap = (playerLeft < obstacleRight && playerRight > obstacleLeft) || (playerRight < obstacleLeft && playerLeft > obstacleRight);
if (xOverlap) {
//println("Collision!");
// print
playerDidCollide();
controllerPort.write('Z');
return true;
}
}
return false;
}
}
void setup() {
size(600, 800);
bg = loadImage("macos.jpg");
background(bg);
gtd = loadImage("done.png");
blackberry = loadImage("crackberry.png");
for (int i = 0; i<5; i++) {
PImage img = loadImage("cow" + i + ".png");
coworkerImages.add(img);
}
for (int i = 0; i<3; i++) {
PImage img = loadImage("message" + i + ".png");
messageImages.add(img);
}
meetingImg0 = loadImage("meeting0.gif");
meetingImg1 = loadImage("meeting1.png");
meetingImg2 = loadImage("meeting2.png");
// textSize(32);
//fill(255, 200);
// text("GTD", width/2, height/2);
// List all the available serial ports
// if using Processing 2.1 or later, use Serial.printArray()
//println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
controllerPort = new Serial(this, Serial.list()[1], 9600);
// don't generate a serialEvent() unless you get a newline character:
controllerPort.bufferUntil('\n');
xPosition = 0.5;
projectileImage = loadImage("projectile.png");
generateObstacles();
chill = new SoundFile(this, "chillstep.mp3");
chill.loop();
meeting = new SoundFile(this, "meeting.mp3");
meeting.amp(100);
pewpew = new SoundFile(this, "pewpew.wav");
pewpew.amp(30);
rectMode(CENTER);
imageMode(CENTER);
noStroke();
}
void generateObstacles() {
int y = 0;
for (int i=0; i<2000; i++) {
obstacles.add(new Obstacle(y));
y = y - 500;
}
}
void updateObstacles(float playerY, float playerLeft, float playerRight) {
for (int i=0; i<obstacles.size(); i++) {
obstacles.get(i).drawObstacle();
obstacles.get(i).scroll();
obstacles.get(i).checkForCollision(playerY, playerLeft, playerRight);
}
}
void updateProjectiles() {
for (int i=0; i<projectiles.size(); i++) {
projectiles.get(i).update();
projectiles.get(i).drawProjectile();
}
}
int meetingImage = 0;
int meetingTime = 0;
int lastMeeting = 0;
void draw() {
// set the background color with the color values:
background(bg);
float yPos = map(0.8, 0, 1, 0, height);
if(inAMeeting) {
if(currentSound == "chill") {
chill.stop();
meeting.loop();
meetingImage = round(random(2));
currentSound = "meeting";
}
background(255);
if(meetingImage == 0) {
image(meetingImg0, width/2, height/2);
} else if(meetingImage == 1) {
image(meetingImg1, width/2, height/2);
} else if(meetingImage == 2) {
image(meetingImg2, width/2, height/2);
}
meetingTime++;
if(meetingTime > 400) {
inAMeeting = false;
meeting.stop();
chill.loop();
currentSound = "chill";
lastMeeting = millis();
meetingTime = 0;
}
}
else {
fill(255);
//rect(xPosition, yPos, playerSize, playerSize);
image(blackberry, xPosition, yPos);
updateObstacles(yPos, xPosition - playerSize/2, xPosition + playerSize/2);
updateProjectiles();
if (shooting) {
//println("shooting");
fire(xPosition, yPos);
pewpew.play();
}
}
}
int lastFired = 0;
void fire(float playerX, float playerY) {
boolean shouldDelay = lastFired > 0 && (millis() - lastFired < 100);
// println(lastFired);
if (!shouldDelay) {
projectiles.add(new Projectile(playerX, playerY));
lastFired = millis();
}
}
void serialEvent(Serial controllerPort) {
// get the ASCII string:
String inString = controllerPort.readStringUntil('\n');
if (inString != null && inString.length() > 15) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the
// resulting substrings into an integer array:
//println(inString);
float[] positions = float(split(inString, ","));
//println(positions);
float x = positions[0];
float buttonIn = positions[3];
xPosition = round(map(x, -9, 9, 0, width));
/*
for (int i = 0; i < numReadings; i++) {
total = total - readings[i];
readings[i] = xPosition;
total = total + readings[i];
}
*/
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = xPosition;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// float newXPosition = total /numReadings;
xPosition = total/numReadings;
//println(xPosition);
xPosition = constrain(xPosition, 15, width - 16);
int buttonState = int(buttonIn);
if (buttonState == 0 ) {
shooting = false;
} else {
if ((frameCount - lastShot) > 10) {
lastShot = frameCount;
shooting = true;
// println(shooting);
} else {
shooting = false;
}
}
}
}
void keyPressed() {
if (keyCode == 32) {
shooting = !shooting;
}
if (keyCode == 37) {
xPosition -= 20;
}
if (keyCode == 39) {
xPosition +=20;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment