package com.gebauz.PonPonChun.game.entities;
import java.util.Vector;
import com.gebauz.Bauzoid.game.Game;
import com.gebauz.Bauzoid.game.GameObject;
import com.gebauz.Bauzoid.graphics.sprite.AtlasSprite;
import com.gebauz.Bauzoid.graphics.sprite.AtlasSpriteInstance;
public class BlockBreakParticles
extends GameObject
{
// Constants========================================================================================
public static final float DEFAULT_LIFE_TIME = 0.4f
;
public static final float MAX_DX = Block.
BREAKBLOCK_SIZE * 7;
public static final float MAX_DY = Block.
BREAKBLOCK_SIZE * 7;
public static final float MIN_DX = Block.
BREAKBLOCK_SIZE;
public static final float MIN_DY = Block.
BREAKBLOCK_SIZE;
public static final float MAX_ANGLE_SPEED = 360.0f
;
// Embedded Types===================================================================================
public class Particle
{
public int spriteIndex
;
public float x
;
public float y
;
public float dx
;
public float dy
;
public float mLifeTimer = 0.0f
;
public float mTotalLifeTime = DEFAULT_LIFE_TIME
;
public float angle
;
public float angleDelta
;
public Particle
(int index,
float _x,
float _y,
float _dx,
float _dy,
float _angle,
float _angleDelta
)
{
this(index, _x, _y, _dx, _dy,_angle, _angleDelta, DEFAULT_LIFE_TIME
);
}
public Particle
(int index,
float _x,
float _y,
float _dx,
float _dy,
float _angle,
float _angleDelta,
float lifeTime
)
{
spriteIndex = index
;
x = _x
;
y = _y
;
dx = _dx
;
dy = _dy
;
angle = _angle
;
angleDelta = _angleDelta
;
mTotalLifeTime = lifeTime
;
}
public void update
(float deltaTime
)
{
x += dx
*deltaTime
;
y += dy
*deltaTime
;
angle += angleDelta
*deltaTime
;
float s = 500.0f
;
if (dx
> 0.0f
)
{
dx -= deltaTime
* s
;
if (dx
< 0.0f
)
dx = 0.0f
;
}
if (dy
> 0.0f
)
{
dy -= deltaTime
* s
;
if (dy
< 0.0f
)
dy = 0.0f
;
}
if (dx
< 0.0f
)
{
dx += deltaTime
* s
;
if (dx
> 0.0f
)
dx = 0.0f
;
}
if (dy
< 0.0f
)
{
dy += deltaTime
* s
;
if (dy
> 0.0f
)
dy = 0.0f
;
}
mLifeTimer += deltaTime
;
}
public void render
()
{
AtlasSpriteInstance s = mBreakSprites
[spriteIndex
];
s.
x = x
;
s.
y = y
;
s.
w = Block.
BREAKBLOCK_SIZE;
s.
h = Block.
BREAKBLOCK_SIZE;
s.
pivotX = s.
w/
2;
s.
pivotY = s.
h/
2;
s.
angle = angle
;
s.
alpha = 1.0f -
(mLifeTimer/mTotalLifeTime
);
s.
render();
}
public boolean isAlive
()
{
return (mLifeTimer
< mTotalLifeTime
);
}
}
// Members==========================================================================================
//private PlayField mPlayField = null;
private BlockSprites mBlockSprites =
null;
private AtlasSpriteInstance mBreakSprites
[] =
new AtlasSpriteInstance
[BlockSprites.
BREAKBLOCK_SHARD_REGIONS];
private Vector<Particle
> mParticles =
new Vector<Particle
>();
// Methods==========================================================================================
public BlockBreakParticles
(Game game, BlockSprites sprites
)
{
super(game
);
mBlockSprites = sprites
;
}
public void init
()
{
AtlasSprite sprite = mBlockSprites.
getBreakSprite();
for (int i =
0; i
< mBreakSprites.
length; i++
)
{
mBreakSprites
[i
] = sprite.
createSpriteInstance(BlockSprites.
BREAKBLOCK_ANIM_REGIONS + i
);
}
}
public void exit
()
{
mParticles.
clear();
for (int i =
0; i
< mBreakSprites.
length; i++
)
{
mBreakSprites
[i
] =
null;
}
}
public void update
(float deltaTime
)
{
// update particles
int i =
0;
while (i
< mParticles.
size())
{
Particle p = mParticles.
get(i
);
p.
update(deltaTime
);
// remove dead particles
if (!p.
isAlive())
{
mParticles.
remove(i
);
continue;
}
i++
;
}
}
public void render
()
{
for (int i =
0; i
< mParticles.
size(); i++
)
{
mParticles.
get(i
).
render();
}
}
public void spawnParticles
(float x,
float y
)
{
for (int i =
0; i
< BlockSprites.
BREAKBLOCK_SHARD_REGIONS; i++
)
{
float angle = getGame
().
getRandomFloat(0,
360);
float dx = getGame
().
getRandomFloat(MIN_DX, MAX_DX
);
float dy = getGame
().
getRandomFloat(MIN_DY, MAX_DY
);
dx
*= getGame
().
getRandomSign();
dy
*= getGame
().
getRandomSign();
float angleDelta = getGame
().
getRandomFloat(-MAX_ANGLE_SPEED, MAX_ANGLE_SPEED
);
mParticles.
add(new Particle
(i, x, y, dx, dy, angle, angleDelta
));
}
}
// Getters/Setters==================================================================================
public final int getNumParticles
()
{
return mParticles.
size();
}
public final Particle getParticle
(int index
)
{
return mParticles.
get(index
);
}
}