Roguelike游戏闪烁着画面

时间:2014-09-24 23:27:34

标签: java drawing ascii game-engine roguelike

我正在为基于ascii角色的roguelike游戏编写绘图系统(类似于矮人堡垒的图形)。我正在使用来自here的AsciiPanel。我的问题是,当我在地图上绘制实体时,它们似乎会眨眼,它们应该是实体的。

在这个gif中,顶行中的r个字符是实体。blinking entities

这是每帧调用的地图绘制方法。

public void draw(final Display display) {
        for (int x = getViewportX(); x < getViewportX() + viewportWidthInTiles; x++) {
            for (int y = viewportY; y < viewportY + viewportHeightInTiles; y++) {
                final char character = background[x][y].getCharacter();
                final Color foreground = background[x][y].getForeground();
                final Color backgroundColor = background[x][y].getBackground();
                final AsciiCharacterData data = new AsciiCharacterData(
                        character, foreground, backgroundColor);
                display.setCharacterAt(x - getViewportX(), y - viewportY,
                        background[x][y].getDrawingLayer(), data);
            }
        }
        display.clearLayer(DrawingLayer.PRIMARY);
        for (int i = 0; i < entities.size(); i++) {
            final Entity e = entities.get(i);

            final char character = e.getCharacter();
            final Color foreground = e.getForeground();
            final Color backgroundColor = e.getBackground();
            final AsciiCharacterData data = new AsciiCharacterData(character,
                    foreground, backgroundColor);
            display.setCharacterAt(e.getX() - getViewportX(), e.getY()
                    - viewportY, e.getDrawingLayer(), data);
        }
    }

我想我知道是什么导致了这个问题,因为如果我在绘制背景图块之前写display.clearLayer(DrawingLayer.BACKGROUND);(图块被绘制到的图层),就会产生更加荒谬的东西。

crazy

这是Display类,我认为我犯了一些错误。

public class Display {
    private static final char TRANSPARENT_CHARACTER = ' ';

    private final AsciiPanel displayPanel;
    private final int widthInCharacters, heightInCharacters;

    private final static int Z_LEVELS = DrawingLayer.values().length;

    private final AsciiCharacterData[][][] characterMap;

    public Display(final AsciiPanel panel) {
        displayPanel = panel;
        widthInCharacters = panel.getWidthInCharacters();
        heightInCharacters = panel.getHeightInCharacters();

        characterMap = new AsciiCharacterData[widthInCharacters][heightInCharacters][Z_LEVELS];
        for (int x = 0; x < widthInCharacters; x++) {
            for (int y = 0; y < heightInCharacters; y++) {
                for (int z = 0; z < Z_LEVELS; z++) {
                    characterMap[x][y][z] = new AsciiCharacterData(
                            TRANSPARENT_CHARACTER,
                            displayPanel.getDefaultForegroundColor(),
                            displayPanel.getDefaultBackgroundColor());
                }
            }
        }
    }

    public void setCharacterAt(final int x, final int y, final DrawingLayer z,
            final AsciiCharacterData c) {
        if (x < 0 || x >= widthInCharacters || y < 0 || y >= heightInCharacters)
            return;
        characterMap[x][y][z.layer] = c;

        // if z is not the top level
        if (z.layer != Z_LEVELS - 1) {
            // check all levels above
            for (int i = z.layer + 1; i < Z_LEVELS; i++) {
                // if there is an opaque character
                if (characterMap[x][y][i].character != TRANSPARENT_CHARACTER)
                    // we dont need to draw anything
                    return;
            }
        }

        if (c.character == TRANSPARENT_CHARACTER) {
            // loop through all characters under the transparent character
            for (int i = z.layer - 1; i >= 0; i--) {
                // if we find a non transparent character
                if (characterMap[x][y][i].character != TRANSPARENT_CHARACTER) {
                    // display that one instead
                    displayPanel.write(characterMap[x][y][i].character, x, y,
                            characterMap[x][y][i].foregroundColor,
                            characterMap[x][y][i].backgroundColor);
                    return;
                }
            }
            // if there were no non trasparent characters
            displayPanel.write(TRANSPARENT_CHARACTER, x, y);
            // if we are a highlighter, we draw the below character and then
            // just draw on top
        } else {
            displayPanel.write(c.character, x, y, c.foregroundColor,
                    c.backgroundColor);
        }
        displayPanel.repaint();
    }

    public AsciiCharacterData getCharacterAt(final int x, final int y,
            final DrawingLayer z) {
        return characterMap[x][y][z.layer];
    }

    public int getWidth() {
        return widthInCharacters;
    }

    public int getHeight() {
        return heightInCharacters;
    }

    public void clearLayer(final DrawingLayer layer) {
        for (int x = 0; x < widthInCharacters; x++) {
            for (int y = 0; y < heightInCharacters; y++) {
                setCharacterAt(x, y, layer,
                        new AsciiCharacterData(TRANSPARENT_CHARACTER,
                                displayPanel.getDefaultForegroundColor(),
                                displayPanel.getDefaultBackgroundColor()));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

解决!它是setCharacterAt方法中的一行。每次我设置一个角色(虽然效率低下)也会产生闪烁时,我正在重新粉刷。

相关问题