Commits

Eugene Veretennikov committed 52f8993

MixTest port

  • Participants
  • Parent commits c96cc09

Comments (0)

Files changed (4)

+<?xml version="1.0" encoding="utf-8"?>
+<project>
+
+    <meta title="spinehx - AnimationStateTest" package="org.bitbucket.nitrobin.spinehx.AnimationStateTest" version="1.0.0" company="spinehx" />
+    <!--<app main="spinehx.AnimationStateTest" path="out" file="spinehx-AnimationStateTest" />-->
+    <app main="spinehx.MixTest" path="out" file="spinehx-AnimationStateTest" />
+    <window width="640" height="480" fps="200" unless="mobile"/>
+    <window fps="60" if="mobile"/>
+    <source path="src" />
+    <source path="src-test" />
+
+    <haxelib name="nme" />
+    <haxedef name="haxeJSON"/>
+    <!--<haxedef name="HXCPP_FLOAT32" if="mobile"/>-->
+
+    <assets path="assets" rename="assets" exclude="nme.svg" />
+    <icon path="assets/nme.svg" />
+
+    <ndll name="std" />
+    <ndll name="regexp" />
+    <ndll name="zlib" />
+    <ndll name="nme" haxelib="nme" nekoapi="1"/>
+
+</project>

src-test/spinehx/AnimationStateTest.hx

     var skeleton:Skeleton;
     var root_:Bone;
     var state:AnimationState;
-    var lastTime:Float;
+    var lastTime:Float = 0.0;
 
     var mode:Int = 1;
 

src-test/spinehx/MixTest.hx

