Skip to content

Commit

Permalink
added support for multiple chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
thatshawt committed May 12, 2022
1 parent a6c55c4 commit d38d21a
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 86 deletions.
3 changes: 3 additions & 0 deletions GameClient/src/main/java/me/thatshawt/gameClient/Camera.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.thatshawt.gameClient;

import me.thatshawt.gameCore.tile.ChunkMap;
import me.thatshawt.gameCore.tile.TileChunk;

import java.util.concurrent.atomic.AtomicInteger;

public class Camera {
Expand Down
33 changes: 21 additions & 12 deletions GameClient/src/main/java/me/thatshawt/gameClient/ClientPlayer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.thatshawt.gameClient;

import me.thatshawt.gameCore.game.Direction;
import me.thatshawt.gameCore.game.Player;
import me.thatshawt.gameCore.packets.ClientPacket;
import me.thatshawt.gameCore.tile.ChunkCoord;
Expand All @@ -16,16 +17,16 @@ public class ClientPlayer extends Player{
public transient boolean cameraFollow = true;

public ClientPlayer(GameClient gameClient, int x, int y, UUID uuid) {
super(x, y, uuid);
super(gameClient.chunks, x, y, uuid);
this.camera = new Camera(x,y,10);
this.gameClient = gameClient;
}

private void sendMovement(int xOffset, int yOffset) {
private void sendMovement(Direction direction) {
ByteBuffer byteBuffer = ByteBuffer.allocate(4*2);

byteBuffer.putInt(xOffset);
byteBuffer.putInt(yOffset);
byteBuffer.putInt(direction.xOffset);
byteBuffer.putInt(direction.yOffset);

try {
gameClient.sendPacket(ClientPacket.PLAYER_MOVE, byteBuffer.array());
Expand All @@ -51,30 +52,38 @@ public void sendChat(String msg) throws IOException {
}

public boolean moveDown(){
if(!gameClient.chunks.containsKey(ChunkCoord.fromTileXY(x.get(),y.get()+1)))return false;
sendMovement(0, 1);
// if(!gameClient.chunks.containsKey(ChunkCoord.fromTileXY(x.get(),y.get()+1)))return false;
if(!checkCollision(Direction.DOWN))return false;

sendMovement(Direction.DOWN);
// System.out.println("down");
return true;
}

public boolean moveUp(){
if(!gameClient.chunks.containsKey(ChunkCoord.fromTileXY(x.get(),y.get()-1)))return false;
// if(!gameClient.chunks.containsKey(ChunkCoord.fromTileXY(x.get(),y.get()-1)))return false;
if(!checkCollision(Direction.UP))return false;

// this.y.decrementAndGet();
sendMovement(0, -1);
sendMovement(Direction.UP);
return true;
}

public boolean moveRight(){
if(!gameClient.chunks.containsKey(ChunkCoord.fromTileXY(x.get()+1,y.get())))return false;
// if(!gameClient.chunks.containsKey(ChunkCoord.fromTileXY(x.get()+1,y.get())))return false;
if(!checkCollision(Direction.RIGHT))return false;

// this.x.incrementAndGet();
sendMovement(1,0);
sendMovement(Direction.RIGHT);
return true;
}

public boolean moveLeft(){
if(!gameClient.chunks.containsKey(ChunkCoord.fromTileXY(x.get()-1,y.get())))return false;
// if(!gameClient.chunks.containsKey(ChunkCoord.fromTileXY(x.get()-1,y.get())))return false;
if(!checkCollision(Direction.LEFT))return false;

// this.x.decrementAndGet();
sendMovement(-1,0);
sendMovement(Direction.LEFT);
return true;
}

Expand Down
105 changes: 68 additions & 37 deletions GameClient/src/main/java/me/thatshawt/gameClient/GameClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,45 @@ public GameClient(){
}
});

this.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {
if(e.getButton() == 1){
try {
Point pixel = e.getPoint();
Point tileCoord = pixelToTile(pixel);
chunks.setTile(tileCoord.x, tileCoord.y, new WallTile());
}catch (Exception ignore){}
}else{
try {
Point pixel = e.getPoint();
Point tileCoord = pixelToTile(pixel);
chunks.setTile(tileCoord.x, tileCoord.y, new AirTile());
}catch (Exception ignore){}
}
}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}
});

