TiledMap is being rendered and MapObjects for collisions added and extracted from the map

master
GammelJAN 5 years ago
parent da93e763f2
commit e0de478f21

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" infinite="0" nextlayerid="4" nextobjectid="9">
<tileset firstgid="1" source="nice2.tsx"/>
<layer id="1" name="Kachelebene 1" width="10" height="10">
<data encoding="csv">
235,233,235,234,233,237,233,235,234,232,
232,237,234,237,234,232,233,233,233,282,
234,234,232,232,234,233,233,236,237,234,
235,232,237,234,234,235,232,235,235,232,
234,236,233,233,236,236,235,232,236,237,
232,236,235,233,234,232,236,233,232,235,
236,232,233,237,237,236,234,236,232,233,
235,236,237,236,233,235,235,234,236,237,
234,233,233,232,235,235,234,236,234,234,
234,236,236,232,235,237,235,232,235,236
</data>
</layer>
<layer id="2" name="Kachelebene 2" width="10" height="10">
<data encoding="csv">
137,221,221,221,221,221,221,222,260,261,
201,0,236,237,233,0,0,0,281,178,
201,0,236,233,232,232,0,0,0,199,
201,0,233,178,179,180,0,0,0,199,
201,0,234,199,200,201,0,0,0,199,
201,0,0,220,221,222,236,234,0,199,
201,0,0,0,233,235,235,235,0,199,
201,0,0,0,0,237,232,235,0,199,
158,179,179,179,179,179,179,179,179,159,
200,200,200,200,200,200,200,200,200,200
</data>
</layer>
<objectgroup id="3" name="Objektebene 1">
<object id="1" x="0" y="0" width="256" height="32"/>
<object id="2" x="0" y="32" width="32" height="288"/>
<object id="3" x="32" y="288" width="288" height="32"/>
<object id="4" x="288" y="64" width="32" height="224"/>
<object id="5" x="256" y="0" width="64" height="64"/>
<object id="6" x="96" y="128" width="96" height="64"/>
</objectgroup>
</map>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.4" tiledversion="1.4.3" name="nice2" tilewidth="32" tileheight="32" tilecount="483" columns="21">
<image source="terrain.png" width="672" height="736"/>
</tileset>

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

