Skip to content

Commit

Permalink
Merge branch 'ent-sprites-in-allsprites' of https://github.com/SJulie…
Browse files Browse the repository at this point in the history
…z/megamek into carry_objects
  • Loading branch information
NickAragua committed Jul 28, 2024
2 parents ae91fb9 + 1cc47e5 commit 9b4908e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
36 changes: 21 additions & 15 deletions megamek/src/megamek/client/ui/swing/boardview/BoardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ public final class BoardView extends AbstractBoardView implements BoardListener,
// to specialized lists when created
private final TreeSet<Sprite> overTerrainSprites = new TreeSet<>();
private final TreeSet<HexSprite> behindTerrainHexSprites = new TreeSet<>();
private final TreeSet<HexSprite> hexSprites = new TreeSet<>();

/**
* Construct a new board view for the specified game
Expand Down Expand Up @@ -906,9 +905,6 @@ public void draw(Graphics g) {
drawSprites(g, flyOverSprites);
}

// draw onscreen entities
drawSprites(g, entitySprites);

// draw moving onscreen entities
drawSprites(g, movingEntitySprites);

Expand Down Expand Up @@ -1626,9 +1622,6 @@ public BufferedImage getEntireBoardImage(boolean ignoreUnits, boolean useBaseZoo
drawSprites(boardGraph, flyOverSprites);
}

// draw onscreen entities
drawSprites(boardGraph, entitySprites);

// draw moving onscreen entities
drawSprites(boardGraph, movingEntitySprites);

Expand Down Expand Up @@ -1723,7 +1716,6 @@ private void drawHexes(Graphics g, Rectangle view, boolean saveBoardImage) {
if (GUIP.getShowWrecks()) {
drawIsometricWreckSpritesForHex(c, g, isometricWreckSprites);
}
drawIsometricSpritesForHex(c, g, isometricSprites);
}
}
}
Expand Down Expand Up @@ -2622,6 +2614,7 @@ public void redrawMovingEntity(Entity entity, Coords position, int facing, int e

// Remove sprite for Entity, so it's not displayed while moving
if (sprite != null) {
removeSprite(sprite);
newSprites = new PriorityQueue<>(entitySprites);
newSpriteIds = new HashMap<>(entitySpriteIds);

Expand All @@ -2633,6 +2626,7 @@ public void redrawMovingEntity(Entity entity, Coords position, int facing, int e
}
// Remove iso sprite for Entity, so it's not displayed while moving
if (isoSprite != null) {
removeSprite(isoSprite);
isoSprites = new PriorityQueue<>(isometricSprites);
newIsoSpriteIds = new HashMap<>(isometricSpriteIds);

Expand Down Expand Up @@ -2814,10 +2808,16 @@ public void redrawEntity(Entity entity, Entity oldEntity) {
}

// Update Sprite state with new collections
removeSprites(entitySprites);
removeSprites(isometricSprites);
entitySprites = newSprites;
entitySpriteIds = newSpriteIds;
isometricSprites = isoSprites;
isometricSpriteIds = newIsoSpriteIds;
addSprites(entitySprites);
if (drawIsometric) {
addSprites(isometricSprites);
}

// Remove C3 sprites
for (Iterator<C3Sprite> i = c3Sprites.iterator(); i.hasNext(); ) {
Expand Down Expand Up @@ -2944,12 +2944,21 @@ void redrawAllEntities() {
}
}

removeSprites(entitySprites);
removeSprites(isometricSprites);

entitySprites = newSprites;
entitySpriteIds = newSpriteIds;

isometricSprites = newIsometricSprites;
isometricSpriteIds = newIsoSpriteIds;

addSprites(entitySprites);
if (drawIsometric) {
addSprites(isometricSprites);
}


wreckSprites = newWrecks;
isometricWreckSprites = newIsometricWrecks;

Expand Down Expand Up @@ -4297,7 +4306,6 @@ public void clearSprites() {

overTerrainSprites.clear();
behindTerrainHexSprites.clear();
hexSprites.clear();

super.clearSprites();
}
Expand Down Expand Up @@ -4869,7 +4877,6 @@ private Image scale(Image img, int width, int height) {
public boolean toggleIsometric() {
drawIsometric = !drawIsometric;
allSprites.forEach(Sprite::prepare);
hexSprites.forEach(HexSprite::updateBounds);

clearHexImageCache();
updateBoard();
Expand All @@ -4885,6 +4892,10 @@ public void updateEntityLabels() {
for (EntitySprite eS : entitySprites) {
eS.prepare();
}

for (IsometricSprite eS : isometricSprites) {
eS.prepare();
}
boardPanel.repaint();
}

Expand Down Expand Up @@ -5097,17 +5108,12 @@ public void addSprites(Collection<? extends Sprite> sprites) {
.map(s -> (HexSprite) s)
.filter(HexSprite::isBehindTerrain)
.forEach(behindTerrainHexSprites::add);
sprites.stream()
.filter(s -> s instanceof HexSprite)
.map(s -> (HexSprite) s)
.forEach(hexSprites::add);
}

@Override
public void removeSprites(Collection<? extends Sprite> sprites) {
super.removeSprites(sprites);
overTerrainSprites.removeAll(sprites);
behindTerrainHexSprites.removeAll(sprites);
hexSprites.removeAll(sprites);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Sprite used for isometric rendering to render an entity partially hidden behind a hill.
*/
class IsometricSprite extends Sprite {
class IsometricSprite extends HexSprite {

Entity entity;
private Image radarBlipImage;
Expand All @@ -26,7 +26,7 @@ class IsometricSprite extends Sprite {
private static final GUIPreferences GUIP = GUIPreferences.getInstance();

public IsometricSprite(BoardView boardView1, Entity entity, int secondaryPos, Image radarBlipImage) {
super(boardView1);
super(boardView1, secondaryPos == -1 ? entity.getPosition() : entity.getSecondaryPositions().get(secondaryPos));
this.entity = entity;
this.radarBlipImage = radarBlipImage;
this.secondaryPos = secondaryPos;
Expand Down Expand Up @@ -176,6 +176,7 @@ public void drawImmobileElements(Graphics graph, int x, int y, ImageObserver obs

@Override
public void prepare() {
updateBounds();
// create image for buffer
GraphicsConfiguration config = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
Expand Down Expand Up @@ -237,6 +238,6 @@ private boolean onlyDetectedBySensors() {

@Override
protected int getSpritePriority() {
return entity.getSpriteDrawPriority();
return entity.getSpriteDrawPriority() + 10;
}
}
5 changes: 5 additions & 0 deletions megamek/src/megamek/client/ui/swing/boardview/Sprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ public int compareTo(Sprite o) {
public boolean equals(Object obj) {
return super.equals(obj);
}

@Override
public String toString() {
return "[" + getClass().getSimpleName() + "] Prio: " + getSpritePriority() + ((image == null) ? "; no image" : "");
}
}

0 comments on commit 9b4908e

Please sign in to comment.