Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PlayerRect player = new PlayerRect(new PVector(40, 94), 5, 5, 0, 0, 255);
- GravRect testGrav = new GravRect(new PVector(400,400),10,10, 255, 0, 0,.1);
- final int windowX = 800;
- final int windowY = 600;
- void setup() { size(windowX,windowY); }
- void draw() {
- background(150);
- player.update();
- player.render();
- player.moveself();
- testGrav.render();
- testGrav.affect(player);
- }
- class Rectangle {
- PVector pos;
- int h;
- int w;
- int r;
- int g;
- int b;
- Rectangle (PVector inpos, int inw, int inh, int inr, int ing, int inb) { //define with a vector
- pos = inpos;
- h = inh;
- w = inw;
- r = inr;
- g = ing;
- b = inb;
- }
- void render () {
- fill(r, g, b);
- noStroke();
- rect(pos.x, pos.y, w, h);
- }
- float getCX() { //get center-x
- return pos.x-(w/2);
- }
- float getCY() { //get center-y
- return pos.y-(h/2);
- }
- }
- class MotileRect extends Rectangle {
- PVector speed; //in pixels per 1/10th second.
- Trigger timer;
- MotileRect(PVector inpos, int inw, int inh, int inr, int ing, int inb, PVector inspd) {
- super(inpos, inw, inh, inr, ing, inb);
- speed = inspd;
- timer = new Trigger(10);
- }
- void update() {
- while (timer.fires ()) {
- pos.add(speed);
- }
- }
- }
- class PlayerRect extends MotileRect {
- PlayerRect(PVector inpos, int inw, int inh, int inr, int ing, int inb) {
- super(inpos, inw, inh, inr, ing, inb, new PVector(0,0)); //start out speedless
- }
- void moveself() {
- if (checkKey("Up")) {
- speed.y -= .1;
- }
- if (checkKey("Down")) {
- speed.y += .1;
- }
- if (checkKey("Left")) {
- speed.x -= .1;
- }
- if (checkKey("Right")) {
- speed.x += .1;
- }
- }
- }
- class GravRect extends Rectangle {
- float magnitude;
- GravRect (PVector inpos, int inw, int inh, int inr, int ing, int inb, float inmag) {
- super(inpos,inw,inh,inr,ing,inb);
- magnitude = inmag;
- }
- void affect(MotileRect target) {
- PVector effect = new PVector( magnitude/(pos.x-target.pos.x), magnitude/(pos.y-target.pos.y));
- print(effect.x);
- print(" , ");
- println(effect.y);
- target.speed.add(effect);
- }
- }
- boolean collDetect(Rectangle rect1, Rectangle rect2) {
- if (rect1.pos.x+rect1.w < rect2.pos.x) {
- return false;
- }
- if (rect1.pos.x > rect2.pos.x+rect2.w) {
- return false;
- }
- if (rect1.pos.y+rect1.h < rect2.pos.y) {
- return false;
- }
- if (rect1.pos.y > rect2.pos.y+rect2.h) {
- return false;
- }
- return true;
- }
- boolean[] keys = new boolean[526];
- boolean checkKey(String k) {
- for (int i = 0; i < keys.length; i++) {
- if (KeyEvent.getKeyText(i).toLowerCase().equals(k.toLowerCase())) {
- return keys[i];
- }
- }
- return false;
- }
- void keyPressed()
- {
- keys[keyCode] = true;
- //println(KeyEvent.getKeyText(keyCode));
- }
- void keyReleased()
- {
- keys[keyCode] = false;
- }
- class Trigger { //by kritzikratzi
- long start = millis();
- int rate;
- public Trigger( int rate ) {
- this( rate, false );
- }
- /**
- * additional boolean parameter to indicate whether the trigger
- * should fire immediately
- */
- public Trigger( int rate, boolean immediately) {
- setRate( rate );
- if ( immediately ) start -= rate;
- }
- /**
- * changes the rate at which the trigger fires in milliseconds
- */
- public void setRate( int rate ) {
- this.rate = rate;
- }
- public int getRate() {
- return rate;
- }
- /**
- * returns true if the trigger has fired, false otherwise
- */
- public boolean fires() {
- if ( millis() - start >= rate ) {
- start += rate;
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * resets the timer,
- * next trigger will occur in _rate_ milliseconds.
- */
- public void reset() {
- start = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement