Advertisement
Guest User

frictionless.pde

a guest
Dec 27th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. PlayerRect player = new PlayerRect(new PVector(40, 94), 5, 5, 0, 0, 255);
  2. GravRect testGrav = new GravRect(new PVector(400,400),10,10, 255, 0, 0,.1);
  3. final int windowX = 800;
  4. final int windowY = 600;
  5. void setup() { size(windowX,windowY); }
  6. void draw() {
  7.   background(150);
  8.   player.update();
  9.   player.render();
  10.   player.moveself();
  11.   testGrav.render();
  12.   testGrav.affect(player);
  13. }
  14.  
  15. class Rectangle {
  16.   PVector pos;
  17.  
  18.   int h;
  19.   int w;
  20.  
  21.   int r;
  22.   int g;
  23.   int b;
  24.  
  25.   Rectangle (PVector inpos, int inw, int inh, int inr, int ing, int inb) { //define with a vector
  26.     pos = inpos;
  27.     h = inh;
  28.     w = inw;
  29.     r = inr;
  30.     g = ing;
  31.     b = inb;
  32.   }
  33.  
  34.   void render () {
  35.     fill(r, g, b);
  36.     noStroke();
  37.     rect(pos.x, pos.y, w, h);
  38.   }
  39.   float getCX() { //get center-x
  40.     return pos.x-(w/2);
  41.   }
  42.   float getCY() { //get center-y
  43.     return pos.y-(h/2);
  44.   }
  45. }
  46.  
  47. class MotileRect extends Rectangle {
  48.   PVector speed; //in pixels per 1/10th second.
  49.   Trigger timer;
  50.   MotileRect(PVector inpos, int inw, int inh, int inr, int ing, int inb, PVector inspd) {
  51.     super(inpos, inw, inh, inr, ing, inb);
  52.     speed = inspd;
  53.     timer = new Trigger(10);
  54.   }
  55.   void update() {
  56.     while (timer.fires ()) {
  57.       pos.add(speed);
  58.     }
  59.   }
  60. }
  61.  
  62. class PlayerRect extends MotileRect {
  63.   PlayerRect(PVector inpos, int inw, int inh, int inr, int ing, int inb) {
  64.     super(inpos, inw, inh, inr, ing, inb, new PVector(0,0)); //start out speedless
  65.   }
  66.   void moveself() {
  67.     if (checkKey("Up")) {
  68.       speed.y -= .1;
  69.     }
  70.     if (checkKey("Down")) {
  71.       speed.y += .1;
  72.     }
  73.     if (checkKey("Left")) {
  74.       speed.x -= .1;
  75.     }
  76.     if (checkKey("Right")) {
  77.       speed.x += .1;
  78.     }
  79.   }
  80. }
  81.  
  82. class GravRect extends Rectangle {
  83.   float magnitude;
  84.   GravRect (PVector inpos, int inw, int inh, int inr, int ing, int inb, float inmag) {
  85.     super(inpos,inw,inh,inr,ing,inb);
  86.     magnitude = inmag;
  87.   }
  88.   void affect(MotileRect target) {
  89.     PVector effect = new PVector( magnitude/(pos.x-target.pos.x), magnitude/(pos.y-target.pos.y));
  90.     print(effect.x);
  91.     print(" , ");
  92.     println(effect.y);
  93.     target.speed.add(effect);
  94.   }
  95. }
  96.  
  97. boolean collDetect(Rectangle rect1, Rectangle rect2) {
  98.   if (rect1.pos.x+rect1.w < rect2.pos.x) {
  99.     return false;
  100.   }
  101.   if (rect1.pos.x > rect2.pos.x+rect2.w) {
  102.     return false;
  103.   }
  104.   if (rect1.pos.y+rect1.h < rect2.pos.y) {
  105.     return false;
  106.   }
  107.   if (rect1.pos.y > rect2.pos.y+rect2.h) {
  108.     return false;
  109.   }
  110.   return true;
  111. }
  112.  
  113. boolean[] keys = new boolean[526];
  114. boolean checkKey(String k) {
  115.   for (int i = 0; i < keys.length; i++) {
  116.     if (KeyEvent.getKeyText(i).toLowerCase().equals(k.toLowerCase())) {
  117.       return keys[i];
  118.     }
  119.   }
  120.   return false;
  121. }
  122. void keyPressed()
  123. {
  124.   keys[keyCode] = true;
  125.   //println(KeyEvent.getKeyText(keyCode));
  126. }
  127.  
  128. void keyReleased()
  129. {
  130.   keys[keyCode] = false;
  131. }
  132.  
  133. class Trigger { //by kritzikratzi
  134.   long start = millis();
  135.   int rate;
  136.  
  137.   public Trigger( int rate ) {
  138.     this( rate, false );
  139.   }
  140.   /**
  141.    * additional boolean parameter to indicate whether the trigger
  142.    * should fire immediately
  143.    */
  144.   public Trigger( int rate, boolean immediately) {
  145.     setRate( rate );
  146.     if ( immediately ) start -= rate;
  147.   }
  148.  
  149.   /**
  150.    * changes the rate at which the trigger fires in milliseconds
  151.    */
  152.   public void setRate( int rate ) {
  153.     this.rate = rate;
  154.   }
  155.   public int getRate() {
  156.     return rate;
  157.   }
  158.   /**
  159.    * returns true if the trigger has fired, false otherwise
  160.    */
  161.   public boolean fires() {
  162.     if ( millis() - start >= rate ) {
  163.       start += rate;
  164.       return true;
  165.     }
  166.     else {
  167.       return false;
  168.     }
  169.   }
  170.   /**
  171.    * resets the timer,
  172.    * next trigger will occur in _rate_ milliseconds.  
  173.    */
  174.   public void reset() {
  175.     start = millis();
  176.   }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement