package com.gebauz.burutaru.game.entities;
import com.gebauz.bauzoid.graphics.sprite.Sprite;
import com.gebauz.bauzoid.graphics.sprite.SpriteInstance;
import com.gebauz.burutaru.GameConsts;
import com.gebauz.burutaru.game.GameLogic;
public class StarField
extends Entity
{
// Constants========================================================================================
public static final int MAX_STARS =
28;
public static final float MIN_SPEED =
2;
public static final float MAX_SPEED =
5;
public static final float MIN_SIZE =
4;
public static final float MAX_SIZE =
8;
public static final float NORMAL_ALPHA = 0.4f
;
public static final float WARP_ALPHA = 0.8f
;
public static final float WARP_SCALE_FACTOR = 10.0f
;
public static final float WARP_SPEED_FACTOR = 4.0f
;
// Embedded Types===================================================================================
public enum Status
{
DISABLED,
NORMAL,
WARP,
}
public class Particle
{
public float x =
0;
public float y =
0;
public float w =
0;
public float h =
0;
//public float speed = MAX_SPEED/2;
public float speedFactor = 1.0f
;
public boolean isAlive =
false;
private SpriteInstance mSpriteInstance =
null;
public Particle
()
{
x = getGame
().
getRandomFloat(0, GameConsts.
VIRTUAL_SCREEN_WIDTH+
150);
y = getGame
().
getRandomFloat(0, GameConsts.
VIRTUAL_SCREEN_HEIGHT);
w = getGame
().
getRandomFloat(MIN_SIZE, MAX_SIZE
);
h = w/
2;
//speed = getGame().getRandomFloat(MIN_SPEED, MAX_SPEED);
//speedFactor = getGame().getRandomFloat(0.1f, 2.0f);
speedFactor = getGame
().
getRandomFloat(MIN_SPEED, MAX_SPEED
);
isAlive =
true;
mSpriteInstance = mStarSprite.
createSpriteInstanceForAll();
}
public void spawn
()
{
x = GameConsts.
VIRTUAL_SCREEN_WIDTH + getGame
().
getRandomFloat(10,
150);
y = getGame
().
getRandomFloat(0, GameConsts.
VIRTUAL_SCREEN_HEIGHT);
w = getGame
().
getRandomFloat(MIN_SIZE, MAX_SIZE
);
h = w
;
//speed = getGame().getRandomFloat(MIN_SPEED, MAX_SPEED);
isAlive =
true;
}
public void update
(float deltaTime
)
{
//x -= speed * deltaTime * speedFactor;
x -= getGameLogic
().
getLevel().
getCurrentScrollVelocity().
x * speedFactor
* mWarpSpeedFactor
;
y -= getGameLogic
().
getLevel().
getCurrentScrollVelocity().
y * (speedFactor/
2);
if (x
< -w
)
isAlive =
false;
}
public void render
()
{
if (mCurrentStatus == Status.
DISABLED)
return;
mSpriteInstance.
transform.
x = x
;
mSpriteInstance.
transform.
y = y
;
mSpriteInstance.
transform.
w = w
* (1 + speedFactor/
5);
mSpriteInstance.
alpha = NORMAL_ALPHA
;
mSpriteInstance.
transform.
h = h
;
mSpriteInstance.
centerPivot();
mSpriteInstance.
transform.
w *= mWarpScaleFactor
;
mSpriteInstance.
alpha = mWarpAlpha
;
mSpriteInstance.
render();
}
}
// Fields===========================================================================================
private Sprite mStarSprite =
null;
private Particle mParticles
[] =
new Particle
[MAX_STARS
];
private Status mCurrentStatus = Status.
DISABLED;
private Status mNextStatus = Status.
DISABLED;
private float mStatusTimer = 0.0f
;
private float mStatusSwitchDuration = 1.0f
;
private float mWarpSpeedFactor = 1.0f
;
private float mWarpScaleFactor = 1.0f
;
private float mWarpAlpha = NORMAL_ALPHA
;
// Methods==========================================================================================
public StarField
(GameLogic gameLogic
)
{
super(gameLogic
);
mStarSprite =
new Sprite
(getGraphics
(),
"data/textures/star.png");
}
@
Override
public void initAsync
()
{
mStarSprite.
initAsync();
}
@
Override
public void init
()
{
mStarSprite.
init();
for (int i =
0; i
< mParticles.
length; i++
)
{
mParticles
[i
] =
new Particle
();
}
}
@
Override
public void exit
()
{
mStarSprite.
dispose();
}
@
Override
public void update
(float deltaTime
)
{
for (int i =
0; i
< mParticles.
length; i++
)
{
if (mParticles
[i
].
isAlive)
mParticles
[i
].
update(deltaTime
);
if (!mParticles
[i
].
isAlive)
spawn
(i
);
}
if (mNextStatus
!= mCurrentStatus
)
{
if (mStatusTimer
<= 0.0f
)
{
setStatusImmediately
(mNextStatus
);
}
else
mStatusTimer -= deltaTime
;
}
applyParameters
();
}
public void applyParameters
()
{
float t = 0.0f
;
if (mStatusSwitchDuration
!= 0.0f
)
t = mStatusTimer / mStatusSwitchDuration
;
if (checkStatusSwitch
(Status.
DISABLED, Status.
WARP))
{
mWarpSpeedFactor = WARP_SPEED_FACTOR
;
mWarpScaleFactor = WARP_SCALE_FACTOR
;
mWarpAlpha = WARP_ALPHA
;
}
else if (checkStatusSwitch
(Status.
NORMAL, Status.
WARP))
{
mWarpSpeedFactor =
1 +
(1-t
) * (WARP_SPEED_FACTOR-
1);
mWarpScaleFactor =
1 +
(1-t
) * (WARP_SCALE_FACTOR-
1);
}
else if (checkStatusSwitch
(Status.
WARP, Status.
WARP))
{
mWarpSpeedFactor = WARP_SPEED_FACTOR
;
mWarpScaleFactor = WARP_SCALE_FACTOR
;
mWarpAlpha = WARP_ALPHA
;
}
else if (checkStatusSwitch
(Status.
DISABLED, Status.
NORMAL))
{
mWarpSpeedFactor = 1.0f
;
mWarpScaleFactor = 1.0f
;
mWarpAlpha =
(1-t
) * NORMAL_ALPHA
;
}
else if (checkStatusSwitch
(Status.
WARP, Status.
NORMAL))
{
mWarpSpeedFactor =
1 +
(t
) * (WARP_SPEED_FACTOR-
1);
mWarpScaleFactor =
1 +
(t
) * (WARP_SCALE_FACTOR-
1);
mWarpAlpha = NORMAL_ALPHA +
(t
) * (WARP_ALPHA-NORMAL_ALPHA
);
}
else if (checkStatusSwitch
(Status.
NORMAL, Status.
NORMAL))
{
mWarpSpeedFactor =
1;
mWarpScaleFactor = 1.0f
;
mWarpAlpha = NORMAL_ALPHA
;
}
else if (checkStatusSwitch
(Status.
NORMAL, Status.
DISABLED))
{
mWarpSpeedFactor = 1.0f
;
mWarpScaleFactor = 1.0f
;
mWarpAlpha = t
* NORMAL_ALPHA
;
}
else if (checkStatusSwitch
(Status.
WARP, Status.
DISABLED))
{
mWarpSpeedFactor =
1 +
(t
) * (WARP_SPEED_FACTOR-
1);
mWarpScaleFactor =
1 +
(t
) * (WARP_SCALE_FACTOR-
1);
mWarpAlpha = t
* WARP_ALPHA
;
}
else if (checkStatusSwitch
(Status.
DISABLED, Status.
DISABLED))
{
mWarpSpeedFactor =
1;
mWarpScaleFactor = 1.0f
;
mWarpAlpha = 0.0f
;
}
}
public boolean checkStatusSwitch
(Status current, Status next
)
{
return ((mCurrentStatus == current
) && (mNextStatus == next
));
}
@
Override
public void render
()
{
for (int i =
0; i
< mParticles.
length; i++
)
{
if (mParticles
[i
].
isAlive)
mParticles
[i
].
render();
}
}
public void spawn
(int index
)
{
if (!mParticles
[index
].
isAlive)
{
mParticles
[index
].
spawn();
}
}
public void switchStatus
(Status nextStatus,
float statusSwitchDuration
)
{
mNextStatus = nextStatus
;
mStatusSwitchDuration = statusSwitchDuration
;
mStatusTimer = mStatusSwitchDuration
;
}
public void setStatusImmediately
(Status status
)
{
mCurrentStatus = status
;
mNextStatus = status
;
mStatusTimer = 0.0f
;
mStatusSwitchDuration = 0.0f
;
applyParameters
();
}
// Getters/Setters==================================================================================
}