added playerMap for location playerAssets

This commit is contained in:
Cedric Beck
2024-11-15 21:26:23 +01:00
parent 1cca5a9c6b
commit 81facb869f
4 changed files with 69 additions and 13 deletions

View File

@@ -5,9 +5,13 @@
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Spatial;
import pp.mdga.client.MdgaApp;
import pp.mdga.game.Color;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BoardView {
private static final float GRID_SIZE = 1.72f;
@@ -15,6 +19,7 @@ public class BoardView {
private static final int GRID_EXTEND = 5;
private static final String MAP_NAME = "map.mdga";
private final MdgaApp mdgaApp;
private PileControl drawPile = new PileControl();
@@ -23,35 +28,60 @@ public class BoardView {
private ArrayList<NodeControl> infield = new ArrayList<NodeControl>(40);
private ArrayList<PieceControl> pieces;
private Map<Color, List<AssetOnMap>> playerMap;
public BoardView(int playerCount, MdgaApp mdgaApp) {
assert(2 <= playerCount && playerCount <= 4);
assert(mdgaApp != null);
this.mdgaApp = mdgaApp;
pieces = new ArrayList<PieceControl>(4 * playerCount);
this.pieces = new ArrayList<>(4 * playerCount);
this.playerMap = new HashMap<>();
initMap();
}
private void addFigureToPlayerMap(Color col, AssetOnMap assetOnMap){
List<AssetOnMap> inMap = playerMap.getOrDefault(col, new ArrayList<>());
inMap.add(assetOnMap);
if(inMap.size() > 4) throw new RuntimeException("to many assets for one player");
playerMap.put(col, inMap);
}
private void initMap() {
List<AssetOnMap> assetOnMaps = new MapLoader().loadMap(MAP_NAME);
List<AssetOnMap> assetOnMaps = MapLoader.loadMap(MAP_NAME);
for (AssetOnMap assetOnMap : assetOnMaps){
int x = assetOnMap.x();
int y = assetOnMap.y();
Spatial model = createModel(assetOnMap.asset());
model.setLocalTranslation(gridToWorld(x,y));
switch (assetOnMap.asset()){
case lw -> addFigureToPlayerMap(assetToColor(Asset.lw), assetOnMap);
case heer -> addFigureToPlayerMap(assetToColor(Asset.heer), assetOnMap);
case cir -> addFigureToPlayerMap(assetToColor(Asset.cir), assetOnMap);
case marine -> addFigureToPlayerMap(assetToColor(Asset.marine), assetOnMap);
default -> displayAsset(assetOnMap);
}
}
}
private Spatial createModel(Asset asset){
private Color assetToColor(Asset asset){
return switch(asset){
case lw -> Color.AIRFORCE;
case heer -> Color.ARMY;
case marine -> Color.NAVY;
case cir -> Color.CYBER;
default -> null;
};
}
private Spatial createModel(Asset asset, Vector3f pos){
String modelName = asset.getModelPath();
String texName = asset.getDiffPath();
Spatial model = mdgaApp.getAssetManager().loadModel(modelName);
model.scale(asset.getSize());
model.rotate((float) Math.toRadians(0), 0, (float) Math.toRadians(90));
model.setLocalTranslation(pos);
model.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
Material mat = new Material(mdgaApp.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
mat.setTexture("DiffuseMap", mdgaApp.getAssetManager().loadTexture(texName));
@@ -66,4 +96,19 @@ private static Vector3f gridToWorld(int x, int y) {
return new Vector3f(GRID_SIZE * x, GRID_SIZE * y, GRID_ELEVATION);
}
public void addPlayer(Color color){
List<AssetOnMap> playerAssets = playerMap.get(color);
if (playerAssets == null) throw new RuntimeException("Assets for Player color are not defined");
for (AssetOnMap assetOnMap : playerAssets){
displayAsset(assetOnMap);
}
}
private void displayAsset(AssetOnMap assetOnMap){
int x = assetOnMap.x();
int y = assetOnMap.y();
Spatial model = createModel(assetOnMap.asset(), gridToWorld(x,y));
}
}

View File

@@ -8,13 +8,13 @@
import java.util.List;
public class MapLoader {
public MapLoader(){
private MapLoader(){
}
public List<AssetOnMap> loadMap(String mapName) {
public static List<AssetOnMap> loadMap(String mapName) {
List<AssetOnMap> assetsOnMap = new ArrayList<>();
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(mapName);
try (InputStream inputStream = MapLoader.class.getClassLoader().getResourceAsStream(mapName);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
while (true) {
@@ -49,8 +49,12 @@ public List<AssetOnMap> loadMap(String mapName) {
return assetsOnMap;
}
private Asset getLoadedAsset(String assetName) {
private static Asset getLoadedAsset(String assetName) {
return switch(assetName){
case "lw" -> Asset.lw;
case "cir" -> Asset.cir;
case "marine" -> Asset.marine;
case "heer" -> Asset.heer;
case "node" -> Asset.node_normal;
case "node_start" -> Asset.node_start;
case "node_bonus" -> Asset.node_bonus;

View File

@@ -15,6 +15,7 @@
import pp.mdga.client.Board.AssetOnMap;
import pp.mdga.client.Board.BoardView;
import pp.mdga.client.Board.MapLoader;
import pp.mdga.game.Color;
import java.util.List;
@@ -38,7 +39,7 @@ public static void main(String[] args) {
public void simpleInitApp() {
animationHandler = new AnimationHandler(this);
boardView = new BoardView(4,this);
// boardView.addPlayer(Color.NAVY);
flyCam.setEnabled(true);
int zoom = 20;
cam.setLocation(new Vector3f(zoom,0,zoom));

View File

@@ -1,5 +1,11 @@
world 0,0
#Marine Pos
marine 4,-3
marine 4,-4
marine 5,-4
marine 5,-3
#Nodes für Map
node_start -1,-5
node -1,-4