Worked on the rendering

This commit is contained in:
2020-03-27 19:30:03 -06:00
parent d7d8b71a1e
commit 4e7e8ac99c
9 changed files with 152 additions and 111 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 KiB

BIN
core/assets/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 558 B

View File

@@ -1,56 +1,17 @@
package com.mygdx.game;
import com.badlogic.gdx.graphics.Color;
public class Block {
int x,y,width,height;
Color color;
private String material;
public Block(int tempX, int tempY,int tempWidth, int tempHeight, Color tempColor) {
x = tempX;
y = tempY;
color = tempColor;
width = tempWidth;
height = tempHeight;
public Block(String tempMaterial) {
material = tempMaterial;
}
public int getX() {
return x;
public String getMaterial() {
return material;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
public void setMaterial(String material) {
this.material = material;
}
}

View File

@@ -0,0 +1,45 @@
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import javafx.util.Pair;
import java.util.HashMap;
public class Main extends ApplicationAdapter {
private SpriteBatch batch;
private WorldRenderer worldRenderer;
@Override
public void create () {
batch = new SpriteBatch();
World world = new World();
Player player = new Player(0, 0);
worldRenderer = new WorldRenderer(world, player);
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
worldRenderer.render(batch);
batch.end();
}
@Override
public void dispose () {
worldRenderer.dispose();
batch.dispose();
}
}

View File

@@ -1,39 +0,0 @@
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
World world;
WorldRenderer worldRenderer;
@Override
public void create () {
Block[] blocks = new Block[4];
blocks[0] = new Block(0,0, 6,1,new Color(0,1,0,1));
blocks[1] = new Block(1,0, 1,1,new Color(0,1,0,1));
blocks[2] = new Block(2,0, 1,1,new Color(0,1,0,1));
blocks[3] = new Block(3,0, 1,1,new Color(0,1,0,1));
world = new World(blocks);
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
worldRenderer = new WorldRenderer(world.getBlocks());
worldRenderer.render();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
}
}

View File

@@ -0,0 +1,28 @@
package com.mygdx.game;
import javafx.util.Pair;
public class Player {
private int x,y = 0;
public Player(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}

View File

@@ -1,17 +1,36 @@
package com.mygdx.game;
public class World {
Block[] blocks;
import javafx.util.Pair;
public World(Block[] blocks) {
import java.util.HashMap;
import java.util.Map;
public class World {
Map<Pair<Integer,Integer>,Block> blocks;
public World(HashMap<Pair<Integer, Integer>, Block> blocks) {
this.blocks = blocks;
}
public Block[] getBlocks() {
public World() {
this.blocks = new HashMap<>();
fillWorld();
}
public Map<Pair<Integer, Integer>, Block> getBlocks() {
return blocks;
}
public void setBlocks(Block[] blocks) {
this.blocks = blocks;
public void addTile(int x, int y, Block block){
this.blocks.put(new Pair<>(x,y),block);
}
public void fillWorld(){
this.addTile(1,1,new Block("grass"));
this.addTile(1,2,new Block("grass"));
this.addTile(1,3,new Block("grass"));
this.addTile(-1,-2,new Block("grass"));
}
}

View File

@@ -1,34 +1,60 @@
package com.mygdx.game;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import javafx.util.Pair;
import java.util.HashMap;
import java.util.Map;
public class WorldRenderer {
private Texture texture;
private OrthographicCamera cam;
private World world;
private ShapeRenderer debugRenderer = new ShapeRenderer();
private Player player;
public WorldRenderer(World world) {
this.cam = new OrthographicCamera(10,7);
this.cam.position.set(5,3.5f,0);
this.cam.update();
public WorldRenderer(World world, Player player) {
this.player = player;
this.world = world;
texture = new Texture("core/assets/grass.png");
cam = new OrthographicCamera(10, 7);
cam.position.set(this.player.getX(), this.player.getY(),0);
}
public WorldRenderer(Block[] blocks) {
this.cam = new OrthographicCamera(10,7);
this.cam.position.set(5,3.5f,0);
this.cam.update();
this.world = new World(blocks);
public WorldRenderer(OrthographicCamera cam, World world, Player player) {
this.cam = cam;
this.world = world;
this.player = player;
texture = new Texture("core/assets/grass.png");
cam.zoom = 1;
cam = new OrthographicCamera(10, 7);
cam.position.set(player.getX(), player.getY(),0);
}
public void render(){
debugRenderer.setProjectionMatrix(cam.combined);
debugRenderer.begin(ShapeRenderer.ShapeType.Filled);
for (Block block : world.getBlocks()) {
debugRenderer.setColor(block.getColor());
debugRenderer.rect(block.getX()+0.5f, block.getY()+0.5f, block.getWidth(), block.getHeight());
public void render(Batch batch){
cam.position.x = player.getX();
cam.position.y = player.getY();
drawWorld(batch, player);
cam.update();
}
private void drawWorld(Batch batch,Player player) {
for(int x = player.getX()-10; x<player.getX()+10; x++) {
for(int y = player.getY()-10; y<player.getY()+10; y++) {
batch.draw(texture, x, y, 1, 1);
}
}
debugRenderer.end();
}
public void dispose(){
texture.dispose();
}
}

View File

@@ -2,11 +2,12 @@ package com.mygdx.game.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.MyGdxGame;
import com.mygdx.game.Main;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
new LwjglApplication(new MyGdxGame(), config);
config.foregroundFPS = 60;
new LwjglApplication(new Main(), config);
}
}