package com.gebauz.burutaru.game.entities;
import com.badlogic.gdx.audio.Sound;
import com.gebauz.bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteFrame;
import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteInstance;
import com.gebauz.bauzoid.math.MathUtil;
import com.gebauz.bauzoid.math.Matrix4;
import com.gebauz.bauzoid.math.collision.AABoundingBox;
import com.gebauz.bauzoid.math.collision.Shape;
import com.gebauz.bauzoid.math.collision.ShapeUtil;
import com.gebauz.burutaru.game.GameLogic;
public class BeamShots
extends Entity
{
// Constants========================================================================================
public static final int MAX_BEAMSHOTS =
4;
public static final int BEAMSHOT_DAMAGE =
10;
public static final float BEAMSHOT_WIDTH =
180;
public static final float BEAMSHOT_HEIGHT =
60;
public static final float BEAMSHOT_WIDTH_MIN =
30;
public static final float BEAMSHOT_HEIGHT_MIN =
10;
public static final float BEAMSHOT_SPEED =
1200;
public static final float BEAMSHOT_GROWTH_PERIOD = 0.15f
;
public static final int BEAMSHOT_PARTICLE_FRAME =
1;
public static final float BEAMSHOT_PARTICLE_WIDTH =
60;
public static final float BEAMSHOT_PARTICLE_HEIGHT =
20;
public static final float BEAMSHOT_PARTICLE_LIFETIME = 0.3f
;
public static final float BEAMSHOT_PARTICLE_INTERVAL = 0.1f
;
public static final int MAX_PARTICLES =
10;
// Embedded Types===================================================================================
public class BeamShotParticle
{
public float x =
0;
public float y =
0;
public float lifeTime =
0;
public BeamShotParticle
()
{
}
public void spawn
(float _x,
float _y
)
{
lifeTime =
0;
x = _x
; y = _y
;
}
public void update
(float deltaTime
)
{
lifeTime += deltaTime
;
x +=
(BEAMSHOT_SPEED
*0.5f
) * deltaTime
;
}
public void render
()
{
float t = lifeTime / BEAMSHOT_PARTICLE_LIFETIME
;
float a = MathUtil.
clamp(1.0f -
(t
*t
),
0,
1);
mBeamShotFrames
[BEAMSHOT_PARTICLE_FRAME
].
param.
x = x
;
mBeamShotFrames
[BEAMSHOT_PARTICLE_FRAME
].
param.
y = y
;
mBeamShotFrames
[BEAMSHOT_PARTICLE_FRAME
].
param.
w = BEAMSHOT_PARTICLE_WIDTH
;
mBeamShotFrames
[BEAMSHOT_PARTICLE_FRAME
].
param.
h = BEAMSHOT_PARTICLE_HEIGHT
;
mBeamShotFrames
[BEAMSHOT_PARTICLE_FRAME
].
param.
alpha = a
;
mBeamShotFrames
[BEAMSHOT_PARTICLE_FRAME
].
centerPivot();
mBeamShotFrames
[BEAMSHOT_PARTICLE_FRAME
].
render();
}
public boolean isAlive
()
{
return (lifeTime
< BEAMSHOT_PARTICLE_LIFETIME
);
}
}
public class BeamShotInstance
extends Projectile
{
public AtlasSpriteInstance spriteInstance =
null;
public float lifeTime = 0.0f
;
public float particleTimer = 0.0f
;
private BeamShotParticle mParticles
[] =
new BeamShotParticle
[MAX_PARTICLES
];
public BeamShotInstance
()
{
super(BEAMSHOT_DAMAGE
);
spriteInstance =
new AtlasSpriteInstance
(getGraphics
(), mBeamShotSprite, mBeamShotFrames, mBeamShotShape
);
spriteInstance.
param.
w = BEAMSHOT_WIDTH
;
spriteInstance.
param.
h = BEAMSHOT_HEIGHT
;
spriteInstance.
param.
pivotX = BEAMSHOT_WIDTH
;
spriteInstance.
param.
pivotY = BEAMSHOT_HEIGHT/
2;
for (int i =
0; i
< MAX_PARTICLES
; i++
)
mParticles
[i
] =
new BeamShotParticle
();
setAlive
(false);
}
public void spawn
(float x,
float y
)
{
spriteInstance.
param.
x = x
;
spriteInstance.
param.
y = y
;
particleTimer = 0.0f
;
lifeTime = 0.0f
;
for (int i =
0; i
< MAX_PARTICLES
; i++
)
mParticles
[i
].
lifeTime = BEAMSHOT_PARTICLE_LIFETIME +
10;
setAlive
(true);
}
public void update
(float deltaTime
)
{
spriteInstance.
param.
x += deltaTime
* BEAMSHOT_SPEED
;
float w = MathUtil.
clamp((lifeTime/BEAMSHOT_GROWTH_PERIOD
) * BEAMSHOT_WIDTH, BEAMSHOT_WIDTH_MIN, BEAMSHOT_WIDTH
);
float h = MathUtil.
clamp((lifeTime/BEAMSHOT_GROWTH_PERIOD
) * BEAMSHOT_HEIGHT, BEAMSHOT_HEIGHT_MIN, BEAMSHOT_HEIGHT
);
spriteInstance.
param.
w = w
;
spriteInstance.
param.
h = h
;
spriteInstance.
param.
pivotX = w
;
spriteInstance.
param.
pivotY = h/
2;
for (int i =
0; i
< MAX_PARTICLES
; i++
)
{
if (mParticles
[i
].
isAlive())
mParticles
[i
].
update(deltaTime
);
}
lifeTime += deltaTime
;
particleTimer += deltaTime
;
getGameLogic
().
checkProjectileHit(this);
if (particleTimer
>= BEAMSHOT_PARTICLE_INTERVAL
)
{
spawnParticles
();
particleTimer = 0.0f
;
}
if (getGameLogic
().
isOutOfScreen(spriteInstance.
param))
setAlive
(false);
}
public void render
()
{
spriteInstance.
renderFrame(0);
for (int i =
0; i
< MAX_PARTICLES
; i++
)
{
if (mParticles
[i
].
isAlive())
mParticles
[i
].
render();
}
}
@
Override
public void processHit
(ICollidable hitObject
)
{
// beam shots dont get destroyed on impact
hitObject.
hit(getDamage
());
}
@
Override
public AABoundingBox getBounds
()
{
return spriteInstance.
param.
getBoundingBox();
}
@
Override
public Shape getShape
()
{
return spriteInstance.
getShape();
}
@
Override
public Matrix4 getShapeTransform
()
{
return spriteInstance.
param.
getTransform();
}
public void spawnParticles
()
{
// always spawn if free
for (int i =
0; i
< MAX_PARTICLES
; i++
)
{
if (!mParticles
[i
].
isAlive())
{
float x = spriteInstance.
param.
x - getGame
().
getRandomFloat(BEAMSHOT_WIDTH_MIN, BEAMSHOT_WIDTH/
2);
float y = spriteInstance.
param.
y + getGame
().
getRandomFloat(-spriteInstance.
param.
h/
3, spriteInstance.
param.
h/
3);
mParticles
[i
].
spawn(x, y
);
break;
}
}
}
}
// Fields===========================================================================================
private AtlasSprite mBeamShotSprite =
null;
private AtlasSpriteFrame mBeamShotFrames
[] =
null;
private Shape mBeamShotShape =
null;
private BeamShotInstance mBeamShots
[] =
new BeamShotInstance
[MAX_BEAMSHOTS
];
private Sound mBeamShotSfx =
null;
// Methods==========================================================================================
public BeamShots
(GameLogic gameLogic
)
{
super(gameLogic
);
mBeamShotSprite =
new AtlasSprite
(getGraphics
(),
"data/textures/beamshot.png",
"data/textures/beamshot.txt");
}
@
Override
public void initAsync
()
{
mBeamShotSprite.
initAsync();
}
@
Override
public void init
()
{
mBeamShotSfx = getAudio
().
newManagedSound("data/sounds/beam_shot.wav");
mBeamShotSprite.
init();
mBeamShotFrames = mBeamShotSprite.
createSpriteInstances();
mBeamShotShape = ShapeUtil.
createShapeFromFile("data/textures/beamshot.shape");
for (int i =
0; i
< mBeamShots.
length; i++
)
mBeamShots
[i
] =
new BeamShotInstance
();
}
@
Override
public void exit
()
{
mBeamShotShape =
null;
mBeamShotFrames =
null;
if (mBeamShotSprite
!=
null)
{
mBeamShotSprite.
dispose();
mBeamShotSprite =
null;
}
if (mBeamShotSfx
!=
null)
{
getAudio
().
removeManagedSound(mBeamShotSfx
);
mBeamShotSfx =
null;
}
}
@
Override
public void update
(float deltaTime
)
{
for (int i =
0; i
< mBeamShots.
length; i++
)
{
if (mBeamShots
[i
].
isAlive())
mBeamShots
[i
].
update(deltaTime
);
}
}
@
Override
public void render
()
{
for (int i =
0; i
< mBeamShots.
length; i++
)
{
if (mBeamShots
[i
].
isAlive())
mBeamShots
[i
].
render();
}
}
public void spawnShot
(float x,
float y
)
{
for (int i =
0; i
< MAX_BEAMSHOTS
; i++
)
{
if (!mBeamShots
[i
].
isAlive())
{
mBeamShots
[i
].
spawn(x, y
);
mBeamShotSfx.
play();
break;
}
}
}
// Getters/Setters==================================================================================
}