gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


src/scene2d/MapActor.java
a9b2a64 1
package scene2d;
a62d533 2
import com.badlogic.gdx.Gdx;
a62d533 3
import com.badlogic.gdx.graphics.g2d.Animation;
a62d533 4
import com.badlogic.gdx.graphics.g2d.Batch;
a62d533 5
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
a62d533 6
import com.badlogic.gdx.graphics.g2d.TextureRegion;
a62d533 7
import com.badlogic.gdx.scenes.scene2d.Actor;
a62d533 8
a62d533 9
/** The MapActor Class
a62d533 10
 * <p>
a62d533 11
 * The MapActor is a SceneActor that can be used as a static tile, animated tile, animated actor or as a plain
a62d533 12
 * actor.<br>
a62d533 13
 * 1.For using it as a Static Tile use:<br>
a62d533 14
 *  	MapActor(TextureRegion region, int row, int col, int id, int tileSize)<br>
a62d533 15
 * 2.For using it as a Animated Tile/Actor use:<br>
a62d533 16
 * 	 	MapActor(Animation a, int row, int col, int id, int tileSize)<br>
a62d533 17
 * 3.For using it as a plain Actor use:<br>
a62d533 18
 * 	 	MapActor(int row, int col, int tileSize)<br>
a62d533 19
 * 
a62d533 20
 * It has many important methods like moveTo, moveBy, collides, intersects, getCenterX, getCenterY
a62d533 21
 * 
a62d533 22
 * <b>Note:</b> Only Use setPositionXY and SetPositionRC on this actor do not use the Actor's setPosition method
a62d533 23
 * as it causes problems
a62d533 24
 * @author pyros2097 */
