correct star rendering

This commit is contained in:
Daniel
2024-10-16 02:08:19 +03:00
parent 4528e09c8c
commit c064e4c521
4 changed files with 79 additions and 50 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 101 KiB

After

Width:  |  Height:  |  Size: 135 KiB

View File

@@ -97,8 +97,6 @@ public class GameScreen implements Screen {
ParticleManager.getInstance().update(delta); ParticleManager.getInstance().update(delta);
player.render(); player.render();
ParticleManager.getInstance().render(spriteBatch); ParticleManager.getInstance().render(spriteBatch);
enemySpawnTimer += delta; enemySpawnTimer += delta;

View File

@@ -12,14 +12,16 @@ public class SpaceBackground {
private float[] starPositionsX; private float[] starPositionsX;
private float[] starPositionsY; private float[] starPositionsY;
private int numStars = 1000; // Number of stars to generate
private float galaxyScrollSpeed = 0.1f; // Galaxy scroll speed private int numStars = 10000; // Number of stars to generate
private float nebulaScrollSpeed = 0.05f; // Nebula scroll speed
private float starScrollSpeed = 0.2f; // Star scroll speed
private float galaxyOffset = 0; // Speeds for different layers
private float nebulaOffset = 0; private float galaxyScrollSpeedX = 0.2f; // Galaxy horizontal scroll speed
private float galaxyScrollSpeedY = 0.2f; // Galaxy vertical scroll speed
private float nebulaScrollSpeedX = 0.025f; // Nebula horizontal scroll speed
private float nebulaScrollSpeedY = 0.025f; // Nebula vertical scroll speed
private float starScrollSpeedX = 0.5f; // Star horizontal scroll speed
private float starScrollSpeedY = 0.5f; // Star vertical scroll speed
public SpaceBackground() { public SpaceBackground() {
starTexture = new Texture(Gdx.files.internal("images/star.png")); starTexture = new Texture(Gdx.files.internal("images/star.png"));
@@ -29,9 +31,11 @@ public class SpaceBackground {
starPositionsX = new float[numStars]; starPositionsX = new float[numStars];
starPositionsY = new float[numStars]; starPositionsY = new float[numStars];
// Randomly place stars within a larger range to create dispersion
for (int i = 0; i < numStars; i++) { for (int i = 0; i < numStars; i++) {
starPositionsX[i] = MathUtils.random(0, Gdx.graphics.getWidth()); // Increase the range to make stars more dispersed
starPositionsY[i] = MathUtils.random(0, Gdx.graphics.getHeight()); starPositionsX[i] = MathUtils.random(-Gdx.graphics.getWidth() * 10, Gdx.graphics.getWidth() * 10);
starPositionsY[i] = MathUtils.random(-Gdx.graphics.getHeight() * 10, Gdx.graphics.getHeight() * 10);
} }
} }
@@ -44,60 +48,79 @@ public class SpaceBackground {
// Draw nebulae with parallax effect // Draw nebulae with parallax effect
drawNebulae(); drawNebulae();
// Draw star particles // Draw star particles with parallax effect
drawStars(); drawStars();
GameScreen.spriteBatch.end(); GameScreen.spriteBatch.end();
// Update offsets for parallax scrolling
updateOffsets();
} }
private void drawStars() { private void drawStars() {
float starScale = 0.1f; // Scale factor for stars float cameraX = GameScreen.cam.position.x;
float cameraY = GameScreen.cam.position.y;
// Adjust the star positions based on the camera's position
for (int i = 0; i < numStars; i++) { for (int i = 0; i < numStars; i++) {
// Draw the star with scaling // Calculate new positions based on camera movement
GameScreen.spriteBatch.draw(starTexture, starPositionsX[i], starPositionsY[i], float starX = starPositionsX[i] - cameraX * starScrollSpeedX; // Horizontal parallax
starTexture.getWidth() * starScale / 2, // Origin X float starY = starPositionsY[i] - cameraY * starScrollSpeedY; // Vertical parallax
starTexture.getHeight() * starScale / 2, // Origin Y
starTexture.getWidth() * starScale, // Width // Add a slight random offset to the star's position
starTexture.getHeight() * starScale, // Height float offsetX = MathUtils.random(-2f, 2f); // Change the offset range as needed
1, // Scale X float offsetY = MathUtils.random(-2f, 2f); // Change the offset range as needed
1 // Scale Y
); // Rotation // Define a scale factor for the stars
float starScale = 0.1f; // Adjust this value to make stars smaller or larger
// Calculate scaled dimensions
float scaledWidth = starTexture.getWidth() * starScale;
float scaledHeight = starTexture.getHeight() * starScale;
// Draw the star with the correct parameters
GameScreen.spriteBatch.draw(starTexture,
starX + offsetX, // X position
starY + offsetY, // Y position
scaledWidth, // Origin X
scaledHeight, // Origin Y
0, // Scaled Width
0, // Scaled Height
1, // Scale X (since we're already scaling)
1 // Scale Y (since we're already scaling)
);
} }
} }
private void drawGalaxies() { private void drawGalaxies() {
float screenHeight = Gdx.graphics.getHeight(); float cameraX = GameScreen.cam.position.x;
float galaxyY = (float) (Gdx.graphics.getHeight() - galaxyTexture.getHeight()) / 2 + galaxyOffset; float cameraY = GameScreen.cam.position.y;
// Determine Y positions for galaxies based on camera position and scroll speed
float galaxyY1 = (cameraY * galaxyScrollSpeedY) % galaxyTexture.getHeight() - galaxyTexture.getHeight();
float galaxyY2 = galaxyY1 + galaxyTexture.getHeight(); // Draw a second galaxy texture for seamless scrolling
// Draw galaxies with horizontal offset
float galaxyX1 = -cameraX * galaxyScrollSpeedX; // Horizontal position based on camera
float galaxyX2 = galaxyX1 + galaxyTexture.getWidth();
// Draw the galaxy texture // Draw the galaxy texture
GameScreen.spriteBatch.draw(galaxyTexture, 0, galaxyY); GameScreen.spriteBatch.draw(galaxyTexture, galaxyX1, galaxyY1);
GameScreen.spriteBatch.draw(galaxyTexture, 0, galaxyY + galaxyTexture.getHeight()); GameScreen.spriteBatch.draw(galaxyTexture, galaxyX2, galaxyY1);
} }
private void drawNebulae() { private void drawNebulae() {
float screenHeight = Gdx.graphics.getHeight(); float cameraX = GameScreen.cam.position.x;
float nebulaY = (float) (Gdx.graphics.getHeight() - nebulaTexture.getHeight()) / 2 + nebulaOffset; float cameraY = GameScreen.cam.position.y;
// Determine Y positions for nebulae based on camera position and scroll speed
float nebulaY1 = (cameraY * nebulaScrollSpeedY) % nebulaTexture.getHeight() - nebulaTexture.getHeight();
float nebulaY2 = nebulaY1 + nebulaTexture.getHeight(); // Draw a second nebula texture for seamless scrolling
// Draw nebulae with horizontal offset
float nebulaX1 = -cameraX * nebulaScrollSpeedX; // Horizontal position based on camera
float nebulaX2 = nebulaX1 + nebulaTexture.getWidth();
// Draw the nebula texture // Draw the nebula texture
GameScreen.spriteBatch.draw(nebulaTexture, 0, nebulaY); GameScreen.spriteBatch.draw(nebulaTexture, nebulaX1, nebulaY1);
GameScreen.spriteBatch.draw(nebulaTexture, 0, nebulaY + nebulaTexture.getHeight()); GameScreen.spriteBatch.draw(nebulaTexture, nebulaX2, nebulaY1);
}
private void updateOffsets() {
// Update offsets for scrolling
galaxyOffset -= galaxyScrollSpeed; // Move galaxy layer up
nebulaOffset -= nebulaScrollSpeed; // Move nebula layer up
// Reset offset if it goes out of bounds
if (galaxyOffset <= -galaxyTexture.getHeight()) {
galaxyOffset = 0;
}
if (nebulaOffset <= -nebulaTexture.getHeight()) {
nebulaOffset = 0;
}
} }
public void dispose() { public void dispose() {

View File

@@ -2,6 +2,7 @@ package org.lumijiez.bugger.entities;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import org.lumijiez.bugger.entities.weapons.Arrow; import org.lumijiez.bugger.entities.weapons.Arrow;
@@ -53,8 +54,11 @@ public class Player extends Entity {
private void updateSpriteRotation() { private void updateSpriteRotation() {
float mouseX = Gdx.input.getX(); float mouseX = Gdx.input.getX();
float mouseY = Gdx.input.getY(); float mouseY = Gdx.input.getY();
Vector2 mousePosition = new Vector2(mouseX, Gdx.graphics.getHeight() - mouseY);
Vector2 direction = mousePosition.cpy().sub(new Vector2(cam.position.x, cam.position.y)).nor(); Vector3 mousePosition = cam.unproject(new Vector3(mouseX, mouseY, 0));
Vector2 direction = new Vector2(mousePosition.x, mousePosition.y).sub(body.getPosition()).nor();
float angle = direction.angleDeg() + 270f; float angle = direction.angleDeg() + 270f;
body.setTransform(body.getPosition(), angle * (float) Math.PI / 180f); body.setTransform(body.getPosition(), angle * (float) Math.PI / 180f);
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI)); sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
@@ -62,13 +66,17 @@ public class Player extends Entity {
public Arrow shootArrow() { public Arrow shootArrow() {
Vector2 direction = new Vector2(); Vector2 direction = new Vector2();
float mouseX = Gdx.input.getX(); float mouseX = Gdx.input.getX();
float mouseY = Gdx.input.getY(); float mouseY = Gdx.input.getY();
Vector2 mousePosition = new Vector2(mouseX, Gdx.graphics.getHeight() - mouseY);
direction.set(mousePosition).sub(new Vector2(cam.position.x, cam.position.y)).nor(); Vector3 mousePosition = cam.unproject(new Vector3(mouseX, mouseY, 0));
direction.set(mousePosition.x, mousePosition.y).sub(getPosition()).nor();
Arrow arrow = new Arrow(world, getPosition(), direction); Arrow arrow = new Arrow(world, getPosition(), direction);
arrow.body.setUserData(arrow); arrow.body.setUserData(arrow);
return arrow; return arrow;
} }
} }