package com.gebauz.pingk.entities;
import java.util.Vector;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.gebauz.Bauzoid.graphics.sprite.Sprite;
import com.gebauz.Bauzoid.math.Interpolation;
import com.gebauz.Bauzoid.math.MathUtil;
import com.gebauz.Bauzoid.math.Vector2;
import com.gebauz.pingk.game.GameConsts;
import com.gebauz.pingk.game.GameLogic;
public class Barriers
extends Entity
{
public class Particle
{
private float x
;
private float y
;
private boolean mIsHorizontal =
false;
private float mLifeTime = 0.0f
;
private int mLives = GameConsts.
POWERUP_BARRIERS_LIVES;
private float mFlickerTimer = -1.0f
;
public Particle
(float _x,
float _y,
boolean _horizontal
)
{
mIsHorizontal = _horizontal
;
x = _x
;
y = _y
;
}
public void update
(float deltaTime
)
{
mLifeTime += deltaTime
;
if (mFlickerTimer
> 0.0f
)
mFlickerTimer -= deltaTime
;
// no collision before fully grown
if (mLifeTime
< GameConsts.
POWERUP_BARRIERS_GROWTH_TIME)
return;
Ball ball = getGameLogic
().
getBall();
float growth = getGrowth
();
float length = GameConsts.
POWERUP_BARRIERS_LENGTH * growth
;
Vector2 v =
new Vector2
(ball.
getX() - ball.
getLastX(), ball.
getY() - ball.
getLastY());
if (mIsHorizontal
)
{
if (v.
y !=
0)
{
float t =
(y - ball.
getLastY()) / v.
y;
float sx = ball.
getLastX() + t
* v.
x;
if ((sx
>=
(x - length/2.0f
)) && (sx
<=
(x + length/2.0f
)))
{
if ((ball.
getLastY() < y
) && (ball.
getY() >= y
))
{
// ball going down - reflect up
collideWithBall
(sx, y, 180.0f
);
if (!isAlive
())
{
createHorizontalShards
(x, y, sx, length, -getGameLogic
().
getGame().
getRandomFloat(30.0f, 50.0f
));
}
}
else if ((ball.
getLastY() > y
) && (ball.
getY() <= y
))
{
// ball going up - reflect down
collideWithBall
(sx, y, 0.0f
);
if (!isAlive
())
{
createHorizontalShards
(x, y, sx, length, getGameLogic
().
getGame().
getRandomFloat(30.0f, 50.0f
));
}
}
}
}
}
else
{
if (v.
x !=
0)
{
float t =
(x - ball.
getLastX()) / v.
x;
float sy = ball.
getLastY() + t
* v.
y;
if ((sy
>=
(y - length/2.0f
)) && (sy
<=
(y + length/2.0f
)))
{
if ((ball.
getLastX() < x
) && (ball.
getX() >= x
))
{
// ball going right - reflect left
collideWithBall
(x, sy, 270.0f
);
if (!isAlive
())
{
createVerticalShards
(x, y, sy, length, -getGameLogic
().
getGame().
getRandomFloat(30.0f, 50.0f
));
}
//mShards
}
else if ((ball.
getLastX() > x
) && (ball.
getX() <= x
))
{
// ball going left - reflect right
collideWithBall
(x, sy, 90.0f
);
if (!isAlive
())
{
createVerticalShards
(x, y, sy, length, getGameLogic
().
getGame().
getRandomFloat(30.0f, 50.0f
));
}
}
}
}
}
}
public void render
()
{
float growth = getGrowth
();
mBarrier.
x = x
;
mBarrier.
y = y
;
mBarrier.
w = GameConsts.
POWERUP_BARRIERS_LENGTH * growth
;
mBarrier.
h = GameConsts.
POWERUP_BARRIERS_WIDTH;
if (mIsHorizontal
)
{
mBarrier.
angle = 0.0f
;
}
else
{
mBarrier.
angle = 90.0f
;
}
mBarrier.
pivotX = mBarrier.
w / 2.0f
;
mBarrier.
pivotY = mBarrier.
h / 2.0f
;
if (mFlickerTimer
> 0.0f
)
{
mBarrier.
color.
w = getGameLogic
().
getGame().
getRandomFloat(0.4f, 0.9f
);
mBarrier.
x += getGameLogic
().
getGame().
getRandomFloat(-GameConsts.
POWERUP_BARRIERS_WAGGLE, GameConsts.
POWERUP_BARRIERS_WAGGLE);
mBarrier.
y += getGameLogic
().
getGame().
getRandomFloat(-GameConsts.
POWERUP_BARRIERS_WAGGLE, GameConsts.
POWERUP_BARRIERS_WAGGLE);
}
else
{
mBarrier.
color.
w = growth
;
}
mBarrier.
render();
}
public void collideWithBall
(float x,
float y,
float angle
)
{
Ball ball = getGameLogic
().
getBall();
ball.
setX(x
);
ball.
setY(y
);
ball.
reflectAlongNormal(angle
);
mFlickerTimer = GameConsts.
POWERUP_BARRIERS_FLICKER_TIME;
mLives--
;
if (isAlive
())
getAudio
().
playSound(mBarrierHit
);
//mGameLogic.playSound(R.raw.barrier_hit);
else
getAudio
().
playSound(mBarrierShatter
);
//mGameLogic.playSound(R.raw.barrier_shatter);
// add hit
getGameLogic
().
getCrystalHitCounter().
addHit(x, y
);
}
public boolean isAlive
()
{
return (mLives
> 0);
}
public float getGrowth
()
{
//return MathUtil.ease(MathUtil.clamp(mLifeTime / GameConsts.POWERUP_BARRIERS_GROWTH_TIME, 0.0f, 1.0f));
return Interpolation.
simpleEaseInOut(MathUtil.
clamp(mLifeTime / GameConsts.
POWERUP_BARRIERS_GROWTH_TIME, 0.0f, 1.0f
));
}
}
public class Shard
{
private float x
;
private float y
;
private float w
;
private float h
;
private float mPivotX
;
private float mPivotY
;
private float mAngle
;
private float mTargetAngle
;
private float mLifeTime = 0.0f
;
public Shard
(float _x,
float _y,
float _w,
float _h,
float _pX,
float _pY,
float _angle,
float _targetAngle
)
{
x = _x
;
y = _y
;
w = _w
;
h = _h
;
mPivotX = _pX
;
mPivotY = _pY
;
mAngle = _angle
;
mTargetAngle = _targetAngle
;
}
public void update
(float deltaTime
)
{
float diff = mTargetAngle - mAngle
;
mAngle +=
(diff
* deltaTime
* 3.0f
);
mLifeTime += deltaTime
;
}
public void render
()
{
mBarrier.
x = x
;
mBarrier.
y = y
;
mBarrier.
w = w
;
mBarrier.
h = h
;
mBarrier.
angle = mAngle
;
mBarrier.
pivotX = mPivotX
;
mBarrier.
pivotY = mPivotY
;
float alpha = 1.0f - MathUtil.
clamp(mLifeTime / 1.0f, 0.0f, 1.0f
);
mBarrier.
color.
w = alpha
;
mBarrier.
render();
}
public boolean isAlive
()
{
return (mLifeTime
< 1.0f
);
}
}
private Vector<Particle
> mParticles =
new Vector<Particle
>();
private Vector<Shard
> mShards =
new Vector<Shard
>();
private Sprite mBarrier =
null;
private Sound mBarrierHit =
null;
private Sound mBarrierShatter =
null;
public Barriers
(GameLogic gameLogic
)
{
super(gameLogic
);
}
@
Override
public void init
()
{
mBarrier =
new Sprite
(getGraphics
(),
new Texture
(Gdx.
files.
internal("data/textures/barrier.png")));
mBarrier.
color = GameConsts.
PINGK_COLOR.
copy();
mBarrierHit = getAudio
().
newManagedSound("data/sounds/barrier_hit.wav");
mBarrierShatter = getAudio
().
newManagedSound("data/sounds/barrier_shatter.wav");
}
@
Override
public void exit
()
{
if (mBarrier
!=
null)
{
mBarrier.
dispose();
mBarrier =
null;
}
getAudio
().
removeManagedSound(mBarrierHit
);
getAudio
().
removeManagedSound(mBarrierShatter
);
mBarrierHit =
null;
mBarrierShatter =
null;
}
@
Override
public void update
(float deltaTime
)
{
int i =
0;
while (i
< mParticles.
size())
{
Particle p = mParticles.
get(i
);
p.
update(deltaTime
);
if (!p.
isAlive())
{
mParticles.
remove(i
);
continue;
}
i++
;
}
i =
0;
while (i
< mShards.
size())
{
Shard s = mShards.
get(i
);
s.
update(deltaTime
);
if (!s.
isAlive())
{
mShards.
remove(i
);
continue;
}
i++
;
}
a += deltaTime
* 90.0f
;
}
float a = 90.0f
;
@
Override
public void render
()
{
for (int i = mParticles.
size()-
1; i
>=
0; i--
)
{
mParticles.
get(i
).
render();
}
for (int i = mShards.
size()-
1; i
>=
0; i--
)
{
mShards.
get(i
).
render();
}
/* mBarrier.x = GameConsts.VIRTUAL_SCREEN_WIDTH/2.0f;
mBarrier.y = GameConsts.VIRTUAL_SCREEN_HEIGHT/2.0f;
mBarrier.w = GameConsts.POWERUP_BARRIERS_LENGTH/1.3f;
mBarrier.h = GameConsts.POWERUP_BARRIERS_WIDTH;
mBarrier.angle = a;
mBarrier.color.w = 1.0f;
mBarrier.pivotX = mBarrier.w;
mBarrier.pivotY = GameConsts.POWERUP_BARRIERS_WIDTH/2.0f;
mBarrier.render();*/
}
public void spawnBarrier
(float posX,
float posY,
boolean horizontal
)
{
Particle p =
new Particle
(posX, posY, horizontal
);
mParticles.
add(p
);
}
public int getNumBarriers
()
{
return mParticles.
size();
}
public void createVerticalShards
(float x,
float y,
float sy,
float length,
float angleDirection
)
{
mShards.
add(new Shard
(
x,
y - length/2.0f,
sy -
(y - length/2.0f
),
GameConsts.
POWERUP_BARRIERS_WIDTH,
sy -
(y - length/2.0f
),
GameConsts.
POWERUP_BARRIERS_WIDTH/2.0f,
90.0f,
90.0f - angleDirection
));
/* mShards.add(new Shard(
x,
y - length/2.0f,
sy - (y - length/2.0f),
GameConsts.POWERUP_BARRIERS_WIDTH,
0,
GameConsts.POWERUP_BARRIERS_WIDTH/2.0f,
90.0f,
90.0f - angleDirection
));
*/
mShards.
add(new Shard
(
x,
y + length/2.0f,
length/2.0f - sy + y,
GameConsts.
POWERUP_BARRIERS_WIDTH,
0,
GameConsts.
POWERUP_BARRIERS_WIDTH/2.0f,
90.0f,
90.0f + angleDirection
));
}
public void createHorizontalShards
(float x,
float y,
float sx,
float length,
float angleDirection
)
{
mShards.
add(new Shard
(
x - length/2.0f,
y,
sx -
(x - length/2.0f
),
GameConsts.
POWERUP_BARRIERS_WIDTH,
0,
GameConsts.
POWERUP_BARRIERS_WIDTH/2.0f,
0.0f,
angleDirection
));
mShards.
add(new Shard
(
x + length/2.0f,
y,
length/2.0f - sx + x,
GameConsts.
POWERUP_BARRIERS_WIDTH,
length/2.0f - sx + x,
GameConsts.
POWERUP_BARRIERS_WIDTH/2.0f,
0.0f,
-angleDirection
));
}
}