+/*******************************************************************************
+ * Copyright (c) 2013, Esoteric Software
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ ******************************************************************************/
+
+package spinehx;
+
+import flash.display.Sprite;
+import flash.events.Event;
+import flash.events.MouseEvent;
+import spinehx.Bone;
+import spinehx.atlas.TextureAtlas;
+import spinehx.SkeletonJson;
+import spinehx.Animation;
+import spinehx.Skeleton;
+import spinehx.SkeletonData;
+import spinehx.SkeletonRendererDebug;
+import spinehx.SkeletonRenderer;
+
+class MixTest extends Sprite {
+	var time:Float = 0.0;
+	var renderer:SkeletonRenderer;
+	var debugRenderer:SkeletonRendererDebug;
+
+	var skeletonData:SkeletonData;
+	var skeleton:Skeleton;
+	var walkAnimation:Animation;
+	var jumpAnimation:Animation;
+    var lastTime:Float = 0.0;
+    var mode:Int = 1;
+
+    public function new() {
+        super();
+		var name = "spineboy";
+
+		var atlas:TextureAtlas = TextureAtlas.create("assets/" + name + ".atlas", "assets/");
+
+		if (true) {
+			var json = SkeletonJson.create(atlas);
+			// json.setScale(2);
+			skeletonData = json.readSkeletonData(name, nme.Assets.getText("assets/" + name + ".json"));
+		} /*else {
+			SkeletonBinary binary = new SkeletonBinary(atlas);
+			// binary.setScale(2);
+			skeletonData = binary.readSkeletonData(Gdx.files.internal(name + ".skel"));
+		}*/
+		walkAnimation = skeletonData.findAnimation("walk");
+		jumpAnimation = skeletonData.findAnimation("jump");
+
+		skeleton = Skeleton.create(skeletonData);
+		skeleton.setToBindPose();
+
+		var root_:Bone = skeleton.getRootBone();
+		root_.x = -50;
+		root_.y = 20;
+		root_.scaleX = 1.0;
+		root_.scaleY = 1.0;
+        skeleton.setFlipY(true);
+		skeleton.updateWorldTransform();
+        lastTime = haxe.Timer.stamp();
+
+        renderer = new SkeletonRenderer(skeleton);
+        debugRenderer = new SkeletonRendererDebug(skeleton);
+
+        renderer.x = 0;
+        renderer.y = 300;
+        debugRenderer.x = 0;
+        debugRenderer.y = 300;
+        addChild(renderer);
+        addChild(debugRenderer);
+        addChild(new nme.display.FPS());
+
+        addEventListener(Event.ENTER_FRAME, render);
+        addEventListener(Event.ADDED_TO_STAGE, added);  renderer.draw();
+    }
+
+    public function added(e:Event):Void {
+        this.mouseChildren = false;
+        stage.addEventListener(MouseEvent.CLICK, onClick);
+    }
+
+    public function onClick(e:Event):Void {
+        mode++;
+        mode%=3;
+    }
+
+    public function render(e:Event):Void {
+        var deltaTime:Float = haxe.Timer.stamp() - lastTime;
+        var delta = (deltaTime) / 4.0;   // Reduced to make mixing easier to see.
+        lastTime = haxe.Timer.stamp();
+
+		var jump:Float = jumpAnimation.getDuration();
+		var beforeJump:Float = 1.0;
+		var blendIn:Float = 0.4;
+		var blendOut:Float = 0.4;
+		var blendOutStart:Float = beforeJump + jump - blendOut;
+		var total:Float = 3.75;
+
+		time += delta;
+
+		var root_:Bone = skeleton.getRootBone();
+		var speed:Float = 180;
+		if (time > beforeJump + blendIn && time < blendOutStart) speed = 360;
+		root_.setX(root_.getX() + speed * delta);
+
+		// This shows how to manage state manually. See AnimationStatesTest.
+		if (time > total) {
+			// restart
+			time = 0;
+			root_.setX(-50);
+		} else if (time > beforeJump + jump) {
+			// just walk after jump
+			walkAnimation.apply(skeleton, time, true);
+		} else if (time > blendOutStart) {
+			// blend out jump
+			walkAnimation.apply(skeleton, time, true);
+			jumpAnimation.mix(skeleton, time - beforeJump, false, 1 - (time - blendOutStart) / blendOut);
+		} else if (time > beforeJump + blendIn) {
+			// just jump
+			jumpAnimation.apply(skeleton, time - beforeJump, false);
+		} else if (time > beforeJump) {
+			// blend in jump
+			walkAnimation.apply(skeleton, time, true);
+			jumpAnimation.mix(skeleton, time - beforeJump, false, (time - beforeJump) / blendIn);
+		} else {
+			// just walk before jump
+			walkAnimation.apply(skeleton, time, true);
+		}
+
+		skeleton.updateWorldTransform();
+		skeleton.update(deltaTime);
+
+
+        if(mode == 0 || mode == 1){
+            renderer.visible = true;
+            renderer.draw();
+        } else renderer.visible = false;
+        if(mode == 0 || mode == 2){
+            debugRenderer.visible = true;
+            debugRenderer.draw();
+        } else debugRenderer.visible = false;
+	}
+
+//	public void resize (int width, int height) {
+//		batch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
+//		debugRenderer.getShapeRenderer().setProjectionMatrix(batch.getProjectionMatrix());
+//	}
+
+//	public static void main (String[] args) throws Exception {
+//		new LwjglApplication(new MixTest());
+//	}
+}

src/spinehx/SkeletonRenderer.hx

     public function new (skeleton:Skeleton) {
        super();
        this.skeleton = skeleton;
-       vs = new nme.Vector<Float>();
-       idx = new nme.Vector<Int>();
-       uvt = new nme.Vector<Float>();
-   }
+        vs = nme.Vector.fromArray([0.0]);
+        idx = nme.Vector.fromArray([0]);
+        uvt = nme.Vector.fromArray([0.0]);
+//        vs = new nme.Vector<Float>();
+//        idx = new nme.Vector<Int>();
+//        uvt = new nme.Vector<Float>();
+    }
 
     public function draw () {
         var vi:Int = 0;