• fullscreen
  • bullet.pde
  • drop.pde
  • enemy.pde
  • globals.pde
  • gun.pde
  • misc.pde
  • mygame.pde
  • player.pde
  • states.pde
  • zombieslocal.pde
  • class Bullet {
      /**** Fields ****/
      Player owner;
      PVector location;
      PVector trajectory;
      float speed;
      int damage;
      int breadth;
      color colNorm;
      color colCur;
      
      /**** Constructors ****/
      Bullet(Player tempOwner) {
        owner = tempOwner;
        location = owner.location.get();
        breadth = 10;
        speed = 15.0;
        damage = 1;
        trajectory = PVector.sub(owner.mouseLoc, location);
        trajectory.normalize();
        trajectory.mult(speed);
        colNorm = color(25, 75, 200);
        colCur = colNorm;
      }
      
      /**** Methods ****/
      void travel() {
        // Bullet is moved along the trajectory
        location.add(trajectory);
      }
      
      void stillAlive() {
        // Delete bullet if it goes off screen.
        // Also, a cheesy Portal reference.
        // http://www.youtube.com/watch?v=8IGS9qY7xko
        // Also, thanks amnon.owned from the Processing forums
        // for the refactored code tip!
        if (location.y > height || location.y < 0 || location.x > width || location.x < 0) {
          destroy();
        }
      }
      
      void hasHit() {
        for (int i = 0; i < theEnemies.size(); i++) {
          Enemy e = theEnemies.get(i);
          float distance = dist(location.x, location.y, e.location.x, e.location.y);
         
          if (distance < breadth/2 + e.breadth/2) {
            e.takeDamage(this);
            destroy();
            break;
          }
        }
      }
      
      void destroy() {
        theBullets.remove(this);
      }
      
      
      /**** Update and Display ****/
      void update() {
        travel();
        stillAlive();
        hasHit();
      }
      
      void display() {
        pushMatrix();
        ellipseMode(CENTER);
        fill(colCur);
        ellipse(location.x, location.y, breadth, breadth);
        noFill();
        popMatrix();
      }
      
    }
    
    class Drop {
      PVector location;
      int breadth;
      color colNorm;
      int type;
      
      Drop(int i) {
        location = new PVector(int(random(width)), int(random(height)));
        breadth = 10;
        type = i;
        
        switch(i) {
          // Health pack.
          case(0):
            colNorm = color(255, 118, 246); break;
          // SMG
          case(1):
            colNorm = color(255, 255, 0); break;
        }
      }
      
      void hasHit() {
        for (int i = 0; i < thePlayers.size(); i++) {
          Player p = thePlayers.get(i);
          float distance = dist(location.x, location.y, p.location.x, p.location.y);
          
          if (distance < breadth/2 + p.breadth/2) {
            giveEffect(p);
            destroy();
            break;
          }
        }
      }
      
      void giveEffect(Player p) {
        switch(type) {
          case(0): p.heal(3); break;
          case(1): p.weapon = new SMG(p); break;
        }
      }
      
      void destroy() {
        theDrops.remove(this);
      }
      
      void update() {
        hasHit();
      }
      
      void display() {
        fill(colNorm);
        ellipse(location.x, location.y, breadth, breadth);
        noFill();
      }
      
    }
    
    class Enemy {
      /**** Fields ****/
      PVector location;
      PVector trajectory;
      Player target;
      float speed;
      float speedAcc;
      int healthMax;
      int healthCur;
      int damage;
      int breadth;
      Timer hitTimer;
      color colNorm;
      color colHit;
      color colCur;
      
      /**** Constructors ****/
      Enemy() {
        location = generateSpawn();
        target = getTarget();
        speed = 1;
        breadth = 18;
        healthMax = 2;
        healthCur = healthMax;
        damage = 1;
        hitTimer = new Timer(25);
        colNorm = color(125, 75, 100);
        colHit = color(255, 0, 0);
        colCur = colNorm;
      }
      
      /**** Methods ****/
      void walk() {
        if (gamestate == 1) {
          // Enemy walks towards player.
          trajectory = PVector.sub(target.location, location);
          trajectory.normalize();
          trajectory.mult(speed);
        }
        location.add(trajectory);
      }
      
      void hasHit() {
        for (int i = 0; i < thePlayers.size(); i++) {
          Player p = thePlayers.get(i);
          
          if (p.alive == true) {
            float distance = dist(location.x, location.y, p.location.x, p.location.y);
            
            // If there is a collision...
            if (distance < breadth/2 + p.breadth/2) {
              p.takeDamage(this);
              destroy();
              break;
            }
          }
        }
      }
      
      void takeDamage(Bullet b) {
        healthCur -= b.damage;
        if (healthCur <= 0) {
          destroy();
          b.owner.score++;
        } else {
          // Flash colors
          colCur = colHit;
          hitTimer.start();
        }
      }
      
      void colChange() {
        if (colCur == colHit && hitTimer.isFinished()) {
          colCur = colNorm;
        }
      }
      
      void destroy() {
        theEnemies.remove(this);
      }
      
      void shamble() {
        this.speed = random(0.4, 1);
        trajectory.set(random(-50, 50), random(-50, 50), 0);
        trajectory.normalize();
        trajectory.mult(speed);
      }
      
      PVector generateSpawn() {
        int side;
        int spawnX = 0;
        int spawnY = 0;
        PVector spawnCoords;
        
        side = int(random(4));
        switch (side) {
          case(0): 
            spawnX = int(random(width));
            spawnY = 0;
            break;
          case(1): 
            spawnX = width;
            spawnY = int(random(height));
            break;
          case(2): 
            spawnX = int(random(width));
            spawnY = height;
            break;
          case(3): 
            spawnX = 0;
            spawnY = int(random(height));
            break;
        }
        
        return new PVector(spawnX, spawnY);
      }
      
      Player getTarget() {
        int targetId;
        
        targetId = int(random(thePlayers.size()));
        
        return thePlayers.get(targetId);
      }
      
      /**** Update and Display ****/
      void update() {
        walk();
        hasHit();
        colChange();
      }
      
      void display() {
        // Draw the enemy.
        pushMatrix();
        ellipseMode(CENTER);
        fill(colCur);
        ellipse(location.x, location.y, breadth, breadth);
        popMatrix();
        
        // Show health as number
        fill(colNorm);
        textAlign(CENTER);
        text(healthCur, location.x, location.y - 25);
        
        // Health Bar!
        pushMatrix();
        stroke(0);
        strokeWeight(1);
        fill(255, 0, 0);
        rect(location.x - 25, location.y - 20, 50, 5);
        stroke(0, 255);
        fill(0, 255, 0);
        rect(location.x - 25, location.y - 20, map(healthCur, 0, healthMax, 0, 50), 5);
        noFill();
        popMatrix();
      }  
    }
    
    
    
    
    /**** Alternate Enemies ****/
    
    class FastEnemy extends Enemy {
      /**** Constructors ****/
      FastEnemy() {
        location = generateSpawn();
        speed = 1.9;
        breadth = 18;
        healthMax = 1;
        healthCur = healthMax;
        damage = 1;
        colNorm = color(25, 255, 25);
        colHit = color(255, 0, 0);
        colCur = colNorm;
      }
    }
    
    
    class BossEnemy extends Enemy {
      /**** Constructors ****/
      BossEnemy() {
        location = generateSpawn();
        speed = 1.8;
        breadth = 125;
        healthMax = 200;
        healthCur = healthMax;
        damage = 100;
        colNorm = color(255, 0, 0);
        colHit = color(255, 0, 0);
        colCur = colNorm;
      }
    }
    
    // Sets all globals to their defaults; handy for restarting.
    void initialize() {
      background(50);
      debug = false;
      gamestate = 1;
      spawnInterval = 0.0;
      spawnSpeed = 30.0;
      spawnModifier = 0.05;
      dropInterval = 0.0;
      dropSpeed = 800.0;
      dropModifier = 30;
      screenFade = 0.0;
      myPlayer = new Player();
      thePlayers = new ArrayList<Player>();
      thePlayers.add(myPlayer);
      theBullets = new ArrayList<Bullet>();
      theEnemies = new ArrayList<Enemy>();
      theDrops = new ArrayList<Drop>();
    }
    
    // Creates enemies on an interval which decreases slowly.
    // Should be a static method of "enemy" but Processing
    // doesn't like static methods :(
    void enemySpawn() {
      if (spawnInterval < spawnSpeed) {
        spawnInterval++;
      } else {
        spawnInterval = 0;
        if (spawnSpeed > 15) {
          spawnSpeed -= spawnModifier;
        } else {
          spawnSpeed = 15;
        }
        
        int enemyType;
        enemyType = int(random(3));
        
        switch (enemyType) {
          case(0): theEnemies.add(new Enemy()); break;
          default: theEnemies.add(new FastEnemy()); break;
        }
      }
    }
    
    // Creates drops on an interval which decreases slowly.
    // Should be a static method of "drop" but Processing
    // doesn't like static methods :(
    void dropSpawn() {
      if (dropInterval < dropSpeed) {
        dropInterval++;
      } else {
        dropInterval = 0;
        if (dropSpeed > 300) {
          dropSpeed -= dropModifier;
        } else {
          dropSpeed = 300;
        }
        
        int dropType;
        dropType = int(random(3));
        
        switch (dropType) {
          case(0): theDrops.add(new Drop(1)); break;
          default: theDrops.add(new Drop(0)); break;
        }
      }
    }
    
    void shambleTheZombies() {
      for (int i = 0; i < theEnemies.size(); i++) {
        theEnemies.get(i).shamble();
      }
    }
    
    // Adds a circle on the cursor, a line from the player to mouse,
    // and some debug information at the top-left.
    void debug() {
      fill(255);
      textAlign(LEFT);
      text("FPS: " + int(frameRate), 5, 40);
      text("Drop Interval: " + dropInterval, 5, 55);
      text("Drop Speed: " + dropSpeed, 5, 70);
      text("Spawn Interval: " + spawnInterval, 5, 85);
      text("Spawn Speed: " + spawnSpeed, 5, 100);
      noFill();
      fill(0);
      /*
      // Draws a line from each enemy to it's target.
      for (int i = 0; i < theEnemies.size(); i++) {
        Enemy e = theEnemies.get(i);
        line(e.location.x, e.location.y, e.target.location.x, e.target.location.y);
      }
      */
      noFill();
    }
    
    class Gun {
      /**** Fields ****/
      Player owner;
      boolean firing;
      // Type: 0 = SemiAuto, 1 = FullyAuto
      byte type;
      
      
      /**** Constructors ****/
      Gun(Player tempOwner) {
        owner = tempOwner;
        firing = false;
        type = 0;
      }
      
      /**** Methods *****/
      void fire() {
        if (owner.alive) {
          if (firing) {
            theBullets.add(new Bullet(owner));
            firing = false;
          }
        }
      }
      
      void update() {
        fire();
      }
      
    }
    
    
    class Pistol extends Gun {
      /**** Constructors ****/
      Pistol(Player tempOwner) {
        super(tempOwner);
      }
    }
    
    
    class SMG extends Gun {
      /**** Fields ****/
      Timer fireRate;
      int ammoMax;
      int ammoCur;
      
      /**** Constructors ****/
      SMG(Player tempOwner) {
        super(tempOwner);
        type = 1;
        fireRate = new Timer(115);
        ammoMax = 100;
        ammoCur = ammoMax;
      }
      
      
      /**** Methods *****/
      // This fires the gun like an automatic weapon.
      void fire() {
        if (owner.alive) {
          if (firing && fireRate.isFinished() && ammoCur > 0) {
            // Fire off a bullet and reduce ammo count.
            theBullets.add(new Bullet(owner));
            ammoCur--;
            
            // If we have more ammo, restart the firing cooldown.
            // Otherwise, give the player back the pistol.
            if (ammoCur > 0) {
              fireRate.start();
            } else {
              owner.weapon = new Pistol(owner);
            }
          }
        }
      }
    }
    
    // Timer class; currently used for hit flashing.
    class Timer {
      int timeStarted;
      int timeLength;
      
      Timer(int tempTimeLength) {
        timeLength = tempTimeLength;
      }
      
      void start() {
        timeStarted = millis();
      }
      
      boolean isFinished() {
        int timePassed = millis() - timeStarted;
        
        if (timePassed >= timeLength) {
          return true;
        } else {
          return false;
        }
        
      }
    }
    
    //TODO: Implement networking.
    //TODO: Add guns, powerups, health drops.
    //TODO: Gameover feels really messy...
    //TODO: Make player zombie when killed (for effect).
    //TODO: Gun firing is odd for semi-auto. Temp fix in place.
    //TODO: Add movement acceleration to enemy and player.
    
    /**** Global Variables ****/
    Player myPlayer;
    ArrayList<Player> thePlayers;
    ArrayList<Bullet> theBullets;
    ArrayList<Enemy> theEnemies;
    ArrayList<Drop> theDrops;
    PVector mouseLoc;
    final static int NORTH = 1;
    final static int EAST = 2;
    final static int SOUTH = 4;
    final static int WEST = 8;
    // Not sure if these should be globals, but...
    float enemyInterval;
    float spawnSpeed;
    float spawnModifier;
    float screenFade;
    boolean gameover;
    
    void setup() {
      size(600, 480);
      frameRate(60);
      cursor(CROSS);
      initialize();
    }
    
    void draw() {
      /**** Miscellaneous ****/
      background(50);
      if (!gameover) {
        spawn();
      }
      
      /**** Updates ****/
      // Player
      for (int i = 0; i < thePlayers.size(); i++) {
        thePlayers.get(i).update();
      }
      
      // Bullets
      for (int i = 0; i < theBullets.size(); i++) {
        theBullets.get(i).update();
      }
      
      // Enemies
      for (int i = 0; i < theEnemies.size(); i++) {
        theEnemies.get(i).update();
      }
      
      // Drops
      for (int i = 0; i < theDrops.size(); i++) {
        theDrops.get(i).update();
      }
      
      
      
      /**** Displays ****/
      // Player
      for (int i = 0; i < thePlayers.size(); i++) {
        thePlayers.get(i).display();
      }
      
      // Bullets
      for (int i = 0; i < theBullets.size(); i++) {
        theBullets.get(i).display();
      }
      
      // Enemies
      for (int i = 0; i < theEnemies.size(); i++) {
        theEnemies.get(i).display();
      }
      
      // Drops
      for (int i = 0; i < theDrops.size(); i++) {
        theDrops.get(i).display();
      }
      
      if (gameover) {
        gameoverDraw();
      }
      
      if (myPlayer.alive) {
        debug();
      }
    }
    
    void keyPressed() {
      switch(key) {
        // For player direction.
        case('w'):case('W'):myPlayer.direction |=NORTH;break;
        case('d'):case('D'):myPlayer.direction |=EAST;break;
        case('s'):case('S'):myPlayer.direction |=SOUTH;break;
        case('a'):case('A'):myPlayer.direction |=WEST;break;
        // Enemy Spawning (For Debugging!)
        //case('z'): theEnemies.add(new Enemy());break;
        case('z'): enemyInterval = 100;break;
        // Restart game if it's over
        case('r'):case('R'): if (gameover) { initialize(); }break;
      }
    }
     
    void keyReleased() {
      // For player direction.
      switch(key) {
        case('w'):case('W'):myPlayer.direction ^=NORTH;break;
        case('d'):case('D'):myPlayer.direction ^=EAST;break;
        case('s'):case('S'):myPlayer.direction ^=SOUTH;break;
        case('a'):case('A'):myPlayer.direction ^=WEST;break;
      }
    }
    
    void mouseMoved() {
      mouseLoc.x = mouseX;
      mouseLoc.y = mouseY;
    }
    
    void mouseDragged() {
      mouseLoc.x = mouseX;
      mouseLoc.y = mouseY;
    }
    
    void mousePressed() {
      myPlayer.weapon.firing = true;
    }
    
    void mouseReleased() {
      // If the gun is an automatic, enable this.
      if (myPlayer.weapon.type == 1) {
        myPlayer.weapon.firing = false;
      }
    }
    
    class Player {
      /**** Fields ****/
      PVector location;
      PVector mouseLoc;
      int direction;
      float speed;
      int breadth;
      boolean alive;
      int healthMax;
      int healthCur;
      int score;
      Gun weapon;
      Timer hitTimer;
      color colNorm;
      color colHit;
      color colCur;
      
      
      /**** Constructors ****/
      Player() {
        location = new PVector(width/2, height/2);
        mouseLoc = new PVector(0,0);
        direction = 0;
        speed = 2;
        breadth = 20;
        alive = true;
        healthMax = 10;
        healthCur = healthMax;
        score = 0;
        weapon = new Pistol(this);
        hitTimer = new Timer(25);
        colNorm = color(200, 100, 0);
        colHit = color(255, 0, 0);
        colCur = colNorm;
      }
      
      /**** Methods ****/
      void walk() {
        switch (direction) {
          case NORTH: location.y -= speed; break;
          case WEST: location.x -= speed; break;
          case SOUTH: location.y += speed; break;
          case EAST: location.x += speed; break;
          /*
          // Normalized?
          case NORTH|WEST: location.y -= speed / 2; location.x -= speed / 2; break;
          case NORTH|EAST: location.y -= speed / 2; location.x += speed / 2; break;
          case SOUTH|WEST: location.y += speed / 2; location.x -= speed / 2; break;
          case SOUTH|EAST: location.y += speed / 2; location.x += speed / 2; break;
          */
          case NORTH|WEST: location.y -= speed; location.x -= speed; break;
          case NORTH|EAST: location.y -= speed; location.x += speed; break;
          case SOUTH|WEST: location.y += speed; location.x -= speed; break;
          case SOUTH|EAST: location.y += speed; location.x += speed; break;
          case NORTH|WEST|EAST: location.y -= speed; break;
          case SOUTH|WEST|EAST: location.y += speed; break;
          case NORTH|WEST|SOUTH: location.x -= speed; break;
          case NORTH|SOUTH|EAST: location.x += speed; break;
        }
        location.x = constrain(location.x, 0 + (breadth / 2), width - (breadth / 2));
        location.y = constrain(location.y, 0 + (breadth / 2), height - (breadth / 2));
      }
      
      void takeDamage(Enemy e) {
        healthCur = constrain(healthCur - e.damage, 0, healthMax);
        if (healthCur <= 0) {
          destroy();
        } else {
          // Flash colors
          colCur = colHit;
          hitTimer.start();
        }
      }
      
      void heal(int healAmt) {
        healthCur = constrain(healthCur + healAmt, 0, healthMax);
      }
      
      void colChange() {
        if (colCur == colHit && hitTimer.isFinished()) {
          colCur = colNorm;
        }
      }
      
      void destroy() {
        thePlayers.remove(this);
        alive = false;
        gamestate = 2;
        
        for (int i = 0; i < thePlayers.size(); i++) {
          Player p = thePlayers.get(i);
          
          if (p.alive == true) {
            gamestate = 1;
          }
        }
        
        if (gamestate == 2) {
          shambleTheZombies();
        } else {
          // If the game is still going, zombies choose a new target.
          for (int i = 0; i < theEnemies.size(); i++) {
            Enemy e = theEnemies.get(i);
            
            if (e.target == this) {
              e.target = e.getTarget();
            }
          } 
        }
          
      }
      
      
      /**** Update and Display ****/
      void update() {
        walk();
        colChange();
        weapon.update();
      }
      
      void display() {
        // Draw the player.
        fill(colCur);
        ellipse(location.x, location.y, breadth, breadth);
        
        // This code draws a rotating player - good for the future.
        /*
        pushMatrix();
        translate(location.x, location.y);
        rotate(atan2(mouseY-location.y, mouseX-location.x));
        fill(colCur);
        ellipse(0, 0, breadth, breadth);
        popMatrix();
        */
        
        
        // Show health as number.
        fill(colNorm);
        textAlign(CENTER);
        text(healthCur, location.x, location.y - 25);
        
        // Health Bar!
        pushMatrix();
        stroke(0);
        strokeWeight(1);
        fill(255, 0, 0);
        rect(location.x - 25, location.y - 20, 50, 5);
        stroke(0, 255);
        fill(0, 255, 0);
        rect(location.x - 25, location.y - 20, map(healthCur, 0, healthMax, 0, 50), 5);
        noFill();
        popMatrix();
        
        // Only show the score for our player at the top.
        if (this == myPlayer) {
          fill(255);
          textAlign(LEFT);
          text("Score: " + score, 5, 20);
          noFill();
        }
      }
      
    }
    
    void gameUpdate() {
      /**** Miscellaneous ****/
      background(50);
      if (gamestate == 1) {
        enemySpawn();
        dropSpawn();
      }
      
      /**** Updates ****/
      // Player
      for (int i = 0; i < thePlayers.size(); i++) {
        thePlayers.get(i).update();
      }
      
      // Bullets
      for (int i = 0; i < theBullets.size(); i++) {
        theBullets.get(i).update();
      }
      
      // Enemies
      for (int i = 0; i < theEnemies.size(); i++) {
        theEnemies.get(i).update();
      }
      
      // Drops
      for (int i = 0; i < theDrops.size(); i++) {
        theDrops.get(i).update();
      }
    }
    
    void gameDraw() {
      /**** Displays ****/
      // Player
      for (int i = 0; i < thePlayers.size(); i++) {
        thePlayers.get(i).display();
      }
      
      // Bullets
      for (int i = 0; i < theBullets.size(); i++) {
        theBullets.get(i).display();
      }
      
      // Enemies
      for (int i = 0; i < theEnemies.size(); i++) {
        theEnemies.get(i).display();
      }
      
      // Drops
      for (int i = 0; i < theDrops.size(); i++) {
        theDrops.get(i).display();
      }
    }
    
    void gameoverDraw() {
      // Draw a big transparent square over the window and
      // slowly make the square become less transparent.
      fill(0, 0, 0, screenFade);
      screenFade = constrain(screenFade + .8, 0, 255);
      rect(0, 0, width, height);
      noFill();
      // Write the gameover text.
      textAlign(CENTER);
      textSize(24);
      fill(255, 0, 0);
      text("BRAAAAAAINS!", width / 2, 50);
      noFill();
      textSize(16);
      text("Score: " + myPlayer.score, width / 2, 75);
      textSize(14);
      text("Press R to restart.", width / 2, 100);
      textSize(12);
    }
    
    //TODO: Implement states (Gameover, menus, etc.)
    //TODO: Implement networking.
    //TODO: HasHit should be a boolean (methinks).
    //TODO: Use a timer for drops and enemies.
    //TODO: Make player zombie when killed (for effect).
    //TODO: Add movement acceleration to enemy and player.
    
    /**** Global Variables ****/
    Player myPlayer;
    ArrayList<Player> thePlayers;
    ArrayList<Bullet> theBullets;
    ArrayList<Enemy> theEnemies;
    ArrayList<Drop> theDrops;
    final static int NORTH = 1;
    final static int EAST = 2;
    final static int SOUTH = 4;
    final static int WEST = 8;
    // Not sure if these should be globals, but...
    float spawnInterval;
    float spawnSpeed;
    float spawnModifier;
    float dropInterval;
    float dropSpeed;
    float dropModifier;
    float screenFade;
    boolean debug;
    int gamestate; // 0 = Main menu, 1 = Game, 2 = Gameover
    
    void setup() {
      size(600, 480);
      frameRate(60);
      cursor(CROSS);
      
      initialize();
    }
    
    void draw() {
      
      if (gamestate == 1) {
        gameUpdate();
        gameDraw();
      } else if (gamestate == 2) {
        gameUpdate();
        gameDraw();
        gameoverDraw();
      }
      
    }
    
    void keyPressed() {
      switch(key) {
        // For player direction.
        case('w'):case('W'):myPlayer.direction |=NORTH;break;
        case('d'):case('D'):myPlayer.direction |=EAST;break;
        case('s'):case('S'):myPlayer.direction |=SOUTH;break;
        case('a'):case('A'):myPlayer.direction |=WEST;break;
        // Restart game if it's over
        case('r'):case('R'): if (gamestate == 2) { initialize(); }break;
        // Enable debug mode.
          case('t'):case('T'): if (debug) { debug = false;} else { debug = true; }break;
        // Enemy Spawning (For Debugging!)
        //case('z'): theEnemies.add(new Enemy());break;
        //case('z'): spawnInterval = 100;break;
      }
    }
     
    void keyReleased() {
      // For player direction.
      switch(key) {
        case('w'):case('W'):myPlayer.direction ^=NORTH;break;
        case('d'):case('D'):myPlayer.direction ^=EAST;break;
        case('s'):case('S'):myPlayer.direction ^=SOUTH;break;
        case('a'):case('A'):myPlayer.direction ^=WEST;break;
      }
    }
    
    void mouseMoved() {
      myPlayer.mouseLoc.x = mouseX;
      myPlayer.mouseLoc.y = mouseY;
    }
    
    void mouseDragged() {
      myPlayer.mouseLoc.x = mouseX;
      myPlayer.mouseLoc.y = mouseY;
    }
    
    void mousePressed() {
      myPlayer.weapon.firing = true;
    }
    
    void mouseReleased() {
      // If the gun is an automatic, enable this.
      // Fixes an issue with fully/semi auto guns.
      if (myPlayer.weapon.type == 1) {
        myPlayer.weapon.firing = false;
      }
    }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Daniel

    Top-Down Shooter Pre-Pre-Alpha

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    My attempt at making an OOP game. This is a work in progress!

    Controls are
    W,A,S,D to move
    Aim with cursor
    Click to shoot

    john
    24 Nov 2011
    I love your game man its cool
    have you done any super mario before?
    Daniel
    24 Nov 2011
    @john This was my first game in Processing so I haven't made Super Mario before. Why do you ask?
    john
    25 Nov 2011
    thats awesome, I am try to do a game like super mario in processing and the requirement is 6 classes or subclass. I need help on that. are you expert ?
    Daniel
    25 Nov 2011
    No, I'm not, sorry :(

    The best advice I can give you is to go and look for other games on this site, look at their code, and learn from it.
    john
    5 Dec 2011
    hi , quick question, what does the DROP class do ?
    I dnt get it
    Daniel
    6 Dec 2011
    Play the game for about 30 seconds and you'll see that a small pink or yellow circle will appear on the map. If your character touches that, they will get

    A health buff (if it's pink)

    or

    A machine gun (if it's yellow)

    That's the drop class.
    andre
    10 Mar 2012
    In your player class, what does healthCur mean?
    Daniel
    11 Mar 2012
    @andre

    Basically, we need to know what the players maximum amount of health is (which we set to 10) and how much health the player has currently. The max health a player can have is stored in "healthMax" and the current amount of health the player has is stored in "healthCur".

    We separate these for a couple of reasons. One, if a player has 10 health and picks up a health pack, they shouldn't go over their max health. Two, when we draw health bars over everyone, we need something to compare it to. So basically, healthMax never changes (unless in the future I add some sort of "Health Upgrade" item) but healthCur changes as the game goes on (Oh, you got hit by a zombie? healthCur - 1! Oh, you picked up a health pack? healthCur + 3, constraining healthCur to be less than or equal to healthMax.)

    I hope it explained it well enough!
    andre
    11 Mar 2012
    Thanks, makes perfect sense.
    Im actually making a game very similar to yours.
    Check it out http://www.openprocessing.org/sketch/55600
    its not finished yet, im referring a lot to parts of your code to help me out...

    perhaps yo could help me out on a problem i have faced? i cant get me character to shoot
    and suggestions ?
    Daniel
    11 Mar 2012
    It depends on how you're doing this. How does a player shoot (with the mouse or keyboard?) Which direction do the bullets come out of?

    My code should really be able to help though - take a few hours to study it and try and understand what exactly is going on (the most confusing parts are math-related. Just accept those at face value and move on in the code.)
    andre
    12 Mar 2012
    I have been looking, but i will keep studying your code.
    The player will shoot via the mouse in one direction.
    andre
    18 Mar 2012
    Hey Daniel, im using the same bullet class for my game
    and i was wondering if you would be able to help me out?

    I would like the bullets to fire in only one direction, for example only shoot to the left of the screen.
    Ive tried many different ways but cant find an solution :(
    andre
    18 Mar 2012
    I forgot to add, Im trying to get shooting to be based on the mouseX, mouseY location not based on your mouseDragged function. shoot the bullets will shoot continiously to the left of the screen horizontally from the characters current location on a mousePressed function.

    I would be very thankful if you where able to help....
    You need to login/register to comment.