mercredi 7 décembre 2016

Sharing a Health Bar between the target and the player in a shooting game?

I have not been able to find an example for 2 players sharing 1 health bar. I am trying to create a health bar that will decrease the health of a target when the player hits it (5 consecutive times to win).(Red)

However if the target hits the player before they can hit 5 times the health bar resets itself (White) and becomes the health bar of the player were they will lose if the target hits 5 consecutive times.

I currently have the option for the first part but I can't seem to get the second part. The if statement seems to be unreachable. (Yellow)

Health Bar

public static void drawHealthBar(Graphics g){
      int index = findTargetMissilePosition();
      g.setColor(Color.BLACK);
      g.drawRect(PANEL_WIDTH - 4 - HEALTH_WIDTH*5,5,HEALTH_WIDTH*5,HEALTH_HEIGHT);
//      g.setColor(Color.GREEN);
//      g.fillRect(PANEL_WIDTH - 4 - 5*HEALTH_WIDTH +1, 5+1, 5*HEALTH_WIDTH -1,HEALTH_HEIGHT-1);
      boolean b = false;
      boolean c = false;
      if(detectHitTarget() && !b){
          g.setColor(Color.RED);
          g.fillRect(PANEL_WIDTH - 4 - Math.abs(hitCount-shooterHitCount)*HEALTH_WIDTH +1, 5+1, Math.abs(hitCount-shooterHitCount)*HEALTH_WIDTH -1,HEALTH_HEIGHT-1);
          if(detectHitShooter(index)){
            g.setColor(BACKGROUND_COLOR);
            g.fillRect(PANEL_WIDTH - 4 - 5*HEALTH_WIDTH +1, 5+1, 5*HEALTH_WIDTH -1,HEALTH_HEIGHT-1);
            b = true;
          }
        }

        if(detectHitShooter(index) && !c){
          g.setColor(Color.YELLOW);
          g.fillRect(PANEL_WIDTH - 4 - Math.abs(hitCount-shooterHitCount)*HEALTH_WIDTH +1, 5+1, Math.abs(hitCount-shooterHitCount)*HEALTH_WIDTH -1,HEALTH_HEIGHT-1);
          if(detectHitTarget()){
            g.setColor(BACKGROUND_COLOR);
            g.fillRect(PANEL_WIDTH - 4 - 5*HEALTH_WIDTH +1, 5+1, 5*HEALTH_WIDTH -1,HEALTH_HEIGHT-1);
            c = true;
          }
        }

   }

Calling the method

 public static void drawAll(Graphics g) {
        g.setColor(Color.BLACK);
        g.drawString("Project 3 written by Jasmine Ramirez", 10, 15);
        String hitString = "Hits: ";
        g.drawString(hitString + hitDisplayString, 10, 30);
        drawHealthBar(g);
        drawShooter(g, SHOOTER_COLOR);
        if (targetHitTimer > 0)
            drawTarget(g, TARGET_HIT_COLOR);
        else
            drawTarget(g, TARGET_COLOR);
        Color shieldColor = BACKGROUND_COLOR; // default: do not draw
        if (shieldActive) {
            if (shieldHitTimer > 0)
                shieldColor = SHIELD_HIT_COLOR;
            else
                shieldColor = SHIELD_COLOR;
        }
        drawShield(g, shieldColor);
    }

let me know if the rest of the code is needed or if this is something in the method im just doing wrong. I've tried using a while loop but that stops the flow of the other methods in drawAll() (were I call the health bar method). Honestly lost here

Aucun commentaire:

Enregistrer un commentaire