Skip to content
Snippets Groups Projects
Commit 274db359 authored by timeo's avatar timeo
Browse files

début test animation

parent c45497ba
No related branches found
No related tags found
1 merge request!1Restart
...@@ -13,6 +13,7 @@ import com.badlogic.gdx.physics.box2d.World; ...@@ -13,6 +13,7 @@ import com.badlogic.gdx.physics.box2d.World;
public class Joyau { public class Joyau {
CircleShape shapeStruct; CircleShape shapeStruct;
private int value; private int value;
private float x;
public Joyau(float x, float y, World world, char value){ public Joyau(float x, float y, World world, char value){
BodyDef bodyDef = new BodyDef(); BodyDef bodyDef = new BodyDef();
......
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; }
}
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();
}
}
...@@ -68,7 +68,7 @@ public class Play extends GameState { ...@@ -68,7 +68,7 @@ public class Play extends GameState {
b2dCam.setToOrtho(false, PlatVenture.largeur /PPM, PlatVenture.hauteur/PPM); b2dCam.setToOrtho(false, PlatVenture.largeur /PPM, PlatVenture.hauteur/PPM);
textCam.setToOrtho(false, Gdx.graphics.getWidth() / PPM, Gdx.graphics.getHeight() / PPM); textCam.setToOrtho(false, Gdx.graphics.getWidth() / PPM, Gdx.graphics.getHeight() / PPM);
//textCam.position.set(Gdx.graphics.getWidth() / PPM / 2, 0, 0); //textCam.position.set(Gdx.graphics.getWidth() / PPM / 2, 0, 0);
loadTexture(); //loadTexture();
} }
...@@ -115,8 +115,8 @@ public class Play extends GameState { ...@@ -115,8 +115,8 @@ public class Play extends GameState {
if (!isInside()){ if (!isInside()){
if (cl.isTriggerEndLevel()){ if (cl.isTriggerEndLevel()){
isChangingLevel = true; isChangingLevel = true;
cl.setTriggerEndLevel(false); cl.setTriggerEndLevel(false);
}else{ }else{
if (!isChangingLevel) { if (!isChangingLevel) {
death(dt); death(dt);
...@@ -172,7 +172,7 @@ public class Play extends GameState { ...@@ -172,7 +172,7 @@ public class Play extends GameState {
batch.setProjectionMatrix(b2dCam.combined); batch.setProjectionMatrix(b2dCam.combined);
batch.begin(); batch.begin();
drawWorld(); 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(); batch.end();
//dessine le monde box2d //dessine le monde box2d
b2dr.render(world, b2dCam.combined); b2dr.render(world, b2dCam.combined);
...@@ -238,7 +238,7 @@ public class Play extends GameState { ...@@ -238,7 +238,7 @@ public class Play extends GameState {
BodyDef bdP = new BodyDef(); BodyDef bdP = new BodyDef();
bdP.type = BodyDef.BodyType.DynamicBody; bdP.type = BodyDef.BodyType.DynamicBody;
bdP.fixedRotation = true; bdP.fixedRotation = true;
bdP.position.set(15/PPM, 20/PPM); bdP.position.set(15/PPM, 20/PPM); // 15, 20
player = world.createBody(bdP); player = world.createBody(bdP);
player.setUserData("player"); player.setUserData("player");
...@@ -286,7 +286,6 @@ public class Play extends GameState { ...@@ -286,7 +286,6 @@ public class Play extends GameState {
} }
private void death(float dt){ private void death(float dt){
/*TODO vérifier pourquoi pas de mort niveau 2*/
if (totalTimeSinceAction > 2){ if (totalTimeSinceAction > 2){
player.setLinearVelocity(0, 0); player.setLinearVelocity(0, 0);
player.setTransform(15/PPM, 20/PPM, 0); player.setTransform(15/PPM, 20/PPM, 0);
...@@ -337,19 +336,19 @@ public class Play extends GameState { ...@@ -337,19 +336,19 @@ public class Play extends GameState {
} }
private void drawWorld(){ 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; String key;
for (int i = hauteur; i > 0; i--){ for (int i = hauteur; i > 0; i--){
for (int j = 0; j < largeur; j++) { for (int j = 0; j < largeur; j++) {
switch (map[i-1][j]){ switch (map[i-1][j]){
case '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; break;
case 'K': 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; break;
case 'L': 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; break;
case 'A': case 'A':
case 'B': case 'B':
...@@ -361,13 +360,13 @@ public class Play extends GameState { ...@@ -361,13 +360,13 @@ public class Play extends GameState {
case 'H': case 'H':
case 'I': case 'I':
key = "brick" + map[i-1][j]; 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; break;
case 'Z': 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; break;
case 'W': 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);
} }
} }
......
...@@ -3,52 +3,59 @@ package handlers; ...@@ -3,52 +3,59 @@ package handlers;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class Animation { public class Animation {
private TextureRegion[] frames; private TextureRegion[] frames;
private float time; private float time;
private float delay; private float delay;
private int currentFrame; private int currentFrame;
private int timesPlayed; private int timesPlayed;
public Animation(){} public Animation() {}
public Animation (TextureRegion[] frames){ public Animation(TextureRegion[] frames) {
this(frames, 1/12f); this(frames, 1 / 12f);
} }
public Animation(TextureRegion[] frames, float delay){ public Animation(TextureRegion[] frames, float delay) {
setFrames(frames, 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.frames = frames;
this.delay = delay;
time = 0; time = 0;
currentFrame = 0; currentFrame = 0;
timesPlayed = 0; timesPlayed = 0;
this.delay = delay;
} }
public void update(float dt){ public void update(float dt) {
if (delay<=0) return; if(delay <= 0) return;
time += dt; time += dt;
while (time >= delay){ while(time >= delay) {
step(); step();
} }
} }
private void step(){ private void step() {
time -= delay; time -= delay;
currentFrame++; currentFrame++;
if (currentFrame == frames.length){ if(currentFrame == frames.length) {
currentFrame = 0; currentFrame = 0;
timesPlayed++; timesPlayed++;
} }
} }
public TextureRegion[] getFrames() { public TextureRegion getFrame() { return frames[currentFrame]; }
return frames; public int getTimesPlayed() { return timesPlayed; }
} public boolean hasPlayedOnce() { return timesPlayed > 0; }
public int getTimesPlayed() {
return timesPlayed;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment