diff --git a/src/test/java/commonGraphics/AbstractKeyListener.java b/src/test/java/commonGraphics/AbstractKeyListener.java index 3021ef0..349c892 100644 --- a/src/test/java/commonGraphics/AbstractKeyListener.java +++ b/src/test/java/commonGraphics/AbstractKeyListener.java @@ -102,7 +102,7 @@ public UndefinedKeyActionException(int keyCode) { // private static final class KeyActionUpdater extends AbstractUpdater { - private static final long UPDATE_DELAY = 20; // ms + private static final long UPDATE_DELAY = 16; // ms private final @NotNull AbstractKeyListener listener; // diff --git a/src/test/java/commonGraphics/ColorUtils.java b/src/test/java/commonGraphics/ColorUtils.java index be14ba2..a640318 100644 --- a/src/test/java/commonGraphics/ColorUtils.java +++ b/src/test/java/commonGraphics/ColorUtils.java @@ -6,10 +6,29 @@ //Color utilities. public final class ColorUtils { - public static final @NotNull Color TRANSPARENT_BLACK = new Color(0, 0, 0, 0); + private static final int OPAQUE_ALPHA = 255; - //gets a gray color from brightness and alpha + @SuppressWarnings("unused") + public static final @NotNull Color TRANSPARENT_BLACK = getGray(0, 0); + + /** + * Gets a gray color from brightness and alpha. + * + * @param brightness A brightness value 0-255 (inclusive). + * @param alpha Alpha value for transparency, 0-255 (inclusive). Lower values are more transparent. + * @return A gray color. + */ public static @NotNull Color getGray(int brightness, int alpha) { return new Color(brightness, brightness, brightness, alpha); } + + /** + * Gets an opaque gray color from brightness. + * + * @param brightness A brightness value 0-255 (inclusive). + * @return An opaque gray color. + */ + public static @NotNull Color getOpaqueGray(int brightness) { + return getGray(brightness, OPAQUE_ALPHA); + } } \ No newline at end of file diff --git a/src/test/java/commonGraphics/panels/MinimalPanel.java b/src/test/java/commonGraphics/panels/MinimalPanel.java index 43fd783..3212ba1 100644 --- a/src/test/java/commonGraphics/panels/MinimalPanel.java +++ b/src/test/java/commonGraphics/panels/MinimalPanel.java @@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import static commonGraphics.ColorUtils.TRANSPARENT_BLACK; +import static consoleUtils.SimplePrinting.printLine; //a minimal panel with most common features public abstract class MinimalPanel extends JPanel { @@ -22,7 +22,7 @@ public MinimalPanel(@Nullable Color background, @Nullable Color borderColor, boolean drawBorders, @Nullable Color diagonalColor, boolean drawDiagonals) { super(); - setBackground(Objects.requireNonNullElse(background, TRANSPARENT_BLACK)); + setBackground(background); this.borderColor = Objects.requireNonNullElse(borderColor, DEFAULT_BORDERS_AND_DIAGONALS_COLOR); this.drawBorders = drawBorders; this.diagonalColor = Objects.requireNonNullElse(diagonalColor, DEFAULT_BORDERS_AND_DIAGONALS_COLOR); @@ -44,8 +44,8 @@ public final void setDrawDiagonals(boolean drawDiagonals) { // @Override - protected void paintComponent(@NotNull Graphics g) { - super.paintComponent(g); + public void paint(@NotNull Graphics g) { + super.paint(g); mainPaint(g); finalPaint(g); } @@ -73,4 +73,8 @@ private void drawPanelDiagonals(@NotNull Graphics g, int width, int height) { g.drawLine(0, 0, width, height); g.drawLine(0, height, width, 0); } + + public void printSizeToConsole(@NotNull String message) { + printLine(message + " size: " + getWidth() + " x " + getHeight()); + } } \ No newline at end of file diff --git a/src/test/java/commonGraphics/panels/sidePanels/AbstractSectionPanel.java b/src/test/java/commonGraphics/panels/sidePanels/AbstractSectionPanel.java index ef0b268..e6ba590 100644 --- a/src/test/java/commonGraphics/panels/sidePanels/AbstractSectionPanel.java +++ b/src/test/java/commonGraphics/panels/sidePanels/AbstractSectionPanel.java @@ -4,7 +4,6 @@ import org.jetbrains.annotations.Nullable; -import static commonGraphics.ColorUtils.TRANSPARENT_BLACK; import commonGraphics.panels.FixedHorizontalPanel; //a horizontal section for a vertical side-panel @@ -14,7 +13,7 @@ public AbstractSectionPanel(int height, @Nullable Color borderColor, boolean drawBorders, @Nullable Color diagonalColor, boolean drawDiagonals) { super( - height, TRANSPARENT_BLACK, + height, null, borderColor, drawBorders, diagonalColor, drawDiagonals); } diff --git a/src/test/java/commonGraphics/panels/sidePanels/commonLeftSidePanel/CommonTopSection.java b/src/test/java/commonGraphics/panels/sidePanels/commonLeftSidePanel/CommonTopSection.java index 63d5897..ab30945 100644 --- a/src/test/java/commonGraphics/panels/sidePanels/commonLeftSidePanel/CommonTopSection.java +++ b/src/test/java/commonGraphics/panels/sidePanels/commonLeftSidePanel/CommonTopSection.java @@ -6,7 +6,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import static commonGraphics.ColorUtils.TRANSPARENT_BLACK; import commonGraphics.panels.MinimalPanel; import commonGraphics.panels.sidePanels.SectionContainerInterface; @@ -17,7 +16,7 @@ public abstract class CommonTopSection extends MinimalPanel implements SectionCo // public CommonTopSection(@Nullable Color borderColor, @Nullable Color diagonalColor) { super( - TRANSPARENT_BLACK, + null, borderColor, false, diagonalColor, false); this.borderColor = borderColor; diff --git a/src/test/java/playerTest/graphics/TestWindow.java b/src/test/java/playerTest/graphics/TestWindow.java index a65fd63..47fc566 100644 --- a/src/test/java/playerTest/graphics/TestWindow.java +++ b/src/test/java/playerTest/graphics/TestWindow.java @@ -1,15 +1,21 @@ package playerTest.graphics; import java.awt.Color; +import java.awt.Graphics; import java.awt.LayoutManager; +import java.awt.BorderLayout; import javax.swing.BoxLayout; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import static commonGraphics.ColorUtils.getGray; +import static consoleUtils.SimplePrinting.printLine; + +import commonGraphics.panels.MinimalPanel; import commonGraphics.UpdatingWindow; import playerTest.graphics.panels.leftSidePanel.LeftSidePanel; -import playerTest.graphics.panels.CentralPanel; +import playerTest.graphics.panels.centralPanel.CentralPanel; import playerTest.graphics.panels.rightSidePanel.RightSidePanel; // @@ -33,9 +39,41 @@ public TestWindow() { public void addPanels() { LayoutManager layout = new BoxLayout(getContentPane(), BoxLayout.X_AXIS); getContentPane().setLayout(layout); - add(new LeftSidePanel(this, MAIN_PANEL_BACKGROUND_COLOR, MAIN_PANEL_BORDER_COLOR)); - add(new CentralPanel(MAIN_PANEL_BORDER_COLOR, MAIN_PANEL_BORDER_COLOR)); - add(new RightSidePanel(MAIN_PANEL_BACKGROUND_COLOR, MAIN_PANEL_BORDER_COLOR)); - // Add more panels here, if needed. + @NotNull LeftSidePanel leftSidePanel = new LeftSidePanel( + this, + MAIN_PANEL_BACKGROUND_COLOR, MAIN_PANEL_BORDER_COLOR); + add(leftSidePanel); + @NotNull CentralAndRightPanel centralAndRightPanel = new CentralAndRightPanel( + MAIN_PANEL_BACKGROUND_COLOR, MAIN_PANEL_BORDER_COLOR); + add(centralAndRightPanel); + + //debugging: + revalidate(); + leftSidePanel.printSizeToConsole("Left side panel created,"); + centralAndRightPanel.printSizeToConsole("CentralAndRightPanel created,"); + } + + // + private static final class CentralAndRightPanel extends MinimalPanel { + // + CentralAndRightPanel(@Nullable Color mainPanelBackgroundColor, @Nullable Color mainPanelBorderColor) { + super(null, null, false, null, false); + printLine("Creating CentralAndRightPanel"); + LayoutManager layout = new BorderLayout(); + setLayout(layout); + @NotNull RightSidePanel rightSidePanel = new RightSidePanel(mainPanelBackgroundColor, mainPanelBorderColor); + //add(rightSidePanel, BorderLayout.EAST); + @NotNull CentralPanel centralPanel = new CentralPanel(mainPanelBackgroundColor, mainPanelBorderColor); + add(centralPanel); + revalidate(); + + //debugging: + rightSidePanel.printSizeToConsole("Right side panel created,"); + centralPanel.printSizeToConsole("Central panel created,"); + } + + // + @Override + public void mainPaint(@NotNull Graphics g) {} } } \ No newline at end of file diff --git a/src/test/java/playerTest/graphics/panels/centralPanel/BottomSection.java b/src/test/java/playerTest/graphics/panels/centralPanel/BottomSection.java new file mode 100644 index 0000000..043e8fb --- /dev/null +++ b/src/test/java/playerTest/graphics/panels/centralPanel/BottomSection.java @@ -0,0 +1,48 @@ +package playerTest.graphics.panels.centralPanel; + +import java.awt.Color; +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static commonGraphics.ColorUtils.getGray; +import static commonGraphics.StringUtils.drawNumberedString; +import static consoleUtils.SimplePrinting.printLine; + +import commonGraphics.panels.sidePanels.AbstractSectionPanel; + +// +final class BottomSection extends AbstractSectionPanel { + private static final int PANEL_HEIGHT = 300; + private static final int @NotNull [] TEXT_LOCATION = new int [] {10, 10}; + private static final @NotNull Color + HEADING_COLOR = Color.white, + TEXT_COLOR = getGray(170, 255); + + // + BottomSection(@Nullable Color borderColor, @Nullable Color diagonalColor) { + super( + PANEL_HEIGHT, + borderColor, true, + diagonalColor, true); + printLine("Creating central panel bottom section"); + printSizeToConsole(""); + } + + // + @Override + public void mainPaint(@NotNull Graphics g) { + g.setColor(HEADING_COLOR); + drawInfoLine(g, "Bottom section", 1); + //TODO: improve this + g.setColor(TEXT_COLOR); + drawInfoLine(g, "Coming soon...", 2); + // Paint more stuff here, if needed. + } + + @SuppressWarnings("SameParameterValue") + private void drawInfoLine(@NotNull Graphics g, @Nullable String line, int lineNumber) { + drawNumberedString(g, line, TEXT_LOCATION, lineNumber); + } +} \ No newline at end of file diff --git a/src/test/java/playerTest/graphics/panels/centralPanel/CentralPanel.java b/src/test/java/playerTest/graphics/panels/centralPanel/CentralPanel.java new file mode 100644 index 0000000..8a9409e --- /dev/null +++ b/src/test/java/playerTest/graphics/panels/centralPanel/CentralPanel.java @@ -0,0 +1,54 @@ +package playerTest.graphics.panels.centralPanel; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.LayoutManager; +import java.awt.BorderLayout; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static consoleUtils.SimplePrinting.printLine; + +import commonGraphics.panels.MinimalPanel; +import commonGraphics.panels.sidePanels.SectionContainerInterface; + +// +public final class CentralPanel extends MinimalPanel implements SectionContainerInterface { + private final @Nullable Color sectionBorderColor, sectionDiagonalColor; + + // + public CentralPanel(@Nullable Color background, @Nullable Color borderColor) { + super( + background, + borderColor, true, + borderColor, true); + printLine("Creating central panel"); + this.sectionBorderColor = borderColor; + this.sectionDiagonalColor = borderColor; + addSections(); + } + + @Override + public void addSections(){ + LayoutManager layout = new BorderLayout(); + setLayout(layout); + //TODO: fix this + @NotNull BottomSection bottomSection = new BottomSection(sectionBorderColor, sectionDiagonalColor); + add(bottomSection, BorderLayout.SOUTH); + @NotNull MainPanel mainPanel = new MainPanel(sectionBorderColor, sectionDiagonalColor); + add(mainPanel); + //validate(); + revalidate(); + + //debugging: + //bottomSection.printSizeToConsole("Central bottom section created,"); + mainPanel.printSizeToConsole("Main panel created,"); + } + + // + @Override + public void mainPaint(@NotNull Graphics g) { + printLine("asd 1"); + } +} \ No newline at end of file diff --git a/src/test/java/playerTest/graphics/panels/CentralPanel.java b/src/test/java/playerTest/graphics/panels/centralPanel/MainPanel.java similarity index 57% rename from src/test/java/playerTest/graphics/panels/CentralPanel.java rename to src/test/java/playerTest/graphics/panels/centralPanel/MainPanel.java index e5bfdf6..aa3c000 100644 --- a/src/test/java/playerTest/graphics/panels/CentralPanel.java +++ b/src/test/java/playerTest/graphics/panels/centralPanel/MainPanel.java @@ -1,4 +1,4 @@ -package playerTest.graphics.panels; +package playerTest.graphics.panels.centralPanel; import java.awt.Color; import java.awt.Graphics; @@ -9,22 +9,27 @@ import commonGraphics.panels.graphicalPanels.CenteredDrawPanel; import commonGraphics.panels.graphicalPanels.ScaledDrawInterface; +import static consoleUtils.SimplePrinting.printLine; +import static consoleUtils.stringTools.NumberFormatter.doubleToString; + // -public final class CentralPanel extends CenteredDrawPanel implements ScaledDrawInterface { +public class MainPanel extends CenteredDrawPanel implements ScaledDrawInterface { private static final double DEFAULT_SCALE = 2.0; private static final @NotNull Color BACKGROUND = Color.black, - TEXT_COLOR = Color.white; + TEXT_COLOR = Color.red;//Color.white; @SuppressWarnings("FieldMayBeFinal") private double scale; // - public CentralPanel(@Nullable Color borderColor, @Nullable Color diagonalColor) { + public MainPanel(@Nullable Color borderColor, @Nullable Color diagonalColor) { super( - BACKGROUND, + null,//BACKGROUND, borderColor, false, - diagonalColor, true); + /*diagonalColor*/Color.red, true); + printLine("Creating main panel"); scale = DEFAULT_SCALE; + printSizeToConsole(""); } // @@ -38,6 +43,7 @@ public double getScale() { public void mainPaint(@NotNull Graphics g) { g.setColor(TEXT_COLOR); g.drawString("A player test", 100, 100); + g.drawString("Scale: " + doubleToString(scale, 3), 100, 120); // Paint more stuff here, if needed. } } \ No newline at end of file diff --git a/src/test/java/playerTest/graphics/panels/leftSidePanel/LeftSidePanel.java b/src/test/java/playerTest/graphics/panels/leftSidePanel/LeftSidePanel.java index 7d6e44d..2b0e63b 100644 --- a/src/test/java/playerTest/graphics/panels/leftSidePanel/LeftSidePanel.java +++ b/src/test/java/playerTest/graphics/panels/leftSidePanel/LeftSidePanel.java @@ -5,6 +5,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static consoleUtils.SimplePrinting.printLine; + import commonGraphics.UpdatingWindow; import commonGraphics.panels.sidePanels.commonLeftSidePanel.CommonLeftSidePanel; import commonGraphics.panels.sidePanels.commonLeftSidePanel.CommonTopSection; @@ -18,8 +20,9 @@ public LeftSidePanel(@NotNull UpdatingWindow window, @Nullable Color background, @Nullable Color borderColor) { super( window, background, - borderColor, true, true, - DIAGONAL_COLOR, false, false); + Color.red/*borderColor*/, true, true, + DIAGONAL_COLOR, true, true); + printLine("Creating left side panel"); addSections(); } diff --git a/src/test/java/playerTest/graphics/panels/rightSidePanel/BottomSection.java b/src/test/java/playerTest/graphics/panels/rightSidePanel/BottomSection.java index 1112d6e..7eb1ecc 100644 --- a/src/test/java/playerTest/graphics/panels/rightSidePanel/BottomSection.java +++ b/src/test/java/playerTest/graphics/panels/rightSidePanel/BottomSection.java @@ -8,7 +8,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import static commonGraphics.ColorUtils.TRANSPARENT_BLACK; import commonGraphics.panels.MinimalPanel; import commonGraphics.panels.sidePanels.SectionContainerInterface; @@ -23,7 +22,7 @@ final class BottomSection extends MinimalPanel implements SectionContainerInterf // BottomSection(@Nullable Color borderColor, boolean drawSectionBorders, @Nullable Color diagonalColor, boolean drawSectionDiagonals) { - super(TRANSPARENT_BLACK, borderColor, DRAW_PANEL_BORDERS, diagonalColor, DRAW_PANEL_DIAGONALS); + super(null, borderColor, DRAW_PANEL_BORDERS, diagonalColor, DRAW_PANEL_DIAGONALS); sectionBorderColor = borderColor; this.drawSectionBorders = drawSectionBorders; sectionDiagonalColor = diagonalColor; diff --git a/src/test/java/playerTest/graphics/panels/rightSidePanel/RightSidePanel.java b/src/test/java/playerTest/graphics/panels/rightSidePanel/RightSidePanel.java index 0496b33..30bb4a9 100644 --- a/src/test/java/playerTest/graphics/panels/rightSidePanel/RightSidePanel.java +++ b/src/test/java/playerTest/graphics/panels/rightSidePanel/RightSidePanel.java @@ -6,6 +6,8 @@ import org.jetbrains.annotations.Nullable; +import static consoleUtils.SimplePrinting.printLine; + import commonGraphics.panels.sidePanels.AbstractSidePanel; // @@ -22,6 +24,7 @@ public RightSidePanel(@Nullable Color background, @Nullable Color borderColor) { PANEL_WIDTH, background, borderColor, true, null, false); + printLine("Creating right side panel"); this.sectionBorderColor = borderColor; this.sectionDiagonalColor = borderColor; addSections(); diff --git a/src/test/java/playerTest/models/engines/Vehicle.java b/src/test/java/playerTest/models/engines/Vehicle.java new file mode 100644 index 0000000..49a3deb --- /dev/null +++ b/src/test/java/playerTest/models/engines/Vehicle.java @@ -0,0 +1,32 @@ +package playerTest.models.engines; + +import java.util.List; + +//initially a ship? +public class Vehicle { + VehicleInformation information; + VehicleExterior exterior; + VehicleInterior interior; + + Vehicle() { + // + } +} + +class VehicleInformation { + String name; + //owner, affiliation, price, history, etc +} + +class VehicleExterior { + // + void getSize() {} +} + +class VehicleInterior { + List rooms; +} + +class VehicleRoom { + // +} \ No newline at end of file diff --git a/src/test/java/test3/Test3.java b/src/test/java/test3/Test3.java new file mode 100644 index 0000000..9de26e3 --- /dev/null +++ b/src/test/java/test3/Test3.java @@ -0,0 +1,48 @@ +package test3; + +import org.jetbrains.annotations.NotNull; + +import graphicalTestAbstraction.GraphicalTest; +import test3.models.StructuralModule; +import test3.models.TestModule; +import test3.models.Spacecraft; +import test3.player.Player; +import test3.graphics.Window; + +// +public class Test3 extends GraphicalTest { + private static final int STARTING_MONEY = 1000; + + // + public static void main(String[] args) { + new Test3(new Player(STARTING_MONEY)); + } + + // + private Test3(@NotNull Player player) { + super(new Window(player)); + addInitialShipParts(player); + addInitialShips(player); + } + + private void addInitialShipParts(@NotNull Player player) { + //adds 2 structural modules + player.addShipPart(new StructuralModule()); + player.addShipPart(new StructuralModule()); + + //adds 3 test modules + player.addShipPart(new TestModule()); + player.addShipPart(new TestModule()); + player.addShipPart(new TestModule()); + + //adds 2 more structural modules + player.addShipPart(new StructuralModule()); + player.addShipPart(new StructuralModule()); + } + + private void addInitialShips(@NotNull Player player) { + //adds 2 ships + player.addShip(new Spacecraft("Ship 1", new StructuralModule())); + player.addShip(new Spacecraft("Ship 2", new StructuralModule())); + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java new file mode 100644 index 0000000..c4d3b59 --- /dev/null +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -0,0 +1,246 @@ +package test3.graphics; + +import java.util.ArrayList; +import java.util.List; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; + +import org.jetbrains.annotations.NotNull; + +import static consoleUtils.SimplePrinting.printLine; + +import commonGraphics.panels.FixedHorizontalPanel; +import test3.models.TestModule; +import test3.models.WrongModuleTypeException; +import test3.models.SpacecraftModule; +import test3.player.Player; + +// +final class BottomPanel extends FixedHorizontalPanel { + private static final int PANEL_HEIGHT = 300; + private static final @NotNull Color TEXT_COLOR = Color.white; + private final @NotNull Player player; + private int hoveredPart; + @SuppressWarnings("FieldCanBeLocal") + private final @NotNull BottomPanelMouseListener mouseListener; + private final @NotNull BottomPanelMouseMotionListener mouseMotionListener; + + // + public BottomPanel(@NotNull Player player, @NotNull PanelPalette palette) { + super(PANEL_HEIGHT, palette.getPanelBackground(), + palette.getPanelBorder(), true, + palette.getPanelDiagonals(), false); + this.player = player; + hoveredPart = -1; + mouseListener = new BottomPanelMouseListener(this); + addMouseListener(mouseListener); + mouseMotionListener = new BottomPanelMouseMotionListener(); + addMouseMotionListener(mouseMotionListener); + } + + @NotNull Player getPlayer() { + return player; + } + + // + @Override + public void mainPaint(@NotNull Graphics g) { + switch (player.getPaintMode()) { + case FLIGHT -> paintMode_flight(g); + case SHIPYARD -> paintMode_shipyard(g); + case MISSIONS -> paintMode_missions(g); + default -> paintMode_default(g); + } + } + + private void paintMode_default(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Painting for this PaintMode not defined.", 50, 50); + } + + private void paintMode_flight(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Flight mode not ready yet...", 50, 50); + } + + private void paintMode_shipyard(@NotNull Graphics g) { + setDrawDiagonals(false); + int @NotNull [] partDrawLocation = new int [] {100, 100}; + int + partSeparation = 160, + infoY = 180, textOffsetX = -40; + @NotNull List<@NotNull SpacecraftModule> partInventory = player.getShipPartInventory(); + hoveredPart = -1; //resets the hovered part + partInventory = filterParts(partInventory); + for (int i = 0; i < partInventory.size(); i++) { + @NotNull SpacecraftModule part = partInventory.get(i); + int partX = partDrawLocation[0] + partSeparation * i; + boolean active = ShipPartPainter.paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}, mouseMotionListener.getLocation()); + ShipPartPainter.paintPartInfo(g, part, TEXT_COLOR, new int [] {partX + textOffsetX, infoY}); + if (active) { + hoveredPart = i; + } + } + } + + private @NotNull List<@NotNull SpacecraftModule> filterParts(@NotNull List<@NotNull SpacecraftModule> partInventory) { + @NotNull List<@NotNull SpacecraftModule> filteredParts = new ArrayList<>(); + for (@NotNull SpacecraftModule part : partInventory) { + switch (player.getShipyardMode()) { + case CREATE_SHIP -> { + if (part instanceof TestModule) { + continue; + } + } + case ADD_MODULE -> { + } + default -> { + throw new RuntimeException("Undefined ShipyardMode"); + } + } + filteredParts.add(part); + } + return filteredParts; + } + + int getHoveredPart() { + return hoveredPart; + } + + private void paintMode_missions(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Missions mode not ready yet...", 50, 50); + } + + // + private static final class BottomPanelMouseListener implements MouseListener { + private final @NotNull BottomPanel panel; + + // + BottomPanelMouseListener(@NotNull BottomPanel panel) { + this.panel = panel; + } + + /** + * Invoked when a mouse button has been pressed on a component. + * + * @param e the event to be processed + */ + @Override + public void mousePressed(MouseEvent e) { + printLine("Mouse pressed at " + e.getX() + " x " + e.getY()); + int hoveredPart = panel.getHoveredPart(); + if (hoveredPart >= 0) { + printLine("Part number " + hoveredPart); + } + } + + /** + * Invoked when a mouse button has been released on a component. + * + * @param e the event to be processed + */ + @Override + public void mouseReleased(MouseEvent e) { + printLine("Mouse released at " + e.getX() + " x " + e.getY()); + int hoveredPart = panel.getHoveredPart(); + if (hoveredPart >= 0) { + printLine("Part number " + hoveredPart); + switch (panel.getPlayer().getShipyardMode()) { + case CREATE_SHIP -> { + try { + panel.getPlayer().createNewShip(hoveredPart); + } catch (@NotNull WrongModuleTypeException exception) { + printLine(exception.getMessage()); + } + } + case ADD_MODULE -> {} + default -> { + throw new RuntimeException("Undefined ShipyardMode"); + } + } + + } + } + + /** + * Invoked when the mouse button has been clicked (pressed + * and released) on a component. + * + * @param e the event to be processed + */ + @Override + public void mouseClicked(MouseEvent e) {} + + /** + * Invoked when the mouse enters a component. + * + * @param e the event to be processed + */ + @Override + public void mouseEntered(MouseEvent e) {} + + /** + * Invoked when the mouse exits a component. + * + * @param e the event to be processed + */ + @Override + public void mouseExited(MouseEvent e) {} + } + + // + private static final class BottomPanelMouseMotionListener implements MouseMotionListener { + private final int @NotNull [] location; + + // + BottomPanelMouseMotionListener() { + location = new int[2]; + } + + // + public int @NotNull [] getLocation() { + return new int [] {location[0], location[1]}; + } + + private void setLocation(@NotNull MouseEvent e) { + location[0] = e.getX(); + location[1] = e.getY(); + } + + /** + * Invoked when the mouse cursor has been moved onto a component + * but no buttons have been pushed. + * + * @param e the event to be processed + */ + @Override + public void mouseMoved(@NotNull MouseEvent e) { + setLocation(e); + } + + /** + * Invoked when a mouse button is pressed on a component and then + * dragged. {@code MOUSE_DRAGGED} events will continue to be + * delivered to the component where the drag originated until the + * mouse button is released (regardless of whether the mouse position + * is within the bounds of the component). + *

+ * Due to platform-dependent Drag&Drop implementations, + * {@code MOUSE_DRAGGED} events may not be delivered during a native + * Drag&Drop operation. + * + * @param e the event to be processed + */ + @Override + public void mouseDragged(@NotNull MouseEvent e) { + setLocation(e); + } + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/MainPanel.java b/src/test/java/test3/graphics/MainPanel.java new file mode 100644 index 0000000..32ca819 --- /dev/null +++ b/src/test/java/test3/graphics/MainPanel.java @@ -0,0 +1,168 @@ +package test3.graphics; + +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.util.List; +import java.util.ArrayList; +import java.awt.Color; +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import commonGraphics.panels.MinimalPanel; +import test3.models.Spacecraft; +import test3.player.PaintMode; +import test3.player.Player; + +// +final class MainPanel extends MinimalPanel { + private static final @NotNull Color TEXT_COLOR = Color.white; + private final @NotNull Player player; + + // + MainPanel(@NotNull Player player, @NotNull PanelPalette palette) { + super( + palette.getMainBackground(), + palette.getPanelBorder(), true, + palette.getPanelDiagonals(), false); + this.player = player; + } + + // + @Override + public void mainPaint(@NotNull Graphics g) { + @NotNull PaintMode paintMode = player.getPaintMode(); + paintCommonInfo(g, paintMode); + switch (paintMode) { + case FLIGHT -> paintMode_flight(g); + case SHIPYARD -> paintMode_shipyard(g); + case MISSIONS -> paintMode_missions(g); + default -> paintMode_default(g); + } + } + + private void paintCommonInfo(@NotNull Graphics g, @NotNull PaintMode mode) { + int textHeight = 15; + int @NotNull [] textLocation = new int [] {20, 20}; + g.setColor(TEXT_COLOR); + drawStringList(g, textLocation, textHeight, new ArrayList<>() {{ + add("Mode: " + mode); + add("Money: " + player.getWallet().getMoney()); + }}); + } + + private void drawStringList(@NotNull Graphics g, int @NotNull [] location, int textHeight, + @NotNull List<@Nullable String> list) { + for (int i = 0; i < list.size(); i++) { + @Nullable String string = list.get(i); + if (string != null) { + g.drawString(string, location[0], location[1] + textHeight * (i + 1)); + } + } + } + + private void paintMode_default(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Painting for this PaintMode not defined.", 50, 50); + } + + private void paintMode_flight(@NotNull Graphics g) { + setDrawDiagonals(true); + int x = getWidth() / 2; + int y = getHeight() / 2; + g.setColor(TEXT_COLOR); + g.drawString("Flight mode not ready yet...", x, y); + } + + private void paintMode_shipyard(@NotNull Graphics g) { + setDrawDiagonals(false); + @NotNull List<@NotNull Spacecraft> ships = player.getOwnedShips(); + int shipCount = ships.size(); + int gap = getWidth() / (shipCount + 1); + int @NotNull [] firstShipLocation = new int [] {gap, getHeight() / 2}; + for (int i = 0; i < shipCount; i++) { + @NotNull Spacecraft ship = ships.get(i); + ShipPainter.paintShip(g, ship, new int [] {firstShipLocation[0] + gap * i, firstShipLocation[1]}); + } + } + + private void paintMode_missions(@NotNull Graphics g) { + setDrawDiagonals(true); + int x = getWidth() / 2; + int y = getHeight() / 2; + g.setColor(TEXT_COLOR); + g.drawString("Missions mode not ready yet...", x, y); + } + + private static final class MainPanelMouseListener implements MouseListener { + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseClicked(MouseEvent e) {} + + @Override + public void mouseEntered(MouseEvent e) {} + + @Override + public void mouseExited(MouseEvent e) {} + } + + private static final class MainPanelMouseMotionListener implements MouseMotionListener { + private final int @NotNull [] location; + + // + MainPanelMouseMotionListener() { + location = new int[2]; + } + + // + public int @NotNull [] getLocation() { + return new int [] {location[0], location[1]}; + } + + private void setLocation(@NotNull MouseEvent e) { + location[0] = e.getX(); + location[1] = e.getY(); + } + + /** + * Invoked when the mouse cursor has been moved onto a component + * but no buttons have been pushed. + * + * @param e the event to be processed + */ + @Override + public void mouseMoved(@NotNull MouseEvent e) { + setLocation(e); + } + + /** + * Invoked when a mouse button is pressed on a component and then + * dragged. {@code MOUSE_DRAGGED} events will continue to be + * delivered to the component where the drag originated until the + * mouse button is released (regardless of whether the mouse position + * is within the bounds of the component). + *

+ * Due to platform-dependent Drag&Drop implementations, + * {@code MOUSE_DRAGGED} events may not be delivered during a native + * Drag&Drop operation. + * + * @param e the event to be processed + */ + @Override + public void mouseDragged(@NotNull MouseEvent e) { + setLocation(e); + } + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/MyKeyListener.java b/src/test/java/test3/graphics/MyKeyListener.java new file mode 100644 index 0000000..55cdf7e --- /dev/null +++ b/src/test/java/test3/graphics/MyKeyListener.java @@ -0,0 +1,76 @@ +package test3.graphics; + +import org.jetbrains.annotations.NotNull; + +import static consoleUtils.SimplePrinting.printLine; + +import commonGraphics.AbstractKeyListener; +import test3.player.PaintMode; +import test3.player.Player; +import test3.player.ShipyardMode; + +// +final class MyKeyListener extends AbstractKeyListener { + private final @NotNull Player player; + + // + public MyKeyListener(@NotNull Player player) { + super(); + this.player = player; + } + + // + @SuppressWarnings("SwitchStatementWithTooFewBranches") + @Override + public void keyActionSwitch_byCode(int keyCode) throws UndefinedKeyActionException { + switch (keyCode) { + //check keys by keyCode here + default -> throw new UndefinedKeyActionException(); + } + } + + // + @Override + public void keyActionSwitch_byText(@NotNull String keyText) throws UndefinedKeyActionException { + switch (keyText) { + case "F1" -> player.setPaintMode(PaintMode.FLIGHT); + case "F2" -> player.setPaintMode(PaintMode.SHIPYARD); + case "F3" -> player.setPaintMode(PaintMode.MISSIONS); + //check more universal keys here + default -> { + switch (player.getPaintMode()) { + case FLIGHT -> keyActionSwitch_byText_FlightMode(keyText); + case SHIPYARD -> keyActionSwitch_byText_ShipyardMode(keyText); + case MISSIONS -> keyActionSwitch_byText_MissionsMode(keyText); + default -> throw new UndefinedKeyActionException(); + } + } + } + } + + @SuppressWarnings("SwitchStatementWithTooFewBranches") + private void keyActionSwitch_byText_FlightMode(@NotNull String keyText) throws UndefinedKeyActionException { + switch (keyText) { + case "Space" -> printLine("A space bar has been pressed!"); + //check more flight-specific keys here + default -> throw new UndefinedKeyActionException(); + } + } + + private void keyActionSwitch_byText_ShipyardMode(@NotNull String keyText) throws UndefinedKeyActionException { + switch (keyText) { + case "1" -> player.setShipyardMode(ShipyardMode.CREATE_SHIP); + case "2" -> player.setShipyardMode(ShipyardMode.ADD_MODULE); + default -> throw new UndefinedKeyActionException(); + } + } + + @SuppressWarnings("SwitchStatementWithTooFewBranches") + private void keyActionSwitch_byText_MissionsMode(@NotNull String keyText) throws UndefinedKeyActionException { + switch (keyText) { + case "Space" -> printLine("A space bar has been pressed!"); + //check more missions-specific keys here + default -> throw new UndefinedKeyActionException(); + } + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/PanelPalette.java b/src/test/java/test3/graphics/PanelPalette.java new file mode 100644 index 0000000..e68a89f --- /dev/null +++ b/src/test/java/test3/graphics/PanelPalette.java @@ -0,0 +1,42 @@ +package test3.graphics; + +import java.awt.Color; + +import org.jetbrains.annotations.NotNull; + +import commonGraphics.ColorUtils; + +// +final class PanelPalette { + @SuppressWarnings("FieldMayBeFinal") + private @NotNull Color + mainBackground, panelBackground, + panelBorder, panelDiagonals; + + public PanelPalette() { + mainBackground = Color.black; + panelBackground = ColorUtils.getOpaqueGray(45); + panelBorder = ColorUtils.getOpaqueGray(30); + panelDiagonals = new Color(255, 120, 0); + } + + // + public @NotNull Color getMainBackground() { + return mainBackground; + } + + // + public @NotNull Color getPanelBackground() { + return panelBackground; + } + + // + public @NotNull Color getPanelBorder() { + return panelBorder; + } + + // + public @NotNull Color getPanelDiagonals() { + return panelDiagonals; + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/ShipPainter.java b/src/test/java/test3/graphics/ShipPainter.java new file mode 100644 index 0000000..92c74df --- /dev/null +++ b/src/test/java/test3/graphics/ShipPainter.java @@ -0,0 +1,60 @@ +package test3.graphics; + +import java.util.List; +import java.util.ArrayList; +import java.awt.Color; +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import test3.models.Spacecraft; + +// +final class ShipPainter { + private static final int TEXT_HEIGHT = 15; + + //call this to paint + static void paintShip(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNull [] location) { + paintParts(g, ship, location); + paintOutline(g, ship, location); + int shipInfoOffsetX = -20; + int @NotNull [] shipInfoLocation = new int [] { + location[0] + shipInfoOffsetX, + location[1] + ship.getSize() / 2}; + paintShipInfo(g, ship, ship.getColor(), shipInfoLocation); + } + + private static void paintParts(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNull [] location) { + ShipPartPainter.paintShipPart(g, ship.getCore(), location, new int[2]); + } + + private static void paintOutline(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNull [] location) { + int size = ship.getSize(); + int @NotNull [] drawStart = new int [] { + location[0] - size / 2, + location[1] - size / 2}; + g.setColor(ship.getColor()); + g.drawRect(drawStart[0], drawStart[1], size, size); + } + + private static void paintShipInfo(@NotNull Graphics g, @NotNull Spacecraft ship, @NotNull Color textColor, int @NotNull [] location) { + // + g.setColor(textColor); + drawStringList(g, location, TEXT_HEIGHT, new ArrayList<>() {{ + add(ship.getName()); + //add more ship info lines here + }}); + } + + @SuppressWarnings("SameParameterValue") + private static void drawStringList(@NotNull Graphics g, int @NotNull [] location, int textHeight, + @NotNull List<@Nullable String> list) { + for (int i = 0; i < list.size(); i++) { + @Nullable String string = list.get(i); + if (string != null) { + g.drawString(string, location[0], location[1] + textHeight * (i + 1)); + } + } + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/ShipPartPainter.java b/src/test/java/test3/graphics/ShipPartPainter.java new file mode 100644 index 0000000..357542f --- /dev/null +++ b/src/test/java/test3/graphics/ShipPartPainter.java @@ -0,0 +1,61 @@ +package test3.graphics; + +import java.util.List; +import java.util.ArrayList; +import java.awt.Color; +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import test3.models.SpacecraftModule; + +// +final class ShipPartPainter { + private static final int TEXT_HEIGHT = 15; + + //call this to paint part only; returns within status (true, if mouse is within this part) + static boolean paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] paintCenter, + int @NotNull [] mouseLocation) { + int size = part.getSize(); + int @NotNull [] drawStart = new int [] { + paintCenter[0] - size / 2, + paintCenter[1] - size / 2}; + g.setColor(part.getColor()); + boolean within = within(drawStart, size, mouseLocation); + if (within) { + g.fillRect(drawStart[0], drawStart[1], size, size); + } else { + g.drawRect(drawStart[0], drawStart[1], size, size); + } + return within; + } + + private static boolean within(int @NotNull [] drawStart, int size, int @NotNull [] target) { + boolean + withinX = (target[0] >= drawStart[0]) && (target[0] <= drawStart[0] + size), + withinY = (target[1] >= drawStart[1]) && (target[1] <= drawStart[1] + size); + return withinX && withinY; + } + + //call this to paint part info only + static void paintPartInfo(@NotNull Graphics g, @NotNull SpacecraftModule part, + @NotNull Color textColor, int @NotNull [] location) { + g.setColor(textColor); + drawStringList(g, location, TEXT_HEIGHT, new ArrayList<>() {{ + add(part.getName()); + //add more part info lines here + }}); + } + + @SuppressWarnings("SameParameterValue") + private static void drawStringList(@NotNull Graphics g, int @NotNull [] location, int textHeight, + @NotNull List<@Nullable String> list) { + for (int i = 0; i < list.size(); i++) { + @Nullable String string = list.get(i); + if (string != null) { + g.drawString(string, location[0], location[1] + textHeight * (i + 1)); + } + } + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/TestWindowSettings.java b/src/test/java/test3/graphics/TestWindowSettings.java new file mode 100644 index 0000000..403acc6 --- /dev/null +++ b/src/test/java/test3/graphics/TestWindowSettings.java @@ -0,0 +1,20 @@ +package test3.graphics; + +import java.awt.Dimension; +import java.awt.Point; + +import org.jetbrains.annotations.NotNull; + +import commonGraphics.WindowSettings; + +// +final class TestWindowSettings extends WindowSettings { + private static final @NotNull Dimension WINDOW_SIZE = new Dimension(1100, 700); + private static final @NotNull Point WINDOW_LOCATION = new Point(50, 50); + private static final @NotNull String WINDOW_TITLE = "Test 3"; + + // + TestWindowSettings() { + super(WINDOW_SIZE, WINDOW_LOCATION, WINDOW_TITLE); + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/Window.java b/src/test/java/test3/graphics/Window.java new file mode 100644 index 0000000..41c82c5 --- /dev/null +++ b/src/test/java/test3/graphics/Window.java @@ -0,0 +1,36 @@ +package test3.graphics; + +import java.awt.LayoutManager; +import java.awt.BorderLayout; + +import org.jetbrains.annotations.NotNull; + +import commonGraphics.UpdatingWindow; +import test3.player.Player; + +// +public class Window extends UpdatingWindow { + private final @NotNull Player player; + private final @NotNull PanelPalette panelPalette; + + // + public Window(@NotNull Player player) { + super(new TestWindowSettings()); //default frame rate + this.player = player; + panelPalette = new PanelPalette(); + //add stuff before panels here + addPanels(); + addKeyListener(new MyKeyListener(this.player)); + revalidate(); + startUpdating(); + } + + // + @Override + public void addPanels() { + @NotNull LayoutManager layout = new BorderLayout(); + getContentPane().setLayout(layout); + add(new BottomPanel(player, panelPalette), BorderLayout.SOUTH); + add(new MainPanel(player, panelPalette)); + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/ModuleConnection.java b/src/test/java/test3/models/ModuleConnection.java new file mode 100644 index 0000000..6ee7900 --- /dev/null +++ b/src/test/java/test3/models/ModuleConnection.java @@ -0,0 +1,44 @@ +package test3.models; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class ModuleConnection { + private final @NotNull SpacecraftModule parentModule; + private @Nullable ModuleConnection tetheredConnection; + private boolean isParentConnection; + private final int @NotNull [] location; + private final double orientation; + + public ModuleConnection(@NotNull SpacecraftModule parentModule, int @NotNull [] location, double orientation) { + this.parentModule = parentModule; + this.tetheredConnection = null; + this.isParentConnection = false; + this.location = location; + this.orientation = orientation; + } + + public @NotNull SpacecraftModule getParentModule() { + return parentModule; + } + + public boolean getIsParentConnection() { + return isParentConnection; + } + + public void setIsParentConnection(boolean isParentConnection) { + this.isParentConnection = isParentConnection; + } + + public void setTetheredConnection(@Nullable ModuleConnection tetheredConnection) { + this.tetheredConnection = tetheredConnection; + } + + public @Nullable ModuleConnection getTetheredConnection() { + return tetheredConnection; + } + + public int @NotNull [] getLocation() { + return location; + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/Spacecraft.java b/src/test/java/test3/models/Spacecraft.java new file mode 100644 index 0000000..273c21f --- /dev/null +++ b/src/test/java/test3/models/Spacecraft.java @@ -0,0 +1,53 @@ +package test3.models; + +import java.awt.Color; +import java.util.List; + +import org.jetbrains.annotations.NotNull; + +// +public class Spacecraft { + private final @NotNull String name; + private final @NotNull StructuralModule core; + private final @NotNull Color color; //for graphical purposes + + // + public Spacecraft(@NotNull String name, @NotNull StructuralModule core) { + this.name = name; + this.core = core; + color = Color.red; + } + + // + public @NotNull String getName() { + return name; + } + + // + public @NotNull StructuralModule getCore() { + return core; + } + + // + public @NotNull List<@NotNull ModuleConnection> getFreeConnections() { + @NotNull List<@NotNull ModuleConnection> freeConnections = core.getFreeConnections(); + + + + return freeConnections; + } + + //gets the total size of the ship + public int getSize() { + //TODO: improve this and remove the "+10" + return core.getSize() + 10; + } + + //for graphical purposes + public @NotNull Color getColor() { + return color; + } + + public void addModule(SpacecraftModule part) { + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/SpacecraftModule.java b/src/test/java/test3/models/SpacecraftModule.java new file mode 100644 index 0000000..bfd48dc --- /dev/null +++ b/src/test/java/test3/models/SpacecraftModule.java @@ -0,0 +1,84 @@ +package test3.models; + +import java.awt.Color; +import java.util.ArrayList; +import java.util.List; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +// +public abstract class SpacecraftModule { + private final @NotNull String name; + private final int size; + private final @NotNull Color color; // for graphical purposes + private @NotNull List<@NotNull ModuleConnection> connections; + + // + protected SpacecraftModule(@NotNull String name, int size, @NotNull Color color) { + this.name = name; + this.size = size; + this.color = color; + this.connections = new ArrayList<>(); + } + + // + public @NotNull String getName() { + return name; + } + + // + public int getSize() { + return size; + } + + //for graphical purposes + public @NotNull Color getColor() { + return color; + } + + public void addConnection(@NotNull ModuleConnection connection){ + connections.add(connection); + } + + // + public @NotNull List<@NotNull ModuleConnection> getConnections(){ + return connections; + } + + // + public @Nullable ModuleConnection getParentConnection() { + for (@NotNull ModuleConnection connection : connections) { + if (connection.getIsParentConnection()) { + return connection; + } + } + return null; + } + + // + public @NotNull List<@NotNull ModuleConnection> getFreeConnections() { + @NotNull List<@NotNull ModuleConnection> freeConnections = new ArrayList<>(); + for (@NotNull ModuleConnection connection : connections) { + if (connection.getTetheredConnection() == null) { + freeConnections.add(connection); + } + } + + return freeConnections; + } + + // + public @NotNull List<@NotNull ModuleConnection> getOccupiedConnections(boolean getStructuralConnections) { + @NotNull List<@NotNull ModuleConnection> occupiedConnections = new ArrayList<>(); + for (@NotNull ModuleConnection connection : connections) { + @Nullable ModuleConnection tetheredConnection = connection.getTetheredConnection(); + if (tetheredConnection != null && (!getStructuralConnections || (!connection.getIsParentConnection() && + tetheredConnection.getParentModule() instanceof StructuralModule))) { + occupiedConnections.add(connection); + } + } + return occupiedConnections; + } + +} \ No newline at end of file diff --git a/src/test/java/test3/models/StructuralModule.java b/src/test/java/test3/models/StructuralModule.java new file mode 100644 index 0000000..7b4c5f4 --- /dev/null +++ b/src/test/java/test3/models/StructuralModule.java @@ -0,0 +1,15 @@ +package test3.models; + +import org.jetbrains.annotations.NotNull; + +import java.awt.Color; +import java.util.ArrayList; +import java.util.List; + +// +public class StructuralModule extends SpacecraftModule { + // + public StructuralModule(){ + super("Structural module", 50, Color.green); + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/TestModule.java b/src/test/java/test3/models/TestModule.java new file mode 100644 index 0000000..5f4a819 --- /dev/null +++ b/src/test/java/test3/models/TestModule.java @@ -0,0 +1,11 @@ +package test3.models; + +import java.awt.Color; + +// +public class TestModule extends SpacecraftModule { + // + public TestModule() { + super("Test module", 50, Color.orange, connections); + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/WrongModuleTypeException.java b/src/test/java/test3/models/WrongModuleTypeException.java new file mode 100644 index 0000000..677eb56 --- /dev/null +++ b/src/test/java/test3/models/WrongModuleTypeException.java @@ -0,0 +1,9 @@ +package test3.models; + +// +public final class WrongModuleTypeException extends Exception { + // + public WrongModuleTypeException() { + super("Wrong model type exception"); + } +} \ No newline at end of file diff --git a/src/test/java/test3/player/InsufficientFundsException.java b/src/test/java/test3/player/InsufficientFundsException.java new file mode 100644 index 0000000..63eaf55 --- /dev/null +++ b/src/test/java/test3/player/InsufficientFundsException.java @@ -0,0 +1,17 @@ +package test3.player; + +// +public final class InsufficientFundsException extends Exception { + private final int missingFunds; + + // + public InsufficientFundsException(int missingFunds) { + super("Insufficient funds exception"); + this.missingFunds = missingFunds; + } + + // + public int getMissingFunds() { + return missingFunds; + } +} \ No newline at end of file diff --git a/src/test/java/test3/player/PaintMode.java b/src/test/java/test3/player/PaintMode.java new file mode 100644 index 0000000..0319739 --- /dev/null +++ b/src/test/java/test3/player/PaintMode.java @@ -0,0 +1,8 @@ +package test3.player; + +// +public enum PaintMode { + FLIGHT, + SHIPYARD, + MISSIONS +} \ No newline at end of file diff --git a/src/test/java/test3/player/Player.java b/src/test/java/test3/player/Player.java new file mode 100644 index 0000000..f5bc101 --- /dev/null +++ b/src/test/java/test3/player/Player.java @@ -0,0 +1,100 @@ +package test3.player; + +import java.util.List; +import java.util.ArrayList; + +import org.jetbrains.annotations.NotNull; + +import test3.models.*; + +import static consoleUtils.SimplePrinting.printLine; + +// +public class Player { + private final @NotNull Wallet wallet; + private final @NotNull List<@NotNull Spacecraft> ownedShips; + private final @NotNull List<@NotNull SpacecraftModule> shipPartInventory; + private @NotNull PaintMode paintMode; // for graphical purposes + private @NotNull ShipyardMode shipyardMode; // for graphical purposes + private int selectedPartIndex; // for shipyard purposes + + // + public Player(int money) { + wallet = new Wallet(money); + ownedShips = new ArrayList<>(); + shipPartInventory = new ArrayList<>(); + paintMode = PaintMode.SHIPYARD; + shipyardMode = ShipyardMode.CREATE_SHIP; + } + + public @NotNull Wallet getWallet() { + return wallet; + } + + // + public @NotNull List<@NotNull Spacecraft> getOwnedShips() { + return ownedShips; + } + + // + public void addShip(@NotNull Spacecraft ship) { + ownedShips.add(ship); + } + + // + public @NotNull List<@NotNull SpacecraftModule> getShipPartInventory() { + return shipPartInventory; + } + + // + public void addShipPart(@NotNull SpacecraftModule part) { + shipPartInventory.add(part); + } + + // + public void createNewShip(int partInventoryIndex) throws WrongModuleTypeException { + try { + @NotNull SpacecraftModule part = shipPartInventory.get(partInventoryIndex); + if (part instanceof StructuralModule core) { + addShip(new Spacecraft("New ship", core)); + shipPartInventory.remove(partInventoryIndex); + } else { + throw new WrongModuleTypeException(); + } + } catch (@NotNull IndexOutOfBoundsException e) { + throw new RuntimeException(e); + } + } + + // + public void addPartToConnection(@NotNull ModuleConnection connection, int partInventoryIndex) { + try { + @NotNull SpacecraftModule part = shipPartInventory.get(partInventoryIndex); + connection.setTetheredConnection(part); + shipPartInventory.remove(partInventoryIndex); + } catch (@NotNull IndexOutOfBoundsException e) { + throw new RuntimeException(e); + } + } + + // for graphical purposes + public final @NotNull PaintMode getPaintMode() { + return paintMode; + } + + // for graphical purposes + public final @NotNull ShipyardMode getShipyardMode() { + return shipyardMode; + } + + // for graphical purposes + public void setPaintMode(@NotNull PaintMode paintMode) { + this.paintMode = paintMode; + } + + // for graphical purposes + public void setShipyardMode(@NotNull ShipyardMode shipyardMode) { + printLine("Shipyard mode set to " + shipyardMode.toString()); + this.shipyardMode = shipyardMode; + } +} \ No newline at end of file diff --git a/src/test/java/test3/player/ShipyardMode.java b/src/test/java/test3/player/ShipyardMode.java new file mode 100644 index 0000000..cc19d85 --- /dev/null +++ b/src/test/java/test3/player/ShipyardMode.java @@ -0,0 +1,6 @@ +package test3.player; + +public enum ShipyardMode { + CREATE_SHIP, + ADD_MODULE +} diff --git a/src/test/java/test3/player/Wallet.java b/src/test/java/test3/player/Wallet.java new file mode 100644 index 0000000..1ffb2e7 --- /dev/null +++ b/src/test/java/test3/player/Wallet.java @@ -0,0 +1,31 @@ +package test3.player; + +// +public class Wallet { + private int money; + + // + protected Wallet(int money) { + this.money = money; + } + + // + public int getMoney() { + return money; + } + + //adds money; throws an exception, if negative result + public void addMoney(int money) throws InsufficientFundsException { + int result = this.money + money; + if (result < 0) { + throw new InsufficientFundsException(-result); + } else { + this.money = result; + } + } + + //subtracts money; throws an exception, if negative result + public void subtractMoney(int money) throws InsufficientFundsException { + this.addMoney(-money); + } +} \ No newline at end of file