package com.trs.main; import com.trs.main.worldobjects.MapCollisionObject; import com.trs.main.worldobjects.Player; import com.trs.main.worldobjects.Hostile; import com.trs.main.worldobjects.InteractionObject; import com.trs.main.worldobjects.MovingNpc; import com.trs.main.fightscreen.Enemy; import com.trs.main.fightscreen.FightPlayer; import com.trs.main.fightscreen.FightObject; import com.trs.main.fightscreen.FightScreen; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import java.util.ArrayList; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.maps.MapObject; import com.badlogic.gdx.maps.MapProperties; import com.badlogic.gdx.maps.objects.RectangleMapObject; import com.badlogic.gdx.maps.tiled.TiledMap; import com.badlogic.gdx.maps.tiled.TmxMapLoader; import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.utils.viewport.FitViewport; /** * * Layer 0: tilelayer under Player 0 * Layer 1: tilelayer under Player 1 * Layer 2: tilelayer under Player 2 * Layer 3: tilelayer above Player 0 * Layer 4: tilelayer above Player 1 * Layer 5: CollisionRects * Layer 6: DoorRects * destinationDoor * destinationMap * exit * id * Layer 7: InteractionObjects * id * texture * Layer 8: NpcRects * id * texture * * @author Jan */ public class MapContainer { Stage stage; OrthographicCamera camera; TmxMapLoader maploader; TiledMap map; OrthogonalTiledMapRenderer renderer; Door[] doors; public Door collidingDoor; FightScreen fs; TransitionScreen t; final int[] layersBelowPlayer = {0, 1, 2}; final int[] layersAbovePlayer = {3, 4}; // TODO: Value which shows from which door the player is coming? public MapContainer(float CAMERA_WIDTH, float CAMERA_HEIGHT, Player p, String mapString, int inDoor, int mapId) { // CREATION OF STAGE camera = new OrthographicCamera(); camera.setToOrtho(false, CAMERA_WIDTH, CAMERA_HEIGHT); camera.update(); stage = new Stage(new FitViewport(CAMERA_WIDTH, CAMERA_HEIGHT, camera)); Gdx.input.setInputProcessor(stage); //TRANSITION SCREEN t = new TransitionScreen(0.01f); //CREATION OF TILEDMAP maploader = new TmxMapLoader(); map = maploader.load(mapString); renderer = new OrthogonalTiledMapRenderer(map); renderer.setView((OrthographicCamera)stage.getCamera()); stage.getCamera().update(); // adding MapObjects to the Stage for(MapObject object : map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)){ Rectangle rect = ((RectangleMapObject) object).getRectangle(); stage.addActor(new MapCollisionObject((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight())); } // adding the door links ArrayList tempDoors = new ArrayList<>(); for(MapObject object : map.getLayers().get(6).getObjects().getByType(RectangleMapObject.class)){ Rectangle rect = ((RectangleMapObject) object).getRectangle(); MapProperties props = object.getProperties(); int id = props.get("id", Integer.class); int exit = props.get("exit", Integer.class); int destinationMap = props.get("destinationMap", Integer.class); int destinationDoor = props.get("destinationDoor", Integer.class); Door door = new Door(id, exit, destinationMap, destinationDoor, rect); tempDoors.add(door); } // adding the Npcs for(MapObject object : map.getLayers().get(8).getObjects().getByType(RectangleMapObject.class)){ Rectangle rect = ((RectangleMapObject) object).getRectangle(); MapProperties props = object.getProperties(); int id = props.get("id", Integer.class); String texture = props.get("texture", String.class); stage.addActor(new MovingNpc(rect, rect.getX() + (float)(Math.random()*(rect.getWidth()-64)), rect.getY()+(float)(Math.random()*(rect.getHeight()-64)), id, mapId, texture)); } // adding the InteractionObjects for(MapObject object : map.getLayers().get(7).getObjects().getByType(RectangleMapObject.class)){ Rectangle rect = ((RectangleMapObject) object).getRectangle(); MapProperties props = object.getProperties(); int id = props.get("id", Integer.class); String texture = props.get("texture", String.class); if(texture.equals("-")){ stage.addActor(new InteractionObject(rect, rect.getX(), rect.getY(), mapId, id)); } else{ stage.addActor(new InteractionObject(rect, rect.getX(), rect.getY(), mapId, id, texture)); } stage.addActor(new MapCollisionObject((int) rect.x, (int) rect.y, (int) rect.width, (int) rect.height)); } doors = new Door[tempDoors.size()]; for(int i = 0; i < doors.length; i++) { doors[i] = tempDoors.get(i); if(doors[i].id == inDoor) { int facing = doors[i].exit; System.out.println(i + " " + inDoor); switch(facing) { case 0: p.setPosition(doors[i].rect.x, doors[i].rect.y + doors[i].rect.height); break; case 1: p.setPosition(doors[i].rect.x - p.getWidth(), doors[i].rect.y); break; case 2: p.setPosition(doors[i].rect.x, doors[i].rect.y - p.getHeight()); break; default: p.setPosition(doors[i].rect.x + doors[i].rect.width, doors[i].rect.y); break; } } } p.setMovementX(0); p.setMovementY(0); stage.addActor(p); stage.addActor(new Hostile(200, 200, 0, new Stats(), "sprite.png", false)); stage.addActor(new Hostile(265, 200, 1, new Stats(), "sprite.png", true)); } public void render(float f){ if(Gdx.input.isKeyJustPressed(Input.Keys.TAB)){ if(Main.gamestate == 0){ Main.gamestate = 2; // CREATING MAP COLLISION OBJECTS ArrayList mapRectsTemp = new ArrayList<>(); for(Actor a : stage.getActors()){ if(a instanceof MapCollisionObject){ mapRectsTemp.add(((MapCollisionObject)a).getR()); } } Rectangle[] rects = new Rectangle[mapRectsTemp.size()]; for(int i = 0; i< mapRectsTemp.size(); i++){ rects[i] = mapRectsTemp.get(i); } // CREATING FightObject Array // Temporarily only Player ArrayList tempObjects = new ArrayList<>(); tempObjects.add(new FightPlayer(getPlayer().getX(),getPlayer().getY(), getPlayer().getPlayerSprite(), getPlayer().getStats(), 0)); for(Actor a : stage.getActors()) { if(a instanceof Hostile) { if(((Hostile) a).getMovementState() > 0) { ((Hostile) a).setMovementState(2); Enemy e = new Enemy(a.getX(), a.getY(), ((Hostile) a).getSprite(), ((Hostile) a).getStats(), ((Hostile) a).getId(), ((Hostile) a).isIsMelee()); tempObjects.add(e); } } } FightObject[] fightObjects = new FightObject[tempObjects.size()]; for(int i = 0; i< tempObjects.size(); i++){ fightObjects[i] = tempObjects.get(i); } fs = new FightScreen(stage.getBatch(), fightObjects, rects, getPlayer().getX()+32, getPlayer().getY()+32); } else if(Main.gamestate == 2){ Main.gamestate = 0; fs.nuke(); fs.setState(3); } } renderer.setView((OrthographicCamera)stage.getCamera()); renderer.render(layersBelowPlayer); if(Main.gamestate == 0 || Main.gamestate == 1) { Actor[] old = stage.getActors().toArray(); stage.clear(); for(Actor a : sort(old)){ stage.addActor(a); } for(Actor a : stage.getActors()) { if(a instanceof Player) { Rectangle rect = ((Player) a).getCollisionRect(); for(Door d : doors) { if(Intersector.overlaps(rect, d.rect)) { collidingDoor = d; break; } } } } stage.act(f); stage.draw(); } if(Main.gamestate == 2){ if(fs == null) { // CREATING MAP COLLISION OBJECTS ArrayList mapRectsTemp = new ArrayList<>(); for(Actor a : stage.getActors()){ if(a instanceof MapCollisionObject){ mapRectsTemp.add(((MapCollisionObject)a).getR()); } } Rectangle[] rects = new Rectangle[mapRectsTemp.size()]; for(int i = 0; i< mapRectsTemp.size(); i++){ rects[i] = mapRectsTemp.get(i); } // CREATING FightObject Array // Temporarily only Player ArrayList tempObjects = new ArrayList<>(); tempObjects.add(new FightPlayer(getPlayer().getX(),getPlayer().getY(), getPlayer().getPlayerSprite(), getPlayer().getStats(), 0)); for(Actor a : stage.getActors()) { if(a instanceof Hostile) { Enemy e = new Enemy(a.getX(), a.getY(), ((Hostile) a).getSprite(), ((Hostile) a).getStats(), ((Hostile) a).getId(), ((Hostile) a).isIsMelee()); tempObjects.add(e); } } FightObject[] fightObjects = new FightObject[tempObjects.size()]; for(int i = 0; i< tempObjects.size(); i++){ fightObjects[i] = tempObjects.get(i); } fs = new FightScreen(stage.getBatch(), fightObjects, rects, getPlayer().getX()+32, getPlayer().getY()+32); } if(fs.getState() == 3){ for(FightObject object : fs.getObjects()){ if(object instanceof FightPlayer){ getPlayer().setX(object.getX()); getPlayer().setY(object.getY()); getPlayer().setStats(object.getStats()); } else{ for(int i = stage.getActors().size-1; i >= 0; i--){ if(stage.getActors().get(i) instanceof Hostile){ if(((Hostile)stage.getActors().get(i)).getId() == object.getId()){ if(object.getStats().getHp() <= 0){ stage.getActors().removeIndex(i); } else{ stage.getActors().get(i).setPosition(object.getX(), object.getY()); ((Hostile)stage.getActors().get(i)).setStats(object.getStats()); } } } } } } fs = null; Main.gamestate = 0; } else{ fs.act(f); fs.draw(); } } renderer.render(layersAbovePlayer); for(Actor a : stage.getActors()){ if(a instanceof Textbox){ stage.getBatch().begin(); a.draw(stage.getBatch(), f); stage.getBatch().end(); } } if(Main.gamestate == 1) { Textbox t = null; for(Actor a : stage.getActors()){ if(a instanceof Textbox){ t = (Textbox)a; if(t.getState() == 3){ a.remove(); Main.gamestate = 0; t.getSelectedAsw(); // DO STUFF NICENICE } } } } // center camera for(Actor a : stage.getActors()){ if(a instanceof Player){ stage.getCamera().position.set((a.getX()+a.getWidth()/2), (a.getY()+a.getHeight()/2), 0); stage.getCamera().update(); break; } } if(t != null){ t.draw(stage.getBatch(), stage.getCamera().position.x, stage.getCamera().position.y, stage.getCamera().combined); if(t.opacity == 0){ t = null; } } } public void resize(int width, int height){ stage.getViewport().update(width, height, false); } public Player getPlayer(){ for(Actor a : stage.getActors()){ if(a instanceof Player){ return (Player)a; } } System.out.println("OLD MAP DIDNT FIND PLAYER"); return null; } public Actor[] sort(Actor[] unsorted){ // TODO: Textboxes to the end of the Array to draw them last for(int j = 0; j < unsorted.length-1; j++){ for(int i = unsorted.length-1; i >= 0; i--){ if(i > 0 && unsorted[i].getY() > unsorted[i-1].getY()){ Actor temp = unsorted[i-1]; unsorted[i-1] = unsorted[i]; unsorted[i] = temp; } } } return unsorted; } }