Health + Shield bar

时间:2015-06-23 12:36:33

标签: java

所以我试图在游戏中制作健康+盾牌。如果你曾经玩过英雄联盟或风暴英雄,我试图创造一个像这样的健康和盾牌吧。如果没有,我会为您准备一些图片:

(stackoverflow不会让我发布超过2个链接,所以想象一个完整的健康栏:))

第一个例子是显示健康状况的正常健康栏。我通过乘以百分比health(health / maxHealth)并将该数字乘以健康条的长度(51)来实现此工作

第二个例子展示了当玩家处于完全健康状态时健康和盾牌的外观。这也很好。

当玩家身体状况不佳时,我的问题就出现了。正如你所看到的,在第一张照片中,Kalista的健康状况大约为550。然后当她获得盾牌时,她的健康栏总数达到850左右,300健康盾牌是灰色的。然后,一旦她对大约300点生命值造成伤害,300健康护盾就不再像850生命值那样被压缩了。我的HUD健康栏工作到此为止。即使健康栏没有达到最大容量,它也会尝试将盾牌和健康压缩到健康栏中。

这是我目前的代码:

package net.masterzach32.sidescroller.assets.gfx;

import java.awt.*;
import java.awt.image.BufferedImage;

import net.masterzach32.sidescroller.assets.Assets;
import net.masterzach32.sidescroller.entity.EntityPlayer;
import net.masterzach32.sidescroller.util.LogHelper;

public class HUD {

private EntityPlayer player;

private BufferedImage image;
private Font font;

double b0 = 31, b1 = 20, hx = 31, mx = 20;

public HUD(EntityPlayer p) {
    player = p;
    try {
        image = Assets.getImageAsset("hud");

        font = new Font("Arial", Font.PLAIN, 14);
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

public void render(Graphics2D g) {
    g.drawImage(image, 0, 15, null);

    double h0 = player.getHealth() / player.getMaxHealth();
    double h1 = h0 * hx;
    double m0 = player.getShield() / player.getMaxShield();
    double m1 = m0 * mx;

    if((int) (h1 + m1) <= hx + mx) {
        int f = (int) (mx - m1);
        h1 += f;
    }

    LogHelper.logInfo("" + (int) (h1 + m1));
    if(h1 >= b0) b0 = h1;
    if(h1 < b0) b0 -= .7;
    if(m1 >= b1) b1 = m1;
    if(m1 < b1) b1 -= .7;

    // health bar
    g.setColor(new Color(200, 0, 0));
    g.fillRect(17, 18, (int) b0, 13);
    g.setColor(new Color(0, 170, 0));
    g.fillRect(17, 18, (int) h1, 13);
    // mana bar
    g.setColor(new Color(200, 0, 0));
    g.fillRect((int) (17 + b0), 18, (int) b1, 13);
    g.setColor(Color.BLUE);
    g.fillRect((int) (17 + h1), 18, (int) m1, 13);

    g.setFont(font);
    g.setColor(Color.WHITE);
    if(player.getOrbCurrentCd() > 0) g.drawString("" + (player.getOrbCurrentCd() / 60 + 1), 0, 12);
    else g.drawString("" + (player.getOrbCurrentCd() / 60), 0, 12);
    g.drawString(player.getLevel() + " - " + (int) player.getExp() + "/" + (int) player.getMaxExp(), 1, 70);
    g.setFont(font);
    g.drawString((int) (player.getHealth()) + "/" + (int) (player.getMaxHealth()), 16, 29);
}
}    

0 个答案:

没有答案