/**
* File name: AbstractGroup.java
* Version: 1.0
* Date: @date 13:13:26
* Author: Sawan J. Kapai Harpalani
* Copyright: Copyright 200X Sawan J. Kapai Harpalani
*
* This file is part of Math Attack.
*
* Math Attack is free software: you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Math Attack is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General
* Public License along with Math Attack. If not, see
* http://www.gnu.org/licenses/.
*/
package com.sawan.mathattack.scene2d;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.sawan.mathattack.settings.AppSettings;
// TODO: Auto-generated Javadoc
/**
* The Class AbstractGroup.
*/
public abstract class AbstractGroup extends Group {
//
/** The log tag. */
protected final String logTag = "MtxAbstractActorLog";
/** The log active. */
public static boolean logActive = true;
// Texture Region for actor (Not mandatory)
/** The texture region. */
private TextureRegion textureRegion;
/** The is texture region active. */
private boolean isTextureRegionActive = false;
// Animation for actor (Not mandatory)
/** The animation. */
private Animation animation;
/** The animation momentary. */
private Animation animationMomentary;
/** The is animation active. */
private boolean isAnimationActive = false;
/** The is animation momentary active. */
private boolean isAnimationMomentaryActive = false;
/** The is animation momentary finished. */
private boolean isAnimationMomentaryFinished = true;
/** The is animation looping. */
private boolean isAnimationLooping = false;
/** The kill all animations. */
private boolean killAllAnimations = false;
// Animation timer
/** The state time. */
private float stateTime = 0.0f;
// Particle
/** The particle effect. */
private ParticleEffect particleEffect;
/** The particle pos x. */
private float particlePosX = 0.0f;
/** The particle pos y. */
private float particlePosY = 0.0f;
/** The is particle effect active. */
private boolean isParticleEffectActive;
// Actor second counter (1 second tick)
/** The start time. */
private long startTime = System.nanoTime();
/** The seconds time. */
private long secondsTime = 0L;
/**
* Instantiates a new abstract group.
*
* @param textureRegion the texture region
* @param isTextureRegionActive the is texture region active
* @param posX the pos x
* @param posY the pos y
* @param orgnX the orgn x
* @param orgnY the orgn y
* @param width the width
* @param height the height
*/
public AbstractGroup(TextureRegion textureRegion,
boolean isTextureRegionActive, float posX, float posY, float orgnX,
float orgnY, float width, float height) {
super();
this.textureRegion = textureRegion;
this.isTextureRegionActive = isTextureRegionActive;
setBounds(posX, posY, width, height);
setPosition(posX, posY);
setSize(width, height);
setOrigin(orgnX, orgnY);
}
/**
* Instantiates a new abstract group.
*
* @param textureRegion the texture region
* @param isTextureRegionActive the is texture region active
* @param posX the pos x
* @param posY the pos y
* @param width the width
* @param height the height
*/
public AbstractGroup(TextureRegion textureRegion,
boolean isTextureRegionActive, float posX, float posY, float width,
float height) {
super();
this.textureRegion = textureRegion;
this.isTextureRegionActive = isTextureRegionActive;
setBounds(posX, posY, width, height);
setPosition(posX, posY);
setSize(width, height);
}
/**
* Instantiates a new abstract group.
*
* @param posX the pos x
* @param posY the pos y
* @param width the width
* @param height the height
*/
public AbstractGroup(float posX, float posY, float width, float height) {
super();
setBounds(posX, posY, width, height);
setPosition(posX, posY);
setSize(width, height);
}
/**
* Instantiates a new abstract group.
*
* @param width the width
* @param height the height
* @param DIPActive the DIP active
*/
public AbstractGroup(float width, float height, boolean DIPActive) {
super();
if (DIPActive) {
float ratioSize = AppSettings.getWorldSizeRatio();
setSize(width * ratioSize, height * ratioSize);
} else {
setSize(width, height);
}
}
/**
* Instantiates a new abstract group.
*/
public AbstractGroup() {
super();
}
/* (non-Javadoc)
* @see com.badlogic.gdx.scenes.scene2d.Group#act(float)
*/
@Override
public void act(float delta) {
super.act(delta);
stateTime += delta;
// Update time (1 second tick)
// ############################################################
if (System.nanoTime() - startTime >= 1000000000) {
secondsTime++;
startTime = System.nanoTime();
}
}
/* (non-Javadoc)
* @see com.badlogic.gdx.scenes.scene2d.Group#draw(com.badlogic.gdx.graphics.g2d.SpriteBatch, float)
*/
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
// For fade out/in effect
batch.setColor(this.getColor().r, this.getColor().g, this.getColor().b,
parentAlpha * this.getColor().a);
// DRAW TEXTURE REGION (Draw only if set active and not null)
// ##################################################################
if (isTextureRegionActive && textureRegion != null) {
// Draw it due to actors' settings
batch.draw(textureRegion, getX(), getY(), getOriginX(),
getOriginY(), getWidth(), getHeight(), getScaleX(),
getScaleY(), getRotation());
}
// DRAW ANIMATION (Draw only if set active and not null)
// ##################################################################
if (isAnimationActive && animation != null) {
// Get frame by frame and animate
TextureRegion keyFrame = animation.getKeyFrame(stateTime,
isAnimationLooping);
// Draw it due to actors' settings
batch.draw(keyFrame, getX(), getY(), getOriginX(), getOriginY(),
getWidth(), getHeight(), getScaleX(), getScaleY(),
getRotation());
if (animation.isAnimationFinished(stateTime)) {
if (killAllAnimations) {
isAnimationActive = false;
}
}
}
// DRAW ANIMATION MOMENTARY (Draw only if set active and not null)
// ##################################################################
if (isAnimationMomentaryActive && animationMomentary != null) {
if (animationMomentary.isAnimationFinished(stateTime)) {
if (!killAllAnimations) {
isAnimationActive = true;
isAnimationMomentaryActive = false;
isAnimationMomentaryFinished = true;
startTime = 0;
} else {
isAnimationActive = false;
isAnimationMomentaryActive = false;
isAnimationMomentaryFinished = true;
startTime = 0;
}
}
if (isAnimationMomentaryActive) {
// Get frame by frame and animate
TextureRegion keyFrame = animationMomentary.getKeyFrame(
stateTime, false);
// Draw it due to actors' settings
batch.draw(keyFrame, getX(), getY(), getOriginX(),
getOriginY(), getWidth(), getHeight(), getScaleX(),
getScaleY(), getRotation());
}
}
// PARTICLE
// #################################################################
if (isParticleEffectActive) {
particleEffect.draw(batch, Gdx.graphics.getDeltaTime());
particleEffect.setPosition(getX() + particlePosX, getY()
+ particlePosY);
}
}
/**
* Translate actor in a direction of speed without stopping. Actor moves in
* constants speed set without acceleration
*
* @param speedX
* axis-X speed
* @param speedY
* axis-Y speed
* @param delta
* the delta time for accurate speed
* */
public void translateWithoutAcc(float speedX, float speedY, float delta) {
setPosition(getX() + (speedX * delta), getY() + (speedY * delta));
}
/**
* Get textureRegion of the actor.
*
* @return TextureRegion
*/
public TextureRegion getTextureRegion() {
return textureRegion;
}
/**
* Set texture region for the actor, it will be drawn only if texture region
* is set and active.
*
* @param textureRegion texture region of the actor
* @param isTextureRegionActive set texture region active to be drawn or not
*/
public void setTextureRegion(TextureRegion textureRegion,
boolean isTextureRegionActive) {
this.textureRegion = textureRegion;
this.isTextureRegionActive = isTextureRegionActive;
}
/**
* Get animation of the actor.
*
* @return animation
*/
public Animation getAnimation() {
return animation;
}
/**
* Set animation of the actor.
*
* @param animation set animation
* @param isAnimationActive set animation active to be drawn or not
* @param isAnimationLooping set animation to loop or not
*/
public void setAnimation(Animation animation, boolean isAnimationActive,
boolean isAnimationLooping) {
this.animation = animation;
this.isAnimationActive = isAnimationActive;
this.isAnimationLooping = isAnimationLooping;
//
}
/**
* Gets the animation momentary.
*
* @return the animation momentary
*/
public Animation getAnimationMomentary() {
return animationMomentary;
}
/**
* Set a momentary animation for the actor.
*
* <p>
* EXAMPLE<br>
* Actor has two animations idle and blinkeye. If you set the the momentary
* animation as blinkeye, actor will blink eye and it will go its' regular
* animation such as idle
* <p>
* "animationAfterMomentary" For instance, a bat flies and changes into
* dracula. Regular animation is flying bat, animation momentary is the
* smoke at moment of change, animationAfterMomentary is the dracula
* animation
* <p>
* "isAnimationMomentaryWaitingToBeCompleted" prevents the animation to be
* run again and again when this method clicked contineusly, if its "true"
* this method wont be active until momentary animation completed
* <p>
* "killAllAnimations" is for ending animation, like a character dying
* animation, then no more animation will be running. It can be also used
* invisibility features
*
* @param animationMomentary
* set animation momentary
* @param isAnimationMomentaryActive
* set animation momentary active to be drawn or not
* @param animationAfterMomentary
* change regular animation after momentary animation completed
* otherwise set null
* @param isAnimationMomentaryWaitingToBeCompleted
* wait for to be completed, otherwise it will start again when
* this method called
* @param killAllAnimations
* do not run any animations after moementary animation completed
* */
public void setAnimationMomentary(Animation animationMomentary,
boolean isAnimationMomentaryActive,
Animation animationAfterMomentary,
boolean isAnimationMomentaryWaitingToBeCompleted,
boolean killAllAnimations) {
this.killAllAnimations = killAllAnimations;
if (animationAfterMomentary != null) {
animation = animationAfterMomentary;
}
if (!isAnimationMomentaryWaitingToBeCompleted) {
this.animationMomentary = animationMomentary;
this.animationMomentary.setPlayMode(Animation.NORMAL);
this.isAnimationMomentaryActive = isAnimationMomentaryActive;
if (isAnimationMomentaryActive) {
stateTime = 0;
isAnimationActive = false;
}
} else {
if (isAnimationMomentaryFinished) {
this.animationMomentary = animationMomentary;
this.animationMomentary.setPlayMode(Animation.NORMAL);
this.isAnimationMomentaryActive = isAnimationMomentaryActive;
//
if (isAnimationMomentaryActive) {
stateTime = 0;
isAnimationActive = false;
}
isAnimationMomentaryFinished = false;
}
}
}
/**
* Get animation is active or not.
*
* @return boolean value
*/
public boolean isAnimationActive() {
return isAnimationActive;
}
/**
* Set animation active, animation only be drawn if the animation is setted
* and active.
*
* @param isAnimationActive value to set animation active or not
*/
public void setAnimationActive(boolean isAnimationActive) {
this.isAnimationActive = isAnimationActive;
}
/**
* Get the animation is looping or not.
*
* @return boolean value
*/
public boolean isAnimationLooping() {
return isAnimationLooping;
}
/**
* Set animation to loop or not. It will only works is animation set and
* active
*
* @param isAnimationLooping
* boolean value
*
* */
public void setAnimationLooping(boolean isAnimationLooping) {
this.isAnimationLooping = isAnimationLooping;
}
/**
* Returns the state time for this actor, it can be used in animations.
*
* @return state time (delta added)
*/
public float getStateTime() {
return stateTime;
}
/**
* Set state time.
*
* @param stateTime the new state time
*/
public void setStateTime(float stateTime) {
this.stateTime = stateTime;
}
/**
* Get if killAllAnimation active.
*
* @return true, if is kill all animations
*/
public boolean isKillAllAnimations() {
return killAllAnimations;
}
/**
* Set killAllAnimations. If is true, after animations completed it wont be
* visible anymore
*
* @param killAllAnimations the new kill all animations
*/
public void setKillAllAnimations(boolean killAllAnimations) {
this.killAllAnimations = killAllAnimations;
}
/**
* Get seconds from the moment this actor created.
*
* @return the seconds time
*/
public long getSecondsTime() {
return secondsTime;
}
/**
* Set seconds, it can be used the reset seconds for this actor.
*
* @param secondsTime the new seconds time
*/
public void setSecondsTime(long secondsTime) {
this.secondsTime = secondsTime;
}
/**
* Get particle for this actor.
*
* @return the particle effect
*/
public ParticleEffect getParticleEffect() {
return particleEffect;
}
/**
* Set particle for this actor, centerPosition is used to center the
* particle on this actor sizes.
*
* @param particleEffect the particle effect
* @param isParticleEffectActive the is particle effect active
* @param isStart the is start
* @param centerPosition the center position
*/
public void setParticleEffect(ParticleEffect particleEffect,
boolean isParticleEffectActive, boolean isStart,
boolean centerPosition) {
this.particleEffect = particleEffect;
this.isParticleEffectActive = isParticleEffectActive;
if (!centerPosition) {
this.particleEffect.setPosition(getX(), getY());
} else {
particlePosX = getWidth() / 2.0f;
particlePosY = getHeight() / 2.0f;
this.particleEffect.setPosition(getX() + particlePosX, getY()
+ particlePosY);
}
if (isStart) {
this.particleEffect.start();
}
}
/**
* Set particle position.
*
* @param x the x
* @param y the y
*/
public void setParticlePositionXY(float x, float y) {
particlePosX = x;
particlePosY = y;
}
/**
* Check if particle active.
*
* @return true, if is particle effect active
*/
public boolean isParticleEffectActive() {
return isParticleEffectActive;
}
/**
* Set particle active to draw or not.
*
* @param isParticleEffectActive the new particle effect active
*/
public void setParticleEffectActive(boolean isParticleEffectActive) {
this.isParticleEffectActive = isParticleEffectActive;
}
}