From 5e66273396224e29fc8708b023d7edb184593918 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 27 May 2014 14:41:06 -0430 Subject: [PATCH] Added animation support. --- .../nxtar/components/AnimationComponent.java | 20 ++++++++-- .../entities/MarkerTestEntityCreator.java | 38 +++++++++++-------- .../ccg/nxtar/systems/AnimationSystem.java | 7 +++- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/ve/ucv/ciens/ccg/nxtar/components/AnimationComponent.java b/src/ve/ucv/ciens/ccg/nxtar/components/AnimationComponent.java index 5c5131b..36e6ea9 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/components/AnimationComponent.java +++ b/src/ve/ucv/ciens/ccg/nxtar/components/AnimationComponent.java @@ -27,15 +27,29 @@ public class AnimationComponent extends Component { public List animationsIds; public int current; public int next; + public boolean loop; public AnimationComponent(ModelInstance instance) throws IllegalArgumentException{ + this(instance, -1, false); + } + + public AnimationComponent(ModelInstance instance, int next) throws IllegalArgumentException{ + this(instance, next, false); + } + + public AnimationComponent(ModelInstance instance, int next, boolean loop) throws IllegalArgumentException{ if(instance == null) throw new IllegalArgumentException("Instance is null."); + else if(next < 0) + throw new IllegalArgumentException("Next is less than 0."); + else if(next > instance.animations.size) + throw new IllegalArgumentException("Next is greater than the number of animations for this model."); - controller = new AnimationController(instance); + controller = new AnimationController(instance); animationsIds = new LinkedList(); - current = -1; - next = -1; + current = -1; + this.next = next; + this.loop = loop; for(int i = 0; i < instance.animations.size; i++){ animationsIds.add(instance.animations.get(i).id); diff --git a/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java b/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java index c6cfbd7..d9ee15f 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java +++ b/src/ve/ucv/ciens/ccg/nxtar/entities/MarkerTestEntityCreator.java @@ -15,6 +15,7 @@ */ package ve.ucv.ciens.ccg.nxtar.entities; +import ve.ucv.ciens.ccg.nxtar.components.AnimationComponent; import ve.ucv.ciens.ccg.nxtar.components.EnvironmentComponent; import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent; import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; @@ -45,13 +46,14 @@ public class MarkerTestEntityCreator extends EntityCreatorBase { private static final String CLASS_NAME = MarkerTestEntityCreator.class.getSimpleName(); private Model bombModel; + private Model animatedModel; private Model boxModel; private SingleLightPerPixelShader ppShader; @Override public void createAllEntities() { ModelBuilder builder; - Entity bomb1, box, bomb2; + Entity bomb, box, anim; G3dModelLoader loader; Environment environment; Material material; @@ -60,7 +62,9 @@ public class MarkerTestEntityCreator extends EntityCreatorBase { Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Creating the meshes."); loader = new G3dModelLoader(new JsonReader()); + bombModel = loader.loadModel(Gdx.files.internal("models/Bomb_test_2.g3dj")); + animatedModel = loader.loadModel(Gdx.files.internal("models/cube.g3dj")); material = new Material(new FloatAttribute(FloatAttribute.Shininess, 50.0f), new ColorAttribute(ColorAttribute.Diffuse, 1.0f, 1.0f, 1.0f, 1.0f), new ColorAttribute(ColorAttribute.Specular, 1.0f, 1.0f, 1.0f, 1.0f)); @@ -77,19 +81,20 @@ public class MarkerTestEntityCreator extends EntityCreatorBase { // Create the entities. Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Creating the enitites."); - bomb1 = world.createEntity(); - bomb1.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); - bomb1.addComponent(new ModelComponent(bombModel)); - bomb1.addComponent(new EnvironmentComponent(environment)); - bomb1.addComponent(new ShaderComponent(ppShader)); - bomb1.addComponent(new MarkerCodeComponent(1023)); + bomb = world.createEntity(); + bomb.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); + bomb.addComponent(new ModelComponent(bombModel)); + bomb.addComponent(new EnvironmentComponent(environment)); + bomb.addComponent(new ShaderComponent(ppShader)); + bomb.addComponent(new MarkerCodeComponent(1023)); - bomb2 = world.createEntity(); - bomb2.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); - bomb2.addComponent(new ModelComponent(bombModel)); - bomb2.addComponent(new EnvironmentComponent(environment)); - bomb2.addComponent(new MarkerCodeComponent(89)); - bomb2.addComponent(new ShaderComponent(ppShader)); + anim = world.createEntity(); + anim.addComponent(new GeometryComponent(new Vector3(0.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(0.25f, 0.25f, -0.25f))); + anim.addComponent(new ModelComponent(animatedModel)); + anim.addComponent(new AnimationComponent(anim.getComponent(ModelComponent.class).instance, 0, true)); + anim.addComponent(new EnvironmentComponent(environment)); + anim.addComponent(new MarkerCodeComponent(89)); + anim.addComponent(new ShaderComponent(ppShader)); box = world.createEntity(); box.addComponent(new GeometryComponent(new Vector3(-1.0f, 0.0f, 0.0f), new Matrix3().idt(), new Vector3(1.0f, 1.0f, 1.0f))); @@ -100,8 +105,8 @@ public class MarkerTestEntityCreator extends EntityCreatorBase { // Add the entities to the world. Gdx.app.log(TAG, CLASS_NAME + ".createAllEntities(): Adding entities to the world."); //sphere.addToWorld(); - bomb1.addToWorld(); - bomb2.addToWorld(); + bomb.addToWorld(); + anim.addToWorld(); box.addToWorld(); } @@ -110,6 +115,9 @@ public class MarkerTestEntityCreator extends EntityCreatorBase { if(boxModel != null) boxModel.dispose(); + if(animatedModel != null) + animatedModel.dispose(); + if(bombModel != null) bombModel.dispose(); diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/AnimationSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/AnimationSystem.java index 9a08e5d..5e2f2a0 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/systems/AnimationSystem.java +++ b/src/ve/ucv/ciens/ccg/nxtar/systems/AnimationSystem.java @@ -36,8 +36,11 @@ public class AnimationSystem extends EntityProcessingSystem { protected void process(Entity e) { AnimationComponent animation = animationMapper.get(e); - if(animation.current != animation.next){ - animation.controller.setAnimation(animation.animationsIds.get(animation.next)); + if(animation.current != animation.next && animation.next >= 0 && animation.next < animation.animationsIds.size()){ + if(animation.loop) + animation.controller.setAnimation(animation.animationsIds.get(animation.next), -1); + else + animation.controller.setAnimation(animation.animationsIds.get(animation.next)); } animation.controller.update(Gdx.graphics.getDeltaTime());