Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global GameChar = {};
- GameChar.part_defaults =
- {
- localjointoff = [0,0],
- parentjointoff = [0,0],
- renderpolyscale = [1,1],
- minangle = 0,
- maxangle = 0,
- enablefex = false,
- };
- /* static */ function GameChar.load( specfile, targetheight )
- {
- spec = parse_sgs_def( io_file_read( specfile ) );
- if( !spec )
- return INFO( "error in specification syntax" );
- data = spec;
- scale = targetheight / spec.height;
- sortlist = [];
- data.zlist = [];
- data.root = null;
- data.position = vec2(0,0);
- data.angle = 0.0;
- data.texture_filename = data.texture;
- data.texture = create_texture( data.texture );
- data.vfmt = make_vertex_format( "|pf2tf2cf4" );
- foreach( part : data.parts ) part.ch = [];
- foreach( pname, part : data.parts )
- {
- // insert defaults
- foreach( pk, pv : GameChar.part_defaults )
- if( !isset( part, pk ) )
- part[ pk ] = pv;
- // data conversion
- part.tex0 = vec2( part.tex0[0], part.tex0[1] );
- part.tex1 = vec2( part.tex1[0], part.tex1[1] );
- part.renderpolyscale = vec2( part.renderpolyscale[0], part.renderpolyscale[1] );
- part.size = vec2( part.size[0], part.size[1] ) * scale;
- part.localjointoff = vec2( part.localjointoff[0], part.localjointoff[1] ) * scale;
- part.parentjointoff = vec2( part.parentjointoff[0], part.parentjointoff[1] ) * scale;
- part.minangle = deg2rad( part.minangle );
- part.maxangle = deg2rad( part.maxangle );
- // fix-ups
- tmptx = part.tex0.x;
- part.tex0.x = part.tex1.x;
- part.tex1.x = tmptx;
- part.tex0.y = 1 - part.tex0.y;
- part.tex1.y = 1 - part.tex1.y;
- if( part.enablefex )
- part.tex1 = ( part.tex0 + part.tex1 ) * 0.5;
- // runtime data
- part.angle = 0;
- part.offset = vec2(0,0);
- part.real_ang = null; // will be replaced on transform
- part.real_pos = null;
- // structure
- data.zlist.push( part );
- sortlist.push( part.drawdepth );
- part.name = pname;
- if( isset( part, "parent" ) )
- data.parts[ part.parent ].ch.push( pname );
- else
- {
- part.parent = null;
- data.root = part;
- }
- }
- data.zlist.sort_mapped( sortlist );
- printvar(data);
- if( !data.texture )
- return INFO( "no texture loaded" );
- if( !data.root )
- return INFO( "no parent found" );
- // this = data
- self = class( data, GameChar );
- // initialize runtime data
- self.fullTransform();
- return self;
- }
- function GameChar.find( name )
- {
- return this.parts[ name ];
- }
- /* method */ function GameChar.transform( part, pp, pa )
- {
- pra = part.angle + pa;
- prp = pp + part.parentjointoff.rotate( pa ) +
- ( part.offset - part.localjointoff ).rotate( pra );
- part.real_ang = pra;
- part.real_pos = prp;
- foreach( ch : part.ch )
- this.transform( this.parts[ch], prp, pra );
- }
- /* method */ function GameChar.fullTransform(){ this.transform( this.root, this.position, this.angle ); }
- /* method */ function GameChar.draw()
- {
- this.fullTransform();
- vdarr = [].reserve( this.zlist.size * 6 );
- pcolor = fmt_pack( "4f", 1, 1, 1, 1 );
- foreach( part : this.zlist )
- {
- rp = part.real_pos;
- ra = part.real_ang;
- t0 = part.tex0;
- t1 = part.tex1;
- sz = part.size * part.renderpolyscale;
- ptsz1 = sz.rotate( ra );
- ptsz2 = vec2(-sz.x,sz.y).rotate( ra );
- rpx = rp.x;
- rpy = rp.y;
- v00 = fmt_pack( "4f16s", rpx - ptsz2.x, rpy - ptsz2.y, t0.x, t0.y, pcolor );
- v10 = fmt_pack( "4f16s", rpx - ptsz1.x, rpy - ptsz1.y, t1.x, t0.y, pcolor );
- v11 = fmt_pack( "4f16s", rpx + ptsz2.x, rpy + ptsz2.y, t1.x, t1.y, pcolor );
- v01 = fmt_pack( "4f16s", rpx + ptsz1.x, rpy + ptsz1.y, t0.x, t1.y, pcolor );
- vdarr.push( v00, v10, v11, v11, v01, v00 );
- }
- vdata = string_implode( vdarr, "" );
- draw_packed( this.texture, this.vfmt, vdata, 0, this.zlist.size * 6, PT_TRIANGLES );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement