From 9c3643682d4e09a326ccb018d604daa2cbbe1380 Mon Sep 17 00:00:00 2001 From: Jeasonfire Date: Sat, 16 May 2015 22:28:56 +0300 Subject: [PATCH] Added parallaxing & audience. --- .../gladiator/components/CPhysics.java | 10 +++++++ .../saltosion/gladiator/level/TestLevel.java | 26 ++++++++++++++----- .../gladiator/systems/PhysicsSystem.java | 5 ++-- .../gladiator/systems/RenderingSystem.java | 25 +++++++++++------- .../com/saltosion/gladiator/util/Name.java | 1 + .../gladiator/util/SpriteLoader.java | 2 ++ 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/core/src/com/saltosion/gladiator/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index 8434250..c0181b0 100644 --- a/core/src/com/saltosion/gladiator/components/CPhysics.java +++ b/core/src/com/saltosion/gladiator/components/CPhysics.java @@ -11,6 +11,7 @@ public class CPhysics extends Component { private final Vector2 size = new Vector2(); private float movespeed = 15f, jumpForce = 35f, gravity = 100f; private CollisionListener collisionListener = null; + private float zParallax = 1; private boolean movable = true; private boolean gravityApplied = true; @@ -167,4 +168,13 @@ public class CPhysics extends Component { return this.grounded; } + public CPhysics setZParallax(float zParallax) { + this.zParallax = zParallax; + return this; + } + + public float getZParallax() { + return this.zParallax; + } + } diff --git a/core/src/com/saltosion/gladiator/level/TestLevel.java b/core/src/com/saltosion/gladiator/level/TestLevel.java index 873e8bd..e0e6f54 100644 --- a/core/src/com/saltosion/gladiator/level/TestLevel.java +++ b/core/src/com/saltosion/gladiator/level/TestLevel.java @@ -9,18 +9,29 @@ import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.Global; import com.saltosion.gladiator.util.Name; import com.saltosion.gladiator.util.SpriteLoader; +import com.saltosion.gladiator.util.SpriteSequence; public class TestLevel implements Level { @Override public void generate() { - // Generate entities - AppUtil.entityFactory.createPlayer(new Vector2(0, 5)); - AppUtil.entityFactory.createDummy(new Vector2(-6, 5)); + // Audience + Entity audience = new Entity(); - // Generate level + Sprite audienceSprite0 = SpriteLoader.loadSprite(Name.AUDIENCEIMG, 0, 0, 768, 576); + Sprite audienceSprite1 = SpriteLoader.loadSprite(Name.AUDIENCEIMG, 1, 0, 768, 576); + CRenderedObject audienceRO = new CRenderedObject(); + SpriteSequence audienceAnim = new SpriteSequence(1).addSprite(audienceSprite0).addSprite(audienceSprite1); + audienceRO.addSequence("Default-Animation", audienceAnim); + audienceRO.playAnimation("Default-Animation"); + audience.add(audienceRO); + CPhysics audiencePO = new CPhysics().setMovable(false).setGravityApplied(false) + .setProcessCollisions(false).setPosition(0, 10).setZParallax(9); + audience.add(audiencePO); + AppUtil.engine.addEntity(audience); + + // Ground Entity ground = new Entity(); - Sprite groundSprite = SpriteLoader.loadSprite(Name.GROUNDIMG); CRenderedObject renderedObject = new CRenderedObject(groundSprite); ground.add(renderedObject); @@ -29,8 +40,11 @@ public class TestLevel implements Level { groundSprite.getRegionHeight() * Global.SPRITE_SCALE); physics.getPosition().set(new Vector2(0, -4)); ground.add(physics); - AppUtil.engine.addEntity(ground); + + // Generate entities + AppUtil.entityFactory.createPlayer(new Vector2(0, 5)); + AppUtil.entityFactory.createDummy(new Vector2(-6, 5)); } } diff --git a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java index e89a5d4..8f53737 100644 --- a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java +++ b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java @@ -9,7 +9,6 @@ import com.badlogic.ashley.utils.ImmutableArray; import com.saltosion.gladiator.components.CCombat; import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.util.Direction; -import com.saltosion.gladiator.util.Log; public class PhysicsSystem extends EntitySystem { @@ -99,8 +98,8 @@ public class PhysicsSystem extends EntitySystem { } boolean collidedAlready = false; - float precisionX = COLLISION_PRECISION * (cp0.getSize().x > cp1.getSize().x ? cp0.getSize().x : cp1.getSize().x); - float precisionY = COLLISION_PRECISION * (cp0.getSize().y > cp1.getSize().y ? cp0.getSize().y : cp1.getSize().y); + float precisionX = COLLISION_PRECISION * (float) Math.sqrt(cp0.getSize().x > cp1.getSize().x ? cp0.getSize().x : cp1.getSize().x); + float precisionY = COLLISION_PRECISION * (float) Math.sqrt(cp0.getSize().y > cp1.getSize().y ? cp0.getSize().y : cp1.getSize().y); if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.getSize().x + cp1.getSize().x) / precisionX) { // cp0's left side is colliding with cp1's right side diff --git a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java index 8e8a7e7..cc9db65 100644 --- a/core/src/com/saltosion/gladiator/systems/RenderingSystem.java +++ b/core/src/com/saltosion/gladiator/systems/RenderingSystem.java @@ -26,7 +26,6 @@ import com.saltosion.gladiator.gui.nodes.TextNode; import com.saltosion.gladiator.gui.properties.TextProperty; import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.Global; -import com.saltosion.gladiator.util.Log; import com.saltosion.gladiator.util.SpriteLoader; import com.saltosion.gladiator.util.SpriteSequence; import java.util.ArrayList; @@ -87,8 +86,8 @@ public class RenderingSystem extends EntitySystem { public void update(float deltaTime) { if (AppUtil.player != null) { CPhysics phys = pm.get(AppUtil.player); - camera.position.set(phys.getPosition().x, phys.getPosition().y, 0); - fontCamera.position.set(camera.position.x * Global.FONT_SCALE, camera.position.y * Global.FONT_SCALE, 0); + //camera.position.set(phys.getPosition().x, phys.getPosition().y, 0); + //fontCamera.position.set(camera.position.x * Global.FONT_SCALE, camera.position.y * Global.FONT_SCALE, 0); } camera.update(); fontCamera.update(); @@ -194,6 +193,10 @@ public class RenderingSystem extends EntitySystem { } private void renderEntities(float deltaTime) { + if (AppUtil.player == null) { + return; + } + CPhysics playerPhys = pm.get(AppUtil.player); batch.setProjectionMatrix(camera.combined); batch.begin(); for (int i = 0; i < entities.size(); i++) { @@ -208,8 +211,8 @@ public class RenderingSystem extends EntitySystem { int spriteHeight = currSprite.getRegionHeight(); int spriteWidth = currSprite.getRegionWidth(); - currSprite.setPosition(physics.getPosition().x - spriteWidth / 2, - physics.getPosition().y - spriteHeight / 2); + currSprite.setPosition(((physics.getPosition().x - spriteWidth / 2) - playerPhys.getPosition().x / physics.getZParallax() + camera.viewportWidth / 2), + (physics.getPosition().y - spriteHeight / 2) - playerPhys.getPosition().y / physics.getZParallax() + camera.viewportHeight / 3); currSprite.draw(batch); float nextFrame = renderedObject.getCurrentFrame(channel) + deltaTime * currSequence.getPlayspeed(); @@ -245,14 +248,18 @@ public class RenderingSystem extends EntitySystem { private void renderDebug(Camera camera) { if (debug) { + if (AppUtil.player == null) { + return; + } + CPhysics playerPhys = pm.get(AppUtil.player); debugRenderer.setProjectionMatrix(camera.combined); debugRenderer.begin(ShapeType.Line); for (int i = 0; i < entities.size(); i++) { CPhysics physics = pm.get(entities.get(i)); - float x0 = physics.getPosition().x - physics.getSize().x / 2; - float x1 = physics.getPosition().x + physics.getSize().x / 2; - float y0 = physics.getPosition().y - physics.getSize().y / 2; - float y1 = physics.getPosition().y + physics.getSize().y / 2; + float x0 = physics.getPosition().x - physics.getSize().x / 2 - playerPhys.getPosition().x / physics.getZParallax() + camera.viewportWidth / 2; + float x1 = physics.getPosition().x + physics.getSize().x / 2 - playerPhys.getPosition().x / physics.getZParallax() + camera.viewportWidth / 2; + float y0 = physics.getPosition().y - physics.getSize().y / 2 - playerPhys.getPosition().y / physics.getZParallax() + camera.viewportHeight / 3; + float y1 = physics.getPosition().y + physics.getSize().y / 2 - playerPhys.getPosition().y / physics.getZParallax() + camera.viewportHeight / 3; debugRenderer.setColor(debugColor); debugRenderer.line(x0, y0, x1, y0); diff --git a/core/src/com/saltosion/gladiator/util/Name.java b/core/src/com/saltosion/gladiator/util/Name.java index 1a88ccb..e026c37 100644 --- a/core/src/com/saltosion/gladiator/util/Name.java +++ b/core/src/com/saltosion/gladiator/util/Name.java @@ -10,6 +10,7 @@ public class Name { public static final String GROUNDIMG = "GROUNDIMG"; public static final String WALLIMG = "WALLIMG"; public static final String SWINGHITBOXIMG = "SWINGHITBOXIMG"; + public static final String AUDIENCEIMG = "AUDIENCEIMG"; public static final String BUTTON_HUGE = "BUTTON_HUGE"; public static final String BUTTON_HUGE_HOVER = "BUTTON_HUGE_HOVER"; diff --git a/core/src/com/saltosion/gladiator/util/SpriteLoader.java b/core/src/com/saltosion/gladiator/util/SpriteLoader.java index eab96d1..ccddc16 100644 --- a/core/src/com/saltosion/gladiator/util/SpriteLoader.java +++ b/core/src/com/saltosion/gladiator/util/SpriteLoader.java @@ -17,6 +17,8 @@ public class SpriteLoader { loadTexture(Name.GROUNDIMG, "sprites/ground.png"); loadTexture(Name.WALLIMG, "sprites/wall.png"); loadTexture(Name.SWINGHITBOXIMG, "sprites/swinghitbox.png"); + loadTexture(Name.AUDIENCEIMG, "sprites/Audience.png"); + loadTexture(Name.BUTTON_HUGE, "sprites/buttons/Button_Huge.png"); loadTexture(Name.BUTTON_HUGE_HOVER, "sprites/buttons/Button_Huge_Hover.png"); loadTexture(Name.BUTTON_BIG, "sprites/buttons/Button_Big.png");