diff --git a/src/main/java/schule/ngb/zm/Zeichenmaschine.java b/src/main/java/schule/ngb/zm/Zeichenmaschine.java index 69f5e6f..312dcc5 100644 --- a/src/main/java/schule/ngb/zm/Zeichenmaschine.java +++ b/src/main/java/schule/ngb/zm/Zeichenmaschine.java @@ -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); } } diff --git a/src/main/java/schule/ngb/zm/game/Camera.java b/src/main/java/schule/ngb/zm/game/Camera.java new file mode 100644 index 0000000..2f2767f --- /dev/null +++ b/src/main/java/schule/ngb/zm/game/Camera.java @@ -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); + } + +} diff --git a/src/main/game/InputContext.java b/src/main/java/schule/ngb/zm/game/InputContext.java similarity index 100% rename from src/main/game/InputContext.java rename to src/main/java/schule/ngb/zm/game/InputContext.java diff --git a/src/main/game/InputType.java b/src/main/java/schule/ngb/zm/game/InputType.java similarity index 100% rename from src/main/game/InputType.java rename to src/main/java/schule/ngb/zm/game/InputType.java diff --git a/src/main/java/schule/ngb/zm/game/Map.java b/src/main/java/schule/ngb/zm/game/Map.java new file mode 100644 index 0000000..56ee57b --- /dev/null +++ b/src/main/java/schule/ngb/zm/game/Map.java @@ -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 ); + + + +} diff --git a/src/main/java/schule/ngb/zm/game/Sprite.java b/src/main/java/schule/ngb/zm/game/Sprite.java new file mode 100644 index 0000000..ee6c15e --- /dev/null +++ b/src/main/java/schule/ngb/zm/game/Sprite.java @@ -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) + ); + } + } + } + + +} diff --git a/src/main/java/schule/ngb/zm/game/Tile.java b/src/main/java/schule/ngb/zm/game/Tile.java new file mode 100644 index 0000000..f7565aa --- /dev/null +++ b/src/main/java/schule/ngb/zm/game/Tile.java @@ -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); + } + +} diff --git a/src/main/java/schule/ngb/zm/game/TiledMap.java b/src/main/java/schule/ngb/zm/game/TiledMap.java new file mode 100644 index 0000000..a2d21fe --- /dev/null +++ b/src/main/java/schule/ngb/zm/game/TiledMap.java @@ -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); +} diff --git a/src/test/java/schule/ngb/zm/game/TiledMapTest.java b/src/test/java/schule/ngb/zm/game/TiledMapTest.java new file mode 100644 index 0000000..5cc3b9b --- /dev/null +++ b/src/test/java/schule/ngb/zm/game/TiledMapTest.java @@ -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(); + } + +}