Skip to content

Commit

Permalink
Fix tests and fix picture loading, controller DI in Pump.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Takibalu committed May 20, 2024
1 parent 70a2cf2 commit 10eaf90
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 90 deletions.
20 changes: 7 additions & 13 deletions src/main/java/Controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import Model.*;
import View.*;
import View.Window;

import java.awt.*;
import java.io.*;
Expand Down Expand Up @@ -76,10 +75,6 @@ static public Controller getInstance() {
* Ciszternáknál a csövek nevének generálásához használt futóváltozó
*/
public int createdPipeNumber = 1;
/**
* Szöveg méretezésére használt változó, mennyisége a kicsi map-kép hossza
*/
private int scaleForText;
/**
* Az új játék ablaknál létrehozott szerelők száma-1(indexe)
*/
Expand Down Expand Up @@ -207,7 +202,7 @@ public void create(String name, String type,int x, int y) {
gameView.addSaboteurView(saboteurView);
}
case "pump" -> {
Pump pump = new Pump();
Pump pump = new Pump(Controller.getInstance());
steppables.add(pump);
nodes.add(pump);
pumps.add(pump);
Expand Down Expand Up @@ -296,7 +291,7 @@ public void move(String playerName, String steppableName) {
IO_Manager.write(steppableName + ".players = " + listWrite(s.getPlayers()), Controller.filetoWrite != null);
IO_Manager.write(playerName + ".standingOn = " + steppableName, Controller.filetoWrite != null);
if (prev != null)
if (prev.getPlayers().size() == 0)
if (prev.getPlayers().isEmpty())
IO_Manager.write(getObjectName(prev) + ".players = null", Controller.filetoWrite != null);
else
IO_Manager.write(getObjectName(prev) + ".players = " + listWrite(prev.getPlayers()), Controller.filetoWrite != null);
Expand Down Expand Up @@ -738,8 +733,7 @@ public void load(String filename) {
try {
FileInputStream fis = new FileInputStream("program.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Controller c2 = (Controller) ois.readObject();
controller = c2;
controller = (Controller) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -826,16 +820,16 @@ public void startGame() {
* @return - a kiiratott szöveg
*/
private String listWrite(LinkedList<?> list) {
String out = "";
StringBuilder out = new StringBuilder();
try {
for (int i = 0; i < list.size(); i++) {
out += getObjectName(list.get(i));
if (i != list.size() - 1) out += ", ";
out.append(getObjectName(list.get(i)));
if (i != list.size() - 1) out.append(", ");
}
} catch (Exception e) {
e.printStackTrace();
}
return out;
return out.toString();

}

Expand Down
27 changes: 21 additions & 6 deletions src/main/java/Model/Pump.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ public class Pump extends WaterNode implements PickupAble, Serializable {
*/
private Pipe activeOut = null;

private Controller controller;

public Pump(Controller controller) {
this.controller = controller;
}


public Controller getController() {
return controller;
}

public void setController(Controller controller) {
this.controller = controller;
}

/**
* Akkor hívódik, ha egy játékos felveszi ezt a pumpát(amit nem tud)
* @param from melyik elemről vesszük le / csatlakoztatjuk le a pumpát
Expand Down Expand Up @@ -103,7 +118,7 @@ public boolean PlacedDownTo(Pump pickup) {
public void WaterFlow() {

if (broken) {
IO_Manager.writeInfo(Controller.getInstance().getObjectName(this) + " is broken", Controller.filetoWrite != null);
IO_Manager.writeInfo(controller.getObjectName(this) + " is broken", controller.filetoWrite != null);
}
int gained=0;
if(!broken && heldWater != 0 && activeOut != null)
Expand All @@ -112,9 +127,9 @@ public void WaterFlow() {
if(gained==1){
heldWater--;
}
IO_Manager.write(Controller.getInstance().getObjectName(activeOut) + " gained " + gained, Controller.filetoWrite != null);
IO_Manager.write(controller.getObjectName(activeOut) + " gained " + gained, controller.filetoWrite != null);
} else if (heldWater == 0) {
IO_Manager.writeInfo(Controller.getInstance().getObjectName(this) + " is empty", Controller.filetoWrite != null);
IO_Manager.writeInfo(controller.getObjectName(this) + " is empty", controller.filetoWrite != null);
}

if (activeIn != null) {
Expand All @@ -123,7 +138,7 @@ public void WaterFlow() {
int excess = (heldWater + lost > waterCapacity) ? (heldWater + lost - waterCapacity) : 0;
activeIn.GainWater(excess);
heldWater +=lost-excess;
IO_Manager.write(Controller.getInstance().getObjectName(activeIn) + " lost " + (lost - excess), Controller.filetoWrite != null);
IO_Manager.write(controller.getObjectName(activeIn) + " lost " + (lost - excess), controller.filetoWrite != null);
}
}

Expand Down Expand Up @@ -215,8 +230,8 @@ public boolean AddPipe(Pipe p) {
if(valid)
pipes.add(p);
else
IO_Manager.writeInfo("Can't place " + Controller.getInstance().getObjectName(p) + " here, because " +
Controller.getInstance().getObjectName(this) + " reached pipe capacity", Controller.filetoWrite != null);
IO_Manager.writeInfo("Can't place " + controller.getObjectName(p) + " here, because " +
controller.getObjectName(this) + " reached pipe capacity", controller.filetoWrite != null);
return valid;

}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/View/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public void clickAction(MouseEvent e) {
if(this.option == WindowOptions.map1){
this.option = WindowOptions.map2;
Controller.getInstance().getNewGameView().setMap(2);
img = ImageUtility.scaleImage(mappreview2, 200);
img = ImageUtility.scaleImage(getMapPreview2(), 200);
return;
}
if(this.option == WindowOptions.map2){
this.option = WindowOptions.map1;
Controller.getInstance().getNewGameView().setMap(1);
img = ImageUtility.scaleImage(mapPrevButton, 200);
img = ImageUtility.scaleImage(getMapPrevButton(), 200);
return;
}
// játék indítása
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/View/CisternView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CisternView(int x, int y, int r, Cistern c, Window v) {
super(x, y, v);
this.r = r;
cistern = c;
sprite = Pictures.cisternFilledImg;
sprite = Pictures.getCisternFilledImg();
sprite = ImageUtility.scaleImage(sprite, 2*r);
if (WindowOptions.windowOption == WindowOptions.game)
gameView = (GameView) v;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/View/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setFrame(AppFrame frame) {
public GameView(AppFrame frame) {
this.frame = frame;
exit = new Button(frame.getWidth() - 80, 10, 50, 50, this);
exit.img = ImageUtility.scaleImage(exitButton, 45);
exit.img = ImageUtility.scaleImage(getExitButton(), 45);
exit.option = WindowOptions.exit;
addDrawable(exit, true);
addClickable(exit, true);
Expand Down Expand Up @@ -112,7 +112,7 @@ else if (PointCounter.getInstance().GetMechanicPoints() == PointCounter.getInsta
g.setColor(Color.BLACK);
g.setFont(new Font("Inter", Font.BOLD, 50));
g.drawString(Integer.toString(PointCounter.getInstance().GetMechanicPoints()), Controller.getInstance().getFrame().getWidth() - 450 + 50, 55);
g.drawImage(ImageUtility.scaleImage(scoreDivider, 35), Controller.getInstance().getFrame().getWidth() - 300, 20, null);
g.drawImage(ImageUtility.scaleImage(getScoreDivider(), 35), Controller.getInstance().getFrame().getWidth() - 300, 20, null);
g.drawString(Integer.toString(PointCounter.getInstance().GetSaboteurPoints()), Controller.getInstance().getFrame().getWidth() - 450 + 200, 55);

for (Drawable d : drawables) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/View/ImageUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public static BufferedImage ImageLoad(String file) {
BufferedImage image = null;
try {

String nameOfFile = System.getProperty("user.dir") + File.separator + "src" + File.separator + "Assets" + File.separator + file;
String nameOfFile = System.getProperty("user.dir") + File.separator + "src" + File.separator + "main"
+ File.separator + "java" + File.separator + "Assets" + File.separator + file;
File in = new File(nameOfFile);

image = ImageIO.read(in);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/View/MechanicView.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void setColor(Color color) {
this.color = color;
}

Image imageForScale = ImageUtility.scaleImage(mapPrevButton, 200);
Image imageForScale = ImageUtility.scaleImage(getMapPrevButton(), 200);

private int number;

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/View/MenuView.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class MenuView extends Window {

Button newGame, exit;

private BufferedImage background = ImageUtility.backgroundScale(Pictures.background);
private BufferedImage bgText = ImageUtility.scaleImage(menuTitle, 250);
private BufferedImage background = ImageUtility.backgroundScale(getBackground());
private BufferedImage bgText = ImageUtility.scaleImage(getMenuTitle(), 250);

/**
* Konstruktor
Expand All @@ -26,12 +26,12 @@ public MenuView() {
exit = new Button(760, 370, 250 + 250, 50, this);

newGame.text = "Új játék";
newGame.img = ImageUtility.scaleImage(playButton, 50);
newGame.img = ImageUtility.scaleImage(getPlayButton(), 50);

newGame.option = WindowOptions.newgame;

exit.text = "Kilépés";
exit.img = ImageUtility.scaleImage(exitButton, 50);
exit.img = ImageUtility.scaleImage(getExitButton(), 50);
exit.option = WindowOptions.exit;
addDrawable(newGame, true);
addClickable(newGame, true);
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/View/NewGameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public NewGameView() {
exit = new Button(screenWidth - 100, 10, 150, 150, this);
map = new Button(50, screenHeight / 2, 500, 300, this);
map.option = WindowOptions.map1;
map.img = ImageUtility.scaleImage(mapPrevButton, 200);
map.img = ImageUtility.scaleImage(getMapPrevButton(), 200);

int offset = 25;
addSaboteur = new AddPlayerButton(map.x+map.img.getWidth()+140+220, screenHeight / 2 - 35 + 190 + offset, 35, 35, this, 1, "saboteur");
Expand All @@ -59,14 +59,14 @@ public NewGameView() {
increase = new ChangeMaxPointButton(screenWidth / 2 + 16 * 13, screenHeight / 6 - 8, 35, 35, this, 1);
decrease = new ChangeMaxPointButton(screenWidth / 2 + 20 * 13, screenHeight / 6 - 8, 35, 35, this, -1);

start.img = ImageUtility.scaleImage(playButton, 50);
start.img = ImageUtility.scaleImage(getPlayButton(), 50);
start.option = WindowOptions.game;
exit.img = ImageUtility.scaleImage(exitButton, 50);
exit.img = ImageUtility.scaleImage(getExitButton(), 50);
exit.option = WindowOptions.exit;
addMechanic.img = ImageUtility.scaleImage(plusButton, 35);
addSaboteur.img = ImageUtility.scaleImage(plusButton, 35);
increase.img = ImageUtility.scaleImage(plusButton, 35);
decrease.img = ImageUtility.scaleImage(minusButton, 35);
addMechanic.img = ImageUtility.scaleImage(getPlusButton(), 35);
addSaboteur.img = ImageUtility.scaleImage(getPlusButton(), 35);
increase.img = ImageUtility.scaleImage(getPlusButton(), 35);
decrease.img = ImageUtility.scaleImage(getMinusButton(), 35);

addDrawable(start, true);
addDrawable(exit, true);
Expand Down Expand Up @@ -115,7 +115,6 @@ public void paint(Graphics g) {
g.drawString(points, screenWidth / 2 + 75, screenHeight / 6 + 25);
g.drawString(pointsToWin, screenWidth / 2, screenHeight / 6 - 35);
// betűméret és típus beállítása
fontSize = 30;
g.setFont(new Font("Inter", Font.BOLD, fontSize));
// Játékosok hozzáadása felirat kirajzolása
// Felvett játékosok kirajzolása
Expand Down
Loading

0 comments on commit 10eaf90

Please sign in to comment.