First pass map mehanics and TiledMap

This commit is contained in:
ngb 2022-12-21 16:16:42 +01:00
parent 0c4182adb2
commit a94f785be4
9 changed files with 236 additions and 1 deletions

View File

@ -1178,6 +1178,9 @@ public class Zeichenmaschine extends Constants {
//saveMousePosition(evt);
mouseMoved(evt);
break;
case MouseEvent.MOUSE_WHEEL:
mouseWheelMoved(evt);
break;
}
}
@ -1221,6 +1224,14 @@ public class Zeichenmaschine extends Constants {
// Intentionally left blank
}
public void mouseWheelMoved( MouseEvent e ) {
mouseWheelMoved();
}
public void mouseWheelMoved() {
// Intentionally left blank
}
private void saveMousePosition( MouseEvent event ) {
if( mouseEvent != null && event.getComponent() == canvas ) {
pmouseX = mouseX;
@ -1551,7 +1562,7 @@ public class Zeichenmaschine extends Constants {
@Override
public void mouseWheelMoved( MouseWheelEvent e ) {
// enqueueEvent(e);
enqueueEvent(e);
}
}

View File

@ -0,0 +1,25 @@
package schule.ngb.zm.game;
import schule.ngb.zm.Constants;
import schule.ngb.zm.shapes.Bounds;
public class Camera {
double x, y, z = .0;
int width, height;
double zoom = 1.0;
public Camera() {
x = Constants.canvasWidth / 2.0;
y = Constants.canvasHeight / 2.0;
width = Constants.canvasWidth;
height = Constants.canvasHeight;
}
public Bounds getBounds() {
return new Bounds(x - width/2.0, y - height/2.0, width, height);
}
}

View File

@ -0,0 +1,14 @@
package schule.ngb.zm.game;
import schule.ngb.zm.Layer;
import java.awt.Graphics2D;
public abstract class Map {
public abstract void render( Graphics2D g, Camera view );
}

View File

@ -0,0 +1,30 @@
package schule.ngb.zm.game;
import schule.ngb.zm.util.Cache;
import schule.ngb.zm.util.io.ImageLoader;
import java.awt.Image;
import java.awt.image.BufferedImage;
public class Sprite {
public Image[] sprites;
public Sprite( String spriteFile, int cols, int rows ) {
BufferedImage sprite = ImageLoader.loadImage(spriteFile, false);
int w = sprite.getWidth() / cols;
int h = sprite.getHeight() / rows;
sprites = new Image[cols*rows];
for( int i = 0; i < cols; i++ ) {
for( int j = 0; j < rows; j++ ) {
sprites[j*cols + i] = ImageLoader.copyImage(
sprite.getSubimage(i*w, j*h, w, h)
);
}
}
}
}

View File

@ -0,0 +1,28 @@
package schule.ngb.zm.game;
import schule.ngb.zm.Color;
import schule.ngb.zm.Constants;
import java.awt.Graphics2D;
public class Tile {
int c, r;
Color clr;
public Tile( int c, int r ) {
this.c = c;
this.r = r;
clr = Color.getHSBColor(((20-c)*(20-c)+(15-r)*(15-r))/625.0, .7, .8);
}
public void render( Graphics2D g, int x, int y, int size ) {
g.setColor(clr.getJavaColor());
g.fillRect(x, y, size, size);
g.setColor(Color.DARKGRAY.getJavaColor());
g.drawRect(x, y, size, size);
g.drawString("(" + c + "," + r + ")", x+3, y+13);
}
}

View File

@ -0,0 +1,65 @@
package schule.ngb.zm.game;
import schule.ngb.zm.Constants;
import schule.ngb.zm.shapes.Bounds;
import schule.ngb.zm.util.Log;
import java.awt.Graphics2D;
public class TiledMap extends Map {
private int columns, rows, tileSize;
private Tile[][] tiles;
public TiledMap( int columns, int rows, int tileSize ) {
this.columns = columns;
this.rows = rows;
this.tileSize = tileSize;
tiles = new Tile[columns][rows];
}
public void setTile( int column, int row, Tile tile ) {
tiles[column][row] = tile;
}
public void render( Graphics2D g, Camera view ) {
Bounds viewBounds = view.getBounds();
double zoomFactor = Constants.map(view.zoom, -100, 100, -2.0, 2.0);
zoomFactor = Constants.limit(zoomFactor, -2.0, 2.0);
double zoomTileSize = zoomFactor * tileSize;
int minCol, maxCol, minRow, maxRow, dX, dY;
if( viewBounds.getMinX() < 0 ) {
minCol = 0;
dX = (int)(-1 * viewBounds.getMinX());
} else {
minCol = (int)(viewBounds.getMinX() / tileSize);
dX = -1 * (int)(viewBounds.getMinX()) % tileSize;
}
maxCol = Math.min((int)(viewBounds.getMaxX() / tileSize), columns-1);
if( viewBounds.getMinY() < 0 ) {
minRow = 0;
dY = (int)(-1 * viewBounds.getMinY());
} else {
minRow = (int)(viewBounds.getMinY() / tileSize);
dY = -1 * (int)(viewBounds.getMinY()) % tileSize;
}
maxRow = Math.min((int)(viewBounds.getMaxY() / tileSize), rows-1);
for( int r = minRow; r <= maxRow; r++ ) {
for( int c = minCol; c <= maxCol; c++ ) {
if( tiles[c][r] != null ) {
tiles[c][r].render(g, dX + (c-minCol) * tileSize, dY + (r-minRow) * tileSize, tileSize);
}
}
}
}
private static final Log LOG = Log.getLogger(TiledMap.class);
}

View File

@ -0,0 +1,62 @@
package schule.ngb.zm.game;
import schule.ngb.zm.Zeichenmaschine;
import java.awt.event.MouseWheelEvent;
class TiledMapTest extends Zeichenmaschine {
public static final int COLS = 40;
public static final int ROWS = 30;
public static final int TILE_SIZE = 40;
private TiledMap map;
private Camera cam;
public TiledMapTest() {
super(20*TILE_SIZE, 15*TILE_SIZE, "TiledMapTest");
}
@Override
public void setup() {
map = new TiledMap(COLS, ROWS, TILE_SIZE);
for( int i = 0; i < COLS; i++ ) {
for( int j = 0; j < ROWS; j++ ) {
map.setTile(i, j, new Tile(i, j));
}
}
cam = new Camera();
}
@Override
public void update( double delta ) {
}
@Override
public void draw() {
drawing.clear();
map.render(getDrawingLayer().getGraphics(), cam);
}
@Override
public void mouseDragged() {
cam.x -= (mouseX-pmouseX);
cam.y -= (mouseY-pmouseY);
}
@Override
public void mouseWheelMoved() {
MouseWheelEvent mwe = (MouseWheelEvent) mouseEvent;
cam.zoom += mwe.getWheelRotation();
}
public static void main( String[] args ) {
new TiledMapTest();
}
}