diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java
index 93a5f86ea4a9509bfcd40f09f240db06a05ccb64..05bd47f0559b162702919b950bc20ca20d35e159 100644 (file)
package ru.deadsoftware.cavedroid.misc;
package ru.deadsoftware.cavedroid.misc;
-import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
-import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.ArrayMap;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.ArrayMap;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
+import ru.deadsoftware.cavedroid.game.objects.TouchButton;
+import ru.deadsoftware.cavedroid.misc.utils.AssetLoader;
import java.util.HashMap;
public class Assets {
public static final JsonReader jsonReader = new JsonReader();
import java.util.HashMap;
public class Assets {
public static final JsonReader jsonReader = new JsonReader();
-
- private static final GlyphLayout glyphLayout = new GlyphLayout();
- static final BitmapFont minecraftFont = new BitmapFont(Gdx.files.internal("font.fnt"), true);
-
public static final Sprite[][] playerSprite = new Sprite[2][4];
public static final Sprite[][] pigSprite = new Sprite[2][2];
public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
public static final Sprite[][] playerSprite = new Sprite[2][4];
public static final Sprite[][] pigSprite = new Sprite[2][2];
public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
- public static final ArrayMap<String, Rectangle> guiMap = new ArrayMap<>();
- public static final Sprite sandSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/sand.png"))));
- public static final Sprite gravelSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/gravel.png"))));
+ public static final ArrayMap<String, TouchButton> guiMap = new ArrayMap<>();
+ private static final GlyphLayout glyphLayout = new GlyphLayout();
+ static BitmapFont minecraftFont;
private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) {
return new TextureRegion(texture, x, y + height, width, -height);
private static TextureRegion flippedRegion(Texture texture, int x, int y, int width, int height) {
return new TextureRegion(texture, x, y + height, width, -height);
return sprite;
}
return sprite;
}
- private static void loadPlayer() {
- Texture plTex = new Texture(Gdx.files.internal("mobs/char.png"));
- //LOOK TO LEFT
- //head
- playerSprite[0][0] = flippedSprite(new TextureRegion(plTex, 0, 0, 12, 12));
- //body
- playerSprite[0][1] = flippedSprite(new TextureRegion(plTex, 0, 13, 12, 12));
- //hand
- playerSprite[0][2] = flippedSprite(new TextureRegion(plTex, 25, 5, 20, 20));
- //leg
- playerSprite[0][3] = flippedSprite(new TextureRegion(plTex, 25, 27, 20, 20));
- //LOOK TO RIGHT
- //head
- playerSprite[1][0] = flippedSprite(new TextureRegion(plTex, 13, 0, 12, 12));
- //body
- playerSprite[1][1] = flippedSprite(new TextureRegion(plTex, 13, 13, 12, 12));
- //hand
- playerSprite[1][2] = flippedSprite(new TextureRegion(plTex, 37, 5, 20, 20));
- //leg
- playerSprite[1][3] = flippedSprite(new TextureRegion(plTex, 37, 27, 20, 20));
+ private static void loadMob(AssetLoader assetLoader, Sprite[][] sprite, String mob) {
+ for (int i = 0; i < sprite.length; i++) {
+ for (int j = 0; j < sprite[i].length; j++) {
+ sprite[i][j] = flippedSprite(new Texture(
+ assetLoader.getAssetHandle("mobs/" + mob + "/" + i + "_" + j + ".png")));
+ sprite[i][j].setOrigin(sprite[i][j].getWidth() / 2, 0);
+ }
+ }
}
}
- private static void loadPig() {
- Texture pigTex = new Texture(Gdx.files.internal("mobs/pig.png"));
- pigSprite[0][0] = flippedSprite(new TextureRegion(pigTex, 0, 0, 25, 12));
- pigSprite[1][0] = flippedSprite(new TextureRegion(pigTex, 0, 12, 25, 12));
- pigSprite[0][1] = flippedSprite(new TextureRegion(pigTex, 4, 26, 12, 12));
- pigSprite[1][1] = flippedSprite(new TextureRegion(pigTex, 16, 26, 12, 12));
+ private static void setPlayerHeadOrigin() {
+ for (Sprite[] sprites : playerSprite) {
+ sprites[0].setOrigin(sprites[0].getWidth() / 2, sprites[0].getHeight());
+ }
}
/**
* Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
* and puts to {@link #textureRegions} HashMap
*/
}
/**
* Loads texture names and sizes from <b>json/texture_regions.json</b>, cuts them to TextureRegions
* and puts to {@link #textureRegions} HashMap
*/
- private static void loadJSON() {
- JsonValue json = jsonReader.parse(Gdx.files.internal("json/texture_regions.json"));
+ private static void loadJSON(AssetLoader assetLoader) {
+ JsonValue json = jsonReader.parse(assetLoader.getAssetHandle("json/texture_regions.json"));
for (JsonValue file = json.child(); file != null; file = file.next()) {
for (JsonValue file = json.child(); file != null; file = file.next()) {
- Texture texture = new Texture(Gdx.files.internal(file.name() + ".png"));
+ Texture texture = new Texture(assetLoader.getAssetHandle(file.name() + ".png"));
if (file.size == 0) {
if (file.size == 0) {
- textureRegions.put(file.name(), flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
+ textureRegions.put(file.name(),
+ flippedRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()));
} else {
for (JsonValue key = file.child(); key != null; key = key.next()) {
int x = getIntFromJson(key, "x", 0);
} else {
for (JsonValue key = file.child(); key != null; key = key.next()) {
int x = getIntFromJson(key, "x", 0);
}
}
}
}
- public static void load() {
+ public static void load(final AssetLoader assetLoader) {
+ loadMob(assetLoader, playerSprite, "char");
+ loadMob(assetLoader, pigSprite, "pig");
+ loadJSON(assetLoader);
+ setPlayerHeadOrigin();
+ minecraftFont = new BitmapFont(assetLoader.getAssetHandle("font.fnt"), true);
minecraftFont.getData().setScale(.375f);
minecraftFont.getData().setScale(.375f);
- loadPlayer();
- loadPig();
- loadJSON();
}
/**
}
/**
return json.has(name) ? json.getInt(name) : defaultValue;
}
return json.has(name) ? json.getInt(name) : defaultValue;
}
+ public static float getFloatFromJson(JsonValue json, String name, float defaultValue) {
+ return json.has(name) ? json.getFloat(name) : defaultValue;
+ }
+
public static String getStringFromJson(JsonValue json, String name, String defaultValue) {
return json.has(name) ? json.getString(name) : defaultValue;
}
public static String getStringFromJson(JsonValue json, String name, String defaultValue) {
return json.has(name) ? json.getString(name) : defaultValue;
}