瀏覽代碼

added ObjectFollower

ncannasse 9 年之前
父節點
當前提交
34a7e225cd
共有 1 個文件被更改,包括 37 次插入0 次删除
  1. 37 0
      h2d/Scene3D.hx

+ 37 - 0
h2d/Scene3D.hx

@@ -13,4 +13,41 @@ class Scene3D extends Sprite {
 		scene.render(ctx.engine);
 	}
 
+}
+
+class ObjectFollower extends Sprite {
+	
+	public var follow : h3d.scene.Object;
+	
+	public function new( obj, ?parent ) {
+		super(parent);
+		this.follow = obj;
+	}
+	
+	function followObject() {
+		if( follow == null )
+			return;
+		var scene = @:privateAccess follow.getScene();
+		if( scene == null )
+			return;
+		var s2d = getScene();		
+		var width = s2d == null ? h3d.Engine.getCurrent().width : s2d.width;
+		var height = s2d == null ? h3d.Engine.getCurrent().height : s2d.height;
+		var absPos = follow.getAbsPos();
+		var p = scene.camera.project(absPos._41, absPos._42, absPos._43, width, height, true);
+		x = p.x;
+		y = p.y;
+		visible = p.z > 0;
+	}
+	
+	override function syncPos() {
+		followObject();
+		super.syncPos();
+	}
+	
+	override function sync(ctx) {
+		followObject();
+		super.sync(ctx);
+	}
+	
 }