Added crosshair and wrote the coords of the mouse on the screen

This commit is contained in:
2020-03-29 04:32:00 -06:00
parent 26e4a8ab42
commit 47badea54b
15 changed files with 452 additions and 52 deletions

BIN
core/assets/crosshair.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 558 B

After

Width:  |  Height:  |  Size: 2.9 KiB

229
core/assets/man.atlas Normal file
View File

@@ -0,0 +1,229 @@
man.png
size: 256, 128
format: RGBA8888
filter: Nearest,Nearest
repeat: none
man_Up
rotate: false
xy: 0,0
size: 32,32
orig: 32,32
offset: 0,0
index: 0
man_Up
rotate: false
xy: 32,0
size: 32,32
orig: 32,32
offset: 0,0
index: 1
man_Up
rotate: false
xy: 64,0
size: 32,32
orig: 32,32
offset: 0,0
index: 2
man_Up
rotate: false
xy: 96,0
size: 32,32
orig: 32,32
offset: 0,0
index: 3
man_Down
rotate: false
xy: 128,0
size: 32,32
orig: 32,32
offset: 0,0
index: 0
man_Down
rotate: false
xy: 160,0
size: 32,32
orig: 32,32
offset: 0,0
index: 1
man_Down
rotate: false
xy: 192,0
size: 32,32
orig: 32,32
offset: 0,0
index: 2
man_Down
rotate: false
xy: 224,0
size: 32,32
orig: 32,32
offset: 0,0
index: 3
man_Left
rotate: false
xy: 0,32
size: 32,32
orig: 32,32
offset: 0,0
index: 0
man_Left
rotate: false
xy: 32,32
size: 32,32
orig: 32,32
offset: 0,0
index: 1
man_Left
rotate: false
xy: 64,32
size: 32,32
orig: 32,32
offset: 0,0
index: 2
man_Left
rotate: false
xy: 96,32
size: 32,32
orig: 32,32
offset: 0,0
index: 3
man_Right
rotate: false
xy: 128,32
size: 32,32
orig: 32,32
offset: 0,0
index: 0
man_Right
rotate: false
xy: 160,32
size: 32,32
orig: 32,32
offset: 0,0
index: 1
man_Right
rotate: false
xy: 192,32
size: 32,32
orig: 32,32
offset: 0,0
index: 2
man_Right
rotate: false
xy: 224,32
size: 32,32
orig: 32,32
offset: 0,0
index: 3
man_UpLeft
rotate: false
xy: 0,64
size: 32,32
orig: 32,32
offset: 0,0
index: 0
man_UpLeft
rotate: false
xy: 32,64
size: 32,32
orig: 32,32
offset: 0,0
index: 1
man_UpLeft
rotate: false
xy: 64,64
size: 32,32
orig: 32,32
offset: 0,0
index: 2
man_UpLeft
rotate: false
xy: 96,64
size: 32,32
orig: 32,32
offset: 0,0
index: 3
man_UpRight
rotate: false
xy: 128,64
size: 32,32
orig: 32,32
offset: 0,0
index: 0
man_UpRight
rotate: false
xy: 160,64
size: 32,32
orig: 32,32
offset: 0,0
index: 1
man_UpRight
rotate: false
xy: 192,64
size: 32,32
orig: 32,32
offset: 0,0
index: 2
man_UpRight
rotate: false
xy: 224,64
size: 32,32
orig: 32,32
offset: 0,0
index: 3
man_DownLeft
rotate: false
xy: 0,96
size: 32,32
orig: 32,32
offset: 0,0
index: 0
man_DownLeft
rotate: false
xy: 32,96
size: 32,32
orig: 32,32
offset: 0,0
index: 1
man_DownLeft
rotate: false
xy: 64,96
size: 32,32
orig: 32,32
offset: 0,0
index: 2
man_DownLeft
rotate: false
xy: 96,96
size: 32,32
orig: 32,32
offset: 0,0
index: 3
man_DownRight
rotate: false
xy: 128,96
size: 32,32
orig: 32,32
offset: 0,0
index: 0
man_DownRight
rotate: false
xy: 160,96
size: 32,32
orig: 32,32
offset: 0,0
index: 1
man_DownRight
rotate: false
xy: 192,96
size: 32,32
orig: 32,32
offset: 0,0
index: 2
man_DownRight
rotate: false
xy: 224,96
size: 32,32
orig: 32,32
offset: 0,0
index: 3

BIN
core/assets/man.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
core/assets/wood.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@@ -0,0 +1,5 @@
package com.mygdx.game;
public class Chunk {
}

View File

@@ -0,0 +1,27 @@
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
public class GUI {
private BitmapFont font = new BitmapFont();
private Mouse mouse;
private OrthographicCamera cam;
public GUI(Mouse mouse, OrthographicCamera cam) {
this.font = new BitmapFont();
this.mouse = mouse;
this.cam = cam;
}
public void render(SpriteBatch batch){
font.draw(batch, "x: "+ mouse.getTileX(cam) + "y: " + mouse.getTileY(cam),-10, 0);
}
public void dispose(){
font.dispose();
}
}

View File

@@ -3,42 +3,61 @@ package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
public class InputController {
public class InputController implements Restrictions {
private OrthographicCamera cam;
private Player player;
private Animation<TextureAtlas.AtlasRegion> animation;
float pressTime, unpressTime;
int i = 0;
public InputController(OrthographicCamera cam) {
public InputController(OrthographicCamera cam, Player player) {
this.cam = cam;
this.player = player;
}
public void handleInput() {
i++;
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
cam.zoom += 0.02;
cam.zoom += 1;
}
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
cam.zoom -= 0.02;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
cam.translate(-3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
cam.translate(3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
cam.translate(0, -3, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
cam.translate(0, 3, 0);
cam.zoom -= 1;
}
cam.zoom = MathUtils.clamp(cam.zoom, 0.1f, 100/cam.viewportWidth);
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && i > KEY_DELAY) {
i = 0;
directionAnimation("Left");
player.addX(-MOVEMENT_SPEED);
cam.translate(-MOVEMENT_SPEED,0);
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)&& i > KEY_DELAY) {
i = 0;
directionAnimation("Right");
player.addX(MOVEMENT_SPEED);
cam.translate(MOVEMENT_SPEED,0);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)&& i > KEY_DELAY) {
i = 0;
directionAnimation("Down");
player.addY(-MOVEMENT_SPEED);
cam.translate(0,-MOVEMENT_SPEED);
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)&& i > KEY_DELAY) {
i = 0;
directionAnimation("Up");
player.addY(MOVEMENT_SPEED);
cam.translate(0,MOVEMENT_SPEED);
}
cam.update();
}
float effectiveViewportWidth = cam.viewportWidth * cam.zoom;
float effectiveViewportHeight = cam.viewportHeight * cam.zoom;
cam.position.x = MathUtils.clamp(cam.position.x, effectiveViewportWidth / 2f, 100 - effectiveViewportWidth / 2f);
cam.position.y = MathUtils.clamp(cam.position.y, effectiveViewportHeight / 2f, 100 - effectiveViewportHeight / 2f);
public void directionAnimation(String direction) {
player.setAnimation(new Animation<>(1 / 4f, player.getTextureAtlas().findRegions(player.getSpriteName() + "_" + direction)));
}
}

View File

@@ -3,36 +3,45 @@ package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import static com.mygdx.game.Restrictions.*;
public class Main extends ApplicationAdapter {
private SpriteBatch batch;
private WorldRenderer worldRenderer;
private GUI gui;
private Mouse mouse;
private OrthographicCamera cam;
@Override
public void create () {
batch = new SpriteBatch();
World world = new World();
Player player = new Player(0, 0);
worldRenderer = new WorldRenderer(world, player);
mouse = new Mouse(player);
cam = new OrthographicCamera(VIEWPORT_WIDTH,VIEWPORT_HEIGHT);
cam.translate(TILE_SIZE/2,TILE_SIZE/2);
cam.zoom = 25f;
worldRenderer = new WorldRenderer(world, player, mouse,cam);
batch = new SpriteBatch();
gui = new GUI(mouse,cam);
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
worldRenderer.render(batch);
gui.render(batch);
batch.end();
}
@Override
public void dispose () {
worldRenderer.dispose();
gui.dispose();
batch.dispose();
}
}

View File

@@ -0,0 +1,50 @@
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import static java.lang.Math.round;
public class Mouse implements Restrictions {
Player player;
Texture crosshair = new Texture("core/assets/crosshair.png");
public Mouse(Player player) {
this.player = player;
}
public void selectedTile(SpriteBatch batch, OrthographicCamera cam){
Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0);
cam.unproject(mousePos);
batch.draw(crosshair, getSelectedX(mousePos), getSelectedY(mousePos));
}
public int getSelectedX(Vector3 mousePos) {
return round((mousePos.x-8)/16)*16;
}
public int getSelectedY(Vector3 mousePos) {
return round((mousePos.y-8)/16)*16;
}
public int getTileX(OrthographicCamera cam){
Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0);
cam.unproject(mousePos);
return (getSelectedX(mousePos) + player.getX()/2)/TILE_SIZE;
}
public int getTileY(OrthographicCamera cam){
Vector3 mousePos = new Vector3( Gdx.input.getX(), Gdx.input.getY(),0);
cam.unproject(mousePos);
return (getSelectedY(mousePos) + player.getY()/2)/TILE_SIZE;
}
public void render(SpriteBatch batch, OrthographicCamera cam) {
selectedTile(batch,cam);
System.out.println("X = " + getTileX(cam) + " Y = "+ getTileY(cam));
}
}