this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
Expand Down Expand Up @@ -144,7 +183,7 @@ public void keyReleased(KeyEvent e) {
while (!(GameClient.this.serverConnection != null
&& GameClient.this.serverConnection.isConnected())) {
try {
Thread.sleep(500);//200 cant be that long lmao idk bruh
Thread.sleep(500);
// System.out.println("sleeping");
} catch (InterruptedException e) {
e.printStackTrace();
Expand All @@ -161,8 +200,7 @@ public void keyReleased(KeyEvent e) {
int packetLength = input.readInt();
ServerPacket packet = ServerPacket.fromInt(input.readInt());

// System.out.println("got packet idk man");
System.out.println("received packet " + packet.name());
// System.out.println("received packet " + packet.name());
switch(packet){
case ENTITY_POSITION: {
UUID uuid = GamePacket.readUUID(input);
Expand Down Expand Up @@ -201,7 +239,7 @@ public void keyReleased(KeyEvent e) {
UUID uuid = GamePacket.readUUID(input);
// System.out.println("added " + uuid + "????!??!?!?!?11!?/1?");

Player player = new NetworkPlayer(0,0, uuid);
Player player = new NetworkPlayer(chunks, 0,0, uuid);
if(!chunks.containsEntity(uuid)){
chunks.get(ChunkCoord.fromChunkXY(0,0)).addEntity(player);
}
Expand All @@ -221,7 +259,7 @@ public void keyReleased(KeyEvent e) {
TileChunk chunk = PacketDataReader.readChunk(input);
// System.out.println("received map data");
this.chunks.put(coord, chunk);
System.out.println(chunks);
System.out.println("received " + chunk);
}
default:
break;
Expand Down Expand Up @@ -265,34 +303,33 @@ private int getBoxHeight(){

private Point tileToPixel(int tilex, int tiley){
return new Point(
(tilex - player.getCamera().getX() + TileChunk.CHUNK_SIZE/2 + 1)*getBoxWidth(),
(tiley - player.getCamera().getY() + TileChunk.CHUNK_SIZE/2 + 1)*getBoxHeight()
(tilex - player.getCamera().getX() + player.getCamera().getRenderDistance()/2 - 1)*getBoxWidth(),
(tiley - player.getCamera().getY() + player.getCamera().getRenderDistance()/2 - 1)*getBoxHeight()
);
}

private boolean tileWithinRenderDistance(int tilex, int tiley){
Point pixel = tileToPixel(tilex, tiley);
return pixel.x > 0 && pixel.x < this.getWidth()
&& pixel.y > 0 && pixel.y < this.getHeight();
}

private Point pixelToTile(int screenx, int screeny){
final int boxWidth = getBoxWidth();
final int boxHeight = getBoxHeight();
final Camera camera = player.getCamera();
// player.getX() + i - gameMap.tiles.length/2 - 1
int x = (screenx /*- (boxWidth/2)*/)/boxWidth + (camera.getX() - camera.getRenderDistance()/2 + 1);
int y = (screeny /*- (boxHeight/2)*/)/boxHeight + (camera.getY() - camera.getRenderDistance()/2 + 1);
int x = screenx/boxWidth + (camera.getX() - camera.getRenderDistance()/2 + 1);
int y = screeny/boxHeight + (camera.getY() - camera.getRenderDistance()/2 + 1);
return new Point(x,y);
}

private boolean tileWithinRenderDistance(int tilex, int tiley){
Point pixel = tileToPixel(tilex, tiley);
return pixel.x > 0 && pixel.x < this.getWidth()
&& pixel.y > 0 && pixel.y < this.getHeight();
}

private Point pixelToTile(Point point){
return pixelToTile(point.x, point.y);
}

private Tile getTileAtPixel(int x, int y){
private Tile getRenderTileAt(int x, int y){
// System.out.printf("(x,y): %d,%d\n", x, y);
//out of bounds char
Entity entityAtLocation = chunks.getFirstEntityAt(x,y);
ChunkCoord chunkCoord = ChunkCoord.fromChunkXY(x/TileChunk.CHUNK_SIZE,y/TileChunk.CHUNK_SIZE);
if(!chunks.containsKey(chunkCoord))
Expand All @@ -304,19 +341,14 @@ else if(entityAtLocation != null)
}

private char getRenderCharAtTile(int x, int y){
Tile tile = getTileAtPixel(x,y);
Tile tile = getRenderTileAt(x,y);
char renderChar = '#';
if(tile != null){
renderChar = tile.getChar();
}
return renderChar;
}

// private Point getTileCoordinateAtXY(int x, int y){
// player.getX() + i - gameMap.tiles.length/2 - 1,
// player.getY() + j - gameMap.tiles[0].length/2 - 1
// }

private void drawMainGame(Graphics g){
final int screenWidth = this.getWidth();
final int screenHeight = this.getHeight();
Expand Down Expand Up @@ -346,7 +378,7 @@ private void drawMainGame(Graphics g){
camera.getX() + i - HALF_RENDER_DISTANCE + 1,
camera.getY() + j - HALF_RENDER_DISTANCE + 1
);
Tile tile = getTileAtPixel(tileCoord.x, tileCoord.y);
Tile tile = getRenderTileAt(tileCoord.x, tileCoord.y);
char renderChar = getRenderCharAtTile(tileCoord.x, tileCoord.y);
if(tile instanceof PlayerTile)
g.setFont(PLAYER_FONT);
Expand All @@ -372,29 +404,29 @@ private void drawUILayer(Graphics g){
g.setColor(Color.LIGHT_GRAY);
g.setFont(UI_FONT);

for(Entity entity : chunks.get(ChunkCoord.fromChunkXY(0,0)).entityList){
if(entity instanceof Player) {
Player playerEntity = (Player)entity;
Point pixelPosition = tileToPixel(playerEntity.getX(), playerEntity.getY());
// System.out.println(pixelPosition);
g.drawString(playerEntity.getChat(), pixelPosition.x, pixelPosition.y);
for(TileChunk chunk :
chunks.chunksWithinRenderDistance(getX(),getY(), player.getCamera().getRenderDistance())){
for(Entity entity : chunk.entityList){
if(entity instanceof Player) {
Player playerEntity = (Player)entity;
Point pixelPosition = tileToPixel(playerEntity.getX(), playerEntity.getY());
g.drawString(playerEntity.getChat(), pixelPosition.x, pixelPosition.y);
}
}
}


Point point = lastMouseLocation.get();
Point tilePoint = pixelToTile(point);

// g.drawString(String.valueOf(getRenderTileAt(tilePoint.x, tilePoint.y)), point.x, point.y);
// g.drawString(String.format("(%d, %d)", tilePoint.x, tilePoint.y), point.x, point.y);

//lmao
if(chatting){
//lmao
g.drawString(chatMessage, 40, 40);
// System.out.println(chatMessage);
}

if(debug.get()){
Tile tile = getTileAtPixel(tilePoint.x, tilePoint.y);
Tile tile = getRenderTileAt(tilePoint.x, tilePoint.y);
String[] debugMsg = {
String.format("mouseTileXY:(%d,%d)\n",tilePoint.x,tilePoint.y),
String.format("mouseTile:%s", tile == null ? "null" : tile.getClass().getSimpleName()),
Expand Down Expand Up @@ -429,7 +461,6 @@ public void paintComponent(Graphics g) {
drawUILayer(g);
}

//so this class has two in one basically lmao
public void run() {
while(true){//simple loop for simple game
try {
Expand All @@ -455,7 +486,7 @@ public static void testRun(String[] args, boolean debug){

frame.add(gameClient);

frame.setLocation(2300,350); //position i used to make debugging quicker
if(debug)frame.setLocation(2300,350);
frame.setSize(650, 650);

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Expand Down
13 changes: 13 additions & 0 deletions GameCore/src/main/java/me/thatshawt/gameCore/game/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.thatshawt.gameCore.game;

public enum Direction {
UP(0,-1),
DOWN(0,1),
LEFT(-1,0),
RIGHT(1,0);
public final int xOffset,yOffset;
Direction(int xOffset, int yOffset){
this.xOffset = xOffset;
this.yOffset = yOffset;
}
}
Loading

0 comments on commit d38d21a

Please sign in to comment.