Advertisement
Guest User

ragdoll generator

a guest
Sep 22nd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 8.12 KB | None | 0 0
  1. package;
  2.  
  3. import entities.SpineSprite;
  4. import flixel.FlxObject;
  5. import flixel.FlxSprite;
  6. import flixel.addons.editors.spine.FlxSpine;
  7. import flixel.FlxG;
  8. import flixel.FlxState;
  9. import flixel.addons.nape.FlxNapeSpace;
  10. import flixel.addons.nape.FlxNapeSprite;
  11. import flixel.text.FlxText;
  12. import nape.callbacks.CbType;
  13. import nape.callbacks.InteractionType;
  14. import nape.callbacks.PreCallback;
  15. import nape.callbacks.PreFlag;
  16. import nape.callbacks.PreListener;
  17. import nape.constraint.AngleJoint;
  18. import nape.constraint.PivotJoint;
  19. import nape.constraint.WeldJoint;
  20. import nape.geom.Vec2;
  21. import spinehaxe.Bone;
  22. import spinehaxe.Slot;
  23. import spinehaxe.attachments.RegionAttachment;
  24.  
  25. class MenuState extends FlxState
  26. {
  27.     //var spineSprite:FlxSpine;
  28.     var spine_ent:FlxSprite ;
  29.     var spine:SpineSprite;
  30.     var shooter:Shooter;
  31.     var groupTorso:CbType;
  32.     var groupArms:CbType;
  33.     var groupHead:CbType;
  34.     override public function create():Void
  35.     {
  36.         super.create();
  37.         initSpace();
  38.        
  39.         FlxG.cameras.bgColor = 0xff131c1b;
  40.        
  41.         //spineSprite = new SpineBoyTest(FlxSpine.readSkeletonData("spineboy", "spineboy", "assets", 0.6), 300, 440);
  42.         //spineSprite.antialiasing = true;
  43.         //add(spineSprite);
  44.        
  45.         var instructions = new FlxText(0, 0, 250, "Space: Toggle Debug Display\nMove: Arrows / WASD ", 12);
  46.         instructions.ignoreDrawDebug = true;
  47.         add(instructions);
  48.        
  49.        
  50.        
  51.         shooter = new Shooter();
  52.         add(shooter);
  53.        
  54.         spine_ent = new FlxSprite( );
  55.         spine_ent.makeGraphic(30, 122);
  56.         add(spine_ent);
  57.  
  58.         spine_ent.screenCenter();
  59.        
  60.         spine = new SpineSprite("fly", spine_ent, null, true,0.5);
  61.         add(spine);
  62.         spine.state.setAnimationByName(0, "idle", true);
  63.        
  64.        
  65.        
  66.         sprites = new Array<FlxNapeSprite>();
  67.            
  68.         groupTorso = new CbType();
  69.         groupArms = new CbType();
  70.      
  71.         groupHead = new CbType();
  72.        
  73.     //  spine.skeleton.setAttachment("head", "head");
  74.        
  75.         FlxG.debugger.drawDebug = true;
  76.     }
  77.    
  78.     /**
  79.      * Grabs a slot and creates a physic body with the slot attachment image
  80.      * Adding order is important!
  81.      */
  82.     function cloneAllParts():Void
  83.     {
  84.         cloneSlot("larm");
  85.         cloneSlot("rarm");
  86.         cloneSlot("body");
  87.         cloneSlot("head_dead");
  88.        
  89.         tieTogether("head_dead", "body");
  90.         tieTogether("larm", "head_dead", new Vec2(0, -30), new Vec2( -110, 100));//ok
  91.        
  92.         tieTogether("rarm", "head_dead",new Vec2(0,-40),new Vec2(90,70));
  93.     }
  94.    
  95.     /**
  96.      * Ties together two body parts previously created
  97.      * Default anchor is 0,0, meaning the center of the sprite
  98.      * It automatically handles different spine scaling
  99.      * Always set Anchors using original sprite dimensions!
  100.      *
  101.      * @param   part1       what you want to tie
  102.      * @param   part2       where to
  103.      * @param   OffsetAnchor1   first "nail" location offset, X Y
  104.      * @param   OffsetAnchor2   second "nail" location offset, X Y
  105.      */
  106.     function tieTogether(part1:String, part2:String,?OffsetAnchor1:Vec2,?OffsetAnchor2:Vec2):Void
  107.     {
  108.         var nape_spr1:FlxNapeSprite;
  109.         var nape_spr2:FlxNapeSprite;
  110.        
  111.         nape_spr1 = null;
  112.         nape_spr2 = null;
  113.        
  114.         //find 1
  115.         for (i in 0...sprites.length)
  116.         {
  117.             if ( cast(sprites[i], FlxNapeSprite).name == part1 )
  118.             {
  119.                 nape_spr1 = sprites[i];
  120.             //  trace("first match found");
  121.             }
  122.            
  123.             if ( cast(sprites[i], FlxNapeSprite).name == part2 )
  124.             {
  125.                 nape_spr2 = sprites[i];
  126.             //  trace("second match found");
  127.             }
  128.         }  
  129.  
  130.         if (nape_spr1 != null && nape_spr2 != null)
  131.         {
  132.         //  trace("we can tie them both");
  133.            
  134.             //  return  ;
  135.             var anchor1:Vec2 = new Vec2(0, 0);//nail on head location
  136.             var anchor2:Vec2 = new Vec2(0, 0);//nail on torso location
  137.            
  138.             if (OffsetAnchor1 != null)
  139.             {
  140.                 if(nape_spr1.facing==FlxObject.RIGHT)
  141.                 anchor1.x += OffsetAnchor1.x * nape_spr1.scale.x;
  142.                 else
  143.                 anchor1.x -= OffsetAnchor1.x * nape_spr1.scale.x;
  144.                
  145.                 anchor1.y += OffsetAnchor1.y*nape_spr1.scale.y;
  146.             }
  147.            
  148.             if (OffsetAnchor2 != null)
  149.             {
  150.                 if(nape_spr2.facing==FlxObject.RIGHT)
  151.                 anchor2.x += OffsetAnchor2.x * nape_spr2.scale.x;
  152.                 else
  153.                 anchor2.x -= OffsetAnchor2.x * nape_spr2.scale.x;
  154.                
  155.                 anchor2.y += OffsetAnchor2.y*nape_spr2.scale.y;
  156.             }
  157.            
  158.             if (nape_spr2.name.indexOf("body") >= 0)
  159.             {
  160.                 anchor2.y = -(nape_spr2.height*nape_spr2.scale.y) / 2;
  161.             }
  162.            
  163.            
  164.             var constrain:PivotJoint = new PivotJoint(nape_spr1.body, nape_spr2.body, anchor1, anchor2);
  165.  
  166.             constrain.space = FlxNapeSpace.space;
  167.            
  168.             if (nape_spr1.name.indexOf("head") >= 0)
  169.             {
  170.                 var angle_constrain:AngleJoint = new AngleJoint(nape_spr1.body, nape_spr2.body, -0.6, 0.6);
  171.                
  172.                 angle_constrain.space = FlxNapeSpace.space;
  173.                 trace("angle constraint");
  174.             }
  175.         }
  176.     }
  177.    
  178.     public var sprites:Array<FlxNapeSprite>;
  179.    
  180.     function cloneSlot(partName:String):Void
  181.     {
  182.         var nape_spr:FlxNapeSprite;
  183.        
  184.         //create physics body with default size
  185.         nape_spr = new FlxNapeSprite(22, 22  );
  186.  
  187.         //default location
  188.         nape_spr.x = spine.x;
  189.         nape_spr.y = spine.y;  
  190.  
  191.         //give the body a sprite, which is the part -- this sprite will be scaled properly
  192.         nape_spr.loadGraphicFromSprite(spine.getRegionSprite(partName), true );
  193.        
  194.         //resize the body, using the sprite dimensions we just grabbed
  195.         if(partName.indexOf("head") >= 0)
  196.         nape_spr.createCircularBody((nape_spr.width*nape_spr.scale.x)*0.2+(nape_spr.height*nape_spr.scale.y)*0.2);
  197.         else
  198.         nape_spr.createRectangularBody((nape_spr.width*0.5) * nape_spr.scale.x, (nape_spr.height*0.5) * nape_spr.scale.y);//use 50% of the sprite so collision is smaller
  199.            
  200.         //collision flags
  201.         if(partName.indexOf("head") >= 0)
  202.         nape_spr.body.cbTypes.add(groupHead);
  203.         else if(partName.indexOf("body") >= 0)
  204.         nape_spr.body.cbTypes.add(groupTorso);
  205.         else
  206.         nape_spr.body.cbTypes.add(groupArms);
  207.        
  208.         var listener:PreListener;
  209.        
  210.      
  211.         //head ignores torso :D
  212.         listener = new PreListener(InteractionType.COLLISION, groupHead, groupTorso, ignoreCollision, 0, true);
  213.         listener.space = FlxNapeSpace.space;   
  214.        
  215.        
  216.         //make it face left or right correctly
  217.         if (spine.facing == FlxObject.LEFT)
  218.         {
  219.             nape_spr.flipX = true;
  220.         }
  221.         else
  222.         {
  223.             nape_spr.flipX = false;
  224.         }
  225.        
  226.         //move the new physics objects where the original image is at
  227.         var slot:Slot = spine.skeleton.findSlot(partName);
  228.  
  229.         var regionAttachment:RegionAttachment = null;
  230.         if (Std.is(slot.attachment, RegionAttachment))
  231.             regionAttachment = cast slot.attachment;
  232.        
  233.         var wrapper:FlxSprite = spine.getSprite(regionAttachment);         
  234.    
  235.         nape_spr.body.position.x= wrapper.x+(wrapper.width*wrapper.scale.x)/2;
  236.         nape_spr.body.position.y = wrapper.y+(wrapper.height*wrapper.scale.y)/2;
  237.        
  238.         //so it bounces less
  239.         nape_spr.setBodyMaterial(0.5, 0.2, 0.5);
  240.        
  241.         //so we can grab it
  242.         shooter.registerPhysSprite(nape_spr);
  243.  
  244.         //to find it later
  245.         nape_spr.name = partName;
  246.        
  247.         sprites.push(nape_spr);
  248.          
  249.         //after everything is set, finally add it to stage!
  250.         add(nape_spr);
  251.        
  252.         trace(nape_spr.name);
  253.     }
  254.    
  255.     function ignoreCollision(cb:PreCallback):PreFlag
  256.     {
  257.         return PreFlag.IGNORE;
  258.     }
  259.    
  260.     function initSpace():Void
  261.     {
  262.         FlxNapeSpace.init();
  263.         FlxNapeSpace.drawDebug = true;
  264.          
  265.         FlxNapeSpace.createWalls(0,0,FlxG.width, FlxG.height - 30);
  266.         FlxNapeSpace.space.gravity.setxy(0, 1000);
  267.        
  268.         var botomDebug:FlxSprite = new FlxSprite();
  269.         botomDebug.makeGraphic(FlxG.width, 30);
  270.         botomDebug.y = FlxG.height - 30;
  271.         add(botomDebug);
  272.  
  273.     }
  274.    
  275.     override public function update(elapsed:Float):Void
  276.     {
  277.         super.update(elapsed);
  278.        
  279.         // toggle debug display
  280.         if (FlxG.keys.justPressed.SPACE)
  281.             FlxG.debugger.drawDebug = !FlxG.debugger.drawDebug;
  282.        
  283.         if (FlxG.keys.justPressed.ENTER)
  284.         cloneAllParts();   
  285.        
  286.         if (FlxG.keys.justPressed.R)
  287.             FlxG.resetState();
  288.            
  289.         if (FlxG.keys.justPressed.G)
  290.             FlxNapeSpace.drawDebug = !FlxNapeSpace.drawDebug;
  291.            
  292.         // movement
  293.         if (FlxG.keys.anyPressed([W, UP]))
  294.         {
  295.             spine_ent.y -= 500 * elapsed;
  296.         }
  297.         if (FlxG.keys.anyPressed([S, DOWN]))
  298.         {
  299.             spine_ent.y += 500 * elapsed;
  300.         }
  301.         if (FlxG.keys.anyPressed([D, RIGHT]))
  302.         {
  303.             spine_ent.x += 500 * elapsed;
  304.             spine_ent.facing = FlxObject.RIGHT;
  305.         }
  306.         if (FlxG.keys.anyPressed([A, LEFT]))
  307.         {
  308.             spine_ent.x -= 500 * elapsed;
  309.             spine_ent.facing = FlxObject.LEFT;
  310.         }
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement