Commit 1f5a9b71 authored by Colin Thomas's avatar Colin Thomas

Adapting TileSprite and its child to use Animations

parent a0710738
package graphics.guiSkeleton.tileSpriteLinker;
import graphics.guiSkeleton.sprites.Animation;
import graphics.guiSkeleton.sprites.Displayable;
import graphics.guiSkeleton.sprites.Sprite;
import java.awt.image.BufferedImage;
......@@ -23,17 +27,43 @@ public class GroundTileSprite extends TileSprite
{
base = false;
variations = sprite.getWidth() / (4*TileSpriteWidth());
spriteTable = new BufferedImage[variations][20];
for (int k = 0 ; k < variations ; k++)
if (sprite.getHeight() > 6*TileSpriteHeight())
{
for (int i = 0; i < 4; i++)
spriteTable = new Animation[variations][20];
int nbFrames = sprite.getHeight() / (6*TileSpriteHeight());
for (int k = 0; k < variations; k++)
{
// i%2 -> i ; i/2 -> j
spriteTable[k][i] = sprite.getSubimage((4*k + 3 * (i % 2)) * TileSpriteWidth(), (2 + 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][4 + i] = sprite.getSubimage((4*k + 1 + 2 * (i % 2) - (i / 2)) * TileSpriteWidth(), (2 + (i % 2) + 2 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][8 + i] = sprite.getSubimage((4*k + 2 * (i % 2) + (i / 2)) * TileSpriteWidth(), (3 - (i % 2) + 2 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][12 + i] = sprite.getSubimage((4*k + 2 + i % 2) * TileSpriteWidth(), (i / 2) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][16 + i] = sprite.getSubimage((4*k + 1 + i % 2) * TileSpriteWidth(), (3 + i / 2) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
for (int i = 0; i < 4; i++)
{
Sprite[][] animationSpriteTable = new Sprite[5][nbFrames];
for (int j = 0 ; j < nbFrames ; j++)
{
animationSpriteTable[0][j] = new Sprite(sprite.getSubimage((4 * k + 3 * (i % 2)) * TileSpriteWidth(), (6*j+2 + 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[1][j] = new Sprite(sprite.getSubimage((4 * k + 1 + 2 * (i % 2) - (i / 2)) * TileSpriteWidth(), (6*j+2 + (i % 2) + 2 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[2][j] =new Sprite(sprite.getSubimage((4 * k + 2 * (i % 2) + (i / 2)) * TileSpriteWidth(), (6*j+3 - (i % 2) + 2 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[3][j] = new Sprite(sprite.getSubimage((4 * k + 2 + i % 2) * TileSpriteWidth(), (6*j+i / 2) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[4][j] = new Sprite(sprite.getSubimage((4 * k + 1 + i % 2) * TileSpriteWidth(), (6*j+3 + i / 2) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
}
spriteTable[k][i] = new Animation(animationSpriteTable[0],animationDelay);
spriteTable[k][4+i] = new Animation(animationSpriteTable[1],animationDelay);
spriteTable[k][8+i] = new Animation(animationSpriteTable[2],animationDelay);
spriteTable[k][12+i] = new Animation(animationSpriteTable[3],animationDelay);
spriteTable[k][16+i] = new Animation(animationSpriteTable[4],animationDelay);
}
}
}
else
{
spriteTable = new Sprite[variations][20];
for (int k = 0; k < variations; k++) {
for (int i = 0; i < 4; i++) {
// i%2 -> i ; i/2 -> j
spriteTable[k][i] = new Sprite(sprite.getSubimage((4 * k + 3 * (i % 2)) * TileSpriteWidth(), (2 + 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][4 + i] = new Sprite(sprite.getSubimage((4 * k + 1 + 2 * (i % 2) - (i / 2)) * TileSpriteWidth(), (2 + (i % 2) + 2 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][8 + i] = new Sprite(sprite.getSubimage((4 * k + 2 * (i % 2) + (i / 2)) * TileSpriteWidth(), (3 - (i % 2) + 2 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][12 + i] = new Sprite(sprite.getSubimage((4 * k + 2 + i % 2) * TileSpriteWidth(), (i / 2) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][16 + i] = new Sprite(sprite.getSubimage((4 * k + 1 + i % 2) * TileSpriteWidth(), (3 + i / 2) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
}
}
}
}
......@@ -43,12 +73,12 @@ public class GroundTileSprite extends TileSprite
}
@Override
public BufferedImage[][] getSprites(TileAbstractType[][] mask)
public Displayable[][] getSprites(TileAbstractType[][] mask)
{
if (base)
return BaseGetSprites(mask);
int k = randomGenerator.nextInt(variations);
BufferedImage[][] result = new BufferedImage[2][2];
Displayable[][] result = new Displayable[2][2];
for (int i = 0 ; i < 2 ; i++)
{
for (int j = 0 ; j < 2 ; j++)
......
package graphics.guiSkeleton.tileSpriteLinker;
import graphics.guiSkeleton.sprites.Animation;
import graphics.guiSkeleton.sprites.Displayable;
import graphics.guiSkeleton.sprites.Sprite;
import graphics.guiSkeleton.sprites.SpriteLoader;
import map_generation.tiles.TileType;
import java.awt.image.BufferedImage;
......@@ -16,7 +19,7 @@ import core.zone.Point;
* This class is abstract because its provides only the bases cases (if the sprite sheet doesn't need reassembling)
*/
public abstract class TileSprite {
protected BufferedImage[][] spriteTable;
protected Displayable[][] spriteTable;
// true if the sprite sheet doesn't need reassembling
protected boolean base;
......@@ -28,6 +31,9 @@ public abstract class TileSprite {
// random generator used to choose which variation to choose
static Random randomGenerator = new Random();
// frequency of the animated tileSprite (in frames)
protected static final int animationDelay = 10;
// enumeration of the abstract TileTypes used in the mask given in argument to getSprites method
// they are linked to different behaviours for the different subclasses
public static enum TileAbstractType {SELF, NONE, PLAIN, GROUND, WALL}
......@@ -39,7 +45,7 @@ public abstract class TileSprite {
//public static final int TileSpriteHeight() {return 8;}
//public static final int TileSpriteWidth() {return 8;}
private static BufferedImage ErrorSprite = SpriteLoader.getSpriteFromPath("/assets/Test_Error_Tiles/black_64_64.bmp").getSpriteImage();
private static Sprite ErrorSprite = SpriteLoader.getSpriteFromPath("/assets/Test_Error_Tiles/black_64_64.bmp");
/**
* Initialisation method for the base case (no reassembling)
......@@ -48,15 +54,34 @@ public abstract class TileSprite {
{
base = true;
variations = sprite.getWidth() / (2*TileSpriteWidth());
spriteTable = new BufferedImage[variations][4];
for (int k = 0 ; k < variations ; k++)
if (sprite.getHeight() > 2*TileSpriteHeight())
{
for (int i = 0; i < 4; i++)
spriteTable = new Animation[variations][4];
int nbFrames = sprite.getHeight() / (2*TileSpriteHeight());
for (int k = 0; k < variations; k++)
{
spriteTable[k][i] = sprite.getSubimage((2*k+(i/2))*TileSpriteWidth(), (i%2)*TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
for (int i = 0; i < 4; i++)
{
Sprite[] animationSpriteTable = new Sprite[nbFrames];
for (int j = 0 ; j < nbFrames ; j++)
{
animationSpriteTable[j] = new Sprite(sprite.getSubimage((2 * k + (i / 2)) * TileSpriteWidth(), (2*j+(i % 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
}
spriteTable[k][i] = new Animation(animationSpriteTable,animationDelay);
}
}
}
else
{
spriteTable = new Sprite[variations][4];
for (int k = 0; k < variations; k++)
{
for (int i = 0; i < 4; i++)
{
spriteTable[k][i] = new Sprite(sprite.getSubimage((2 * k + (i / 2)) * TileSpriteWidth(), (i % 2) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
}
}
}
}
/**
......@@ -64,19 +89,20 @@ public abstract class TileSprite {
*/
protected void ErrorTileSpriteInitialisation()
{
BaseInitialisation(ErrorSprite);
BaseInitialisation(ErrorSprite.getSpriteImage());
}
public abstract BufferedImage[][] getSprites(TileAbstractType[][] mask);
public abstract Displayable[][] getSprites(TileAbstractType[][] mask);
public abstract char[][] getChar(TileAbstractType[][] mask);
/**
* getSprites method for the base case (no reassembling)
*/
protected BufferedImage[][] BaseGetSprites(@SuppressWarnings("unused") TileAbstractType[][] mask)
protected Displayable[][] BaseGetSprites(@SuppressWarnings("unused") TileAbstractType[][] mask)
{
int k = randomGenerator.nextInt(variations);
BufferedImage[][] result = new BufferedImage[2][2];
Displayable[][] result = new Displayable[2][2];
for (int i = 0 ; i < 2 ; i++)
{
for (int j = 0 ; j < 2 ; j++)
......
package graphics.guiSkeleton.tileSpriteLinker;
import graphics.guiSkeleton.sprites.Displayable;
import graphics.guiSkeleton.sprites.SpriteLoader;
import map_generation.tiles.TileType;
......@@ -32,7 +33,7 @@ public class TileSpriteLinker
spriteMap.put(TileType.WATER , new GroundTileSprite(SpriteLoader.getSpriteFromPath("/assets/Lava.png").getSpriteImage()));
}
public BufferedImage[][] getSpriteOfTile(TileType type, TileSprite.TileAbstractType[][] mask)
public Displayable[][] getSpriteOfTile(TileType type, TileSprite.TileAbstractType[][] mask)
{
TileSprite tileSprite = spriteMap.get(type);
return tileSprite.getSprites(mask);
......
package graphics.guiSkeleton.tileSpriteLinker;
import graphics.guiSkeleton.sprites.Animation;
import graphics.guiSkeleton.sprites.Displayable;
import graphics.guiSkeleton.sprites.Sprite;
import java.awt.image.BufferedImage;
/**
......@@ -21,20 +25,52 @@ public class WallTileSprite extends TileSprite
{
base = false;
variations = sprite.getWidth() / (6*TileSpriteWidth());
spriteTable = new BufferedImage[variations][32];
for (int k = 0 ; k < variations ; k++)
if (sprite.getHeight() > 8*TileSpriteHeight())
{
for (int i = 0; i < 4; i++)
spriteTable = new Animation[variations][32];
int nbFrames = sprite.getHeight() / (8*TileSpriteHeight());
for (int k = 0; k < variations; k++)
{
// i%2 -> i ; i/2 -> j
spriteTable[k][i] = sprite.getSubimage((6*k + 5*(i % 2)) * TileSpriteWidth(), (2 + 5 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][4 + i] = sprite.getSubimage((6*k + 4 + (i % 2)) * TileSpriteWidth(), ((i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][8 + i] = sprite.getSubimage((6*k + 2 + 3 * (i % 2) - 2 * (i / 2)) * TileSpriteWidth(), (2 + 2 * (i % 2) + 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][12 + i] = sprite.getSubimage((6*k + 2 - (i % 2) + 2 * (i / 2)) * TileSpriteWidth(), (6 - 2 * (i % 2) - (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][16 + i] = sprite.getSubimage((6*k + 3 * (i % 2) + 2 * (i / 2)) * TileSpriteWidth(), (4 - 2 * (i % 2) + 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][20 + i] = sprite.getSubimage((6*k + 4 - (i % 2) - 2 * (i / 2)) * TileSpriteWidth(), (4 + 2 * (i % 2) - (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][24 + i] = sprite.getSubimage((6*k + 2 + (i % 2)) * TileSpriteWidth(), ((i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
spriteTable[k][28 + i] = sprite.getSubimage((6*k + 4 - 3 * (i % 2)) * TileSpriteWidth(), (6 - 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight());
for (int i = 0; i < 4; i++)
{
Sprite[][] animationSpriteTable = new Sprite[8][nbFrames];
for (int j = 0 ; j < nbFrames ; j++)
{
animationSpriteTable[0][j] = new Sprite(sprite.getSubimage((6 * k + 5 * (i % 2)) * TileSpriteWidth(), (8*j+2 + 5 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[1][j] = new Sprite(sprite.getSubimage((6 * k + 4 + (i % 2)) * TileSpriteWidth(), (8*j+(i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[2][j] = new Sprite(sprite.getSubimage((6 * k + 2 + 3 * (i % 2) - 2 * (i / 2)) * TileSpriteWidth(), (8*j+2 + 2 * (i % 2) + 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[3][j] = new Sprite(sprite.getSubimage((6 * k + 2 - (i % 2) + 2 * (i / 2)) * TileSpriteWidth(), (8*j+6 - 2 * (i % 2) - (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[4][j] = new Sprite(sprite.getSubimage((6 * k + 3 * (i % 2) + 2 * (i / 2)) * TileSpriteWidth(), (8*j+4 - 2 * (i % 2) + 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[5][j] = new Sprite(sprite.getSubimage((6 * k + 4 - (i % 2) - 2 * (i / 2)) * TileSpriteWidth(), (8*j+4 + 2 * (i % 2) - (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[6][j] = new Sprite(sprite.getSubimage((6 * k + 2 + (i % 2)) * TileSpriteWidth(), (8*j+(i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
animationSpriteTable[7][j] = new Sprite(sprite.getSubimage((6 * k + 4 - 3 * (i % 2)) * TileSpriteWidth(), (8*j+6 - 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
}
spriteTable[k][i] = new Animation(animationSpriteTable[0],animationDelay);
spriteTable[k][4+i] = new Animation(animationSpriteTable[1],animationDelay);
spriteTable[k][8+i] = new Animation(animationSpriteTable[2],animationDelay);
spriteTable[k][12+i] = new Animation(animationSpriteTable[3],animationDelay);
spriteTable[k][16+i] = new Animation(animationSpriteTable[4],animationDelay);
spriteTable[k][20+i] = new Animation(animationSpriteTable[5],animationDelay);
spriteTable[k][24+i] = new Animation(animationSpriteTable[6],animationDelay);
spriteTable[k][28+i] = new Animation(animationSpriteTable[7],animationDelay);
}
}
}
else
{
spriteTable = new Sprite[variations][32];
for (int k = 0 ; k < variations ; k++) {
for (int i = 0; i < 4; i++) {
// i%2 -> i ; i/2 -> j
spriteTable[k][i] = new Sprite(sprite.getSubimage((6 * k + 5 * (i % 2)) * TileSpriteWidth(), (2 + 5 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][4 + i] = new Sprite(sprite.getSubimage((6 * k + 4 + (i % 2)) * TileSpriteWidth(), ((i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][8 + i] = new Sprite(sprite.getSubimage((6 * k + 2 + 3 * (i % 2) - 2 * (i / 2)) * TileSpriteWidth(), (2 + 2 * (i % 2) + 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][12 + i] = new Sprite(sprite.getSubimage((6 * k + 2 - (i % 2) + 2 * (i / 2)) * TileSpriteWidth(), (6 - 2 * (i % 2) - (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][16 + i] = new Sprite(sprite.getSubimage((6 * k + 3 * (i % 2) + 2 * (i / 2)) * TileSpriteWidth(), (4 - 2 * (i % 2) + 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][20 + i] = new Sprite(sprite.getSubimage((6 * k + 4 - (i % 2) - 2 * (i / 2)) * TileSpriteWidth(), (4 + 2 * (i % 2) - (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][24 + i] = new Sprite(sprite.getSubimage((6 * k + 2 + (i % 2)) * TileSpriteWidth(), ((i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
spriteTable[k][28 + i] = new Sprite(sprite.getSubimage((6 * k + 4 - 3 * (i % 2)) * TileSpriteWidth(), (6 - 3 * (i / 2)) * TileSpriteHeight(), TileSpriteWidth(), TileSpriteHeight()));
}
}
}
}
......@@ -44,11 +80,11 @@ public class WallTileSprite extends TileSprite
}
@Override
public BufferedImage[][] getSprites(TileAbstractType[][] mask)
public Displayable[][] getSprites(TileAbstractType[][] mask)
{
if (base)
return BaseGetSprites(mask);
BufferedImage[][] result = new BufferedImage[2][2];
Displayable[][] result = new Displayable[2][2];
int k = randomGenerator.nextInt(variations);
for (int i = 0 ; i < 2 ; i++)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment