Perform general code cleaning & refactoring
This commit is contained in:
@@ -1,65 +1,63 @@
|
||||
package com.bartlomiejpluta.demo.gui;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import com.bartlomiejpluta.base.api.gui.*;
|
||||
import com.bartlomiejpluta.base.lib.gui.*;
|
||||
|
||||
import com.bartlomiejpluta.base.api.screen.*;
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.input.*;
|
||||
import com.bartlomiejpluta.base.api.gui.Color;
|
||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.lib.gui.BaseComponent;
|
||||
import lombok.Setter;
|
||||
|
||||
public class Bar extends BaseComponent {
|
||||
|
||||
@Setter
|
||||
private float value = 1.0f;
|
||||
private float actualValue = 1.0f;
|
||||
private float speed = 0.05f;
|
||||
private final Color stroke;
|
||||
private final Color fill;
|
||||
private final Color stroke;
|
||||
private final Color fill;
|
||||
@Setter
|
||||
private float value = 1.0f;
|
||||
private float actualValue = 1.0f;
|
||||
private final float speed = 0.05f;
|
||||
|
||||
public Bar(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
public Bar(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
|
||||
this.stroke = gui.createColor();
|
||||
this.fill = gui.createColor();
|
||||
this.stroke = gui.createColor();
|
||||
this.fill = gui.createColor();
|
||||
|
||||
stroke.setAlpha(1f);
|
||||
fill.setAlpha(1f);
|
||||
}
|
||||
stroke.setAlpha(1f);
|
||||
fill.setAlpha(1f);
|
||||
}
|
||||
|
||||
public void setStrokeColor(Integer hex) {
|
||||
stroke.setRGB(hex);
|
||||
}
|
||||
public void setStrokeColor(Integer hex) {
|
||||
stroke.setRGB(hex);
|
||||
}
|
||||
|
||||
public void setFillColor(Integer hex) {
|
||||
fill.setRGB(hex);
|
||||
}
|
||||
public void setFillColor(Integer hex) {
|
||||
fill.setRGB(hex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getContentWidth() {
|
||||
return width;
|
||||
}
|
||||
@Override
|
||||
public float getContentWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getContentHeight() {
|
||||
return height;
|
||||
}
|
||||
@Override
|
||||
public float getContentHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
var remainingDistance = value - actualValue;
|
||||
actualValue += remainingDistance * speed;
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
var remainingDistance = value - actualValue;
|
||||
actualValue += remainingDistance * speed;
|
||||
|
||||
gui.beginPath();
|
||||
gui.drawRectangle(x, y, Math.max(width * actualValue, 0), height);
|
||||
gui.setFillColor(fill);
|
||||
gui.fill();
|
||||
gui.closePath();
|
||||
gui.beginPath();
|
||||
gui.drawRectangle(x, y, width, height);
|
||||
gui.setStrokeColor(stroke);
|
||||
gui.stroke();
|
||||
gui.closePath();
|
||||
}
|
||||
gui.beginPath();
|
||||
gui.drawRectangle(x, y, Math.max(width * actualValue, 0), height);
|
||||
gui.setFillColor(fill);
|
||||
gui.fill();
|
||||
gui.closePath();
|
||||
gui.beginPath();
|
||||
gui.drawRectangle(x, y, width, height);
|
||||
gui.setStrokeColor(stroke);
|
||||
gui.stroke();
|
||||
gui.closePath();
|
||||
}
|
||||
}
|
||||
@@ -1,47 +1,50 @@
|
||||
package com.bartlomiejpluta.demo.gui;
|
||||
|
||||
import lombok.*;
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.gui.Color;
|
||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||
import com.bartlomiejpluta.base.api.input.Key;
|
||||
import com.bartlomiejpluta.base.api.input.KeyAction;
|
||||
import com.bartlomiejpluta.base.api.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.input.*;
|
||||
import com.bartlomiejpluta.base.api.gui.*;
|
||||
import com.bartlomiejpluta.base.lib.gui.*;
|
||||
import com.bartlomiejpluta.base.lib.gui.Label;
|
||||
import lombok.Setter;
|
||||
|
||||
public class Button extends Label {
|
||||
private Color color;
|
||||
private final Color color;
|
||||
|
||||
@Setter
|
||||
private Runnable action;
|
||||
@Setter
|
||||
private Runnable action;
|
||||
|
||||
public Button(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
this.color = gui.createColor();
|
||||
this.color.setRGBA(1, 1, 1, 0);
|
||||
public Button(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
this.color = gui.createColor();
|
||||
this.color.setRGBA(1, 1, 1, 0);
|
||||
|
||||
setText("");
|
||||
setFontSize(17f);
|
||||
setAlignment(GUI.ALIGN_TOP | GUI.ALIGN_CENTER);
|
||||
setColor(0.4f, 0.7f, 0.0f, 1f);
|
||||
setPadding(10f);
|
||||
addEventListener(KeyEvent.TYPE, this::handleKeyEvent);
|
||||
}
|
||||
setText("");
|
||||
setFontSize(17f);
|
||||
setAlignment(GUI.ALIGN_TOP | GUI.ALIGN_CENTER);
|
||||
setColor(0.4f, 0.7f, 0.0f, 1f);
|
||||
setPadding(10f);
|
||||
addEventListener(KeyEvent.TYPE, this::handleKeyEvent);
|
||||
}
|
||||
|
||||
private void handleKeyEvent(KeyEvent event) {
|
||||
if(event.getKey() == Key.KEY_ENTER && event.getAction() == KeyAction.PRESS && action != null) {
|
||||
event.consume();
|
||||
action.run();
|
||||
}
|
||||
}
|
||||
private void handleKeyEvent(KeyEvent event) {
|
||||
if (event.getKey() == Key.KEY_ENTER && event.getAction() == KeyAction.PRESS && action != null) {
|
||||
event.consume();
|
||||
action.run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
color.setAlpha(focused ? 0.7f : 0f);
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
color.setAlpha(focused ? 0.7f : 0f);
|
||||
|
||||
gui.beginPath();
|
||||
gui.drawRectangle(x, y, getWidth(), getHeight());
|
||||
gui.setFillColor(color);
|
||||
gui.fill();
|
||||
gui.beginPath();
|
||||
gui.drawRectangle(x, y, getWidth(), getHeight());
|
||||
gui.setFillColor(color);
|
||||
gui.fill();
|
||||
|
||||
super.draw(screen, gui);
|
||||
}
|
||||
super.draw(screen, gui);
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,37 @@
|
||||
package com.bartlomiejpluta.demo.gui;
|
||||
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.gui.Color;
|
||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||
import com.bartlomiejpluta.base.api.gui.Paint;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.gui.*;
|
||||
import com.bartlomiejpluta.base.lib.gui.*;
|
||||
import com.bartlomiejpluta.base.lib.gui.BaseWindow;
|
||||
|
||||
public abstract class DecoratedWindow extends BaseWindow {
|
||||
private Paint paint;
|
||||
private Color inner;
|
||||
private Color outer;
|
||||
private final Paint paint;
|
||||
private final Color inner;
|
||||
private final Color outer;
|
||||
|
||||
public DecoratedWindow(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
public DecoratedWindow(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
|
||||
this.inner = gui.createColor();
|
||||
this.outer = gui.createColor();
|
||||
this.paint = gui.createPaint();
|
||||
this.inner = gui.createColor();
|
||||
this.outer = gui.createColor();
|
||||
this.paint = gui.createPaint();
|
||||
|
||||
inner.setRGBA(0.1f, 0.1f, 0.1f, 1f);
|
||||
outer.setRGBA(0.2f, 0.2f, 0.2f, 1f);
|
||||
}
|
||||
inner.setRGBA(0.1f, 0.1f, 0.1f, 1f);
|
||||
outer.setRGBA(0.2f, 0.2f, 0.2f, 1f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
gui.beginPath();
|
||||
gui.drawRectangle(x, y, getWidth(), getHeight());
|
||||
gui.setFillPaint(paint);
|
||||
gui.boxGradient(x, y, getWidth(), getHeight(), 10f, 100f, inner, outer, paint);
|
||||
gui.fill();
|
||||
gui.stroke();
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
gui.beginPath();
|
||||
gui.drawRectangle(x, y, getWidth(), getHeight());
|
||||
gui.setFillPaint(paint);
|
||||
gui.boxGradient(x, y, getWidth(), getHeight(), 10f, 100f, inner, outer, paint);
|
||||
gui.fill();
|
||||
gui.stroke();
|
||||
|
||||
super.draw(screen, gui);
|
||||
}
|
||||
super.draw(screen, gui);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,12 @@
|
||||
package com.bartlomiejpluta.demo.gui;
|
||||
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.gui.*;
|
||||
import com.bartlomiejpluta.base.lib.gui.*;
|
||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||
|
||||
|
||||
public class EquipmentWindow extends DecoratedWindow {
|
||||
|
||||
public EquipmentWindow(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
}
|
||||
public EquipmentWindow(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,32 @@
|
||||
package com.bartlomiejpluta.demo.gui;
|
||||
|
||||
import lombok.*;
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.gui.*;
|
||||
import com.bartlomiejpluta.base.lib.gui.*;
|
||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||
import com.bartlomiejpluta.base.api.gui.Inflatable;
|
||||
import com.bartlomiejpluta.base.api.gui.Ref;
|
||||
import lombok.Getter;
|
||||
|
||||
|
||||
public class GameMenuWindow extends DecoratedWindow implements Inflatable {
|
||||
|
||||
@Ref("resume_game")
|
||||
@Getter
|
||||
private Button resumeGameBtn;
|
||||
@Ref("resume_game")
|
||||
@Getter
|
||||
private Button resumeGameBtn;
|
||||
|
||||
@Ref("start_menu")
|
||||
@Getter
|
||||
private Button startMenuBtn;
|
||||
@Ref("start_menu")
|
||||
@Getter
|
||||
private Button startMenuBtn;
|
||||
|
||||
@Ref("exit")
|
||||
@Getter
|
||||
private Button exitBtn;
|
||||
@Ref("exit")
|
||||
@Getter
|
||||
private Button exitBtn;
|
||||
|
||||
public GameMenuWindow(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
}
|
||||
public GameMenuWindow(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInflate() {
|
||||
resumeGameBtn.focus();
|
||||
}
|
||||
@Override
|
||||
public void onInflate() {
|
||||
resumeGameBtn.focus();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.bartlomiejpluta.demo.gui;
|
||||
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.gui.*;
|
||||
import com.bartlomiejpluta.base.api.input.*;
|
||||
import com.bartlomiejpluta.base.api.screen.*;
|
||||
import com.bartlomiejpluta.base.lib.gui.*;
|
||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||
import com.bartlomiejpluta.base.api.gui.Ref;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.lib.gui.BorderLayout;
|
||||
import com.bartlomiejpluta.base.lib.gui.IconView;
|
||||
import com.bartlomiejpluta.base.lib.gui.Label;
|
||||
import com.bartlomiejpluta.demo.entity.Player;
|
||||
import com.bartlomiejpluta.demo.event.EnemyDiedEvent;
|
||||
import com.bartlomiejpluta.demo.event.HitEvent;
|
||||
@@ -17,82 +19,82 @@ import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class HUD extends BorderLayout {
|
||||
private static final int MAX_LOG_SIZE = 10;
|
||||
private static final float LOG_VISIBILITY_DURATION = 8000f;
|
||||
private static final float LOG_VISIBILITY_FADING_OUT = 1000f;
|
||||
private final DemoRunner runner;
|
||||
private final Player player;
|
||||
private final Runtime runtime;
|
||||
private LimitedQueue<String> logger = new LimitedQueue<>(MAX_LOG_SIZE);
|
||||
private static final int MAX_LOG_SIZE = 10;
|
||||
private static final float LOG_VISIBILITY_DURATION = 8000f;
|
||||
private static final float LOG_VISIBILITY_FADING_OUT = 1000f;
|
||||
private final DemoRunner runner;
|
||||
private final Player player;
|
||||
private final Runtime runtime;
|
||||
private final LimitedQueue<String> logger = new LimitedQueue<>(MAX_LOG_SIZE);
|
||||
|
||||
private float logVisibilityDuration = 0f;
|
||||
private float logVisibilityDuration = 0f;
|
||||
|
||||
private Weapon currentWeapon;
|
||||
private Weapon currentWeapon;
|
||||
|
||||
@Ref("hp")
|
||||
private Bar hp;
|
||||
@Ref("hp")
|
||||
private Bar hp;
|
||||
|
||||
@Ref("debug")
|
||||
private Label debugLbl;
|
||||
@Ref("debug")
|
||||
private Label debugLbl;
|
||||
|
||||
@Ref("log")
|
||||
private Label logLbl;
|
||||
@Ref("log")
|
||||
private Label logLbl;
|
||||
|
||||
@Ref("weapon")
|
||||
private IconView weapon;
|
||||
@Ref("weapon")
|
||||
private IconView weapon;
|
||||
|
||||
public HUD(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
this.runner = (DemoRunner) context.getGameRunner();
|
||||
this.player = runner.getPlayer();
|
||||
this.runtime = Runtime.getRuntime();
|
||||
context.addEventListener(HitEvent.TYPE, this::logHitEvent);
|
||||
context.addEventListener(EnemyDiedEvent.TYPE, this::logEnemyDiedEvent);
|
||||
}
|
||||
public HUD(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
this.runner = (DemoRunner) context.getGameRunner();
|
||||
this.player = runner.getPlayer();
|
||||
this.runtime = Runtime.getRuntime();
|
||||
context.addEventListener(HitEvent.TYPE, this::logHitEvent);
|
||||
context.addEventListener(EnemyDiedEvent.TYPE, this::logEnemyDiedEvent);
|
||||
}
|
||||
|
||||
private void logHitEvent(HitEvent event) {
|
||||
log(String.format("%s hits %s with damage = %d", event.getAttacker().getName(), event.getTarget().getName(), event.getDamage()));
|
||||
}
|
||||
private void logHitEvent(HitEvent event) {
|
||||
log(String.format("%s hits %s with damage = %d", event.getAttacker().getName(), event.getTarget().getName(), event.getDamage()));
|
||||
}
|
||||
|
||||
private void log(String message) {
|
||||
logger.add(message);
|
||||
log.info(message);
|
||||
logLbl.setText(logger.stream().collect(Collectors.joining("\n")));
|
||||
logVisibilityDuration = LOG_VISIBILITY_DURATION;
|
||||
}
|
||||
private void log(String message) {
|
||||
logger.add(message);
|
||||
log.info(message);
|
||||
logLbl.setText(logger.stream().collect(Collectors.joining("\n")));
|
||||
logVisibilityDuration = LOG_VISIBILITY_DURATION;
|
||||
}
|
||||
|
||||
private void logEnemyDiedEvent(EnemyDiedEvent event) {
|
||||
log(String.format("%s has died with HP = %d", event.getEnemy().getName(), event.getEnemy().getHp()));
|
||||
}
|
||||
private void logEnemyDiedEvent(EnemyDiedEvent event) {
|
||||
log(String.format("%s has died with HP = %d", event.getEnemy().getName(), event.getEnemy().getHp()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
super.update(dt);
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
super.update(dt);
|
||||
|
||||
hp.setValue((float) player.getHp() / (float) player.getMaxHp());
|
||||
hp.setValue((float) player.getHp() / (float) player.getMaxHp());
|
||||
|
||||
if (logVisibilityDuration > 0) {
|
||||
logVisibilityDuration -= dt * 1000;
|
||||
} else {
|
||||
logVisibilityDuration = 0;
|
||||
}
|
||||
if (logVisibilityDuration > 0) {
|
||||
logVisibilityDuration -= dt * 1000;
|
||||
} else {
|
||||
logVisibilityDuration = 0;
|
||||
}
|
||||
|
||||
if (player.getWeapon() != null && player.getWeapon() != currentWeapon) {
|
||||
weapon.setIcon(player.getWeapon().getIcon());
|
||||
this.currentWeapon = player.getWeapon();
|
||||
} else if (player.getWeapon() == null) {
|
||||
this.currentWeapon = null;
|
||||
}
|
||||
}
|
||||
if (player.getWeapon() != null && player.getWeapon() != currentWeapon) {
|
||||
weapon.setIcon(player.getWeapon().getIcon());
|
||||
this.currentWeapon = player.getWeapon();
|
||||
} else if (player.getWeapon() == null) {
|
||||
this.currentWeapon = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
var coords = player.getCoordinates();
|
||||
var pos = player.getPosition();
|
||||
debugLbl.setText(String.format("FPS: %.2f\n" + "Mem: %.2f / %.2f [MB]\n" + "Coords: %d : %d\n" + "Pos: %.2f : %.2f\n" + "Entities: %d", runner.instantFPS(), runtime.totalMemory() / 1024f / 1024f, runtime.maxMemory() / 1024f / 1024f, coords.x(), coords.y(), pos.x(), pos.y(), player.getLayer().getEntities().size() - 1));
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
var coords = player.getCoordinates();
|
||||
var pos = player.getPosition();
|
||||
debugLbl.setText(String.format("FPS: %.2f\n" + "Mem: %.2f / %.2f [MB]\n" + "Coords: %d : %d\n" + "Pos: %.2f : %.2f\n" + "Entities: %d", runner.instantFPS(), runtime.totalMemory() / 1024f / 1024f, runtime.maxMemory() / 1024f / 1024f, coords.x(), coords.y(), pos.x(), pos.y(), player.getLayer().getEntities().size() - 1));
|
||||
|
||||
logLbl.setAlpha(Math.min(1f, logVisibilityDuration / LOG_VISIBILITY_FADING_OUT));
|
||||
logLbl.setAlpha(Math.min(1f, logVisibilityDuration / LOG_VISIBILITY_FADING_OUT));
|
||||
|
||||
super.draw(screen, gui);
|
||||
}
|
||||
super.draw(screen, gui);
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,27 @@
|
||||
package com.bartlomiejpluta.demo.gui;
|
||||
|
||||
import lombok.*;
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.gui.*;
|
||||
import com.bartlomiejpluta.base.lib.gui.*;
|
||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||
import com.bartlomiejpluta.base.api.gui.Inflatable;
|
||||
import com.bartlomiejpluta.base.api.gui.Ref;
|
||||
import lombok.Getter;
|
||||
|
||||
public class StartMenuWindow extends DecoratedWindow implements Inflatable {
|
||||
|
||||
@Ref("new_game")
|
||||
@Getter
|
||||
private Button newGameBtn;
|
||||
@Ref("new_game")
|
||||
@Getter
|
||||
private Button newGameBtn;
|
||||
|
||||
@Ref("exit")
|
||||
@Getter
|
||||
private Button exitBtn;
|
||||
@Ref("exit")
|
||||
@Getter
|
||||
private Button exitBtn;
|
||||
|
||||
public StartMenuWindow(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
}
|
||||
public StartMenuWindow(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInflate() {
|
||||
newGameBtn.focus();
|
||||
}
|
||||
@Override
|
||||
public void onInflate() {
|
||||
newGameBtn.focus();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user