SHARE
TWEET

Untitled

a guest Dec 13th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Unicorn m;
  2. Platform p;
  3. Platform [] platforms;
  4. Enemy [] enemies;
  5.  
  6. String gameState;
  7. PImage spriteImages [];
  8.  
  9. int frames;
  10. int health;
  11. int pHealth;
  12. int a; // health warning alpha value
  13. int enemiesDispatched;
  14.  
  15. boolean left, right, up, down, space;
  16. PImage img;
  17. FrameObject camera, gameWorld;
  18. ImageObject backImage;
  19.  
  20. //Timer firingTimer;
  21. //fireball [] fireballs;
  22. int nextFireball;
  23.  
  24.  
  25. void setup() {
  26.   size (800, 600); //w,h
  27.   frameRate(60);
  28.  
  29.  
  30.   left = false;
  31.   right = false;
  32.   down = false;
  33.   up = false;
  34.   space = false;
  35.  
  36.   img = loadImage("data/map/background0001.png");
  37.   backImage = new ImageObject(0, 0, 1024, 768, img);
  38.   gameWorld = new FrameObject(0, 0, backImage.w*2, backImage.h);
  39.   camera = new FrameObject(0, 0, width, height-100); // shift up
  40.  
  41.   camera.x = (gameWorld.x + gameWorld.w/2) - camera.w/2;
  42.   camera.y = (gameWorld.y + gameWorld.h/2) - camera.h/2;
  43.  
  44.  
  45.  
  46.  
  47.   //character values
  48.   m = new Unicorn();
  49.   frames = 32;
  50.   spriteImages = new PImage[frames];
  51.   for (int i = 0; i<frames; i++) {
  52.     spriteImages[i]=loadImage("data/characters/mallow"+nf(i+1, 4)+".gif");
  53.   }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.   //platform values
  60.  
  61.   // p = new Platform(300, 460, 200, 25, "safe");
  62.   platforms = new Platform[8];
  63.   platforms[0] = new Platform(0, gameWorld.h, gameWorld.w, 25, "safe");
  64.   platforms[1] = new Platform(300, 150, 200, 25, "safe");
  65.   platforms[2] = new Platform(824, 150, 200, 25, "safe");
  66.   platforms[4] = new Platform(600, 300, 200, 25, "safe");
  67.   platforms[5] = new Platform(824, 450, 200, 25, "safe");
  68.   platforms[6] = new Platform(0, 600, 200, 25, "safe");
  69.   platforms[7] = new Platform(600, 600, 200, 25, "safe");
  70.  
  71.  
  72.  
  73.   //enemy values
  74.   //enemies = new Enemy[platforms.length-1];
  75.  // for (int i = 1; i < platforms.length; i++) {
  76.   //  enemies[i-1] = new Enemy(platforms[i]);
  77. //  }
  78.  
  79.   health = 100;
  80.   pHealth = health;
  81.  
  82.  
  83.  
  84. /*
  85.   fireballs = new fireball [10];
  86.   for (int i = 1; i < fireballs.length; i++) {
  87.     fireballs[i] = new fireball();
  88.   }
  89.   nextfireball = 0;
  90.  
  91.   firingTimer = new Timer(1500);
  92.   firingTimer.start(); //shift to state of function once states are in
  93.  
  94. */
  95.   a = 0;
  96.   gameState = "START";
  97. }
  98.  
  99. void draw() {
  100.   background (255);
  101.   m.update();
  102.   // m.collisionSide = rectangleCollisions(m, p);
  103.   // m.display();
  104.   // p.display();
  105.  
  106.   for (int i = 0; i < platforms.length; ++i) {
  107.     m.collisionSide = rectangleCollisions(m, platforms[i]);
  108.  
  109.     if (m.collisionSide !="none" && platforms[i].typeof == "death") {
  110.       health -= 1;//lower health when on bad platform
  111.     }
  112.     //change color when on the ground
  113.     if (pHealth > health) {
  114.       m.c = color(255, 0, 0); // tints red if you get hurt
  115.     } else {
  116.       m.c = color(255);
  117.     }
  118.     m.checkPlatforms();
  119.   }
  120.  
  121.  
  122.   //enemies
  123.   for (int i = 0; i < enemies.length; ++i) {
  124.     enemies[i].update();
  125.     if (rectangleCollisionsPVE(m, enemies[i]) && !enemies[i].isdead) {
  126.       enemies[i].deathJump();
  127.       health -= 25;
  128.     }
  129.   }
  130. /*
  131.   //fireballs
  132.   if (space) {
  133.     if (firingTimer.complete()) {
  134.       fireballs[nextfireball].fire(m.x, m.y, m.w, m.facingRight);
  135.       nextfireball = (nextfireball+1)%fireballs.length;
  136.       fireingTimer.start();
  137.     }
  138.   }
  139.   for (int i = 0; i < fireballs.length; ++i) {
  140.     fireballs[i].update();
  141.     for (int j = 0; j < enemies.length; ++j) {
  142.       if (rectangleCollisionsFVE(fireballs[i], enemies[j]) && !enemies[j].isdead) {
  143.         enemies[j].deathjump();
  144.         fireballs[i].reset();
  145.        // health +=20;
  146.       }
  147.     }
  148.   }
  149. */
  150.  
  151.  
  152.  
  153.   //move the camera
  154.   camera.x = floor(m.x + (m.halfWidth) - (camera.w /2));
  155.   camera.y = floor(m.y + (m.halfHeight) - (camera.h /2));
  156.  
  157.  
  158.  
  159.  
  160.   //keep camera inside gameworld boundaries
  161.  
  162.   if (camera.x < gameWorld.x) {
  163.     camera.x = gameWorld.x;
  164.   }
  165.   if (camera.y < gameWorld.y) {
  166.     camera.y = gameWorld.y;
  167.   }
  168.   if (camera.x + camera.w > gameWorld.x + gameWorld.w) {
  169.     camera.x = gameWorld.x + gameWorld.w - camera.w;
  170.   }
  171.   if (camera.y + camera.h > gameWorld.h) {
  172.     camera.y = gameWorld.h - camera.h;
  173.   }
  174.  
  175.   pushMatrix();
  176.   translate(-camera.x, -camera.y+100);//shift downward for UI
  177.  
  178.   backImage.display();
  179.   m.display();
  180.   for (int i = 0; i < platforms.length; ++i) {
  181.     platforms[i].display();
  182.   }
  183.   for (int i = 0; i < enemies.length; ++i) {
  184.     enemies[i].display();
  185.   }
  186.  
  187.   popMatrix();
  188.   displayPositionData();
  189.   //displayUI();
  190.   if (m.y >=platforms[0].y - m.h) { // strobe effect if on ground
  191.     fill(255, 0, 0, a*8);  //fill color of alpha red
  192.     rect(0, 100, camera.w, camera.h);
  193.     a= (a+1)%20;
  194.   }
  195.   // if (health<=0;){
  196.   //gameState = "lose";
  197.   //}
  198. }
  199.  
  200.  
  201.  
  202. boolean rectangleCollisionsPVE(Unicorn r1, Enemy r2) {
  203.   //r1 is the player
  204.   //r2 is the enemy
  205.  
  206.  
  207.   float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2);
  208.   float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2);
  209.  
  210.   float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
  211.   float combinedHalfHeights = r1.halfHeight + r2.halfHeight;
  212.  
  213.   if (abs(dx) < combinedHalfWidths) {
  214.     //collision has happended on the x axis
  215.     //now check y
  216.     if (abs(dy) < combinedHalfHeights) {
  217.       //collision detected
  218.       //determine overlap
  219.       return true;
  220.     }
  221.   }
  222.   return false;
  223. }
  224.  
  225. boolean rectangleCollisionsFVE(Fireball r1, Enemy r2) {
  226.   //r1 is the player
  227.   //r2 is the enemy
  228.  
  229.  
  230.   float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2);
  231.   float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2);
  232.  
  233.   float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
  234.   float combinedHalfHeights = r1.halfHeight + r2.halfHeight;
  235.  
  236.   if (abs(dx) < combinedHalfWidths) {
  237.     //collision has happended on the x axis
  238.     //now check y
  239.     if (abs(dy) < combinedHalfHeights) {
  240.       //collision detected
  241.       //determine overlap
  242.       return true;
  243.     }
  244.   }
  245.   return false;
  246. }
  247.  
  248.  
  249. String rectangleCollisions(Unicorn r1, Platform r2) {
  250.   //r1 is the player
  251.   //r2 is the platform
  252.   //function returns string collisionside
  253.   //allow to pass through platforms
  254.   //disable if you want to bounce off bottom of platforms
  255.   if (r1.vy < 0) {  
  256.     return "none";
  257.   }
  258.  
  259.   float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2);
  260.   float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2);
  261.  
  262.   float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
  263.   float combinedHalfHeights = r1.halfHeight + r2.halfHeight;
  264.  
  265.   if (abs(dx) < combinedHalfWidths) {
  266.     //collision has happended on the x axis
  267.     //now check y
  268.     if (abs(dy) < combinedHalfHeights) {
  269.       //collision detected
  270.       //determine overlap
  271.       float overlapX = combinedHalfWidths - abs(dx);
  272.       float overlapY = combinedHalfHeights - abs(dy);
  273.       //collision is on axis with smallest overlap
  274.  
  275.       if (overlapX >= overlapY) {
  276.         if (dy > 0 ) {
  277.           //move rectangle back to eliminate overlap
  278.           //before calling display
  279.           //to prevent drawing inside eachother
  280.           r1.y+= overlapY;
  281.           return "top";
  282.         } else {
  283.           r1.y-=overlapY;
  284.           return "bottom";
  285.         }
  286.       } else {
  287.         if (dx > 0) {
  288.           r1.x += overlapX;
  289.           return "left";
  290.         } else {
  291.           r1.x -= overlapX;
  292.           return "right";
  293.         }
  294.       }
  295.     } else {
  296.       //collisions failed on y axis
  297.       return "none";
  298.     }
  299.   } else {
  300.     //collisions failed on x axis
  301.     return "none";
  302.   }
  303. }
  304.  
  305.  
  306. void displayUI() {
  307.   fill(0);
  308.   rect(0, 0, width, 100);
  309.   //String s = "\nvx: "+m.vx+"  vy: ";
  310.   //text(s,50,50);
  311.   fill(255);
  312.   //String s = "cs: "+m.collisionSide + "\nhealth: " + floor(health) + "\niog: " +
  313.  
  314.   //draw health bar
  315.   fill(255, 0, 0);
  316.   rect(20, 10, health * 5, 10);
  317. }
  318.  
  319.  
  320. void resetGame() {
  321.   //for (int i = 1; i < platforms.length; i++) {
  322.   //  enemies[i - 1].reset(platforms[i]);
  323.   //  }
  324.   m.reset();
  325.   //score = 0;
  326.   enemiesDispatched = 0;
  327.   health = 100;
  328. }
  329.  
  330.  
  331.  
  332.  
  333. void displayPositionData() {
  334.   fill(0);
  335.   String s = "\nvx: "+m.vx+" vy: " +m.vy +
  336.     "\ncollisionSide: "+m.collisionSide;
  337.   text(s, 50, 50);
  338. }
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345. void keyPressed() {
  346.   switch (keyCode) {
  347.   case 37: // left
  348.     left = true;
  349.     break;
  350.   case 39: // right
  351.     right = true;
  352.     break;
  353.   case 38: // up
  354.     up = true;
  355.     break;
  356.   case 40: // down
  357.     down = true;
  358.     break;
  359.   case 32: // space
  360.     space = true;
  361.     break;
  362.   }
  363. }
  364.  
  365. void keyReleased() {
  366.   switch (keyCode) {
  367.   case 37: // left
  368.     left = false;
  369.     break;
  370.   case 39: // right
  371.     right = false;
  372.     break;
  373.   case 38: // up
  374.     up = false;
  375.     break;
  376.   case 40: // down
  377.     down = false;
  378.     break;
  379.   case 32: // space
  380.     space = false;
  381.     break;
  382.   }
  383. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top