Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unicorn m;
- Platform p;
- Platform [] platforms;
- Enemy [] enemies;
- String gameState;
- PImage spriteImages [];
- int frames;
- int health;
- int pHealth;
- int a; // health warning alpha value
- int enemiesDispatched;
- boolean left, right, up, down, space;
- PImage img;
- FrameObject camera, gameWorld;
- ImageObject backImage;
- //Timer firingTimer;
- //fireball [] fireballs;
- int nextFireball;
- void setup() {
- size (800, 600); //w,h
- frameRate(60);
- left = false;
- right = false;
- down = false;
- up = false;
- space = false;
- img = loadImage("data/map/background0001.png");
- backImage = new ImageObject(0, 0, 1024, 768, img);
- gameWorld = new FrameObject(0, 0, backImage.w*2, backImage.h);
- camera = new FrameObject(0, 0, width, height-100); // shift up
- camera.x = (gameWorld.x + gameWorld.w/2) - camera.w/2;
- camera.y = (gameWorld.y + gameWorld.h/2) - camera.h/2;
- //character values
- m = new Unicorn();
- frames = 32;
- spriteImages = new PImage[frames];
- for (int i = 0; i<frames; i++) {
- spriteImages[i]=loadImage("data/characters/mallow"+nf(i+1, 4)+".gif");
- }
- //platform values
- // p = new Platform(300, 460, 200, 25, "safe");
- platforms = new Platform[8];
- platforms[0] = new Platform(0, gameWorld.h, gameWorld.w, 25, "safe");
- platforms[1] = new Platform(300, 150, 200, 25, "safe");
- platforms[2] = new Platform(824, 150, 200, 25, "safe");
- platforms[4] = new Platform(600, 300, 200, 25, "safe");
- platforms[5] = new Platform(824, 450, 200, 25, "safe");
- platforms[6] = new Platform(0, 600, 200, 25, "safe");
- platforms[7] = new Platform(600, 600, 200, 25, "safe");
- //enemy values
- //enemies = new Enemy[platforms.length-1];
- // for (int i = 1; i < platforms.length; i++) {
- // enemies[i-1] = new Enemy(platforms[i]);
- // }
- health = 100;
- pHealth = health;
- /*
- fireballs = new fireball [10];
- for (int i = 1; i < fireballs.length; i++) {
- fireballs[i] = new fireball();
- }
- nextfireball = 0;
- firingTimer = new Timer(1500);
- firingTimer.start(); //shift to state of function once states are in
- */
- a = 0;
- gameState = "START";
- }
- void draw() {
- background (255);
- m.update();
- // m.collisionSide = rectangleCollisions(m, p);
- // m.display();
- // p.display();
- for (int i = 0; i < platforms.length; ++i) {
- m.collisionSide = rectangleCollisions(m, platforms[i]);
- if (m.collisionSide !="none" && platforms[i].typeof == "death") {
- health -= 1;//lower health when on bad platform
- }
- //change color when on the ground
- if (pHealth > health) {
- m.c = color(255, 0, 0); // tints red if you get hurt
- } else {
- m.c = color(255);
- }
- m.checkPlatforms();
- }
- //enemies
- for (int i = 0; i < enemies.length; ++i) {
- enemies[i].update();
- if (rectangleCollisionsPVE(m, enemies[i]) && !enemies[i].isdead) {
- enemies[i].deathJump();
- health -= 25;
- }
- }
- /*
- //fireballs
- if (space) {
- if (firingTimer.complete()) {
- fireballs[nextfireball].fire(m.x, m.y, m.w, m.facingRight);
- nextfireball = (nextfireball+1)%fireballs.length;
- fireingTimer.start();
- }
- }
- for (int i = 0; i < fireballs.length; ++i) {
- fireballs[i].update();
- for (int j = 0; j < enemies.length; ++j) {
- if (rectangleCollisionsFVE(fireballs[i], enemies[j]) && !enemies[j].isdead) {
- enemies[j].deathjump();
- fireballs[i].reset();
- // health +=20;
- }
- }
- }
- */
- //move the camera
- camera.x = floor(m.x + (m.halfWidth) - (camera.w /2));
- camera.y = floor(m.y + (m.halfHeight) - (camera.h /2));
- //keep camera inside gameworld boundaries
- if (camera.x < gameWorld.x) {
- camera.x = gameWorld.x;
- }
- if (camera.y < gameWorld.y) {
- camera.y = gameWorld.y;
- }
- if (camera.x + camera.w > gameWorld.x + gameWorld.w) {
- camera.x = gameWorld.x + gameWorld.w - camera.w;
- }
- if (camera.y + camera.h > gameWorld.h) {
- camera.y = gameWorld.h - camera.h;
- }
- pushMatrix();
- translate(-camera.x, -camera.y+100);//shift downward for UI
- backImage.display();
- m.display();
- for (int i = 0; i < platforms.length; ++i) {
- platforms[i].display();
- }
- for (int i = 0; i < enemies.length; ++i) {
- enemies[i].display();
- }
- popMatrix();
- displayPositionData();
- //displayUI();
- if (m.y >=platforms[0].y - m.h) { // strobe effect if on ground
- fill(255, 0, 0, a*8); //fill color of alpha red
- rect(0, 100, camera.w, camera.h);
- a= (a+1)%20;
- }
- // if (health<=0;){
- //gameState = "lose";
- //}
- }
- boolean rectangleCollisionsPVE(Unicorn r1, Enemy r2) {
- //r1 is the player
- //r2 is the enemy
- float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2);
- float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2);
- float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
- float combinedHalfHeights = r1.halfHeight + r2.halfHeight;
- if (abs(dx) < combinedHalfWidths) {
- //collision has happended on the x axis
- //now check y
- if (abs(dy) < combinedHalfHeights) {
- //collision detected
- //determine overlap
- return true;
- }
- }
- return false;
- }
- boolean rectangleCollisionsFVE(Fireball r1, Enemy r2) {
- //r1 is the player
- //r2 is the enemy
- float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2);
- float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2);
- float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
- float combinedHalfHeights = r1.halfHeight + r2.halfHeight;
- if (abs(dx) < combinedHalfWidths) {
- //collision has happended on the x axis
- //now check y
- if (abs(dy) < combinedHalfHeights) {
- //collision detected
- //determine overlap
- return true;
- }
- }
- return false;
- }
- String rectangleCollisions(Unicorn r1, Platform r2) {
- //r1 is the player
- //r2 is the platform
- //function returns string collisionside
- //allow to pass through platforms
- //disable if you want to bounce off bottom of platforms
- if (r1.vy < 0) {
- return "none";
- }
- float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2);
- float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2);
- float combinedHalfWidths = r1.halfWidth + r2.halfWidth;
- float combinedHalfHeights = r1.halfHeight + r2.halfHeight;
- if (abs(dx) < combinedHalfWidths) {
- //collision has happended on the x axis
- //now check y
- if (abs(dy) < combinedHalfHeights) {
- //collision detected
- //determine overlap
- float overlapX = combinedHalfWidths - abs(dx);
- float overlapY = combinedHalfHeights - abs(dy);
- //collision is on axis with smallest overlap
- if (overlapX >= overlapY) {
- if (dy > 0 ) {
- //move rectangle back to eliminate overlap
- //before calling display
- //to prevent drawing inside eachother
- r1.y+= overlapY;
- return "top";
- } else {
- r1.y-=overlapY;
- return "bottom";
- }
- } else {
- if (dx > 0) {
- r1.x += overlapX;
- return "left";
- } else {
- r1.x -= overlapX;
- return "right";
- }
- }
- } else {
- //collisions failed on y axis
- return "none";
- }
- } else {
- //collisions failed on x axis
- return "none";
- }
- }
- void displayUI() {
- fill(0);
- rect(0, 0, width, 100);
- //String s = "\nvx: "+m.vx+" vy: ";
- //text(s,50,50);
- fill(255);
- //String s = "cs: "+m.collisionSide + "\nhealth: " + floor(health) + "\niog: " +
- //draw health bar
- fill(255, 0, 0);
- rect(20, 10, health * 5, 10);
- }
- void resetGame() {
- //for (int i = 1; i < platforms.length; i++) {
- // enemies[i - 1].reset(platforms[i]);
- // }
- m.reset();
- //score = 0;
- enemiesDispatched = 0;
- health = 100;
- }
- void displayPositionData() {
- fill(0);
- String s = "\nvx: "+m.vx+" vy: " +m.vy +
- "\ncollisionSide: "+m.collisionSide;
- text(s, 50, 50);
- }
- void keyPressed() {
- switch (keyCode) {
- case 37: // left
- left = true;
- break;
- case 39: // right
- right = true;
- break;
- case 38: // up
- up = true;
- break;
- case 40: // down
- down = true;
- break;
- case 32: // space
- space = true;
- break;
- }
- }
- void keyReleased() {
- switch (keyCode) {
- case 37: // left
- left = false;
- break;
- case 39: // right
- right = false;
- break;
- case 38: // up
- up = false;
- break;
- case 40: // down
- down = false;
- break;
- case 32: // space
- space = false;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement