import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
* The MapActor is a SceneActor that can be used as a static tile, animated tile, animated actor or as a plain
* 1.For using it as a Static Tile use:<br>
* MapActor(TextureRegion region, int row, int col, int id, int tileSize)<br>
* 2.For using it as a Animated Tile/Actor use:<br>
* MapActor(Animation a, int row, int col, int id, int tileSize)<br>
* 3.For using it as a plain Actor use:<br>
* MapActor(int row, int col, int tileSize)<br>
* It has many important methods like moveTo, moveBy, collides, intersects, getCenterX, getCenterY
* <b>Note:</b> Only Use setPositionXY and SetPositionRC on this actor do not use the Actor's setPosition method
public class MapActor extends Actor {
public TextureRegion tileImage;
protected Animation animation;
protected boolean isAnimationActive = false;
protected boolean isAnimationLooping = true;
protected TextureRegion keyFrame;
public ParticleEffect particleEffect;
public float particlePosX = 0.0f;
public float particlePosY = 0.0f;
public boolean isParticleEffectActive;
// When tiles coords row and column are directly specified
public MapActor(int row, int col, int tileSize){
setSize(tileSize, tileSize); // All map units are 24x24
public MapActor(TextureRegion region, int row, int col, int id, int tileSize){
this(row, col, tileSize);
public MapActor(Animation a, int row, int col, int id, int tileSize) {
this(row, col, tileSize);
isAnimationActive = true;
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
batch.setColor(getColor());
batch.draw(tileImage, getX(), getY(), tileSize, tileSize);
//font("normal").draw(batch, ""+index, getX(), getY()+tileSize);
if (isAnimationActive && animation != null) {
keyFrame = animation.getKeyFrame(Scene.stateTime, isAnimationLooping);
batch.draw(keyFrame, getX(), getY(), tileSize, tileSize);
drawParticleEffect(batch);
public void drawParticleEffect(Batch batch){
if (isParticleEffectActive) {
particleEffect.draw(batch, Gdx.graphics.getDeltaTime());
particleEffect.setPosition(getX() + particlePosX, getY()+ particlePosY);
* Set particle for this actor, centerPosition is used to center the
* particle on this actor sizes
public void setParticleEffect(ParticleEffect particleEffect,
boolean isParticleEffectActive, boolean isStart,
boolean centerPosition) {
this.particleEffect = particleEffect;
this.isParticleEffectActive = isParticleEffectActive;
this.particleEffect.setPosition(getX(), getY());
particlePosX = getWidth() / 2.0f;
particlePosY = getHeight() / 2.0f;
this.particleEffect.setPosition(getX() + particlePosX, getY()
this.particleEffect.start();
public void setParticlePositionXY(float x, float y) {
public void setTileSize(int tsize){
public int getTileSize(){
* Col is always x-axis and Row is y-axis
public void setPositionRC(int row, int col){
setPosition(col*tileSize, row*tileSize);
* The setPosition calls the super Actor's setPosition method and also updates the row and col
public void setPosition(float x, float y){
this.row =(int)this.getY()/tileSize;
this.col = (int)this.getX()/tileSize;
* Translate actor in a direction of speed without stopping. Actor moves in
* constants speed set without acceleration
* the delta time for accurate speed
public void translateWithoutAcc(float speedX, float speedY, float delta) {
setPosition(getX() + (speedX * delta), getY() + (speedY * delta));
* Get center x point of an object
* Object's width 200, and we touched the screen in 400 in position X, and
* we want to center the object according to our touch position. (200 / 2 =
* 100 then 400 - 100), so 300 our center position
public static float getCenterX(float eventX, float objectWidth) {
return eventX - (objectWidth / 2);
public static float getCenterY(float eventY, float objectHeight) {
return eventY - (objectHeight / 2);
public boolean intersects(MapActor other){
//Sink.log(""+row+" : "+other.getRow());
if(col == other.getCol() && row == other.getRow())
public Animation getAnimation(){