attempt to sync cam and body

This commit is contained in:
Daniel
2024-10-16 18:35:15 +03:00
parent 615a608c34
commit 2b5bcc6ace
8 changed files with 235 additions and 73 deletions

View File

@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
@@ -46,8 +47,7 @@ public class Bugger {
this.world.setContactListener(new GameContactListener());
this.debugRenderer = new Box2DDebugRenderer();
spriteBatch = new SpriteBatch();
cam = new OrthographicCamera();
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam = new OrthographicCamera(160, 90);
cam.position.set(Player.getInstance().getPosition().x / 2f, Player.getInstance().getPosition().y / 2f, 0);
cam.update();
}
@@ -60,22 +60,27 @@ public class Bugger {
}
public void cycle(float delta) {
updateCamera(delta);
cam.update();
renderClear();
renderBackground();
renderPlayer();
step();
spriteBatch.setProjectionMatrix(cam.combined);
cam.position.set(player.getPosition().x, player.getPosition().y, 0);
handleInput();
cycleProjectiles(delta);
cycleEnemies();
cycleParticles(delta);
clearEntities();
renderPlayer();
renderEnemies(delta);
renderDebug();
}
@@ -110,12 +115,6 @@ public class Bugger {
debugRenderer.render(world, spriteBatch.getProjectionMatrix());
}
public void updateCamera(float delta) {
cam.update();
spriteBatch.setProjectionMatrix(cam.combined);
cam.position.set(Player.getInstance().getPosition().x, Player.getInstance().getPosition().y, 0);
}
public void cycleProjectiles(float delta) {
for (Arrow arrow : projectiles) {
if (!arrow.isMarkedToDestroy()) {
@@ -153,7 +152,7 @@ public class Bugger {
}
public void step() {
world.step(1f, 6, 2);
world.step(1 / 20f, 6, 2);
}
public void playParticle(float x, float y) {
@@ -179,6 +178,7 @@ public class Bugger {
spriteBatch.dispose();
world.dispose();
spaceBackground.dispose();
ParticleManager.getInstance().dispose();
}
}

View File

@@ -1,17 +1,12 @@
package org.lumijiez.bugger;
import com.badlogic.gdx.Screen;
import org.lumijiez.bugger.entities.Player;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class GameScreen implements Screen {
public final Bugger bugger = Bugger.getInstance();
public GameScreen() {
}
@Override
public void show() {
@@ -24,8 +19,14 @@ public class GameScreen implements Screen {
@Override
public void resize(int i, int i1) {
public void resize(int w, int h) {
// float screenAR = w / (float) h;
// Bugger.cam = new OrthographicCamera(20, 20 / screenAR);
// Bugger.cam.position.set(Bugger.cam.viewportWidth / 2, Bugger.cam.viewportHeight / 2, 0);
// Bugger.cam.update();
//
// Bugger.spriteBatch = new SpriteBatch();
// Bugger.spriteBatch.setProjectionMatrix(Bugger.cam.combined);
}
@Override

View File

@@ -12,7 +12,7 @@ public class Player extends Entity {
private static Player instance;
private Player() {
super(null, "images/wasp.png", 50f);
super(null, "images/wasp.png", 10f);
}
public static Player getInstance() {
@@ -28,7 +28,7 @@ public class Player extends Entity {
}
public void move(float deltaX, float deltaY) {
float speed = 5f;
float speed = 500f;
body.setLinearVelocity(deltaX * speed, deltaY * speed);
}

View File

@@ -9,7 +9,7 @@ public class Wasp extends EnemyEntity {
private static final Random random = new Random();
public Wasp(World world, Vector2 playerPosition) {
super(world, "images/wasp.png", 20f);
super(world, "images/wasp.png", 5f);
float spawnRadius = 100;
float angle = random.nextFloat() * 2 * (float) Math.PI;
float spawnX = playerPosition.x + (float) Math.cos(angle) * (spawnRadius + size);

View File

@@ -8,12 +8,13 @@ public class Arrow extends Entity {
private float timeAlive = 0f;
public Arrow(World world, Vector2 position, Vector2 direction) {
super(world, "images/wasp.png", 10f);
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 15f));
super(world, "images/wasp.png", 1f);
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 1f));
this.body = createBody(offsetPosition.x, offsetPosition.y);
float speed = 4000f;
float speed = 5000f;
this.body.setLinearVelocity(direction.nor().scl(speed));
this.body.setAngularVelocity(speed);
}
protected Body createBody(float x, float y) {

View File

@@ -6,7 +6,6 @@ import com.badlogic.gdx.graphics.g2d.ParticleEffectPool;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;
import org.lumijiez.bugger.Bugger;
import org.lumijiez.bugger.GameScreen;
public class ParticleManager {
private static ParticleManager instance;
@@ -16,7 +15,7 @@ public class ParticleManager {
private ParticleManager() {
ParticleEffect effect = new ParticleEffect();
effect.load(Gdx.files.internal("particles/boom.p"), Gdx.files.internal("particles"));
effect.scaleEffect(0.2f);
effect.scaleEffect(0.6f);
particleEffectPool = new ParticleEffectPool(effect, 1, 20);
activeEffects = new Array<>();
}