MakeTiles: refactor: move TileImage classes into separate file
This commit is contained in:
parent
372c1a6519
commit
56a4b3da27
4 changed files with 115 additions and 101 deletions
7
graphics/fountain.ani
Normal file
7
graphics/fountain.ani
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<micropolis-animation>
|
||||||
|
<frame>misc_animation@0,208</frame>
|
||||||
|
<frame>misc_animation@0,224</frame>
|
||||||
|
<frame>misc_animation@0,240</frame>
|
||||||
|
<frame>misc_animation@0,256</frame>
|
||||||
|
</micropolis-animation>
|
|
@ -6,7 +6,7 @@ import java.util.*;
|
||||||
import javax.xml.stream.*;
|
import javax.xml.stream.*;
|
||||||
import static micropolisj.XML_Helper.*;
|
import static micropolisj.XML_Helper.*;
|
||||||
|
|
||||||
class Animation extends MakeTiles.TileImage
|
class Animation extends TileImage
|
||||||
{
|
{
|
||||||
static final int DEFAULT_DURATION = 125;
|
static final int DEFAULT_DURATION = 125;
|
||||||
List<Frame> frames = new ArrayList<Frame>();
|
List<Frame> frames = new ArrayList<Frame>();
|
||||||
|
@ -70,10 +70,10 @@ class Animation extends MakeTiles.TileImage
|
||||||
|
|
||||||
static class Frame
|
static class Frame
|
||||||
{
|
{
|
||||||
MakeTiles.TileImage frame;
|
TileImage frame;
|
||||||
int duration;
|
int duration;
|
||||||
|
|
||||||
public Frame(MakeTiles.TileImage frame, int duration)
|
public Frame(TileImage frame, int duration)
|
||||||
{
|
{
|
||||||
this.frame = frame;
|
this.frame = frame;
|
||||||
this.duration = duration;
|
this.duration = duration;
|
||||||
|
|
|
@ -11,6 +11,7 @@ import javax.swing.ImageIcon;
|
||||||
import javax.xml.stream.*;
|
import javax.xml.stream.*;
|
||||||
|
|
||||||
import static micropolisj.engine.TileSpec.generateTileNames;
|
import static micropolisj.engine.TileSpec.generateTileNames;
|
||||||
|
import static micropolisj.build_tool.TileImage.*;
|
||||||
|
|
||||||
public class MakeTiles
|
public class MakeTiles
|
||||||
{
|
{
|
||||||
|
@ -20,7 +21,6 @@ public class MakeTiles
|
||||||
static final Charset UTF8 = Charset.forName("UTF-8");
|
static final Charset UTF8 = Charset.forName("UTF-8");
|
||||||
static int SKIP_TILES = 0;
|
static int SKIP_TILES = 0;
|
||||||
static int COUNT_TILES = -1;
|
static int COUNT_TILES = -1;
|
||||||
static final int STD_SIZE = 16;
|
|
||||||
static int TILE_SIZE = STD_SIZE;
|
static int TILE_SIZE = STD_SIZE;
|
||||||
|
|
||||||
public static void main(String [] args)
|
public static void main(String [] args)
|
||||||
|
@ -208,103 +208,6 @@ public class MakeTiles
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static abstract class TileImage
|
|
||||||
{
|
|
||||||
public abstract void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY);
|
|
||||||
/**
|
|
||||||
* @return the end-time of the animation frame identified by frameTime;
|
|
||||||
* -1 if not an animation, or if frameTime is past the end of the animation
|
|
||||||
*/
|
|
||||||
public abstract int getFrameEndTime(int frameTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
static class TileImageLayer extends TileImage
|
|
||||||
{
|
|
||||||
TileImageLayer below;
|
|
||||||
TileImage above;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
|
|
||||||
{
|
|
||||||
if (below != null) {
|
|
||||||
below.drawTo(gr, destX, destY, srcX, srcY);
|
|
||||||
}
|
|
||||||
above.drawTo(gr, destX, destY, srcX, srcY);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getFrameEndTime(int frameTime)
|
|
||||||
{
|
|
||||||
if (below == null) {
|
|
||||||
return above.getFrameEndTime(frameTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
int belowEnd = below.getFrameEndTime(frameTime);
|
|
||||||
int aboveEnd = above.getFrameEndTime(frameTime);
|
|
||||||
|
|
||||||
if (belowEnd < 0) {
|
|
||||||
return aboveEnd;
|
|
||||||
}
|
|
||||||
else if (aboveEnd < 0 || belowEnd < aboveEnd) {
|
|
||||||
return belowEnd;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return aboveEnd;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class TileImageSprite extends TileImage
|
|
||||||
{
|
|
||||||
TileImage source;
|
|
||||||
int offsetX;
|
|
||||||
int offsetY;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
|
|
||||||
{
|
|
||||||
source.drawTo(gr, destX, destY, srcX+offsetX, srcY+offsetY);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getFrameEndTime(int frameTime) {
|
|
||||||
return source.getFrameEndTime(frameTime);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class SourceImage extends TileImage
|
|
||||||
{
|
|
||||||
Image image;
|
|
||||||
int basisSize;
|
|
||||||
int targetSize;
|
|
||||||
|
|
||||||
SourceImage(Image image, int basisSize, int targetSize) {
|
|
||||||
this.image = image;
|
|
||||||
this.basisSize = basisSize;
|
|
||||||
this.targetSize = targetSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
|
|
||||||
{
|
|
||||||
srcX = srcX * basisSize / STD_SIZE;
|
|
||||||
srcY = srcY * basisSize / STD_SIZE;
|
|
||||||
|
|
||||||
gr.drawImage(
|
|
||||||
image,
|
|
||||||
destX, destY,
|
|
||||||
destX+targetSize, destY+targetSize,
|
|
||||||
srcX, srcY,
|
|
||||||
srcX+basisSize, srcY+basisSize,
|
|
||||||
null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getFrameEndTime(int frameTime) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static TileImage parseFrameSpec(TileSpec spec)
|
static TileImage parseFrameSpec(TileSpec spec)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
|
|
104
src/micropolisj/build_tool/TileImage.java
Normal file
104
src/micropolisj/build_tool/TileImage.java
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
package micropolisj.build_tool;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
public abstract class TileImage
|
||||||
|
{
|
||||||
|
public static final int STD_SIZE = 16;
|
||||||
|
|
||||||
|
public abstract void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY);
|
||||||
|
/**
|
||||||
|
* @return the end-time of the animation frame identified by frameTime;
|
||||||
|
* -1 if not an animation, or if frameTime is past the end of the animation
|
||||||
|
*/
|
||||||
|
public abstract int getFrameEndTime(int frameTime);
|
||||||
|
|
||||||
|
|
||||||
|
static class TileImageLayer extends TileImage
|
||||||
|
{
|
||||||
|
TileImageLayer below;
|
||||||
|
TileImage above;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
|
||||||
|
{
|
||||||
|
if (below != null) {
|
||||||
|
below.drawTo(gr, destX, destY, srcX, srcY);
|
||||||
|
}
|
||||||
|
above.drawTo(gr, destX, destY, srcX, srcY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getFrameEndTime(int frameTime)
|
||||||
|
{
|
||||||
|
if (below == null) {
|
||||||
|
return above.getFrameEndTime(frameTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
int belowEnd = below.getFrameEndTime(frameTime);
|
||||||
|
int aboveEnd = above.getFrameEndTime(frameTime);
|
||||||
|
|
||||||
|
if (belowEnd < 0) {
|
||||||
|
return aboveEnd;
|
||||||
|
}
|
||||||
|
else if (aboveEnd < 0 || belowEnd < aboveEnd) {
|
||||||
|
return belowEnd;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return aboveEnd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TileImageSprite extends TileImage
|
||||||
|
{
|
||||||
|
TileImage source;
|
||||||
|
int offsetX;
|
||||||
|
int offsetY;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
|
||||||
|
{
|
||||||
|
source.drawTo(gr, destX, destY, srcX+offsetX, srcY+offsetY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getFrameEndTime(int frameTime) {
|
||||||
|
return source.getFrameEndTime(frameTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class SourceImage extends TileImage
|
||||||
|
{
|
||||||
|
Image image;
|
||||||
|
int basisSize;
|
||||||
|
int targetSize;
|
||||||
|
|
||||||
|
SourceImage(Image image, int basisSize, int targetSize) {
|
||||||
|
this.image = image;
|
||||||
|
this.basisSize = basisSize;
|
||||||
|
this.targetSize = targetSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
|
||||||
|
{
|
||||||
|
srcX = srcX * basisSize / STD_SIZE;
|
||||||
|
srcY = srcY * basisSize / STD_SIZE;
|
||||||
|
|
||||||
|
gr.drawImage(
|
||||||
|
image,
|
||||||
|
destX, destY,
|
||||||
|
destX+targetSize, destY+targetSize,
|
||||||
|
srcX, srcY,
|
||||||
|
srcX+basisSize, srcY+basisSize,
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getFrameEndTime(int frameTime) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue