forked from jzitnik/twodcraft
chore: Minor changes
This commit is contained in:
parent
5127b9ee5f
commit
b4641c5c86
@ -15,7 +15,7 @@ public class Main {
|
|||||||
Terminal terminal = TerminalBuilder.terminal();
|
Terminal terminal = TerminalBuilder.terminal();
|
||||||
terminal.enterRawMode();
|
terminal.enterRawMode();
|
||||||
var spriteList = SpriteLoader.load();
|
var spriteList = SpriteLoader.load();
|
||||||
var screenRenderer = new ScreenRenderer(spriteList);
|
var screenRenderer = new ScreenRenderer(spriteList, terminal);
|
||||||
var game = new Game();
|
var game = new Game();
|
||||||
|
|
||||||
final boolean[] isRunning = {true};
|
final boolean[] isRunning = {true};
|
||||||
@ -41,14 +41,12 @@ public class Main {
|
|||||||
|
|
||||||
if (buttonCode == 0) {
|
if (buttonCode == 0) {
|
||||||
game.handleMouseClick(x, y);
|
game.handleMouseClick(x, y);
|
||||||
throw new RuntimeException("sad");
|
|
||||||
//screenRenderer.render(game.getWorld());
|
//screenRenderer.render(game.getWorld());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle keyboard input as before
|
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case 'a':
|
case 'a':
|
||||||
game.movePlayerLeft(screenRenderer);
|
game.movePlayerLeft(screenRenderer);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package cz.jzitnik.game;
|
package cz.jzitnik.game;
|
||||||
|
|
||||||
import cz.jzitnik.game.sprites.Breaking;
|
|
||||||
import cz.jzitnik.tui.ScreenRenderer;
|
import cz.jzitnik.tui.ScreenRenderer;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -20,7 +19,6 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Block steveBlock = new Block("steve", SpriteLoader.SPRITES.STEVE);
|
Block steveBlock = new Block("steve", SpriteLoader.SPRITES.STEVE);
|
||||||
world[10][2].add(new Block("grass", SpriteLoader.SPRITES.GRASS));
|
|
||||||
player = steveBlock;
|
player = steveBlock;
|
||||||
|
|
||||||
for (int i = 0; i < 50; i++) {
|
for (int i = 0; i < 50; i++) {
|
||||||
@ -163,7 +161,7 @@ public class Game {
|
|||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(500);
|
Thread.sleep(200);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -179,7 +177,7 @@ public class Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void handleMouseClick(int mouseX, int mouseY) {
|
public void handleMouseClick(int mouseX, int mouseY) {
|
||||||
int[] cords = getPlayerCords();
|
/*int[] cords = getPlayerCords();
|
||||||
if (cords == null) return;
|
if (cords == null) return;
|
||||||
|
|
||||||
int playerX = cords[0];
|
int playerX = cords[0];
|
||||||
@ -188,12 +186,6 @@ public class Game {
|
|||||||
int viewUpRadius = 4;
|
int viewUpRadius = 4;
|
||||||
int viewDownRadius = 3;
|
int viewDownRadius = 3;
|
||||||
|
|
||||||
// Calculate visible area boundaries
|
|
||||||
int startX = Math.max(0, playerX - viewXRadius);
|
|
||||||
int endX = Math.min(world[0].length, playerX + viewXRadius + 1);
|
|
||||||
int startY = Math.max(0, playerY - viewUpRadius);
|
|
||||||
int endY = Math.min(world.length, playerY + viewDownRadius + 1);
|
|
||||||
|
|
||||||
// Convert mouse coordinates to world coordinates
|
// Convert mouse coordinates to world coordinates
|
||||||
int blockX = startX + (mouseX / 50); // 50 chars wide per sprite
|
int blockX = startX + (mouseX / 50); // 50 chars wide per sprite
|
||||||
int blockY = startY + (mouseY / 25); // 25 lines high per sprite
|
int blockY = startY + (mouseY / 25); // 25 lines high per sprite
|
||||||
@ -210,6 +202,6 @@ public class Game {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Clicked outside the visible area.");
|
System.out.println("Clicked outside the visible area.");
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package cz.jzitnik.tui;
|
||||||
|
|
||||||
|
public class ScreenMovingCalculationProvider {
|
||||||
|
public static int[] calculate(int x, int y, int terminalHeight, int terminalWidth, int worldX, int worldY) {
|
||||||
|
int spriteWidth = 50;
|
||||||
|
int spriteHeight = 25;
|
||||||
|
|
||||||
|
int viewXRadius = (terminalWidth / 2) / spriteWidth;
|
||||||
|
int viewYRadius = (terminalHeight / 2) / spriteHeight;
|
||||||
|
|
||||||
|
// Ensure at least one sprite is visible
|
||||||
|
viewXRadius = Math.max(viewXRadius, 1);
|
||||||
|
int viewUpRadius = Math.max(viewYRadius, 1);
|
||||||
|
int viewDownRadius = Math.max(viewYRadius, 1);
|
||||||
|
|
||||||
|
// Calculate visible area boundaries
|
||||||
|
int startX = Math.max(0, x - viewXRadius);
|
||||||
|
int endX = Math.min(worldX, x + viewXRadius + 1);
|
||||||
|
int startY = Math.max(0, y - viewUpRadius);
|
||||||
|
int endY = Math.min(worldY, y + viewDownRadius + 1);
|
||||||
|
|
||||||
|
return new int[] {startX, endX, startY, endY};
|
||||||
|
}
|
||||||
|
}
|
@ -2,12 +2,14 @@ package cz.jzitnik.tui;
|
|||||||
|
|
||||||
import cz.jzitnik.game.Block;
|
import cz.jzitnik.game.Block;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.jline.terminal.Terminal;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class ScreenRenderer {
|
public class ScreenRenderer {
|
||||||
private SpriteList spriteList;
|
private SpriteList spriteList;
|
||||||
|
private Terminal terminal;
|
||||||
|
|
||||||
private int[] getPlayerCords(List<Block>[][] world) {
|
private int[] getPlayerCords(List<Block>[][] world) {
|
||||||
for (int i = 0; i < world.length; i++) {
|
for (int i = 0; i < world.length; i++) {
|
||||||
@ -17,7 +19,6 @@ public class ScreenRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,17 +31,30 @@ public class ScreenRenderer {
|
|||||||
|
|
||||||
int playerX = cords[0];
|
int playerX = cords[0];
|
||||||
int playerY = cords[1];
|
int playerY = cords[1];
|
||||||
int viewXRadius = 5;
|
|
||||||
int viewUpRadius = 4;
|
int terminalWidth = terminal.getWidth();
|
||||||
int viewDownRadius = 3;
|
int terminalHeight = terminal.getHeight();
|
||||||
|
|
||||||
|
int[] data = ScreenMovingCalculationProvider.calculate(playerX, playerY, terminalHeight, terminalWidth, world[0].length, world.length);
|
||||||
|
|
||||||
// Calculate visible area boundaries
|
// Calculate visible area boundaries
|
||||||
int startX = Math.max(0, playerX - viewXRadius);
|
int startX = data[0];
|
||||||
int endX = Math.min(world[0].length, playerX + viewXRadius + 1);
|
int endX = data[1];
|
||||||
int startY = Math.max(0, playerY - viewUpRadius);
|
int startY = data[2];
|
||||||
int endY = Math.min(world.length, playerY + viewDownRadius + 1);
|
int endY = data[3];
|
||||||
|
|
||||||
StringBuilder[] lines = new StringBuilder[(endY - startY) * 25 + 1];
|
int visibleWidth = endX - startX;
|
||||||
|
int visibleHeight = endY - startY;
|
||||||
|
|
||||||
|
// If the width is odd, reduce viewXRadius by 1
|
||||||
|
if (visibleWidth % 2 != 0) {
|
||||||
|
endX = Math.max(startX, endX - 1);
|
||||||
|
}
|
||||||
|
/*if (visibleHeight % 2 != 0) {
|
||||||
|
endY = Math.max(startY, endY - 1);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
StringBuilder[] lines = new StringBuilder[(endY - startY) * 26];
|
||||||
for (int i = 0; i < lines.length; i++) {
|
for (int i = 0; i < lines.length; i++) {
|
||||||
lines[i] = new StringBuilder();
|
lines[i] = new StringBuilder();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user