Advertisement
snake5

SGScript - game character

Oct 25th, 2013
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global GameChar = {};
  2.  
  3. GameChar.part_defaults =
  4. {
  5.     localjointoff = [0,0],
  6.     parentjointoff = [0,0],
  7.     renderpolyscale = [1,1],
  8.     minangle = 0,
  9.     maxangle = 0,
  10.     enablefex = false,
  11. };
  12.  
  13. /* static */ function GameChar.load( specfile, targetheight )
  14. {
  15.     spec = parse_sgs_def( io_file_read( specfile ) );
  16.     if( !spec )
  17.         return INFO( "error in specification syntax" );
  18.    
  19.     data = spec;
  20.     scale = targetheight / spec.height;
  21.     sortlist = [];
  22.     data.zlist = [];
  23.     data.root = null;
  24.     data.position = vec2(0,0);
  25.     data.angle = 0.0;
  26.     data.texture_filename = data.texture;
  27.     data.texture = create_texture( data.texture );
  28.     data.vfmt = make_vertex_format( "|pf2tf2cf4" );
  29.    
  30.     foreach( part : data.parts ) part.ch = [];
  31.     foreach( pname, part : data.parts )
  32.     {
  33.         // insert defaults
  34.         foreach( pk, pv : GameChar.part_defaults )
  35.             if( !isset( part, pk ) )
  36.                 part[ pk ] = pv;
  37.        
  38.         // data conversion
  39.         part.tex0 = vec2( part.tex0[0], part.tex0[1] );
  40.         part.tex1 = vec2( part.tex1[0], part.tex1[1] );
  41.         part.renderpolyscale = vec2( part.renderpolyscale[0], part.renderpolyscale[1] );
  42.         part.size = vec2( part.size[0], part.size[1] ) * scale;
  43.         part.localjointoff = vec2( part.localjointoff[0], part.localjointoff[1] ) * scale;
  44.         part.parentjointoff = vec2( part.parentjointoff[0], part.parentjointoff[1] ) * scale;
  45.         part.minangle = deg2rad( part.minangle );
  46.         part.maxangle = deg2rad( part.maxangle );
  47.        
  48.         // fix-ups
  49.         tmptx = part.tex0.x;
  50.         part.tex0.x = part.tex1.x;
  51.         part.tex1.x = tmptx;
  52.         part.tex0.y = 1 - part.tex0.y;
  53.         part.tex1.y = 1 - part.tex1.y;
  54.         if( part.enablefex )
  55.             part.tex1 = ( part.tex0 + part.tex1 ) * 0.5;
  56.        
  57.         // runtime data
  58.         part.angle = 0;
  59.         part.offset = vec2(0,0);
  60.         part.real_ang = null; // will be replaced on transform
  61.         part.real_pos = null;
  62.        
  63.         // structure
  64.         data.zlist.push( part );
  65.         sortlist.push( part.drawdepth );
  66.         part.name = pname;
  67.         if( isset( part, "parent" ) )
  68.             data.parts[ part.parent ].ch.push( pname );
  69.         else
  70.         {
  71.             part.parent = null;
  72.             data.root = part;
  73.         }
  74.     }
  75.     data.zlist.sort_mapped( sortlist );
  76.     printvar(data);
  77.    
  78.     if( !data.texture )
  79.         return INFO( "no texture loaded" );
  80.     if( !data.root )
  81.         return INFO( "no parent found" );
  82.    
  83.     // this = data
  84.     self = class( data, GameChar );
  85.    
  86.     // initialize runtime data
  87.     self.fullTransform();
  88.    
  89.     return self;
  90. }
  91.  
  92. function GameChar.find( name )
  93. {
  94.     return this.parts[ name ];
  95. }
  96.  
  97. /* method */ function GameChar.transform( part, pp, pa )
  98. {
  99.     pra = part.angle + pa;
  100.     prp = pp + part.parentjointoff.rotate( pa ) +
  101.         ( part.offset - part.localjointoff ).rotate( pra );
  102.        
  103.     part.real_ang = pra;
  104.     part.real_pos = prp;
  105.    
  106.     foreach( ch : part.ch )
  107.         this.transform( this.parts[ch], prp, pra );
  108. }
  109.  
  110. /* method */ function GameChar.fullTransform(){ this.transform( this.root, this.position, this.angle ); }
  111.  
  112. /* method */ function GameChar.draw()
  113. {
  114.     this.fullTransform();
  115.    
  116.     vdarr = [].reserve( this.zlist.size * 6 );
  117.     pcolor = fmt_pack( "4f", 1, 1, 1, 1 );
  118.     foreach( part : this.zlist )
  119.     {
  120.         rp = part.real_pos;
  121.         ra = part.real_ang;
  122.         t0 = part.tex0;
  123.         t1 = part.tex1;
  124.         sz = part.size * part.renderpolyscale;
  125.        
  126.         ptsz1 = sz.rotate( ra );
  127.         ptsz2 = vec2(-sz.x,sz.y).rotate( ra );
  128.        
  129.         rpx = rp.x;
  130.         rpy = rp.y;
  131.        
  132.         v00 = fmt_pack( "4f16s", rpx - ptsz2.x, rpy - ptsz2.y, t0.x, t0.y, pcolor );
  133.         v10 = fmt_pack( "4f16s", rpx - ptsz1.x, rpy - ptsz1.y, t1.x, t0.y, pcolor );
  134.         v11 = fmt_pack( "4f16s", rpx + ptsz2.x, rpy + ptsz2.y, t1.x, t1.y, pcolor );
  135.         v01 = fmt_pack( "4f16s", rpx + ptsz1.x, rpy + ptsz1.y, t0.x, t1.y, pcolor );
  136.        
  137.         vdarr.push( v00, v10, v11, v11, v01, v00 );
  138.     }
  139.    
  140.     vdata = string_implode( vdarr, "" );
  141.     draw_packed( this.texture, this.vfmt, vdata, 0, this.zlist.size * 6, PT_TRIANGLES );
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement