package com.gebauz.PonPonChun.game.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.gebauz.Bauzoid.graphics.Graphics;
import com.gebauz.Bauzoid.graphics.GraphicsObject;
import com.gebauz.Bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.Bauzoid.graphics.sprite.AtlasSpriteInstance;
import com.gebauz.Bauzoid.graphics.sprite.Sprite;
import com.gebauz.Bauzoid.graphics.sprite.SpriteRegion;
import com.gebauz.Bauzoid.math.MathUtil;
/** Blocks Sprite manager.
*
* @author chiu
*
*/
public class BlockSprites
extends GraphicsObject
{
// Constants========================================================================================
public static final int NUM_BLOCK_SPRITES =
8;
public static final float BLOCK_REGION_SIZE =
16;
public static final float BREAKBLOCK_REGION_SIZE =
18;
// Embedded Types===================================================================================
// Members==========================================================================================
private AtlasSprite mBlockAtlas =
null;
private AtlasSpriteInstance mBlockSprites
[] =
new AtlasSpriteInstance
[NUM_BLOCK_SPRITES
];
private AtlasSprite mBreakBlock =
null;
/** Selection cursor. */
private Sprite mCursor =
null;
// Methods==========================================================================================
public BlockSprites
(Graphics graphics
)
{
super(graphics
);
}
public void init
()
{
Texture blockTexture =
new Texture
(Gdx.
files.
internal("data/textures/blocks.png"));
SpriteRegion
[] blockRegions =
new SpriteRegion
[NUM_BLOCK_SPRITES
];
for (int i =
0; i
< blockRegions.
length; i++
)
{
// offset by 1 to account for the margin between regions
blockRegions
[i
] =
new SpriteRegion
(blockTexture,
(float)i
* (BLOCK_REGION_SIZE+
1), 0.0f, BLOCK_REGION_SIZE, BLOCK_REGION_SIZE
);
}
mBlockAtlas =
new AtlasSprite
(getGraphics
(), blockTexture, blockRegions
);
for (int i =
0; i
< blockRegions.
length; i++
)
{
mBlockSprites
[i
] = mBlockAtlas.
createSpriteInstance(i
);
}
Texture breakBlockTexture =
new Texture
(Gdx.
files.
internal("data/textures/breakblock.png"));
SpriteRegion
[] breakBlockRegions =
new SpriteRegion
[3];
for (int i =
0; i
< breakBlockRegions.
length; i++
)
{
// offset by 1 to account for the margin between regions
breakBlockRegions
[i
] =
new SpriteRegion
(breakBlockTexture,
(float)i
* (BREAKBLOCK_REGION_SIZE+
1), 0.0f, BREAKBLOCK_REGION_SIZE, BREAKBLOCK_REGION_SIZE
);
}
mBreakBlock =
new AtlasSprite
(getGraphics
(), breakBlockTexture, breakBlockRegions
);
Texture cursorTexture =
new Texture
(Gdx.
files.
internal("data/textures/cursor.png"));
mCursor =
new Sprite
(getGraphics
(), cursorTexture
);
mCursor.
pivotX = Block.
BLOCK_SIZE;
mCursor.
pivotY = Block.
BLOCK_SIZE;
mCursor.
x =
0;
mCursor.
y =
0;
mCursor.
w = Block.
BLOCK_SIZE*2;
mCursor.
h = Block.
BLOCK_SIZE*2;
}
public void exit
()
{
for (int i =
0; i
< mBlockSprites.
length; i++
)
{
mBlockSprites
[i
] =
null;
}
if (mBlockAtlas
!=
null)
{
mBlockAtlas.
dispose();
mBlockAtlas =
null;
}
if (mBreakBlock
!=
null)
{
mBreakBlock.
dispose();
mBreakBlock =
null;
}
if (mCursor
!=
null)
{
mCursor.
dispose();
mCursor =
null;
}
}
// Getters/Setters==================================================================================
public final AtlasSpriteInstance getBlockSprite
(int i
)
{
if (!MathUtil.
isInRange(i,
0, mBlockSprites.
length-
1))
return null;
return mBlockSprites
[i
];
}
public final AtlasSprite getBreakSprite
()
{
return mBreakBlock
;
}
public final Sprite getCursor
()
{
return mCursor
;
}
}