Selaa lähdekoodia

added h2d.Anim, various fixes

ncannasse 12 vuotta sitten
vanhempi
commit
f06fda5b58
4 muutettua tiedostoa jossa 46 lisäystä ja 5 poistoa
  1. 31 0
      h2d/Anim.hx
  2. 4 0
      h2d/Scene.hx
  3. 10 4
      h2d/Tile.hx
  4. 1 1
      hxd/Resource.hx

+ 31 - 0
h2d/Anim.hx

@@ -0,0 +1,31 @@
+package h2d;
+
+class Anim extends Drawable {
+
+	public var frames : Array<Tile>;
+	public var currentFrame : Float;
+	public var speed : Float;
+	
+	public function new( ?parent ) {
+		super(parent);
+		this.frames = [];
+		this.currentFrame = 0;
+		this.speed = 1.;
+	}
+	
+	public function play( frames ) {
+		this.frames = frames;
+		this.currentFrame = 0;
+	}
+	
+	override function sync( ctx : RenderContext ) {
+		currentFrame += speed * ctx.elapsedTime;
+		currentFrame %= frames.length;
+	}
+	
+	override function draw( ctx : RenderContext ) {
+		var t = frames[Std.int(currentFrame)];
+		if( t != null ) drawTile(ctx.engine,t);
+	}
+	
+}

+ 4 - 0
h2d/Scene.hx

@@ -341,6 +341,10 @@ class Scene extends Layers implements h3d.IDrawable {
 			onDelete();
 	}
 	
+	public function setElapsedTime( v : Float ) {
+		ctx.elapsedTime = v;
+	}
+	
 	public function render( engine : h3d.Engine ) {
 		ctx.engine = engine;
 		ctx.frame++;

+ 10 - 4
h2d/Tile.hx

@@ -109,11 +109,17 @@ class Tile {
 	}
 	
 	
-	public function split( frames : Int ) {
+	public function split( frames : Int, vertical = false ) {
 		var tl = [];
-		var stride = Std.int(width / frames);
-		for( i in 0...frames )
-			tl.push(sub(i * stride, 0, stride, height));
+		if( vertical ) {
+			var stride = Std.int(height / frames);
+			for( i in 0...frames )
+				tl.push(sub(0, i * stride, width, stride));
+		} else {
+			var stride = Std.int(width / frames);
+			for( i in 0...frames )
+				tl.push(sub(i * stride, 0, stride, height));
+		}
 		return tl;
 	}
 	

+ 1 - 1
hxd/Resource.hx

@@ -99,7 +99,7 @@ class Resource {
 						params : [],
 						pack : ["res"],
 						name : className,
-						meta : [ { name : ":bitmap", params : [ { expr : EConst(CString(path)), pos : pos } ], pos : pos } ],
+						meta : [ { name : ":bitmap", params : [ { expr : EConst(CString(path)), pos : pos } ], pos : pos }, { name : ":keep", params : [], pos : pos } ],
 						kind : TDClass({ pack : ["flash","display"], name : "BitmapData", params : [] }),
 						isExtern : false,
 						fields : [],