Added a bunch of new classes to help implement an inventory and a minimap

This commit is contained in:
2020-04-13 06:52:59 -06:00
parent 515d781390
commit 394b0906ef
13 changed files with 151 additions and 75 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@@ -4,6 +4,7 @@ import Collector.Main;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector3;
import Collector.Dimension.Chunks;
import Collector.Restrictions;
@@ -12,23 +13,23 @@ import java.util.HashMap;
public class InputController implements Restrictions {
private final Player player;
int i = 0;
private final HashMap<String, Animation<com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion>> animations;
private Player player;
private HashMap<String, Animation<TextureAtlas.AtlasRegion>> animations;
private Mouse mouse;
int i = 0;
public InputController(Player player, Mouse mouse) {
this.player = player;
this.mouse = mouse;
animations = new HashMap<>();
animations.put("Up", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Up")));
animations.put("UpRight", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "UpRight")));
animations.put("UpLeft", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "UpLeft")));
animations.put("Down", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Down")));
animations.put("DownRight", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "DownRight")));
animations.put("DownLeft", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "DownLeft")));
animations.put("Left", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Left")));
animations.put("Right", new Animation<>(1 / 4f, player.textureAtlas.findRegions(player.getSpriteName() + "_" + "Right")));
animations.put("Up", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "Up")));
animations.put("UpRight", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "UpRight")));
animations.put("UpLeft", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "UpLeft")));
animations.put("Down", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "Down")));
animations.put("DownRight", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "DownRight")));
animations.put("DownLeft", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "DownLeft")));
animations.put("Left", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "Left")));
animations.put("Right", new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + "Right")));
}
public void handleInput() {

View File

@@ -1,31 +1,29 @@
package Collector.Character;
import Collector.Dimension.BlockMaterials;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import Collector.Restrictions;
import Collector.Dimension.Inventory;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Array;
import Collector.Restrictions;
public class Player implements Restrictions {
public static int x;
public static int y;
private final String spriteName;
public TextureAtlas textureAtlas;
private static Animation<TextureAtlas.AtlasRegion> animation;
public static Sprite sprite;
private String spriteName;
private TextureAtlas textureAtlas;
private Animation<TextureAtlas.AtlasRegion> animation;
private Inventory playerInventory;
public Player(OrthographicCamera cam, int x, int y) {
public Player(int x, int y) {
//Player location
Player.x = x<<TILE_SHIFT;
Player.y = y<<TILE_SHIFT;
//Player Inventory
playerInventory = new Inventory();
//Player animation
spriteName = "man";
sprite = new Sprite(new Texture("assets/wood.png"),TILE_SIZE,TILE_SIZE);
textureAtlas = new TextureAtlas("man.atlas");
Array<TextureAtlas.AtlasRegion> keyFrames = textureAtlas.findRegions(spriteName + "_Down");
float frameDuration = 1 / 4f;
@@ -48,16 +46,23 @@ public class Player implements Restrictions {
Player.y += y;
}
String getSpriteName() {
public String getSpriteName() {
return spriteName;
}
public static Animation<TextureAtlas.AtlasRegion> getAnimation() {
public Animation<TextureAtlas.AtlasRegion> getAnimation() {
return animation;
}
void setAnimation(Animation<TextureAtlas.AtlasRegion> animationTemp) {
public void setAnimation(Animation<TextureAtlas.AtlasRegion> animationTemp) {
animation = animationTemp;
}
public void dispose(){
textureAtlas.dispose();
}
public TextureAtlas getTextureAtlas() {
return textureAtlas;
}
}

View File

@@ -2,7 +2,7 @@
package Collector.Dimension;
import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
import Collector.OpenSimplexNoise;
import Collector.ThirdPartyCode.OpenSimplexNoise;
import java.util.HashMap;
@@ -15,7 +15,7 @@ public class Chunks {
public static OpenSimplexNoise gen1 = new OpenSimplexNoise(SEED + 1);
public static OpenSimplexNoise gen2 = new OpenSimplexNoise(SEED);
public static void create() {
public static void createStructures() {
int i = 10;
setBlock(3 + i, 2,1,"wall");
setBlock(4 + i, 2,1,"wall");

View File

@@ -0,0 +1,23 @@
package Collector.Dimension;
import java.util.LinkedList;
public class Inventory {
private LinkedList<ItemStack> inventory = new LinkedList<ItemStack>();
public LinkedList<ItemStack> getInventory() {
return inventory;
}
public void setInventory(LinkedList<ItemStack> inventory) {
this.inventory = inventory;
}
public void addItem(Block block){
inventory.add((ItemStack) block);
}
public void removeItem(ItemStack itemStack){
inventory.remove(itemStack);
}
}

View File

@@ -0,0 +1,14 @@
package Collector.Dimension;
import Collector.Dimension.Block;
public class ItemStack extends Block {
int value;
int quantity;
public ItemStack(String name, int value, int quantity) {
super(name);
this.value = value;
this.quantity = quantity;
}
}

View File

@@ -15,19 +15,20 @@ import Collector.Restrictions;
import java.util.Iterator;
public class WorldRenderer implements Restrictions {
private final Mouse mouse;
private final InputController inputController;
private Mouse mouse;
private InputController inputController;
private Player player;
public WorldRenderer(InputController inputController, Mouse mouse) {
public WorldRenderer(InputController inputController, Mouse mouse, Player player) {
this.inputController = inputController;
this.mouse = mouse;
this.player = player;
}
public void drawWorld(Stage stage, int layer) {
for (Iterator<Triple<Integer, Integer, Integer>> iterator = Chunks.loadedChunks.keySet().iterator(); iterator.hasNext(); ) {
Triple<Integer, Integer, Integer> chunkpair = iterator.next();
public void drawWorld(SpriteBatch batch, int layer) {
for (Triple<Integer, Integer, Integer> chunkpair : Chunks.loadedChunks.keySet()) {
if (chunkpair.getThird() == layer) {
stage.getBatch().draw(
batch.draw(
BlockMaterials.textures.get(Chunks.loadedChunks.get(chunkpair).getName()),
chunkpair.getFirst() << TILE_SHIFT,
chunkpair.getSecond() << TILE_SHIFT
@@ -36,21 +37,21 @@ public class WorldRenderer implements Restrictions {
}
}
private void mouseCrosshair(Stage stage) {
private void mouseCrosshair(SpriteBatch batch) {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
Main.cam.unproject(mousePos);
int x = mouse.getSelectedX(mousePos);
int y = mouse.getSelectedY(mousePos);
stage.getBatch().draw(mouse.getCrosshair(), x, y);
batch.draw(mouse.getCrosshair(), x, y);
}
public void render(Stage stage, float timeSinceLastUpdate) {
public void render(SpriteBatch batch, float timeSinceLastUpdate) {
//Higher means draws in a lower layer
drawWorld(stage,0);
stage.getBatch().draw(Player.getAnimation().getKeyFrame(timeSinceLastUpdate, true), Player.x, Player.y,TILE_SIZE,TILE_SIZE);
drawWorld(stage,1);
mouseCrosshair(stage);
drawWorld(batch,0);
batch.draw(player.getAnimation().getKeyFrame(timeSinceLastUpdate, true), Player.x, Player.y,TILE_SIZE,TILE_SIZE);
drawWorld(batch,1);
mouseCrosshair(batch);
inputController.handleInput();
stage.getBatch().setProjectionMatrix(stage.getCamera().combined);
batch.setProjectionMatrix(Main.cam.combined);
}
}

View File

@@ -7,7 +7,7 @@ import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import Collector.Character.InputController;
import Collector.Character.Mouse;
import Collector.Character.Player;
@@ -20,7 +20,7 @@ public class Main extends ApplicationAdapter {
private SpriteBatch batch;
private WorldRenderer worldRenderer;
private GUI gui;
private FitViewport fitViewport;
private ExtendViewport extendViewport;
public static float chunkLoadingTime = 0;
float playerAnimationTime = 0f; //accumulator
public static OrthographicCamera cam;
@@ -32,18 +32,22 @@ public class Main extends ApplicationAdapter {
@Override
public void create () {
Gdx.graphics.setWindowedMode(1024, 576);
//Declaring all objects needed for the game
cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
fitViewport = new FitViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, cam);
extendViewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, cam);
mouse = new Mouse();
gui = new GUI(fitViewport);
player = new Player(cam, 0, 0);
player = new Player(0, 0);
inputController = new InputController(player,mouse);
worldRenderer = new WorldRenderer(inputController,mouse);
worldRenderer = new WorldRenderer(inputController,mouse,player);
batch = new SpriteBatch();
stage = new Stage(fitViewport,batch);
stage = new Stage();
gui = new GUI(stage);
Gdx.input.setInputProcessor(stage);
BlockMaterials.create();
Chunks.create();
Chunks.createStructures();
//cam.translate(TILE_SIZE, TILE_SIZE);
cam.zoom = 25f;
@@ -62,9 +66,11 @@ public class Main extends ApplicationAdapter {
playerAnimationTime += deltaTime;
chunkLoadingTime += deltaTime;
gui.debugInfo(stage,mouse);
stage.act(deltaTime);
stage.getBatch().begin();
worldRenderer.render(stage,playerAnimationTime);
batch.begin();
worldRenderer.render(batch,playerAnimationTime);
if (chunkLoadingTime > RENDER_TIME) {
chunkLoadingTime -= RENDER_TIME;
@@ -72,10 +78,8 @@ public class Main extends ApplicationAdapter {
World.unloadChunks();
}
stage.getCamera().update();
gui.render();
stage.getBatch().end();
cam.update();
batch.end();
stage.draw();
}
@@ -83,7 +87,8 @@ public class Main extends ApplicationAdapter {
@Override
public void dispose () {
gui.dispose();
player.textureAtlas.dispose();
player.dispose();
stage.dispose();
batch.dispose();
}
}

View File

@@ -1,4 +1,4 @@
package Collector;/*
package Collector.ThirdPartyCode;/*
* OpenSimplex Noise in Java.
* by Kurt Spencer
*

View File

@@ -1,34 +1,52 @@
package Collector.UI;
import Collector.Character.Mouse;
import Collector.Main;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import Collector.Restrictions;
public class GUI implements Restrictions {
private final Stage stage;
private final BitmapFont font;
private ExtendViewport guiViewport;
private final Table table;
private final Label label;
public GUI(FitViewport fitViewport) {
public GUI(Stage stage) {
font = new BitmapFont();
stage = new Stage(fitViewport);
table = new Table();
table.setDebug(true);
guiViewport = new ExtendViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),stage.getCamera());
stage.setViewport(guiViewport);
//Creating block selection debug menu
table.top().left();
label = new Label("x=N/A y=N/A", new Label.LabelStyle(font,Color.WHITE));
table.add(label);
//Creating Minimap
Label label = new Label("Start", new Label.LabelStyle(font, new Color(Color.WHITE)));
Gdx.input.setInputProcessor(stage);
stage.addActor(label);
}
public void render() {
stage.act();
stage.draw();
public void debugInfo(Stage stage, Mouse mouse){
table.setFillParent(true);
stage.addActor(table);
//Mouse position
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
Main.cam.unproject(mousePos);
int x = mouse.getSelectedX(mousePos) >> TILE_SHIFT;
int y = mouse.getSelectedY(mousePos) >> TILE_SHIFT;
label.setText("x="+ x + " y=" + y);
}
public void dispose(){

View File

@@ -0,0 +1,4 @@
package Collector.UI;
public class InventoryInterface {
}

View File

@@ -0,0 +1,5 @@
package Collector.UI;
public class MiniMap {
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB