Я создаю игру. Я пытаюсь приблизить камеру и проследить за объектом Портера. Он принесен в мир в. json файле
Вот код для игры
package net.hasanbilal.pr;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import net.hasanbilal.pr.world.GMap;
import net.hasanbilal.pr.world.TileType;
import net.hasanbilal.pr.world.TiledGMap;
public class PrisonRevelations extends ApplicationAdapter {
OrthographicCamera c;
SpriteBatch b;
GMap gm;
@Override
public void create () {
b = new SpriteBatch();
c = new OrthographicCamera();
c.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
c.update();
gm = new TiledGMap();
}
@Override
public void render () {
Gdx.gl.glClearColor(255, 255, 255, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
/***
if (Gdx.input.isTouched()) {
c.translate(-Gdx.input.getDeltaX(), Gdx.input.getDeltaY());
c.update();
}
if (Gdx.input.justTouched()) {
Vector3 pos = c.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));
TileType t = gm.getByLocation(1, pos.x, pos.y);
if (t != null) {
System.out.println("You clicked on tile with id" + t.getId() + " " + t.getName()+ " " + t.isCollidable() + " " + t.getDamage());
}
}
**/
c.update();
gm.update(Gdx.graphics.getDeltaTime());
gm.render(c, b);
}
@Override
public void dispose () {
b.dispose();
gm.dispose();
}
}
Вот код для класса сущности
package net.hasanbilal.pr.entities;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import net.hasanbilal.pr.world.GMap;
public abstract class Entity {
protected Vector2 pos;
protected EntityType t;
protected float velocityY = 0;
protected GMap m;
protected boolean grounded = false;
public void create (EntitySnapshot snapshot, EntityType type, GMap map) {
this.pos = new Vector2(snapshot.getX(),snapshot.getY());
this.t = type;
this.m = map;
}
public void update (float delta, float g) {
float newY = pos.y;
this.velocityY += g * delta * getWeight();
newY += this.velocityY * delta;
if (m.doesRectCollideWithMap(pos.x, newY, getWidth(), getHeight())) {
if (velocityY < 0) {
this.pos.y = (float) Math.floor(pos.y);
grounded = true;
}
this.velocityY = 0;
} else {
this.pos.y = newY;
grounded = false;
}
}
public abstract void render (SpriteBatch b);
protected void moveX(float amount) {
float newX = pos.x + amount;
if (!m.doesRectCollideWithMap(newX, pos.y, getWidth(), getHeight()))
this.pos.x = newX;
}
public EntitySnapshot getSaveSnapshot(){
return new EntitySnapshot(t.getId(), pos.x, pos.y);
}
public Vector2 getPos() {
return pos;
}
public float getX() {
return pos.x;
}
public float getY() {
return pos.y;
}
public EntityType getT() {
return t;
}
public float getVelocityY() {
return velocityY;
}
public boolean isGrounded() {
return grounded;
}
public int getWidth() {
return t.getWidth();
}
public int getHeight() {
return t.getHeight();
}
public float getWeight() {
return t.getWeight();
}
}
Вот класс EntityType
package net.hasanbilal.pr.entities;
import java.util.HashMap;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import net.hasanbilal.pr.world.GMap;
@SuppressWarnings("rawtypes")
public enum EntityType {
PLAYER("player", Porter.class, 14, 14, 20);
private String id;
private Class loaderClass;
private int width, height;
private float weight;
private EntityType(String id, Class loaderClass, int width, int height, float weight) {
this.id = id;
this.loaderClass = loaderClass;
this.width = width;
this.height = height;
this.weight = weight;
}
public String getId() {
return id;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public float getWeight() {
return weight;
}
public static Entity createEntityUsingSnapshot (EntitySnapshot entitySnapshot, GMap map) {
EntityType type = entityTypes.get(entitySnapshot.type);
try {
@SuppressWarnings("unchecked")
Entity entity = ClassReflection.newInstance(type.loaderClass);
entity.create(entitySnapshot, type, map);
return entity;
} catch (ReflectionException e) {
Gdx.app.error("Entity Loader", "Could not load entity of type " + type.id);
return null;
}
}
private static HashMap<String, EntityType> entityTypes;
static {
entityTypes = new HashMap<String, EntityType>();
for (EntityType type: EntityType.values())
entityTypes.put(type.id, type);
}
}
Вот класс EntityLOader
package net.hasanbilal.pr.entities;
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Json;
import net.hasanbilal.pr.world.GMap;
public class EntityLoader {
private static Json json = new Json();
public static ArrayList<Entity> loadEntities (String id, GMap map, ArrayList<Entity> currentEntities) {
Gdx.files.local("maps/").file().mkdirs();
FileHandle file = Gdx.files.local("maps/" + id + ".entities");
if (file.exists()) {
EntitySnapshot[] snapshots = json.fromJson(EntitySnapshot[].class, file.readString());
ArrayList<Entity> entities = new ArrayList<Entity>();
for (EntitySnapshot snapshot : snapshots) {
entities.add(EntityType.createEntityUsingSnapshot(snapshot, map));
}
return entities;
} else {
saveEntities(id, currentEntities);
return currentEntities;
}
}
public static void saveEntities (String id, ArrayList<Entity> entities) {
ArrayList<EntitySnapshot> snapshots = new ArrayList<EntitySnapshot>();
for (Entity entity : entities)
snapshots.add(entity.getSaveSnapshot());
Gdx.files.local("maps/").file().mkdirs();
FileHandle file = Gdx.files.local("maps/" + id + ".entities");
file.writeString(json.prettyPrint(snapshots), false);
}
}
Вот класс EntitySnapshot
package net.hasanbilal.pr.entities;
import java.util.HashMap;
public class EntitySnapshot {
private float x, y;
private HashMap<String, String> data;
String type;
public EntitySnapshot() {}
public EntitySnapshot(String type, float x, float y) {
this.type = type;
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void putFloat (String key, float value) {
data.put(key, "" + value);
}
public void putInt (String key, int value) {
data.put(key, "" + value);
}
public void putBoolean (String key, boolean value) {
data.put(key, "" + value);
}
public void putString (String key, String value) {
data.put(key, "" + value);
}
public float getFloat (String key, float defaultValue) {
if (data.containsKey(key)) {
try {
return Float.parseFloat(data.get(key));
} catch (Exception e) {
return defaultValue;
}
}else
return defaultValue;
}
public int getInt (String key, int defaultValue) {
if (data.containsKey(key)) {
try {
return Integer.parseInt(data.get(key));
} catch (Exception e) {
return defaultValue;
}
}else
return defaultValue;
}
public boolean getBoolean (String key, boolean defaultValue) {
if (data.containsKey(key)) {
try {
return Boolean.parseBoolean(data.get(key));
} catch (Exception e) {
return defaultValue;
}
}else
return defaultValue;
}
public String getString (String key, String defaultValue) {
if (data.containsKey(key)) {
return data.get(key);
}else
return defaultValue;
}
}
Это. json file
[
{type:"porter", x:40, y:90, data:{}},
]
Любая помощь будет принята с благодарностью.