mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 14:43:33 +02:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a11e8607fc | ||
| a94f785be4 | |||
| 0c4182adb2 | |||
| 97f8eaeb49 |
25
src/main/java/schule/ngb/zm/game/Camera.java
Normal file
25
src/main/java/schule/ngb/zm/game/Camera.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
5
src/main/java/schule/ngb/zm/game/InputContext.java
Normal file
5
src/main/java/schule/ngb/zm/game/InputContext.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package schule.ngb.zm.game;
|
||||
|
||||
public class InputContext {
|
||||
|
||||
}
|
||||
71
src/main/java/schule/ngb/zm/game/InputType.java
Normal file
71
src/main/java/schule/ngb/zm/game/InputType.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package schule.ngb.zm.game;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
|
||||
public class InputType {
|
||||
|
||||
static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
|
||||
|
||||
public class ActionInput extends InputType {
|
||||
|
||||
private int inputCode;
|
||||
|
||||
private String action;
|
||||
|
||||
private ActionListener al;
|
||||
|
||||
private double delay = 0.0;
|
||||
|
||||
private long lastTriggered = 0L;
|
||||
|
||||
private boolean repeats = false;
|
||||
|
||||
private boolean onPress = true;
|
||||
|
||||
private boolean pressed = false;
|
||||
|
||||
|
||||
public void press( int inputCode, long time ) {
|
||||
pressed = true;
|
||||
if( onPress ) {
|
||||
trigger(inputCode, time);
|
||||
}
|
||||
}
|
||||
|
||||
public void release( int inputCode, long time ) {
|
||||
pressed = false;
|
||||
if( !onPress ) {
|
||||
trigger(inputCode, time);
|
||||
}
|
||||
}
|
||||
|
||||
public void trigger( int inputCode, long time ) {
|
||||
if( shouldTrigger(inputCode, time) ) {
|
||||
//al.actionTriggered(action);
|
||||
lastTriggered = time;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean shouldTrigger( int inputCode, long time ) {
|
||||
return this.inputCode == inputCode && time - lastTriggered > delay;
|
||||
}
|
||||
|
||||
|
||||
public int hashCode() {
|
||||
return inputCode;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class TriggerInput extends InputType {
|
||||
|
||||
}
|
||||
|
||||
public class RangeInput extends InputType {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
14
src/main/java/schule/ngb/zm/game/Map.java
Normal file
14
src/main/java/schule/ngb/zm/game/Map.java
Normal 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 );
|
||||
|
||||
|
||||
|
||||
}
|
||||
30
src/main/java/schule/ngb/zm/game/Sprite.java
Normal file
30
src/main/java/schule/ngb/zm/game/Sprite.java
Normal 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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
28
src/main/java/schule/ngb/zm/game/Tile.java
Normal file
28
src/main/java/schule/ngb/zm/game/Tile.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
65
src/main/java/schule/ngb/zm/game/TiledMap.java
Normal file
65
src/main/java/schule/ngb/zm/game/TiledMap.java
Normal 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);
|
||||
}
|
||||
62
src/test/java/schule/ngb/zm/game/TiledMapTest.java
Normal file
62
src/test/java/schule/ngb/zm/game/TiledMapTest.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user