2016-05-13

posted May 13, 2016, 8:49 AM by Samuel Konstantinovich   [ updated May 13, 2016, 9:15 AM ]
To install processing run this script on a terminal:
/home/support/konstans/public_html/processing/copy.sh


Friday The 13th The Game:
///////////////////////////////////////////////////////////////////////////
//Positionable
///////////////////////////////////////////////////////////////////////////
public interface Positionable {
  public float getX();
  public float getY();
  public void setState(String newState); 
  public String getState();
}
    
///////////////////////////////////////////////////////////////////////////
//Displayable
///////////////////////////////////////////////////////////////////////////
public interface Displayable extends Positionable{
 public void display(); 
}

///////////////////////////////////////////////////////////////////////////
//Moveable
///////////////////////////////////////////////////////////////////////////
public interface Moveable extends Positionable {
  public void move(); 
  public void collide(ArrayList<Positionable> others); 

}

///////////////////////////////////////////////////////////////////////////
//Main Program
///////////////////////////////////////////////////////////////////////////

//(instance variables)
ArrayList<Moveable> thingsToMove = new ArrayList<Moveable>();
ArrayList<Displayable> thingsToDisplay = new ArrayList<Displayable>();
ArrayList<Positionable> thingsThatExist = new ArrayList<Positionable>();
EvilCamper player = new EvilCamper();
String lastKey = "";
boolean keyUsed = false;

public void setup() {
  size(600, 400);

  for (int i = 0; i < 10; i++) {
    Camper c = new Camper();
    thingsToMove.add(c);
    thingsToDisplay.add(c);
    thingsThatExist.add(c);
  }
  thingsToMove.add(player);
  thingsToDisplay.add(player);
  thingsThatExist.add(player);
}

public void keyPressed() {
  print(keyCode+",");
  keyUsed = true;
  //WASD
  if (keyCode == 65) { //A
    lastKey = "A";
  }
  if (keyCode == 68) { //D
    lastKey = "D";
  }
  if (keyCode == 87) { //W
    lastKey = "W";
  }
  if (keyCode == 83) { //S
    lastKey = "S";
  }
}

public void mouseClicked() {
  delete(mouseX, mouseY);
}

public void delete(float x, float y) {
  for (int i = thingsThatExist.size() - 1; i>=0; i-- ) {
    Positionable p = thingsThatExist.get(i);
    if ( dist(x, y, p.getX(), p.getY())<20) {
      p.setState("REMOVE");
    }
  }
}

public void draw() {
  /***********************************************
  get user input
  */
  handleUserInput();

  /************************************************
  *change object states
   */
  for ( Moveable m : thingsToMove) {
    //move objects
    m.move();
    //check for collisions/events
    m.collide(thingsThatExist);
  }

  /************************************************
  *draw objects
   */  
  //clear the world
  background(255);
  //draw things
  for ( Displayable d : thingsToDisplay) {
    d.display();
  }
  //draw overlay
  drawOverlay();




  /***********************************************
  *remove objects from the game if they need to be
   */
   /*
  for (int i = thingsToMove.size() - 1; i >= 0; i--) {
    String state = thingsToMove.get(i).getState();
      if (state.equals("DEAD") || state.equals("REMOVE")) {
      thingsToMove.remove(i);
    }
  }
  */
  /*
  for (int i = thingsToDisplay.size() - 1; i >= 0; i--) {
    String state = thingsToDisplay.get(i).getState();
      if ( state.equals("REMOVE")) {
      thingsToDisplay.remove(i);
    }
  }
  */
  /*
  for (int i = thingsThatExist.size() - 1; i >= 0; i--) {
    String state = thingsThatExist.get(i).getState();
      if ( state.equals("REMOVE")) {
      thingsThatExist.remove(i);
    }
  }
  */
}

public void handleUserInput() {
  if (keyUsed) {
    if (lastKey.equals("W")) {
    }
    if (lastKey.equals("A")) {
    }
    if (lastKey.equals("S")) {
    }
    if (lastKey.equals("D")) {
    }
    //only allow one thing per key press
    keyUsed = false;
  }
}

public void drawOverlay() {
  text("Things that exist: "+thingsThatExist.size(), 20, 20);
  text("Things to display: "+thingsToDisplay.size(), 20, 40);
  text("Things to move: "+thingsToMove.size(), 20, 60);
}


///////////////////////////////////////////////////////////////////////////
//Camper
///////////////////////////////////////////////////////////////////////////
public class Camper implements Moveable, Displayable {
  private float x, y, heading, speed;
  private String state;

  public float getX() { 
    return x;
  }
  public float getY() { 
    return y;
  }
  public float getHeading() { 
    return heading;
  }
  public float getSpeed() { 
    return speed;
  }
  public String getState() { 
    return state;
  }
  public void setState(String newState) { 
    state = newState;
  }

  public Camper() {
    state = "MOVING";
    x = width/2;
    y = height/2;
    heading = (int)random(6)*60;
    speed = (1+random(10))/10.0;
  }

  public void move() {
    if (state.equals("MOVING")) {
      x += speed * cos(radians(heading));
      y += speed * sin(radians(heading));
    }else if (state.equals("PATTERN")) {
      //DO SOEMTHING ELSE HERE
      //SOMETIMES CHANGE THE STATE
    }
  }

  public void collide(ArrayList<Positionable> others) {
    if (x > width - 50 || x < 50) {
      heading = (360 + 180 - heading ) % 360;
    }
    if (y > height - 50 || y < 50) {
      heading = (360 - heading);
    }
  }

  public void display() {
    fill(255);
    if (getState().equals("DEAD")) {
      fill(255, 0, 0);
    }
    ellipse(getX(), getY(), 30, 30);
    ellipse(  getX() + 15 * cos(radians(getHeading())), getY() + 15 * sin(radians(getHeading())), 10, 10);
    fill(0);
    text("heading:"+getHeading()+"\nState:"+getState(), getX(), getY());
  }
}





///////////////////////////////////////////////////////////////////////////
//Evil Camper
///////////////////////////////////////////////////////////////////////////
public class EvilCamper extends Camper {

  public void display() {
    fill(100, 200, 100);
    ellipse(getX(), getY(), 30, 30);
    fill(255, 0, 0);
    ellipse(  getX() + 15 * cos(radians(getHeading())), getY() + 15 * sin(radians(getHeading())), 10, 10);
    fill(0);
    text("heading:"+getHeading(), getX(), getY());
  }

  public void collide(ArrayList<Positionable> others) {
    super.collide(others);
    //kill the campers here!
    for (Positionable c : others) {
      if ( c != this ) {
        if (c.getState().equals("MOVING") && dist(this.getX(),this.getY(),c.getX(),c.getY()) < 20.0) {
          c.setState("DEAD");
        }
      }
    }
  }
}
Comments