forked from LeenkxTeam/Leenkx_Examples
update
This commit is contained in:
4
macro_lnxpack/README.md
Normal file
4
macro_lnxpack/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
Use build macro to:
|
||||
- Create trait constructor
|
||||
- Create trait init function
|
||||
- Parse exported `Scene.lnx` file and auto create references to objects in scene
|
||||
13
macro_lnxpack/Sources/lnx/Game.hx
Normal file
13
macro_lnxpack/Sources/lnx/Game.hx
Normal file
@ -0,0 +1,13 @@
|
||||
package lnx;
|
||||
|
||||
import iron.math.Vec4;
|
||||
|
||||
@:build(lnx.Macros.build())
|
||||
class Game extends iron.Trait {
|
||||
|
||||
function update() {
|
||||
Cube.transform.rotate( Vec4.zAxis(), 0.01 );
|
||||
Icosphere.transform.rotate( Vec4.yAxis(), 0.01 );
|
||||
Torus.transform.rotate( Vec4.xAxis(), 0.01 );
|
||||
}
|
||||
}
|
||||
85
macro_lnxpack/Sources/lnx/Macros.hx
Normal file
85
macro_lnxpack/Sources/lnx/Macros.hx
Normal file
@ -0,0 +1,85 @@
|
||||
package lnx;
|
||||
|
||||
#if macro
|
||||
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Expr;
|
||||
import iron.data.SceneFormat;
|
||||
import iron.system.LnxPack;
|
||||
import sys.FileSystem;
|
||||
import sys.io.File;
|
||||
|
||||
using StringTools;
|
||||
using haxe.io.Path;
|
||||
|
||||
class Macros {
|
||||
|
||||
static function build() {
|
||||
var fields = Context.getBuildFields();
|
||||
var pos = Context.currentPos();
|
||||
var target = if( Context.defined('kha_krom') ) 'krom';
|
||||
else if( Context.defined('kha_html5') ) 'html5';
|
||||
else Context.fatalError('Only krom and html5 are supported targets for this project', pos );
|
||||
var dir = Sys.getCwd() + target;
|
||||
var files = FileSystem.readDirectory(dir);
|
||||
var sceneData : TSceneFormat = null;
|
||||
var meshes = new Array<String>();
|
||||
for( file in files ) {
|
||||
if( file == 'Scene.lnx' ) {
|
||||
var p = '$dir/$file';
|
||||
sceneData = LnxPack.decode( File.getBytes(p) );
|
||||
trace(sceneData);
|
||||
trace(sceneData.name);
|
||||
for( obj in sceneData.objects ) {
|
||||
if( obj.properties != null )
|
||||
for( p in obj.properties ) trace(p.name +' = '+ p.value);
|
||||
if( obj.type == 'mesh_object' )
|
||||
meshes.push(obj.name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var constructorExprs = new Array<Expr>();
|
||||
constructorExprs.push(macro super() );
|
||||
constructorExprs.push(macro notifyOnInit(init) );
|
||||
fields.push({
|
||||
access: [APublic,AInline],
|
||||
name: 'new',
|
||||
pos: pos,
|
||||
kind: FFun({
|
||||
args: [],
|
||||
ret: macro: Void,
|
||||
expr: { expr: EBlock( constructorExprs ), pos: pos }
|
||||
}),
|
||||
});
|
||||
|
||||
var initExprs = new Array<Expr>();
|
||||
initExprs.push( macro trace("Init") );
|
||||
for( mesh in meshes ) {
|
||||
fields.push({
|
||||
name: mesh,
|
||||
access: [APublic],
|
||||
kind: FVar(macro: iron.object.MeshObject, macro null),
|
||||
pos: pos
|
||||
});
|
||||
initExprs.push( macro this.$mesh = iron.Scene.active.getMesh($v{mesh}) );
|
||||
}
|
||||
initExprs.push( macro notifyOnUpdate(update) );
|
||||
|
||||
fields.push({
|
||||
name: 'init',
|
||||
access: [],
|
||||
kind: FFun({
|
||||
args: [],
|
||||
ret: macro : Void,
|
||||
expr: { expr: EBlock( initExprs ), pos: Context.currentPos() }
|
||||
}),
|
||||
pos: pos
|
||||
});
|
||||
|
||||
return fields;
|
||||
}
|
||||
}
|
||||
|
||||
#end
|
||||
BIN
macro_lnxpack/macro_lnxpack.blend
Normal file
BIN
macro_lnxpack/macro_lnxpack.blend
Normal file
Binary file not shown.
Reference in New Issue
Block a user