View File

@@ -1,13 +1,29 @@
package com.mygdx.game;
import javafx.util.Pair;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Array;
public class Player {
public class Player implements Restrictions {
private int x,y = 0;
private String spriteName;
private TextureAtlas textureAtlas;
private Animation<TextureAtlas.AtlasRegion> animation;
public Player(int x, int y) {
this.x = x;
this.y = y;
Player(int x, int y) {
this.x = 0;
this.y = 0;
this.spriteName = "man";
this.textureAtlas = new TextureAtlas("core/assets/" + spriteName + ".atlas");
Array<TextureAtlas.AtlasRegion> keyFrames = textureAtlas.findRegions(spriteName + "_Down");
float frameDuration = 1 / 4f;
this.animation = new Animation<>(frameDuration, keyFrames);
}
public void render(Batch batch){
float elapsedTime = 1f;
batch.draw(getAnimation().getKeyFrame(elapsedTime, true), getX(), getY(), TILE_SIZE, TILE_SIZE);
}
public int getX() {
@@ -25,4 +41,32 @@ public class Player {
public void setY(int y) {
this.y = y;
}
public void addX(int x){
this.x += x;
}
public void addY(int y){
this.y += y;
}
String getSpriteName() {
return spriteName;
}
public void setSpriteName(String tempSpriteName) {
spriteName = tempSpriteName;
}
TextureAtlas getTextureAtlas() {
return textureAtlas;
}
Animation<TextureAtlas.AtlasRegion> getAnimation() {
return animation;
}
void setAnimation(Animation<TextureAtlas.AtlasRegion> animation) {
this.animation = animation;
}
}

View File

@@ -0,0 +1,9 @@
package com.mygdx.game;
public interface Restrictions {
int TILE_SIZE = 16;
float VIEWPORT_HEIGHT = 9/2f;
float VIEWPORT_WIDTH = 16/2f;
int MOVEMENT_SPEED = 16;
int KEY_DELAY = 20;
}

View File

@@ -6,7 +6,7 @@ import javafx.util.Pair;
import java.util.HashMap;
import java.util.Map;
public class World {
public class World implements Restrictions{
Map<Pair<Integer,Integer>,Block> blocks;
public World(HashMap<Pair<Integer, Integer>, Block> blocks) {
@@ -31,8 +31,11 @@ public class World {
}
public void fillWorld(){
this.addTile(1,1,new Block("grass"));
this.addTile(0,0,new Block("grass"));
for(int x = -10; x< 10;x++){
for(int y = -10; y<10;y++){
this.addTile(x,y,new Block("grass"));
}
}
this.addTile(1,1 , new Block("wood"));
}
}

View File

@@ -1,42 +1,44 @@
package com.mygdx.game;
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 com.badlogic.gdx.math.Vector3;
public class WorldRenderer {
private Texture texture;
public class WorldRenderer implements Restrictions {
private Mouse mouse;
private OrthographicCamera cam;
private World world;
private Player player;
private InputController inputController;
public WorldRenderer(World world, Player player) {
public WorldRenderer(World world, Player player, Mouse mouse, OrthographicCamera cam) {
this.player = player;
this.world = world;
cam = new OrthographicCamera(10, 7);
inputController = new InputController(cam);
this.mouse = mouse;
this.cam = cam;
inputController = new InputController(this.cam,this.player);
}
public void render(Batch batch){
public void render(SpriteBatch batch){
drawWorld(batch, player);
inputController.handleInput();
batch.setProjectionMatrix(cam.combined);
drawWorld(batch, player);
cam.update();
mouse.render(batch,cam);
player.render(batch);
}
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++) {
for(int x = -10; x<10; x++) {
for(int y = -10; y<10; y++) {
try {
batch.draw(world.getTile(x,y).getTexture(), (x * 16), (y * 16));
batch.draw(world.getTile(x,y).getTexture(), (x * TILE_SIZE), (y * TILE_SIZE));
} catch (Exception e){}
}
}
}
public void dispose(){
texture.dispose();
}
}

View File

@@ -8,6 +8,9 @@ public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.foregroundFPS = 60;
config.resizable = true;
config.width = 1280;
config.height = 720;
new LwjglApplication(new Main(), config);
}
}