a62d533 25
public class MapActor extends Actor {
a62d533 26
	private int row;
a62d533 27
	private int col;
a62d533 28
	private int tileSize;
a62d533 29
	
a62d533 30
	public TextureRegion tileImage;
a62d533 31
	public int index;
a62d533 32
	
a62d533 33
  	protected Animation animation;
a62d533 34
  	protected boolean isAnimationActive = false;
a62d533 35
  	protected boolean isAnimationLooping = true;
a62d533 36
	protected TextureRegion keyFrame;
a62d533 37
a62d533 38
	// Particle
a62d533 39
	public ParticleEffect particleEffect;
a62d533 40
	public float particlePosX = 0.0f;
a62d533 41
	public float particlePosY = 0.0f;
a62d533 42
	public boolean isParticleEffectActive;
a62d533 43
a62d533 44
	// When tiles coords row and column are directly specified
a62d533 45
	public MapActor(int row, int col, int tileSize){
a62d533 46
		setTileSize(tileSize);
a62d533 47
		setPositionRC(row, col);
a62d533 48
		setSize(tileSize, tileSize); // All map units are 24x24
a62d533 49
	}
a62d533 50
	
a62d533 51
	public MapActor(TextureRegion region, int row, int col, int id, int tileSize){
a62d533 52
		this(row, col, tileSize);
a62d533 53
		index = id;
a62d533 54
		if(region != null)
a62d533 55
			tileImage = region;
a62d533 56
	}
a62d533 57
	
a62d533 58
	
a62d533 59
	public MapActor(Animation a, int row, int col, int id, int tileSize) {
a62d533 60
		this(row, col, tileSize);
a62d533 61
		index = id;
a62d533 62
		if(a != null){
a62d533 63
			animation = a;
a62d533 64
			isAnimationActive = true;
a62d533 65
		}
a62d533 66
		
a62d533 67
	}
a62d533 68
	
a62d533 69
	@Override
a62d533 70
	public void draw(Batch batch, float parentAlpha) {
a62d533 71
		super.draw(batch, parentAlpha);
a62d533 72
		batch.setColor(getColor());
a62d533 73
		if(tileImage != null)
a62d533 74
			batch.draw(tileImage, getX(), getY(), tileSize, tileSize);
a62d533 75
		//font("normal").draw(batch, ""+index, getX(), getY()+tileSize);
a62d533 76
		if (isAnimationActive && animation != null) {
a9b2a64 77
			keyFrame = animation.getKeyFrame(Scene.stateTime, isAnimationLooping);
a62d533 78
			batch.draw(keyFrame, getX(), getY(), tileSize, tileSize);
a62d533 79
		}
a62d533 80
		drawParticleEffect(batch);
a62d533 81
	}
a62d533 82
	
a62d533 83
	public void drawParticleEffect(Batch batch){
a62d533 84
		if (isParticleEffectActive) {
a62d533 85
			particleEffect.draw(batch, Gdx.graphics.getDeltaTime());
a62d533 86
			particleEffect.setPosition(getX() + particlePosX, getY()+ particlePosY);
a62d533 87
		}
a62d533 88
	}
a62d533 89
a62d533 90
	/**
a62d533 91
	 * Set particle for this actor, centerPosition is used to center the
a62d533 92
	 * particle on this actor sizes
a62d533 93
	 * */
a62d533 94
	public void setParticleEffect(ParticleEffect particleEffect,
a62d533 95
			boolean isParticleEffectActive, boolean isStart,
a62d533 96
			boolean centerPosition) {
a62d533 97
		this.particleEffect = particleEffect;
a62d533 98
		this.isParticleEffectActive = isParticleEffectActive;
a62d533 99
		if (!centerPosition) {
a62d533 100
			this.particleEffect.setPosition(getX(), getY());
a62d533 101
		} else {
a62d533 102
			particlePosX = getWidth() / 2.0f;
a62d533 103
			particlePosY = getHeight() / 2.0f;
a62d533 104
			this.particleEffect.setPosition(getX() + particlePosX, getY()
a62d533 105
					+ particlePosY);
a62d533 106
		}
a62d533 107
a62d533 108
		if (isStart) {
a62d533 109
			this.particleEffect.start();
a62d533 110
		}
a62d533 111
	}
a62d533 112
a62d533 113
	/**
a62d533 114
	 * Set particle position
a62d533 115
	 * */
a62d533 116
	public void setParticlePositionXY(float x, float y) {
a62d533 117
		particlePosX = x;
a62d533 118
		particlePosY = y;
a62d533 119
	}
a62d533 120
	
a62d533 121
	
a62d533 122
	public void setTileSize(int tsize){
a62d533 123
		tileSize = tsize;
a62d533 124
	}
a62d533 125
	
a62d533 126
	public int getTileSize(){
a62d533 127
		return tileSize;
a62d533 128
	}
a62d533 129
	
a62d533 130
	public int getCol(){
a62d533 131
		return col;
a62d533 132
	}
a62d533 133
	
a62d533 134
	public int getRow(){
a62d533 135
		return row;
a62d533 136
	}
a62d533 137
	
a62d533 138
	/*
a62d533 139
	 * Col is always x-axis and Row is y-axis
a62d533 140
	 */
a62d533 141
	public void setPositionRC(int row, int col){
a62d533 142
		setPosition(col*tileSize, row*tileSize);
a62d533 143
	}
a62d533 144
	
a62d533 145
	/*
a62d533 146
	 * The setPosition calls the super Actor's setPosition method and also updates the row and col
a62d533 147
	 * position of the tile
a62d533 148
	 */
a62d533 149
	@Override
a62d533 150
	public void setPosition(float x, float y){
a62d533 151
		super.setPosition(x, y);
a62d533 152
		this.row =(int)this.getY()/tileSize;
a62d533 153
		this.col = (int)this.getX()/tileSize;
a62d533 154
	}
a62d533 155
	
a62d533 156
	/**
a62d533 157
	 * Translate actor in a direction of speed without stopping. Actor moves in
a62d533 158
	 * constants speed set without acceleration
a62d533 159
	 * 
a62d533 160
	 * @param speedX
a62d533 161
	 *            axis-X speed
a62d533 162
	 * @param speedY
a62d533 163
	 *            axis-Y speed
a62d533 164
	 * @param delta
a62d533 165
	 *            the delta time for accurate speed
a62d533 166
	 * */
a62d533 167
	public void translateWithoutAcc(float speedX, float speedY, float delta) {
a62d533 168
		setPosition(getX() + (speedX * delta), getY() + (speedY * delta));
a62d533 169
	}
a62d533 170
	
a62d533 171
	/**
a62d533 172
	 * Get center x point of an object
a62d533 173
	 * <p>
a62d533 174
	 * EXAMPLE<br>
a62d533 175
	 * Object's width 200, and we touched the screen in 400 in position X, and
a62d533 176
	 * we want to center the object according to our touch position. (200 / 2 =
a62d533 177
	 * 100 then 400 - 100), so 300 our center position
a62d533 178
	 * 
a62d533 179
	 * */
a62d533 180
	public static float getCenterX(float eventX, float objectWidth) {
a62d533 181
		return eventX - (objectWidth / 2);
a62d533 182
	}
a62d533 183
a62d533 184
	/**
a62d533 185
	 * @see getCenterX()
a62d533 186
	 * */
a62d533 187
	public static float getCenterY(float eventY, float objectHeight) {
a62d533 188
		return eventY - (objectHeight / 2);
a62d533 189
	}
a62d533 190
	
a62d533 191
	public boolean intersects(MapActor other){
a62d533 192
		//Sink.log(""+row+" : "+other.getRow());
a62d533 193
		if(col == other.getCol() && row == other.getRow())
a62d533 194
			return true;
a62d533 195
		return false;
a62d533 196
	}
a62d533 197
	
a62d533 198
	public Animation getAnimation(){
a62d533 199
		return animation;
a62d533 200
	}
a62d533 201
}