@ -19,6 +19,7 @@ public class AnimatedSprite {
public AnimatedSprite(Texture tx, int tileWidth, int tileHeight){
texture = TextureRegion.split(tx, tileWidth, tileHeight);
sprite = new Sprite();
row = (int) (Math.random()*texture.length);
frame = (int) (Math.random()*texture[row].length);
@ -29,7 +30,7 @@ public class AnimatedSprite {
public void updateAnimation(float delta){
this.delta += delta;
if(this.delta >= 0.2f) {
if(this.delta >= 0.1f) {
this.delta = 0;
if(getFrame() >= texture[getRow()].length - 1){
setFrame(0);

@ -13,7 +13,7 @@ public class Main extends Game{
@Override
public void create () {
screen = new GameScreen(this, 1200f, 800f);
screen = new GameScreen(this, 400f, 400f);
}
@Override
@ -26,7 +26,8 @@ public class Main extends Game{
@Override
public void render () {
Gdx.gl.glClearColor(1f, (20f/255f), (147f/255f), 1);
//Gdx.gl.glClearColor(1f, (20f/255f), (147f/255f), 1);
Gdx.gl.glClearColor(1f, (1), (1), 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
screen.render(Gdx.graphics.getDeltaTime());
}

@ -0,0 +1,36 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.trs.main;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
/**
*
* @author Jan
*/
public class MapObject extends Actor{
Rectangle r;
public MapObject(int x, int y, int width, int height){
setName("mapobject");
r = new Rectangle(x, y, width, height);
setBounds(x, y, width, height);
}
@Override
public void act(float delta) {
super.act(delta); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha); //To change body of generated methods, choose Tools | Templates.
}
}

@ -20,18 +20,23 @@ import com.badlogic.gdx.scenes.scene2d.Actor;
*/
public class Player extends Actor{
Texture t;
private AnimatedSprite playerSprite;
float movementX;
float movementY;
public Player(int xPos, int yPos){
setName("player");
t = new Texture(Gdx.files.internal("badlogic.jpg"));
setBounds(xPos, yPos, t.getWidth(), t.getHeight());
t = new Texture(Gdx.files.internal("player.png"));
playerSprite = new AnimatedSprite(t, 64, 64);
playerSprite.setRow(0);
setBounds(xPos, yPos, playerSprite.getSprite().getWidth(), playerSprite.getSprite().getHeight());
}
@Override
protected void positionChanged() {
playerSprite.setSpritePosition((int)getX(), (int)getY());
super.positionChanged(); //To change body of generated methods, choose Tools | Templates.
}
@ -40,16 +45,20 @@ public class Player extends Actor{
@Override
public void act(float delta) {
if(Gdx.input.isKeyPressed(Input.Keys.D)){
setX(getX()+10);
setX(getX()+5);
}
if(Gdx.input.isKeyPressed(Input.Keys.A)){
setX(getX()-10);
setX(getX()-5);
}
if(Gdx.input.isKeyPressed(Input.Keys.W)){
setY(getY()+10);
setY(getY()+5);
}
if(Gdx.input.isKeyPressed(Input.Keys.S)){
setY(getY()-10);
setY(getY()-5);
}
if(Gdx.input.isKeyJustPressed(Input.Keys.ENTER)){
playerSprite.setRow(playerSprite.getRow()+1);
playerSprite.setFrame(0);
}
playerSprite.updateAnimation(delta);

@ -36,6 +36,8 @@ public class Textbox extends Actor{
String asw2;
public Textbox(String toPrint, String asw1, String asw2) {
printLine = 0;
printChar = 0;
this.asw1 = asw1;
this.asw2 = asw2;
setName("textbox");
@ -60,22 +62,22 @@ public class Textbox extends Actor{
public void act(float delta) {
if(state == 1){
if(selectedAsw == 0){
if(Gdx.input.isButtonJustPressed(Input.Keys.RIGHT)){
if(Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)){
selectedAsw = 1;
}
}
else if(selectedAsw == 1){
if(Gdx.input.isButtonJustPressed(Input.Keys.LEFT)){
if(Gdx.input.isKeyJustPressed(Input.Keys.LEFT)){
selectedAsw = 0;
}
}
if(Gdx.input.isButtonJustPressed(Input.Keys.ENTER)){
if(Gdx.input.isKeyJustPressed(Input.Keys.ENTER)){
state = 2;
}
}
else{
if(printChar >= splitted.get(printLine).length()){
if(splitted.size() <= printLine){
if(splitted.size()-1 <= printLine){
state = 1;
}
else{
@ -114,17 +116,18 @@ public class Textbox extends Actor{
}
}
else{
for(int i = 0; i < printLine; i++){
for(int i = 0; i <= printLine; i++){
font.draw(batch, splitted.get(i), 0, getX() + getHeight()-i*1.2f*getTextHeight("A"));
}
if(selectedAsw == 0){
font.setColor(Color.RED);
}
font.draw(batch, asw1, 0.2f * r.getWidth(), getX() + getHeight() - splitted.size() * 1.2f * getTextHeight("A"));
font.setColor(Color.BLACK);
if(selectedAsw == 1){
font.setColor(Color.RED);
}
font.draw(batch, asw1, 0.6f * r.getWidth(), getX() + getHeight() - splitted.size() * 1.2f * getTextHeight("asdf"));
font.draw(batch, asw2, 0.6f * r.getWidth(), getX() + getHeight() - splitted.size() * 1.2f * getTextHeight("A"));
font.setColor(Color.BLACK);
}
batch.end();

@ -6,10 +6,22 @@
package com.trs.main.view.screens;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapObject;
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.physics.box2d.BodyDef;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.trs.main.Player;
import com.trs.main.Textbox;
import java.util.ArrayList;
/**
*
@ -19,10 +31,27 @@ public class GameScreen extends AbstractScreen{
boolean textbox = false;
TmxMapLoader maploader;
TiledMap map;
OrthogonalTiledMapRenderer renderer;
ArrayList<TiledMapTileMapObject> object = new ArrayList<>();
public GameScreen(Game game, float CAMERA_WIDTH, float CAMERA_HEIGHT) {
super(game, CAMERA_WIDTH, CAMERA_HEIGHT);
stage.addActor(new Player(50,50));
//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("map.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
renderer.setView((OrthographicCamera)stage.getCamera());
stage.getCamera().update();
for(MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)){
Rectangle rect = ((RectangleMapObject) object).getRectangle();
stage.addActor(new com.trs.main.MapObject((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight()));
}
}
@Override
@ -37,6 +66,9 @@ public class GameScreen extends AbstractScreen{
@Override
public void render(float f) {
renderer.render();
if(!textbox){
stage.act(f);
stage.draw();
@ -50,6 +82,8 @@ public class GameScreen extends AbstractScreen{
t = (Textbox)a;
if(t.getState() == 2){
a.remove();
t.getSelectedAsw(); // DO STUFF NICENICE
textbox = false;
}
}
}

Loading…
Cancel
Save