Archived
Más pruebas y un poco de arquitectura.
Se han probado más cosas relativas a los TileMaps y a LibGDX. Se ha esbozado la arquitectura interna de los datos. Pero muy poquito y muy mal. Al menos, se ha pensado sobre el tema para alcanzar soluciones más definitivas.
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
@@ -0,0 +1,58 @@
|
||||
package uberelectron.gameSystems;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Machine
|
||||
{
|
||||
/*-------------
|
||||
* Constructors
|
||||
* -------------*/
|
||||
public Machine(String id)
|
||||
{
|
||||
this.id = id;
|
||||
attributes = new HashMap<>();
|
||||
}
|
||||
|
||||
/*-------------
|
||||
* Methods
|
||||
* -------------*/
|
||||
|
||||
//Se podría unificar con UpdateAtribute
|
||||
public void AddAtribute(String name, int value)
|
||||
{
|
||||
attributes.put(name, value);
|
||||
}
|
||||
|
||||
public void UpdateAtribute(String name, int newValue)
|
||||
{
|
||||
attributes.put(name, newValue);
|
||||
}
|
||||
|
||||
public int GetAtributeValue(String name)
|
||||
{
|
||||
Integer value = attributes.get(name);
|
||||
if(value == null)
|
||||
{
|
||||
System.err.println("El atributo solicitado no existe: " + name
|
||||
+ " en la maquina " + id);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/*-------------
|
||||
* Variables
|
||||
* -------------*/
|
||||
|
||||
String id;
|
||||
public HashMap<String, Integer> attributes;
|
||||
|
||||
/*
|
||||
Permite a la room saber debe realizar comprobación con la capacidad máxima...
|
||||
Lo sé, es muy cutre, pero te callas.
|
||||
Piensa en algo mejor, no te jode...
|
||||
*/
|
||||
boolean maquinaRecursos;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package uberelectron.gameSystems;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/***
|
||||
* Esta clase carga los datos de las maquinas desde el JSON de datos y almacena los "prefabs" de las mismas.
|
||||
*/
|
||||
public class MachineLoader
|
||||
{
|
||||
/*-------------
|
||||
* Constructors
|
||||
* -------------*/
|
||||
|
||||
public MachineLoader()
|
||||
{
|
||||
machineMap = new HashMap<>();
|
||||
}
|
||||
|
||||
/*-------------
|
||||
* Methods
|
||||
* -------------*/
|
||||
|
||||
/**
|
||||
* Carga del archivo JSON indicado los datos de cada m�quina.
|
||||
* @param path Ubicaci�n del JSON
|
||||
*/
|
||||
public void LoadFile(String path)
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
|
||||
public void AddMachine(Machine machineToAdd)
|
||||
{
|
||||
machineMap.put(machineToAdd.id, machineToAdd);
|
||||
}
|
||||
|
||||
public Machine GetMachine(String machineID)
|
||||
{
|
||||
Machine mc = machineMap.get(machineID);
|
||||
if(mc == null)
|
||||
{
|
||||
System.err.println("El mapa no contiene una m�quina de ID: " + machineID);
|
||||
}
|
||||
return mc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-------------
|
||||
* Variables
|
||||
* -------------*/
|
||||
HashMap<String, Machine> machineMap;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package uberelectron.gameSystems;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Room
|
||||
{
|
||||
/*-------------
|
||||
* Constructors
|
||||
* -------------*/
|
||||
|
||||
public Room(String roomName, int width, int height)
|
||||
{
|
||||
this.roomName = roomName;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
bgData = new int[height][width];
|
||||
machineData = new Machine[height][width];
|
||||
|
||||
//También habría que cargar los atributos desde algún sitio...
|
||||
LoadRoom();
|
||||
|
||||
}
|
||||
|
||||
/*-------------
|
||||
* Methods
|
||||
* -------------*/
|
||||
private void LoadRoom()
|
||||
{
|
||||
bgData = new int[][]
|
||||
{
|
||||
{2, 2, 2, 2, 2, 2, 2, 2},
|
||||
{2, 3, 3, 3, 3, 3, 3, 2},
|
||||
{2, 3, 3, 3, 3, 3, 3, 2},
|
||||
{2, 3, 3, 3, 3, 3, 3, 2},
|
||||
{2, 2, 2, 2, 2, 2, 2, 2},
|
||||
};
|
||||
|
||||
for(int y = 0; y < machineData.length; y++)
|
||||
{
|
||||
for(int x = 0; x < machineData[y].length; x++)
|
||||
{
|
||||
machineData[y][x] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddMachine(int x, int y, Machine machineToAdd)
|
||||
{
|
||||
//Comprobamos si tenemos los recursos suficientes para añadir la máquina.
|
||||
if(!ComprobarMaquina(machineToAdd))
|
||||
{
|
||||
System.out.println("No tienes capacidad suficiente para esta máquina...");
|
||||
return;
|
||||
}
|
||||
|
||||
for(String key : machineToAdd.attributes.keySet())
|
||||
{
|
||||
int newCapacidad = capacidadOcupada.get(key) + machineToAdd.GetAtributeValue(key);
|
||||
capacidadOcupada.put(key, newCapacidad);
|
||||
}
|
||||
|
||||
machineData[y][x] = machineToAdd;
|
||||
}
|
||||
|
||||
public boolean ComprobarCapacidad(String atribute, int valorComprobar)
|
||||
{
|
||||
Integer max = capacidadMaxima.get(atribute + "_max");
|
||||
if(max == null)
|
||||
{
|
||||
System.err.println("El atributo " + atribute + "_max no existe en la sala " + roomName);
|
||||
}
|
||||
if(valorComprobar <= max)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean ComprobarMaquina(Machine maquinaComprobar)
|
||||
{
|
||||
//Se procesarían los atributos para comprobar si se puede añadir la máquina.
|
||||
for(String key : maquinaComprobar.attributes.keySet())
|
||||
{
|
||||
int value = maquinaComprobar.GetAtributeValue(key);
|
||||
if(!ComprobarCapacidad(key, value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-------------
|
||||
* Variables
|
||||
* -------------*/
|
||||
String roomName;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
int[][] bgData;
|
||||
Machine[][] machineData;
|
||||
|
||||
|
||||
//Atributos de la sala.
|
||||
//nombreAtributo
|
||||
HashMap<String, Integer> capacidadOcupada;
|
||||
//nombreAtributo_max
|
||||
HashMap<String, Integer> capacidadMaxima;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package uberelectron.inputSystems;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.InputProcessor;
|
||||
|
||||
import java.util.function.IntPredicate;
|
||||
|
||||
public class myInputProcessor implements InputProcessor
|
||||
{
|
||||
@Override
|
||||
public boolean keyDown(int keycode)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyUp(int keycode)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyTyped(char character)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDown(int screenX, int screenY, int pointer, int button)
|
||||
{
|
||||
if(button == Input.Buttons.LEFT)
|
||||
{
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchUp(int screenX, int screenY, int pointer, int button)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDragged(int screenX, int screenY, int pointer)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseMoved(int screenX, int screenY)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean scrolled(float amountX, float amountY)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package uberelectron.testgame;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
@@ -8,7 +10,13 @@ import com.badlogic.gdx.maps.tiled.TiledMap;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
|
||||
import com.badlogic.gdx.maps.tiled.TiledMapTileSet;
|
||||
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.utils.ScreenUtils;
|
||||
import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||
import com.badlogic.gdx.utils.viewport.Viewport;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
|
||||
public class TileMapTest implements Screen
|
||||
@@ -16,6 +24,7 @@ public class TileMapTest implements Screen
|
||||
final testGame game;
|
||||
|
||||
OrthographicCamera camera;
|
||||
Viewport viewport;
|
||||
|
||||
TiledMap map;
|
||||
//TiledMapRenderer mapRenderer;
|
||||
@@ -30,24 +39,35 @@ public class TileMapTest implements Screen
|
||||
{65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65}
|
||||
};
|
||||
|
||||
final int[][] mapData =
|
||||
int[][] mapData =
|
||||
{
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
|
||||
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
||||
{1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2},
|
||||
};
|
||||
|
||||
Vector3 mousePos;
|
||||
|
||||
Vector3 camNewPos;
|
||||
float camMovSpeed = 100f;
|
||||
float tileUnitScale = 6f;
|
||||
|
||||
Texture img;
|
||||
|
||||
public TileMapTest(final testGame game)
|
||||
{
|
||||
this.game = game;
|
||||
this.map = new TiledMap();
|
||||
|
||||
camNewPos = new Vector3(0, 0, 0);
|
||||
img = new Texture(Gdx.files.internal("Frogfolk_lanza.jpg"));
|
||||
|
||||
tileSet = game.tileSetManager.GetTileSet("testTiles");
|
||||
|
||||
camera = new OrthographicCamera();
|
||||
camera.setToOrtho(false, 800, 480);
|
||||
float unitScale = 4f;
|
||||
mapRenderer = new OrthogonalTiledMapRenderer(map, unitScale);
|
||||
viewport = new FitViewport(800, 480, camera);
|
||||
camera.setToOrtho(false, viewport.getScreenWidth(), viewport.getScreenHeight());
|
||||
mapRenderer = new OrthogonalTiledMapRenderer(map, tileUnitScale);
|
||||
|
||||
MapLayers layers = map.getLayers();
|
||||
|
||||
@@ -62,12 +82,10 @@ public class TileMapTest implements Screen
|
||||
TiledMapTileLayer.Cell ce = new TiledMapTileLayer.Cell();
|
||||
ce.setTile(tileSet.getTile(mapData[y][x]));
|
||||
bgLayer.setCell(x, y, ce);
|
||||
|
||||
}
|
||||
}
|
||||
layers.add(bgLayer);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -77,6 +95,9 @@ public class TileMapTest implements Screen
|
||||
@Override
|
||||
public void render(float delta)
|
||||
{
|
||||
ProcessInput();
|
||||
|
||||
//Render.
|
||||
ScreenUtils.clear(0, 0, 0.2f, 1);
|
||||
|
||||
camera.update();
|
||||
@@ -85,15 +106,73 @@ public class TileMapTest implements Screen
|
||||
|
||||
game.batch.begin();
|
||||
|
||||
game.font.draw(game.batch, "Texto de prueba", 100, 150);
|
||||
game.font.draw(game.batch, "Texto de prueba", 0, 0);
|
||||
game.font.draw(game.batch, "Y otro mas", 100, 100);
|
||||
game.batch.draw(img, mousePos.x, mousePos.y);
|
||||
|
||||
mapRenderer.render();
|
||||
game.batch.end();
|
||||
}
|
||||
|
||||
public void ProcessInput()
|
||||
{
|
||||
//Lógica.
|
||||
if(Gdx.input.isKeyPressed(Input.Keys.W))
|
||||
{
|
||||
camNewPos.y += 100f * Gdx.graphics.getDeltaTime();
|
||||
}
|
||||
if(Gdx.input.isKeyPressed(Input.Keys.S))
|
||||
{
|
||||
camNewPos.y -= 100f * Gdx.graphics.getDeltaTime();
|
||||
}
|
||||
if(Gdx.input.isKeyPressed(Input.Keys.D))
|
||||
{
|
||||
camNewPos.x += 100f * Gdx.graphics.getDeltaTime();
|
||||
}
|
||||
if(Gdx.input.isKeyPressed(Input.Keys.A))
|
||||
{
|
||||
camNewPos.x -= 100f * Gdx.graphics.getDeltaTime();
|
||||
}
|
||||
if(Gdx.input.isKeyPressed(Input.Keys.ESCAPE))
|
||||
{
|
||||
Gdx.app.exit();
|
||||
}
|
||||
|
||||
mousePos = camera.unproject(new Vector3(Gdx.input.getX(0), Gdx.input.getY(0), 0));
|
||||
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT))
|
||||
{
|
||||
OnClick();
|
||||
}
|
||||
|
||||
camera.position.lerp(camNewPos, 0.1f);
|
||||
}
|
||||
|
||||
public void OnClick()
|
||||
{
|
||||
TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("bgLayer");
|
||||
Vector2 tileIndex = new Vector2(0, 0);
|
||||
|
||||
tileIndex.x = mousePos.x / (layer.getTileWidth() * tileUnitScale);
|
||||
tileIndex.y = mousePos.y / (layer.getTileHeight() * tileUnitScale);
|
||||
|
||||
|
||||
mapData[(int)tileIndex.y][(int)tileIndex.x] = 3;
|
||||
UpdateMapCell(layer, (int)tileIndex.x, (int)tileIndex.y, 3);
|
||||
System.out.println("Click en tile: " + (int)tileIndex.x + ", " + (int)tileIndex.y);
|
||||
}
|
||||
|
||||
public void UpdateMapCell(TiledMapTileLayer layer, int cellPosX, int cellPosY, int newCellId)
|
||||
{
|
||||
TiledMapTileLayer.Cell cell = new TiledMapTileLayer.Cell();
|
||||
cell.setTile(tileSet.getTile(newCellId));
|
||||
layer.setCell(cellPosX, cellPosY, cell);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {}
|
||||
public void resize(int width, int height)
|
||||
{
|
||||
viewport.update(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pause() {}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package uberelectron.testgame;
|
||||
|
||||
//import com.badlogic.gdx.ApplicationAdapter;
|
||||
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
|
||||
Reference in New Issue
Block a user