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; package com.mygdx.game;
import com.badlogic.gdx.graphics.Color;
public class Block { public class Block {
int x,y,width,height; private String material;
Color color;
public Block(int tempX, int tempY,int tempWidth, int tempHeight, Color tempColor) { public Block(String tempMaterial) {
x = tempX; material = tempMaterial;
y = tempY;
color = tempColor;
width = tempWidth;
height = tempHeight;
} }
public int getX() { public String getMaterial() {
return x; return material;
} }
public void setX(int x) { public void setMaterial(String material) {
this.x = x; this.material = material;
}
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;
} }
} }

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; package com.mygdx.game;
public class World { import javafx.util.Pair;
Block[] blocks;
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; this.blocks = blocks;
} }
public Block[] getBlocks() { public World() {
this.blocks = new HashMap<>();
fillWorld();
}
public Map<Pair<Integer, Integer>, Block> getBlocks() {
return blocks; return blocks;
} }
public void setBlocks(Block[] blocks) { public void addTile(int x, int y, Block block){
this.blocks = blocks; 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; package com.mygdx.game;
import com.badlogic.gdx.graphics.OrthographicCamera; 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 { public class WorldRenderer {
private Texture texture;
private OrthographicCamera cam; private OrthographicCamera cam;
private World world; private World world;
private ShapeRenderer debugRenderer = new ShapeRenderer(); private Player player;
public WorldRenderer(World world) {
this.cam = new OrthographicCamera(10,7); public WorldRenderer(World world, Player player) {
this.cam.position.set(5,3.5f,0); this.player = player;
this.cam.update();
this.world = world; 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) { public WorldRenderer(OrthographicCamera cam, World world, Player player) {
this.cam = new OrthographicCamera(10,7); this.cam = cam;
this.cam.position.set(5,3.5f,0); this.world = world;
this.cam.update(); this.player = player;
this.world = new World(blocks);
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(){ public void render(Batch batch){
debugRenderer.setProjectionMatrix(cam.combined); cam.position.x = player.getX();
debugRenderer.begin(ShapeRenderer.ShapeType.Filled); cam.position.y = player.getY();
for (Block block : world.getBlocks()) { drawWorld(batch, player);
debugRenderer.setColor(block.getColor()); cam.update();
debugRenderer.rect(block.getX()+0.5f, block.getY()+0.5f, block.getWidth(), block.getHeight()); }
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.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.MyGdxGame; import com.mygdx.game.Main;
public class DesktopLauncher { public class DesktopLauncher {
public static void main (String[] arg) { public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
new LwjglApplication(new MyGdxGame(), config); config.foregroundFPS = 60;
new LwjglApplication(new Main(), config);
} }
} }