Stage put into MapContainer - switchin between maps working

master
GammelJAN 5 years ago
parent 145b6eeddf
commit a34723c5e9

@ -32,7 +32,7 @@ public class Main extends Game{
@Override
public void render () {
//Gdx.gl.glClearColor(1f, (20f/255f), (147f/255f), 1);
Gdx.gl.glClearColor(1f, (1), (1), 1);
Gdx.gl.glClearColor(0f, (0), (0), 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
screen.render(Gdx.graphics.getDeltaTime());
}

@ -1,5 +1,6 @@
package com.trs.main;
import com.badlogic.gdx.Gdx;
import java.util.ArrayList;
import com.badlogic.gdx.graphics.OrthographicCamera;
@ -8,26 +9,52 @@ 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.objects.TiledMapTileMapObject;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
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;
public class MapContainer {
Stage stage;
OrthographicCamera camera;
TmxMapLoader tmx;
TiledMap map;
OrthogonalTiledMapRenderer tmr;
TmxMapLoader maploader;
TiledMap map;
OrthogonalTiledMapRenderer renderer;
ArrayList<Door> doors = new ArrayList<>();
public MapContainer(float CAMERA_WIDTH, float CAMERA_HEIGHT, TiledMap map) {
// TODO: Value which shows from which door the player is coming?
public MapContainer(float CAMERA_WIDTH, float CAMERA_HEIGHT, Player p, String mapString) {
// CREATION OF STAGE
camera = new OrthographicCamera();
camera.setToOrtho(false, CAMERA_WIDTH, CAMERA_HEIGHT);
camera.update();
this.map = map;
stage = new Stage(new FitViewport(CAMERA_WIDTH, CAMERA_HEIGHT, camera));
Gdx.input.setInputProcessor(stage);
// set position according to the door the player is coming from
p.setPosition(64, 64);
stage.addActor(p);
stage.addActor(new MovingNpc(new Rectangle(20,20,400,400), 64, 64));
//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(2).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
for(MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)){
Rectangle rect = ((RectangleMapObject) object).getRectangle();
MapProperties props = object.getProperties();
@ -36,6 +63,50 @@ public class MapContainer {
//System.out.println("ID: " + props.get("ID", Integer.class));
//System.out.println("ID: " + props.get("y"));
}
}
}
public void render(float f){
renderer.setView((OrthographicCamera)stage.getCamera());
renderer.render();
stage.act(f);
stage.draw();
if(Main.gamestate == 1) {
Textbox t = null;
for(Actor a : stage.getActors()){
if(a.getName().equals("textbox")){
t = (Textbox)a;
if(t.getState() == 2){
a.remove();
Main.gamestate = 0;
t.getSelectedAsw(); // DO STUFF NICENICE
}
}
}
}
// center camera
for(Actor a : stage.getActors()){
if(a.getName().equals("player")){
stage.getCamera().position.set((a.getX()+a.getWidth()/2), (a.getY()+a.getHeight()/2), 0);
stage.getCamera().update();
break;
}
}
}
public void resize(int width, int height){
stage.getViewport().update(width, height, false);
}
public Player getPlayer(){
for(Actor a : stage.getActors()){
if(a.getName().equals("player")){
return (Player)a;
}
}
System.out.println("OLD MAP DIDNT FIND PLAYER");
return null;
}
}

@ -92,6 +92,7 @@ public class Player extends Actor{
* 3: no movement available
*/
if(movementX == 0 && movementY == 0){
System.out.println(collidingWithMapCollisionObject());
}
else if(movementX == 0 && movementY != 0){
setY(getY()+movementY);

@ -19,23 +19,12 @@ import com.trs.main.Textbox;
*/
public abstract class AbstractScreen implements Screen{
float CAMERA_WIDTH;
float CAMERA_HEIGHT;
protected Game game;
Stage stage;
OrthographicCamera camera;
public AbstractScreen(Game game, float CAMERA_WIDTH, float CAMERA_HEIGHT) {
this.game = game;
this.CAMERA_WIDTH = CAMERA_WIDTH;
this.CAMERA_HEIGHT = CAMERA_HEIGHT;
camera = new OrthographicCamera();
camera.setToOrtho(false, this.CAMERA_WIDTH, this.CAMERA_HEIGHT);
camera.update();
stage = new Stage(new FitViewport(this.CAMERA_WIDTH, this.CAMERA_HEIGHT, camera));
Gdx.input.setInputProcessor(stage);
}
@Override

@ -34,30 +34,19 @@ import java.util.ArrayList;
*/
public class GameScreen extends AbstractScreen{
TmxMapLoader maploader;
TiledMap map;
OrthogonalTiledMapRenderer renderer;
ArrayList<TiledMapTileMapObject> object = new ArrayList<>();
MapContainer map;
public GameScreen(Game game, float CAMERA_WIDTH, float CAMERA_HEIGHT) {
super(game, CAMERA_WIDTH, CAMERA_HEIGHT);
stage.addActor(new Player(250,50));
stage.addActor(new MovingNpc(new Rectangle(20,20,400,400), 80, 80));
//setTextbox(new Textbox("How are you doing my friend How are you doing my friend How are you doing my friend How are you doing my friend", "good", "bad"));
maploader = new TmxMapLoader();
map = maploader.load("map2.tmx");
MapContainer test = new MapContainer(CAMERA_WIDTH, CAMERA_HEIGHT, map);
renderer = new OrthogonalTiledMapRenderer(map);
renderer.setView((OrthographicCamera)stage.getCamera());
stage.getCamera().update();
map = new MapContainer(CAMERA_WIDTH, CAMERA_HEIGHT, new Player(200, 200), "map2.tmx");
for(MapObject object : map.getLayers().get(2).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()));
}
}
public void loadNewMap(String map){
this.map = new MapContainer(Main.CAMERA_WIDTH, Main.CAMERA_HEIGHT, this.map.getPlayer(), map);
}
@Override
@ -66,42 +55,18 @@ public class GameScreen extends AbstractScreen{
@Override
public void render(float f) {
renderer.setView((OrthographicCamera)stage.getCamera());
renderer.render();
stage.act(f);
stage.draw();
if(Main.gamestate == 1) {
Textbox t = null;
for(Actor a : stage.getActors()){
if(a.getName().equals("textbox")){
t = (Textbox)a;
if(t.getState() == 2){
a.remove();
Main.gamestate = 0;
t.getSelectedAsw(); // DO STUFF NICENICE
}
}
}
map.render(f);
if(Gdx.input.isKeyJustPressed(Input.Keys.NUM_1)){
loadNewMap("map.tmx");
}
// center camera
for(Actor a : stage.getActors()){
if(a.getName().equals("player")){
stage.getCamera().position.set((a.getX()+a.getWidth()/2), (a.getY()+a.getHeight()/2), 0);
stage.getCamera().update();
break;
}
if(Gdx.input.isKeyJustPressed(Input.Keys.NUM_2)){
loadNewMap("map2.tmx");
}
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, false);
map.resize(width, height);
}
@Override

Loading…
Cancel
Save