Add stars background
This commit is contained in:
parent
7369e19443
commit
270ba3e4a7
@ -13,6 +13,8 @@ public class InfluenceXXII implements ApplicationListener {
|
||||
|
||||
private Pointer pointer;
|
||||
private EnemiesController enemies;
|
||||
private Stars stars;
|
||||
|
||||
private ShapeRenderer renderer;
|
||||
private BitmapFont font;
|
||||
private SpriteBatch spriteBatch;
|
||||
@ -41,8 +43,8 @@ public class InfluenceXXII implements ApplicationListener {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
enemies.addEnemy();
|
||||
}
|
||||
|
||||
pointer.setEnemiesController(enemies);
|
||||
stars = new Stars(width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -60,6 +62,9 @@ public class InfluenceXXII implements ApplicationListener {
|
||||
Gdx.gl.glClearColor(background.r, background.g, background.b, 1f);
|
||||
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// Draw stars
|
||||
stars.draw(renderer);
|
||||
|
||||
// Draw enemies
|
||||
enemies.draw(spriteBatch, renderer);
|
||||
|
||||
|
64
src/com/annimon/influencexxii/Stars.java
Normal file
64
src/com/annimon/influencexxii/Stars.java
Normal file
@ -0,0 +1,64 @@
|
||||
package com.annimon.influencexxii;
|
||||
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
|
||||
/**
|
||||
* Stars background
|
||||
* @author aNNiMON
|
||||
*/
|
||||
public class Stars {
|
||||
|
||||
private static final int MAX_STARS = 200;
|
||||
|
||||
private float screenWidth, screenHeight;
|
||||
private Vector3[] stars;
|
||||
private float step;
|
||||
|
||||
public Stars(float width, float height) {
|
||||
this.screenWidth = width;
|
||||
this.screenHeight = height;
|
||||
stars = new Vector3[MAX_STARS];
|
||||
for (int i = 0; i < MAX_STARS; i++) {
|
||||
Vector3 v = new Vector3();
|
||||
newStar(v, false);
|
||||
stars[i] = v;
|
||||
}
|
||||
step = MathUtils.random(-20f, 20f);
|
||||
}
|
||||
|
||||
public void draw(ShapeRenderer renderer) {
|
||||
renderer.begin(ShapeType.Filled);
|
||||
final float w2 = screenWidth * 2;
|
||||
for (int i = 0; i < MAX_STARS; i++) {
|
||||
Vector3 v = stars[i];
|
||||
v.z += step;
|
||||
if (v.z <= -200) {
|
||||
newStar(v, false);
|
||||
} else if (v.z >= 2000) {
|
||||
newStar(v, true);
|
||||
v.z = MathUtils.random(-200, 100);
|
||||
}
|
||||
if ( (v.x <= -w2) || (v.x >= w2) ) {
|
||||
v.x = -v.x;
|
||||
}
|
||||
// Color
|
||||
float sx = screenWidth / 2 + v.x * 200 / (v.z + 200);
|
||||
float sy = screenHeight / 2 + v.y * 200 / (v.z + 200);
|
||||
float col = 1f - 1f * (v.z + 200) / 1500;
|
||||
col = MathUtils.clamp(col, 0f, 1f);
|
||||
renderer.setColor(col, col, col, 0.8f);
|
||||
renderer.rect(sx, sy, 2f, 2f);
|
||||
}
|
||||
renderer.end();
|
||||
}
|
||||
|
||||
private void newStar(Vector3 v, boolean backward) {
|
||||
v.x = MathUtils.random(-screenWidth * 2, screenWidth * 2);
|
||||
v.y = MathUtils.random(-screenHeight * 2, screenHeight * 2);
|
||||
if (backward) v.z = MathUtils.random(-200, 100);
|
||||
else v.z = MathUtils.random(1900);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user