diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/InputSynchronizer.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/InputSynchronizer.java index fe2b1d01..99d22d10 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/InputSynchronizer.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/InputSynchronizer.java @@ -127,8 +127,8 @@ private T checkHover(Camera cam, Node root, Class CollisionResults results = new CollisionResults(); Ray ray = new Ray(cam.getLocation(), getMousePos(cam).subtract(cam.getLocation()).normalize()); root.collideWith(ray, results); - if (results.size() > 0) { - return results.getClosestCollision().getGeometry().getControl(controlType); + for(CollisionResult collisionResult : results){ + if(collisionResult.getGeometry().getControl(controlType) != null) return collisionResult.getGeometry().getControl(controlType); } return null; } diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/MdgaApp.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/MdgaApp.java index 57a1d247..6b53b07e 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/MdgaApp.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/MdgaApp.java @@ -1,7 +1,6 @@ package pp.mdga.client; import com.jme3.app.SimpleApplication; -import com.jme3.renderer.RenderManager; import com.simsilica.lemur.GuiGlobals; import pp.mdga.client.acoustic.AcousticHandler; import pp.mdga.client.animation.AnimationHandler; @@ -19,7 +18,7 @@ public class MdgaApp extends SimpleApplication { private AcousticHandler acousticHandler; private NotificationSynchronizer notificationSynchronizer; private InputSynchronizer inputSynchronizer; - private ModelSyncronizer modelSyncronizer; + private ModelSynchronizer modelSynchronizer; MdgaView view = null; private MdgaState state = MdgaState.GAME; @@ -52,7 +51,7 @@ public void simpleInitApp() { acousticHandler = new AcousticHandler(this); notificationSynchronizer = new NotificationSynchronizer(this); inputSynchronizer = new InputSynchronizer(this); - modelSyncronizer = new ModelSyncronizer(this); + modelSynchronizer = new ModelSynchronizer(this); inputManager.deleteMapping("SIMPLEAPP_Exit"); flyCam.setEnabled(false); @@ -90,19 +89,19 @@ public void simpleInitApp() { ((GameView) view).setOwnColor(Color.NAVY); - notificationSynchronizer.addTestNotification(new PlayerInGameNotification(Color.AIRFORCE, test, "Player 1")); - notificationSynchronizer.addTestNotification(new PlayerInGameNotification(Color.NAVY, test_1, "Player 2")); - notificationSynchronizer.addTestNotification(new PlayerInGameNotification(Color.ARMY, test_2, "Player 3")); - notificationSynchronizer.addTestNotification(new PlayerInGameNotification(Color.CYBER, test_3, "Player 4")); +// notificationSynchronizer.addTestNotification(new PlayerInGameNotification(Color.AIRFORCE, test, "Player 1")); +// notificationSynchronizer.addTestNotification(new PlayerInGameNotification(Color.NAVY, test_1, "Player 2")); +// notificationSynchronizer.addTestNotification(new PlayerInGameNotification(Color.ARMY, test_2, "Player 3")); +// notificationSynchronizer.addTestNotification(new PlayerInGameNotification(Color.CYBER, test_3, "Player 4")); - notificationSynchronizer.addTestNotification(new MovePieceNotification(player0, 0, true)); - notificationSynchronizer.addTestNotification(new MovePieceNotification(player0_1, 20, true)); - notificationSynchronizer.addTestNotification(new MovePieceNotification(player0_1, 20, 21)); - notificationSynchronizer.addTestNotification(new MovePieceNotification(player1, 0, true)); - notificationSynchronizer.addTestNotification(new MovePieceNotification(player0, 0, 7)); +// notificationSynchronizer.addTestNotification(new MovePieceNotification(player0, 0, true)); +// notificationSynchronizer.addTestNotification(new MovePieceNotification(player0_1, 20, true)); +// notificationSynchronizer.addTestNotification(new MovePieceNotification(player0_1, 20, 21)); +// notificationSynchronizer.addTestNotification(new MovePieceNotification(player1, 0, true)); +// notificationSynchronizer.addTestNotification(new MovePieceNotification(player0, 0, 7)); // notificationSynchronizer.addTestNotification(new SelectableMoveNotification(new ArrayList<>(List.of(player0, player1)), new ArrayList<>(List.of(7,3)), new ArrayList<>(List.of(false, true)))); // notificationSynchronizer.addTestNotification(new SwapPieceNotification(player0, player0_1)); - notificationSynchronizer.addTestNotification(new SelectableSwapNotification(new ArrayList<>(List.of(player0, player1)), new ArrayList<>(List.of(player0_1)))); +// notificationSynchronizer.addTestNotification(new SelectableSwapNotification(new ArrayList<>(List.of(player0, player1)), new ArrayList<>(List.of(player0_1)))); } @Override @@ -163,8 +162,8 @@ public MdgaView getView() { return view; } - public ModelSyncronizer getModelSyncronizer() { - return modelSyncronizer; + public ModelSynchronizer getModelSyncronizer() { + return modelSynchronizer; } public InputSynchronizer getInputSyncronizer() { return inputSynchronizer; } diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSyncronizer.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSynchronizer.java similarity index 96% rename from Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSyncronizer.java rename to Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSynchronizer.java index fae79021..8488648d 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSyncronizer.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/ModelSynchronizer.java @@ -3,10 +3,10 @@ import pp.mdga.client.view.LobbyView; import pp.mdga.game.Color; -public class ModelSyncronizer { +public class ModelSynchronizer { private MdgaApp app; - ModelSyncronizer(MdgaApp app) { + ModelSynchronizer(MdgaApp app) { this.app = app; } diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/NotificationSynchronizer.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/NotificationSynchronizer.java index 331fc088..fbd9a178 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/NotificationSynchronizer.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/NotificationSynchronizer.java @@ -55,11 +55,9 @@ private void handleMain(Notification notification) { private void handleLobby(Notification notification) { LobbyView lobbyView = (LobbyView) app.getView(); - if (notification instanceof TskSelectNotification) { - TskSelectNotification n = (TskSelectNotification)notification; + if (notification instanceof TskSelectNotification n) { lobbyView.setTaken(n.getColor(), true, n.isSelf(), n.getName()); - } else if (notification instanceof TskUnselectNotification) { - TskUnselectNotification n = (TskUnselectNotification)notification; + } else if (notification instanceof TskUnselectNotification n) { lobbyView.setTaken(n.getColor(), false, false, null); } else if (notification instanceof GameNotification) { app.enter(MdgaState.GAME); @@ -114,6 +112,7 @@ private void handleGame(Notification notification) { boardHandler.addPlayer(n.getColor(),n.getPiecesList()); guiHandler.addPlayer(n.getColor(),n.getName()); } else if (notification instanceof ResumeNotification) { + //TODO } else if (notification instanceof RollDiceNotification n) { if(n.getColor() == gameView.getOwnColor()){ guiHandler.rollDice(n.getEyes(), n.isTurbo() ? n.getMultiplier() : -1); diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/board/BoardHandler.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/board/BoardHandler.java index 3f9d215e..cd26a2d2 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/board/BoardHandler.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/board/BoardHandler.java @@ -21,9 +21,6 @@ public class BoardHandler { private final MdgaApp app; - private final PileControl drawPile = null; - private final PileControl discardPile = null; - private ArrayList infield; private Map pieces; @@ -32,7 +29,6 @@ public class BoardHandler { private Map> waitingNodesMap; private Map> waitingPiecesMap; private Map pieceColor; - private Set outlineControls; private Node node; @@ -40,11 +36,9 @@ public class BoardHandler { private boolean isInitialised; - private boolean scheduleInit = false; - private boolean scheduleShutdown = false; - - private List selectableOwnPieces = new ArrayList<>(); - private List selectableEnemyPieces = new ArrayList<>(); + private List selectableOwnPieces; + private List selectableEnemyPieces; + private List outlineNodes; private PieceControl selectedOwnPiece; private PieceControl selectedEnemyPiece; private DiceControl diceControl; @@ -52,12 +46,29 @@ public class BoardHandler { public BoardHandler(MdgaApp app, FilterPostProcessor fpp) { if(app == null) throw new RuntimeException("app is null"); - this.isInitialised = false; this.app = app; this.fpp = fpp; - selectedEnemyPiece = null; + isInitialised = false; + + node = new Node("BoardHandler root node"); + } + + public void init() { + isInitialised = true; + selectableOwnPieces = new ArrayList<>(); + selectableEnemyPieces = new ArrayList<>(); + outlineNodes = new ArrayList<>(); selectedOwnPiece = null; + selectedEnemyPiece = null; initMap(); + app.getRootNode().attachChild(node); + } + + public void shutdown(){ + clearSelectable(); + isInitialised = false; + initMap(); + app.getRootNode().detachChild(node); } private void addFigureToPlayerMap(Color col, AssetOnMap assetOnMap) { @@ -66,25 +77,16 @@ private void addFigureToPlayerMap(Color col, AssetOnMap assetOnMap) { } private void initMap() { - if (isInitialised) return; - - this.isInitialised = true; - this.node = new Node("Asset Node"); - - this.pieces = new HashMap<>(); - this.colorAssetsMap = new HashMap<>(); - this.infield = new ArrayList<>(40); - this.homeNodesMap = new HashMap<>(); - this.waitingNodesMap = new HashMap<>(); - this.waitingPiecesMap = new HashMap<>(); - this.pieceColor = new HashMap<>(); - this.outlineControls = new HashSet<>(); + pieces = new HashMap<>(); + colorAssetsMap = new HashMap<>(); + infield = new ArrayList<>(40); + homeNodesMap = new HashMap<>(); + waitingNodesMap = new HashMap<>(); + waitingPiecesMap = new HashMap<>(); + pieceColor = new HashMap<>(); diceControl = new DiceControl(app.getAssetManager()); diceControl.create(new Vector3f(0,0,0), 0.7f, true); - - - List assetOnMaps = MapLoader.loadMap(MAP_NAME); for (AssetOnMap assetOnMap : assetOnMaps) { @@ -168,7 +170,7 @@ private float getRotationMove(Vector3f prev, Vector3f next) { return newRot; } - private void movePiece_rek(UUID uuid, int curIndex, int moveIndex){ + private void movePieceRek(UUID uuid, int curIndex, int moveIndex){ if (curIndex == moveIndex) return; curIndex = (curIndex + 1) % infield.size(); @@ -180,7 +182,7 @@ private void movePiece_rek(UUID uuid, int curIndex, int moveIndex){ movePieceToNode(pieceControl, nodeControl); - movePiece_rek(uuid, curIndex, moveIndex); + movePieceRek(uuid, curIndex, moveIndex); } private List addItemToMapList(Map> map, E key, T item){ @@ -190,11 +192,10 @@ private List addItemToMapList(Map> map, E key, T item){ return list; } - private List removeItemFromMapList(Map> map, E key, T item){ + private void removeItemFromMapList(Map> map, E key, T item){ List list = map.getOrDefault(key, new ArrayList<>()); list.remove(item); map.put(key, list); - return list; } private Vector3f getWaitingPos(Color color){ @@ -273,7 +274,7 @@ public void movePieceStart(UUID uuid, int nodeIndex){ public void movePiece(UUID uuid, int curIndex, int moveIndex){ if (!isInitialised) throw new RuntimeException("BoardHandler is not initialized"); - movePiece_rek(uuid, curIndex, moveIndex); + movePieceRek(uuid, curIndex, moveIndex); } public void throwPiece(UUID uuid){ @@ -312,47 +313,25 @@ public void suppressShield(UUID uuid){ public void swapPieces(UUID piece1, UUID piece2){ if (!isInitialised) throw new RuntimeException("BoardHandler is not initialized"); - PieceControl piece1_control = pieces.get(piece1); - PieceControl piece2_control = pieces.get(piece2); + PieceControl piece1Control = pieces.get(piece1); + PieceControl piece2Control = pieces.get(piece2); - if(piece1_control == null) throw new RuntimeException("piece1 UUID is not valid"); - if(piece2_control == null) throw new RuntimeException("piece2 UUID is not valid"); + if(piece1Control == null) throw new RuntimeException("piece1 UUID is not valid"); + if(piece2Control == null) throw new RuntimeException("piece2 UUID is not valid"); - float rot1 = piece1_control.getRotation(); - float rot2 = piece2_control.getRotation(); + float rot1 = piece1Control.getRotation(); + float rot2 = piece2Control.getRotation(); - piece1_control.setRotation(rot2); - piece2_control.setRotation(rot1); + piece1Control.setRotation(rot2); + piece2Control.setRotation(rot1); - Vector3f pos1 = piece1_control.getLocation().clone(); - Vector3f pos2 = piece2_control.getLocation().clone(); + Vector3f pos1 = piece1Control.getLocation().clone(); + Vector3f pos2 = piece2Control.getLocation().clone(); - piece1_control.setLocation(pos2); - piece2_control.setLocation(pos1); + piece1Control.setLocation(pos2); + piece2Control.setLocation(pos1); } - public void init() { -// outlineControls.forEach((outlineControl) -> { -// outlineControl.outline(outlineControl.getColor()); -// }); - - isInitialised = true; - scheduleInit = false; - app.getRootNode().attachChild(node); - } - - public void shutdown(){ - outlineControls.forEach((outlineControl) -> { - outlineControl.deOutline(); - }); - - isInitialised = false; - scheduleShutdown = false; - app.getRootNode().detachChild(node); - } - - //List - //List public void highlight(UUID uuid, boolean bool){ if (!isInitialised) throw new RuntimeException("BoardHandler is not initialized"); @@ -361,23 +340,6 @@ public void highlight(UUID uuid, boolean bool){ } - public void deOutline(UUID uuid){ - if (!isInitialised) throw new RuntimeException("BoardHandler is not initialized"); - - pieces.get(uuid).deOutline(); - - outlineControls.remove(pieces.get(uuid)); - } - - public void outline(int index){ -// infield.get(index).outline(); - - outlineControls.add(infield.get(index)); - } - - - - //called when (dice) moveNum is received from server to display the movable pieces and corresponding moveNodes public void outlineMove(List pieces, List moveIndexe, List homeMoves) { if(pieces.size() != moveIndexe.size() || pieces.size() != homeMoves.size()) throw new RuntimeException("arrays are not the same size"); @@ -403,6 +365,7 @@ public void outlineMove(List pieces, List moveIndexe, List bonusCardControlMap; - private Map bonusCardIntegerMap; - - private static final Vector3f START = new Vector3f(-1.8f,-3.5f,0); - private static final Vector3f MARGIN = new Vector3f(1.8f,0,0); - - private final FilterPostProcessor fpp; - Texture2D backTexture; - private Camera cardLayerCamera; - private DiceControl diceControl; - - private Set selectableCards = new HashSet<>(); - private BonusCard cardSelect = null; - private PlayerNameHandler playerNameHandler; - private ActionTextHandler actionTextHandler; - - private static final float CARDLAYER_CAMERA_ZOOM = 4; - - public GuiHandler_old(MdgaApp app, Texture2D backTexture) { - this.app = app; - this.fpp = new FilterPostProcessor(app.getAssetManager()); - this.backTexture = backTexture; - cardLayerCamera = createOverlayCam(); - bonusCardIntegerMap = new HashMap<>(); - bonusCardControlMap = new HashMap<>(); - } - - private Asset bonusToAsset(BonusCard card){ - return switch (card){ - case TURBO -> Asset.turboCard; - case SHIELD -> Asset.shieldCard; - case SWAP -> Asset.swapCard; - case HIDDEN -> throw new RuntimeException("HIDDEN is not allowed in GUI"); - }; - } - - private Vector3f nextPos() { - return START.add(MARGIN.mult(bonusCardControlMap.size())); - } - - - - private Camera createOverlayCam(){ - Camera originalCam = app.getCamera(); - Camera overlayCam = new Camera(originalCam.getWidth(), originalCam.getHeight()); - overlayCam.setParallelProjection(true); - final float aspect = (float) originalCam.getWidth() / originalCam.getHeight(); - final float size = CARDLAYER_CAMERA_ZOOM; - overlayCam.setFrustum(-1000, 1000, -aspect * size, aspect * size, size, -size); - overlayCam.setLocation(new Vector3f(0, 0, 10)); - overlayCam.lookAt(new Vector3f(0,0,0), Vector3f.UNIT_Y); - return overlayCam; - } - - private K getKeyByValue(Map map, V value) { - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue().equals(value)) { - return entry.getKey(); - } - } - return null; // Key not found - } - - private SymbolControl createSymbol(Asset asset){ - Spatial spatial = app.getAssetManager().loadModel(asset.getModelPath()); - Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); - mat.setTexture("ColorMap", app.getAssetManager().loadTexture(asset.getDiffPath())); - spatial.setMaterial(mat); - spatial.setLocalScale(1f); - spatial.rotate((float)Math.toRadians(90), (float)Math.toRadians(180), (float)Math.toRadians(180)); - SymbolControl control = new SymbolControl(); - spatial.addControl(control); - return control; - } - - //Public methods**************************************************************************************************** - - public void init(){ - cardLayer = new CardLayer(fpp, cardLayerCamera, backTexture); - app.getStateManager().attach(cardLayer); - diceControl = new DiceControl(app.getAssetManager()); - diceControl.create(new Vector3f(0,0,0), 1f, false); - - playerNameHandler = new PlayerNameHandler(app.getGuiNode(), app.getAssetManager(), app.getContext().getSettings()); - actionTextHandler = new ActionTextHandler(app.getGuiNode(), app.getAssetManager(), app.getContext().getSettings()); - } - - public void shutdown(){ - if(cardLayer != null){ - cardLayer.shutdown(); - } - - cardLayer = null; - } - - public void rollDice(int rollNum){ - if(!(1 <= rollNum && rollNum <= 6)) throw new RuntimeException("rollNum is not in the range [1,6]"); - diceControl.rollDice(rollNum); - } - - public void showDice(){ - cardLayer.addSpatial(diceControl.getSpatial()); - diceControl.spin(); - } - - public void addCard(BonusCard card) { - if(card == BonusCard.HIDDEN) throw new RuntimeException("cant add hidden card to gui"); - - if(!bonusCardControlMap.containsKey(card)) { - CardControl control = createCard(bonusToAsset(card), nextPos()); - bonusCardControlMap.put(card, control); - cardLayer.addSpatial(control.getRoot()); - } - - int newNum = bonusCardIntegerMap.getOrDefault(card,0) + 1; - bonusCardIntegerMap.put(card, newNum); - bonusCardControlMap.get(card).setNumCard(newNum); - } - - public void hideDice(){ - diceControl.hide(); - } - - public void addPlayer(Color color, String name, boolean own){ - playerNameHandler.addPlayer(color, name, own); - } - - public void setActivePlayer(Color color){ - playerNameHandler.setActivePlayer(color); - } - - public Camera getCardLayerCamera() { - return cardLayerCamera; - } - - public CardLayer getCardLayer(){ - return cardLayer; - } - - public void clearSelectableCards(){ - for(CardControl control : selectableCards){ - control.setSelectable(false); - control.setHoverable(false); - control.unHighlight(); - control.unSelect(); - } - selectableCards.clear(); - cardSelect = null; - } - - public void setSelectableCards(List select) { - for(BonusCard uuid : select) { - selectableCards.add(bonusCardControlMap.get(uuid)); - } - for(CardControl control : selectableCards){ - control.setSelectable(true); - control.setHoverable(true); - control.setHighlight(); - } - } - - public void selectCard(CardControl cardControl) { - if(cardControl.isSelected()) { - cardControl.unSelect(); - cardSelect = null; - } - else { - for (CardControl control : selectableCards){ - control.unSelect(); - } - cardControl.select(); - cardSelect = getKeyByValue(bonusCardControlMap, cardControl); - } - - } - - public void shield(){ - SymbolControl control = createSymbol(Asset.shieldSymbol); - cardLayer.addSpatial(control.getSpatial()); - control.shield(); - } - - public void swap(){ - SymbolControl control = createSymbol(Asset.swapSymbol); - cardLayer.addSpatial(control.getSpatial()); - control.swap(); - } - - public void turbo(){ - SymbolControl control = createSymbol(Asset.turboSymbol); - cardLayer.addSpatial(control.getSpatial()); - control.turbo(); - } - - public void test(){ - addCard(BonusCard.SHIELD); - addCard(BonusCard.SHIELD); - addCard(BonusCard.TURBO); - addCard(BonusCard.SWAP); - -// setSelectableCards(List.of(uuid,uuid1)); -// showDice(); - } - - public void test2(){ - actionTextHandler.drawCardOwn(Color.NAVY); - } - - private CardControl createCard(Asset card, Vector3f pos){ - Node rootCard = new Node("Root Card"); - Spatial spatial = app.getAssetManager().loadModel(card.getModelPath()); - rootCard.attachChild(spatial); - Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md"); - mat.setTexture("DiffuseMap", app.getAssetManager().loadTexture(card.getDiffPath())); - spatial.setMaterial(mat); - spatial.setLocalScale(1f); - rootCard.setLocalTranslation(pos); - spatial.rotate((float)Math.toRadians(90), (float)Math.toRadians(180), (float)Math.toRadians(180)); - spatial.setShadowMode(RenderQueue.ShadowMode.CastAndReceive); - CardControl control = new CardControl(app, fpp, cardLayer.getOverlayCam(), rootCard); - spatial.addControl(control); - return control; - } -}