Rev 900 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package com.gebauz.burutaru.game.entities;
import com.gebauz.bauzoid.graphics.sprite.Sprite;
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 =
10;
public static final float MAX_SPEED =
400;
public static final float MIN_SIZE =
2;
public static final float MAX_SIZE =
8;
// Embedded Types===================================================================================
public class Particle
{
public float x =
0;
public float y =
0;
public float w =
0;
public float h =
0;
public float speed =
0;
public boolean isAlive =
false;
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
);
isAlive =
true;
}
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/
2;
speed = getGame
().
getRandomFloat(MIN_SPEED, MAX_SPEED
);
isAlive =
true;
}
public void update
(float deltaTime
)
{
x -= speed
* deltaTime
;
x += getGameLogic
().
getGlobalScrollX() * deltaTime
;
y += getGameLogic
().
getGlobalScrollY() * deltaTime
;
if (x
< -w
)
isAlive =
false;
}
public void render
()
{
mStarSprite.
param.
x = x
;
mStarSprite.
param.
y = y
;
mStarSprite.
param.
w = w
;
mStarSprite.
param.
h = h
;
mStarSprite.
centerPivot();
mStarSprite.
render();
}
}
// Fields===========================================================================================
private Particle mParticles
[] =
new Particle
[MAX_STARS
];
private Sprite mStarSprite =
null;
// 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
()
{
if (mStarSprite
!=
null)
{
mStarSprite.
dispose();
mStarSprite =
null;
}
}
@
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
);
}
}
@
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();
}
}
// Getters/Setters==================================================================================
}