fixed camera

This commit is contained in:
Daniel
2024-10-16 01:43:06 +03:00
parent 9f56a9af5b
commit 4528e09c8c
8 changed files with 132 additions and 5 deletions

BIN
assets/images/galaxy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 MiB

BIN
assets/images/nebula.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 MiB

BIN
assets/images/star.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen; import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*; import com.badlogic.gdx.physics.box2d.*;
@@ -18,6 +19,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GameScreen implements Screen { public class GameScreen implements Screen {
public static OrthographicCamera cam;
private SpaceBackground spaceBackground = new SpaceBackground();
private final World world; private final World world;
private final Player player; private final Player player;
public static final SpriteBatch spriteBatch = new SpriteBatch(); public static final SpriteBatch spriteBatch = new SpriteBatch();
@@ -36,6 +39,11 @@ public class GameScreen implements Screen {
arrows = new Array<>(); arrows = new Array<>();
entitiesToDestroy = new Array<>(); entitiesToDestroy = new Array<>();
world.setContactListener(new GameContactListener()); world.setContactListener(new GameContactListener());
cam = new OrthographicCamera();
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(Player.getInstance().getPosition().x / 2f, Player.getInstance().getPosition().y / 2f, 0);
cam.update();
} }
@Override @Override
@@ -45,9 +53,14 @@ public class GameScreen implements Screen {
@Override @Override
public void render(float delta) { public void render(float delta) {
cam.update();
spriteBatch.setProjectionMatrix(cam.combined);
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1); Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spaceBackground.render();
world.step(1f, 6, 2); world.step(1f, 6, 2);
if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) { if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
@@ -100,6 +113,8 @@ public class GameScreen implements Screen {
} }
debugRenderer.render(world, spriteBatch.getProjectionMatrix()); debugRenderer.render(world, spriteBatch.getProjectionMatrix());
cam.position.set(Player.getInstance().getPosition().x, Player.getInstance().getPosition().y, 0);
} }
@@ -127,5 +142,6 @@ public class GameScreen implements Screen {
public void dispose() { public void dispose() {
spriteBatch.dispose(); spriteBatch.dispose();
world.dispose(); world.dispose();
spaceBackground.dispose();
} }
} }

View File

@@ -1,5 +1,6 @@
package org.lumijiez.bugger; package org.lumijiez.bugger;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.ParticleEffect; import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool; import com.badlogic.gdx.graphics.g2d.ParticleEffectPool;
@@ -45,11 +46,11 @@ public class ParticleManager {
} }
public void render(SpriteBatch spriteBatch) { public void render(SpriteBatch spriteBatch) {
spriteBatch.begin();
for (ParticleEffectPool.PooledEffect effect : activeEffects) { for (ParticleEffectPool.PooledEffect effect : activeEffects) {
GameScreen.spriteBatch.begin();
effect.draw(spriteBatch); effect.draw(spriteBatch);
GameScreen.spriteBatch.end();
} }
spriteBatch.end();
} }
public void dispose() { public void dispose() {

View File

@@ -0,0 +1,108 @@
package org.lumijiez.bugger;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
public class SpaceBackground {
private Texture starTexture;
private Texture galaxyTexture;
private Texture nebulaTexture;
private float[] starPositionsX;
private float[] starPositionsY;
private int numStars = 1000; // Number of stars to generate
private float galaxyScrollSpeed = 0.1f; // Galaxy scroll speed
private float nebulaScrollSpeed = 0.05f; // Nebula scroll speed
private float starScrollSpeed = 0.2f; // Star scroll speed
private float galaxyOffset = 0;
private float nebulaOffset = 0;
public SpaceBackground() {
starTexture = new Texture(Gdx.files.internal("images/star.png"));
galaxyTexture = new Texture(Gdx.files.internal("images/galaxy.png"));
nebulaTexture = new Texture(Gdx.files.internal("images/nebula.png"));
starPositionsX = new float[numStars];
starPositionsY = new float[numStars];
for (int i = 0; i < numStars; i++) {
starPositionsX[i] = MathUtils.random(0, Gdx.graphics.getWidth());
starPositionsY[i] = MathUtils.random(0, Gdx.graphics.getHeight());
}
}
public void render() {
GameScreen.spriteBatch.begin();
// Draw galaxies with parallax effect
drawGalaxies();
// Draw nebulae with parallax effect
drawNebulae();
// Draw star particles
drawStars();
GameScreen.spriteBatch.end();
// Update offsets for parallax scrolling
updateOffsets();
}
private void drawStars() {
float starScale = 0.1f; // Scale factor for stars
for (int i = 0; i < numStars; i++) {
// Draw the star with scaling
GameScreen.spriteBatch.draw(starTexture, starPositionsX[i], starPositionsY[i],
starTexture.getWidth() * starScale / 2, // Origin X
starTexture.getHeight() * starScale / 2, // Origin Y
starTexture.getWidth() * starScale, // Width
starTexture.getHeight() * starScale, // Height
1, // Scale X
1 // Scale Y
); // Rotation
}
}
private void drawGalaxies() {
float screenHeight = Gdx.graphics.getHeight();
float galaxyY = (float) (Gdx.graphics.getHeight() - galaxyTexture.getHeight()) / 2 + galaxyOffset;
// Draw the galaxy texture
GameScreen.spriteBatch.draw(galaxyTexture, 0, galaxyY);
GameScreen.spriteBatch.draw(galaxyTexture, 0, galaxyY + galaxyTexture.getHeight());
}
private void drawNebulae() {
float screenHeight = Gdx.graphics.getHeight();
float nebulaY = (float) (Gdx.graphics.getHeight() - nebulaTexture.getHeight()) / 2 + nebulaOffset;
// Draw the nebula texture
GameScreen.spriteBatch.draw(nebulaTexture, 0, nebulaY);
GameScreen.spriteBatch.draw(nebulaTexture, 0, nebulaY + nebulaTexture.getHeight());
}
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() {
starTexture.dispose();
galaxyTexture.dispose();
nebulaTexture.dispose();
}
}

View File

@@ -6,6 +6,8 @@ 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;
import static org.lumijiez.bugger.GameScreen.cam;
public class Player extends Entity { public class Player extends Entity {
private static Player instance; private static Player instance;
private final float speed = 5f; private final float speed = 5f;
@@ -52,7 +54,7 @@ public class Player extends Entity {
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 mousePosition = new Vector2(mouseX, Gdx.graphics.getHeight() - mouseY);
Vector2 direction = mousePosition.cpy().sub(body.getPosition()).nor(); Vector2 direction = mousePosition.cpy().sub(new Vector2(cam.position.x, cam.position.y)).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));
@@ -63,7 +65,7 @@ public class Player extends Entity {
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 mousePosition = new Vector2(mouseX, Gdx.graphics.getHeight() - mouseY);
direction.set(mousePosition).sub(getPosition()).nor(); direction.set(mousePosition).sub(new Vector2(cam.position.x, cam.position.y)).nor();
Arrow arrow = new Arrow(world, getPosition(), direction); Arrow arrow = new Arrow(world, getPosition(), direction);
arrow.body.setUserData(arrow); arrow.body.setUserData(arrow);

View File

@@ -5,7 +5,7 @@ import com.badlogic.gdx.physics.box2d.*;
import org.lumijiez.bugger.entities.Entity; import org.lumijiez.bugger.entities.Entity;
public class Arrow extends Entity { public class Arrow extends Entity {
private final float speed = 5000f; private final float speed = 4000f;
private final float lifetime = 3f; private final float lifetime = 3f;
private float timeAlive = 0f; private float timeAlive = 0f;