Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import entities.SpineSprite;
- import flixel.FlxObject;
- import flixel.FlxSprite;
- import flixel.addons.editors.spine.FlxSpine;
- import flixel.FlxG;
- import flixel.FlxState;
- import flixel.addons.nape.FlxNapeSpace;
- import flixel.addons.nape.FlxNapeSprite;
- import flixel.text.FlxText;
- import nape.callbacks.CbType;
- import nape.callbacks.InteractionType;
- import nape.callbacks.PreCallback;
- import nape.callbacks.PreFlag;
- import nape.callbacks.PreListener;
- import nape.constraint.AngleJoint;
- import nape.constraint.PivotJoint;
- import nape.constraint.WeldJoint;
- import nape.geom.Vec2;
- import spinehaxe.Bone;
- import spinehaxe.Slot;
- import spinehaxe.attachments.RegionAttachment;
- class MenuState extends FlxState
- {
- //var spineSprite:FlxSpine;
- var spine_ent:FlxSprite ;
- var spine:SpineSprite;
- var shooter:Shooter;
- var groupTorso:CbType;
- var groupArms:CbType;
- var groupHead:CbType;
- override public function create():Void
- {
- super.create();
- initSpace();
- FlxG.cameras.bgColor = 0xff131c1b;
- //spineSprite = new SpineBoyTest(FlxSpine.readSkeletonData("spineboy", "spineboy", "assets", 0.6), 300, 440);
- //spineSprite.antialiasing = true;
- //add(spineSprite);
- var instructions = new FlxText(0, 0, 250, "Space: Toggle Debug Display\nMove: Arrows / WASD ", 12);
- instructions.ignoreDrawDebug = true;
- add(instructions);
- shooter = new Shooter();
- add(shooter);
- spine_ent = new FlxSprite( );
- spine_ent.makeGraphic(30, 122);
- add(spine_ent);
- spine_ent.screenCenter();
- spine = new SpineSprite("fly", spine_ent, null, true,0.5);
- add(spine);
- spine.state.setAnimationByName(0, "idle", true);
- sprites = new Array<FlxNapeSprite>();
- groupTorso = new CbType();
- groupArms = new CbType();
- groupHead = new CbType();
- // spine.skeleton.setAttachment("head", "head");
- FlxG.debugger.drawDebug = true;
- }
- /**
- * Grabs a slot and creates a physic body with the slot attachment image
- * Adding order is important!
- */
- function cloneAllParts():Void
- {
- cloneSlot("larm");
- cloneSlot("rarm");
- cloneSlot("body");
- cloneSlot("head_dead");
- tieTogether("head_dead", "body");
- tieTogether("larm", "head_dead", new Vec2(0, -30), new Vec2( -110, 100));//ok
- tieTogether("rarm", "head_dead",new Vec2(0,-40),new Vec2(90,70));
- }
- /**
- * Ties together two body parts previously created
- * Default anchor is 0,0, meaning the center of the sprite
- * It automatically handles different spine scaling
- * Always set Anchors using original sprite dimensions!
- *
- * @param part1 what you want to tie
- * @param part2 where to
- * @param OffsetAnchor1 first "nail" location offset, X Y
- * @param OffsetAnchor2 second "nail" location offset, X Y
- */
- function tieTogether(part1:String, part2:String,?OffsetAnchor1:Vec2,?OffsetAnchor2:Vec2):Void
- {
- var nape_spr1:FlxNapeSprite;
- var nape_spr2:FlxNapeSprite;
- nape_spr1 = null;
- nape_spr2 = null;
- //find 1
- for (i in 0...sprites.length)
- {
- if ( cast(sprites[i], FlxNapeSprite).name == part1 )
- {
- nape_spr1 = sprites[i];
- // trace("first match found");
- }
- if ( cast(sprites[i], FlxNapeSprite).name == part2 )
- {
- nape_spr2 = sprites[i];
- // trace("second match found");
- }
- }
- if (nape_spr1 != null && nape_spr2 != null)
- {
- // trace("we can tie them both");
- // return ;
- var anchor1:Vec2 = new Vec2(0, 0);//nail on head location
- var anchor2:Vec2 = new Vec2(0, 0);//nail on torso location
- if (OffsetAnchor1 != null)
- {
- if(nape_spr1.facing==FlxObject.RIGHT)
- anchor1.x += OffsetAnchor1.x * nape_spr1.scale.x;
- else
- anchor1.x -= OffsetAnchor1.x * nape_spr1.scale.x;
- anchor1.y += OffsetAnchor1.y*nape_spr1.scale.y;
- }
- if (OffsetAnchor2 != null)
- {
- if(nape_spr2.facing==FlxObject.RIGHT)
- anchor2.x += OffsetAnchor2.x * nape_spr2.scale.x;
- else
- anchor2.x -= OffsetAnchor2.x * nape_spr2.scale.x;
- anchor2.y += OffsetAnchor2.y*nape_spr2.scale.y;
- }
- if (nape_spr2.name.indexOf("body") >= 0)
- {
- anchor2.y = -(nape_spr2.height*nape_spr2.scale.y) / 2;
- }
- var constrain:PivotJoint = new PivotJoint(nape_spr1.body, nape_spr2.body, anchor1, anchor2);
- constrain.space = FlxNapeSpace.space;
- if (nape_spr1.name.indexOf("head") >= 0)
- {
- var angle_constrain:AngleJoint = new AngleJoint(nape_spr1.body, nape_spr2.body, -0.6, 0.6);
- angle_constrain.space = FlxNapeSpace.space;
- trace("angle constraint");
- }
- }
- }
- public var sprites:Array<FlxNapeSprite>;
- function cloneSlot(partName:String):Void
- {
- var nape_spr:FlxNapeSprite;
- //create physics body with default size
- nape_spr = new FlxNapeSprite(22, 22 );
- //default location
- nape_spr.x = spine.x;
- nape_spr.y = spine.y;
- //give the body a sprite, which is the part -- this sprite will be scaled properly
- nape_spr.loadGraphicFromSprite(spine.getRegionSprite(partName), true );
- //resize the body, using the sprite dimensions we just grabbed
- if(partName.indexOf("head") >= 0)
- nape_spr.createCircularBody((nape_spr.width*nape_spr.scale.x)*0.2+(nape_spr.height*nape_spr.scale.y)*0.2);
- else
- 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
- //collision flags
- if(partName.indexOf("head") >= 0)
- nape_spr.body.cbTypes.add(groupHead);
- else if(partName.indexOf("body") >= 0)
- nape_spr.body.cbTypes.add(groupTorso);
- else
- nape_spr.body.cbTypes.add(groupArms);
- var listener:PreListener;
- //head ignores torso :D
- listener = new PreListener(InteractionType.COLLISION, groupHead, groupTorso, ignoreCollision, 0, true);
- listener.space = FlxNapeSpace.space;
- //make it face left or right correctly
- if (spine.facing == FlxObject.LEFT)
- {
- nape_spr.flipX = true;
- }
- else
- {
- nape_spr.flipX = false;
- }
- //move the new physics objects where the original image is at
- var slot:Slot = spine.skeleton.findSlot(partName);
- var regionAttachment:RegionAttachment = null;
- if (Std.is(slot.attachment, RegionAttachment))
- regionAttachment = cast slot.attachment;
- var wrapper:FlxSprite = spine.getSprite(regionAttachment);
- nape_spr.body.position.x= wrapper.x+(wrapper.width*wrapper.scale.x)/2;
- nape_spr.body.position.y = wrapper.y+(wrapper.height*wrapper.scale.y)/2;
- //so it bounces less
- nape_spr.setBodyMaterial(0.5, 0.2, 0.5);
- //so we can grab it
- shooter.registerPhysSprite(nape_spr);
- //to find it later
- nape_spr.name = partName;
- sprites.push(nape_spr);
- //after everything is set, finally add it to stage!
- add(nape_spr);
- trace(nape_spr.name);
- }
- function ignoreCollision(cb:PreCallback):PreFlag
- {
- return PreFlag.IGNORE;
- }
- function initSpace():Void
- {
- FlxNapeSpace.init();
- FlxNapeSpace.drawDebug = true;
- FlxNapeSpace.createWalls(0,0,FlxG.width, FlxG.height - 30);
- FlxNapeSpace.space.gravity.setxy(0, 1000);
- var botomDebug:FlxSprite = new FlxSprite();
- botomDebug.makeGraphic(FlxG.width, 30);
- botomDebug.y = FlxG.height - 30;
- add(botomDebug);
- }
- override public function update(elapsed:Float):Void
- {
- super.update(elapsed);
- // toggle debug display
- if (FlxG.keys.justPressed.SPACE)
- FlxG.debugger.drawDebug = !FlxG.debugger.drawDebug;
- if (FlxG.keys.justPressed.ENTER)
- cloneAllParts();
- if (FlxG.keys.justPressed.R)
- FlxG.resetState();
- if (FlxG.keys.justPressed.G)
- FlxNapeSpace.drawDebug = !FlxNapeSpace.drawDebug;
- // movement
- if (FlxG.keys.anyPressed([W, UP]))
- {
- spine_ent.y -= 500 * elapsed;
- }
- if (FlxG.keys.anyPressed([S, DOWN]))
- {
- spine_ent.y += 500 * elapsed;
- }
- if (FlxG.keys.anyPressed([D, RIGHT]))
- {
- spine_ent.x += 500 * elapsed;
- spine_ent.facing = FlxObject.RIGHT;
- }
- if (FlxG.keys.anyPressed([A, LEFT]))
- {
- spine_ent.x -= 500 * elapsed;
- spine_ent.facing = FlxObject.LEFT;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement