diff --git a/core/src/body/Joyau.java b/core/src/body/Joyau.java index a1864ca176e285a1bf70c16a896972be227418ac..1843e3177442aa11c0233ff380cae73dc8e0c2e6 100644 --- a/core/src/body/Joyau.java +++ b/core/src/body/Joyau.java @@ -13,6 +13,7 @@ import com.badlogic.gdx.physics.box2d.World; public class Joyau { CircleShape shapeStruct; private int value; + private float x; public Joyau(float x, float y, World world, char value){ BodyDef bodyDef = new BodyDef(); diff --git a/core/src/entities/B2DSprite.java b/core/src/entities/B2DSprite.java new file mode 100644 index 0000000000000000000000000000000000000000..f7a28511ba0a209170d63799c93d612808142bae --- /dev/null +++ b/core/src/entities/B2DSprite.java @@ -0,0 +1,48 @@ +package entities; + +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.physics.box2d.Body; + +import handlers.Animation; +import handlers.B2DVars; + +public class B2DSprite { + + protected Body body; + protected Animation animation; + protected float width; + protected float height; + + public B2DSprite(Body body) { + this.body = body; + animation = new Animation(); + } + + public void setAnimation(TextureRegion reg, float delay) { + setAnimation(new TextureRegion[] { reg }, delay); + } + + public void setAnimation(TextureRegion[] reg, float delay) { + animation.setFrames(reg, delay); + width = reg[0].getRegionWidth(); + height = reg[0].getRegionHeight(); + } + + public void update(float dt) { + animation.update(dt); + } + + public void render(SpriteBatch sb) { + sb.begin(); + sb.draw(animation.getFrame(), (body.getPosition().x * B2DVars.PPM - width / 2), (int) (body.getPosition().y * B2DVars.PPM - height / 2)); + sb.end(); + } + + public Body getBody() { return body; } + public Vector2 getPosition() { return body.getPosition(); } + public float getWidth() { return width; } + public float getHeight() { return height; } + +} diff --git a/core/src/entities/Gem.java b/core/src/entities/Gem.java new file mode 100644 index 0000000000000000000000000000000000000000..65b54fb92354f994395d25c318decb1b50f7addd --- /dev/null +++ b/core/src/entities/Gem.java @@ -0,0 +1,18 @@ +package entities; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.physics.box2d.Body; +import com.mygdx.game.PlatVenture; + +public class Gem extends B2DSprite{ + public Gem(Body body) { + super(body); + Texture tex = PlatVenture.res.getTexture("crystal"); + TextureRegion[] sprites = TextureRegion.split(tex, 16, 16)[0]; + animation.setFrames(sprites, 1 / 30f); + + width = sprites[0].getRegionWidth(); + height = sprites[0].getRegionHeight(); + } +} diff --git a/core/src/etat/Play.java b/core/src/etat/Play.java index 1f0ed409c5b40f45d7ca6de87260e5c3c25ae5f3..3175b78a991dfab9f78fc0d14ea856a8cfa1a9be 100644 --- a/core/src/etat/Play.java +++ b/core/src/etat/Play.java @@ -68,7 +68,7 @@ public class Play extends GameState { b2dCam.setToOrtho(false, PlatVenture.largeur /PPM, PlatVenture.hauteur/PPM); textCam.setToOrtho(false, Gdx.graphics.getWidth() / PPM, Gdx.graphics.getHeight() / PPM); //textCam.position.set(Gdx.graphics.getWidth() / PPM / 2, 0, 0); - loadTexture(); + //loadTexture(); } @@ -115,8 +115,8 @@ public class Play extends GameState { if (!isInside()){ if (cl.isTriggerEndLevel()){ - isChangingLevel = true; - cl.setTriggerEndLevel(false); + isChangingLevel = true; + cl.setTriggerEndLevel(false); }else{ if (!isChangingLevel) { death(dt); @@ -172,7 +172,7 @@ public class Play extends GameState { batch.setProjectionMatrix(b2dCam.combined); batch.begin(); drawWorld(); - batch.draw(res.getTexture("idle_000"), player.getPosition().x - 5f/PPM, player.getPosition().y - 2f/PPM, 1/10f, 1/10f); + batch.draw(PlatVenture.res.getTexture("idle_000"), player.getPosition().x - 5f/PPM, player.getPosition().y - 2f/PPM, 1/10f, 1/10f); batch.end(); //dessine le monde box2d b2dr.render(world, b2dCam.combined); @@ -238,7 +238,7 @@ public class Play extends GameState { BodyDef bdP = new BodyDef(); bdP.type = BodyDef.BodyType.DynamicBody; bdP.fixedRotation = true; - bdP.position.set(15/PPM, 20/PPM); + bdP.position.set(15/PPM, 20/PPM); // 15, 20 player = world.createBody(bdP); player.setUserData("player"); @@ -286,7 +286,6 @@ public class Play extends GameState { } private void death(float dt){ - /*TODO vérifier pourquoi pas de mort niveau 2*/ if (totalTimeSinceAction > 2){ player.setLinearVelocity(0, 0); player.setTransform(15/PPM, 20/PPM, 0); @@ -337,19 +336,19 @@ public class Play extends GameState { } private void drawWorld(){ - batch.draw(res.getTexture("back"), 0, 0, largeur/10f, hauteur/10f); + batch.draw(PlatVenture.res.getTexture("back"), 0, 0, largeur/10f, hauteur/10f); String key; for (int i = hauteur; i > 0; i--){ for (int j = 0; j < largeur; j++) { switch (map[i-1][j]){ case 'J': - batch.draw(res.getTexture("platJ"), (j*10)/PPM, ((hauteur-i) * 10)/PPM + 2/PPM, 1/10f, 1/12f); + batch.draw(PlatVenture.res.getTexture("platJ"), (j*10)/PPM, ((hauteur-i) * 10)/PPM + 2/PPM, 1/10f, 1/12f); break; case 'K': - batch.draw(res.getTexture("platK"), (j*10)/PPM, ((hauteur-i) * 10)/PPM + 2/PPM, 1/10f, 1/12f); + batch.draw(PlatVenture.res.getTexture("platK"), (j*10)/PPM, ((hauteur-i) * 10)/PPM + 2/PPM, 1/10f, 1/12f); break; case 'L': - batch.draw(res.getTexture("platL"), (j*10)/PPM, ((hauteur-i) * 10)/PPM + 2/PPM, 1/10f, 1/12f); + batch.draw(PlatVenture.res.getTexture("platL"), (j*10)/PPM, ((hauteur-i) * 10)/PPM + 2/PPM, 1/10f, 1/12f); break; case 'A': case 'B': @@ -361,13 +360,13 @@ public class Play extends GameState { case 'H': case 'I': key = "brick" + map[i-1][j]; - batch.draw(res.getTexture(key), (j*10)/PPM, ((hauteur-i) * 10)/PPM, 1/10f, 1/10f); + batch.draw(PlatVenture.res.getTexture(key), (j*10)/PPM, ((hauteur-i) * 10)/PPM, 1/10f, 1/10f); break; case 'Z': - batch.draw(res.getTexture("exit"), (j*10)/PPM, ((hauteur-i) * 10)/PPM, 1/10f, 1/10f); + batch.draw(PlatVenture.res.getTexture("exit"), (j*10)/PPM, ((hauteur-i) * 10)/PPM, 1/10f, 1/10f); break; case 'W': - batch.draw(res.getTexture("water"), (j*10)/PPM, ((hauteur-i)*10)/PPM, 1/10f, 1/10f); + batch.draw(PlatVenture.res.getTexture("water"), (j*10)/PPM, ((hauteur-i)*10)/PPM, 1/10f, 1/10f); } } diff --git a/core/src/handlers/Animation.java b/core/src/handlers/Animation.java index 0ee148a50c8196cde11bef13f3039ad3f19aff25..46635b4963f70d433f2d413625d07769ede9b443 100644 --- a/core/src/handlers/Animation.java +++ b/core/src/handlers/Animation.java @@ -3,52 +3,59 @@ package handlers; import com.badlogic.gdx.graphics.g2d.TextureRegion; public class Animation { + private TextureRegion[] frames; private float time; private float delay; private int currentFrame; + private int timesPlayed; - public Animation(){} + public Animation() {} - public Animation (TextureRegion[] frames){ - this(frames, 1/12f); + public Animation(TextureRegion[] frames) { + this(frames, 1 / 12f); } - public Animation(TextureRegion[] frames, float delay){ - setFrames(frames, delay); + public Animation(TextureRegion[] frames, float delay) { + this.frames = frames; + this.delay = delay; + time = 0; + currentFrame = 0; } - public void setFrames(TextureRegion[] frames, float delay){ + public void setDelay(float f) { delay = f; } + public void setCurrentFrame(int i) { if(i < frames.length) currentFrame = i; } + public void setFrames(TextureRegion[] frames) { + setFrames(frames, 1 / 12f); + } + public void setFrames(TextureRegion[] frames, float delay) { this.frames = frames; - this.delay = delay; time = 0; currentFrame = 0; timesPlayed = 0; + this.delay = delay; } - public void update(float dt){ - if (delay<=0) return; + public void update(float dt) { + if(delay <= 0) return; time += dt; - while (time >= delay){ + while(time >= delay) { step(); } } - private void step(){ + private void step() { time -= delay; currentFrame++; - if (currentFrame == frames.length){ + if(currentFrame == frames.length) { currentFrame = 0; timesPlayed++; } } - public TextureRegion[] getFrames() { - return frames; - } + public TextureRegion getFrame() { return frames[currentFrame]; } + public int getTimesPlayed() { return timesPlayed; } + public boolean hasPlayedOnce() { return timesPlayed > 0; } - public int getTimesPlayed() { - return timesPlayed; - } }