private Image getFrame(Resource resource) { switch (resource.getType()) { case CREATURE_ANIMATION: return ((CreatureAnimation) resource.getObject()).getCurrent().getCurrentFrame(); case IMAGE: return (Image) resource.getObject(); default: return null; } }
private void drawCreatureEntity(Entity entity, int x, int y) { Graphics g = container.getGraphics(); ResourceManager manager = ResourceManager.getInstance(); Movement movement = movementMapper.get(entity); String resName = resourceMapper.get(entity).getResourceName(); Resource res = manager.getResource(resName); Image entityFrame = getFrame(res); switch (res.getType()) { case CREATURE_ANIMATION: CreatureAnimation entityAnim = (CreatureAnimation) res.getObject(); Direction direction = directionMapper.get(entity); entityAnim.setIdle(); Animation animation = null; switch (direction.getDirection()) { case UP: case UP_LEFT: case UP_RIGHT: if (movement.getCurrentSpeed() != 0) { animation = entityAnim.getMoveUp(); } else { animation = entityAnim.getIdleUp(); } break; case DOWN: case DOWN_RIGHT: case DOWN_LEFT: if (movement.getCurrentSpeed() != 0) { animation = entityAnim.getMoveDown(); } else { animation = entityAnim.getIdleDown(); } break; case LEFT: if (movement.getCurrentSpeed() != 0) { animation = entityAnim.getMoveLeft(); } else { animation = entityAnim.getIdleLeft(); } break; case RIGHT: if (movement.getCurrentSpeed() != 0) { animation = entityAnim.getMoveRight(); } else { animation = entityAnim.getIdleRight(); } break; } Color color = Color.white; if (colorChangeMapper.get(entity) != null) { ColorChange colorChange = colorChangeMapper.get(entity); if (new Date().getTime() - colorChange.getLastTime() > colorChange.getTime()) { entity.removeComponent(colorChange); } else { color = colorChange.getColor(); } } g.drawAnimation(animation, x, y, color); break; case IMAGE: Image entityImg = (Image) res.getObject(); g.drawImage(entityImg, x, y); break; } HashMap<StatType, Integer> stats = statsMapper.get(entity).getStats(); int maxHealth = stats.get(StatType.MAX_HEALTH); int health = stats.get(StatType.HEALTH); float barWidth = longestBarWidth; float per = (float) health / (float) maxHealth; if (health > 0 && health != maxHealth) { g.setColor(new Color(0, 0, 0)); g.fillRect( x + entityFrame.getWidth() / 2 - barWidth / 2 - 1, y + entityFrame.getHeight(), barWidth + 2, barHeight); g.setColor(new Color(255, 0, 0)); g.fillRect( x + entityFrame.getWidth() / 2 - barWidth / 2, y + entityFrame.getHeight() + 1, barWidth * per, barHeight - 2); } }