Skip to content

Commit

Permalink
[haxe] Flixel support
Browse files Browse the repository at this point in the history
  • Loading branch information
davidetan committed Jul 16, 2024
1 parent 59bcffc commit 388d56e
Show file tree
Hide file tree
Showing 19 changed files with 4,657 additions and 6 deletions.
3,672 changes: 3,672 additions & 0 deletions spine-haxe/example/assets/spineboy-ess.json

Large diffs are not rendered by default.

Binary file added spine-haxe/example/assets/spineboy-ess.skel
Binary file not shown.
95 changes: 95 additions & 0 deletions spine-haxe/example/assets/spineboy-pma.atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
spineboy-pma.png
size: 1024, 256
filter: Linear, Linear
pma: true
scale: 0.5
crosshair
bounds: 352, 7, 45, 45
eye-indifferent
bounds: 862, 105, 47, 45
eye-surprised
bounds: 505, 79, 47, 45
front-bracer
bounds: 826, 66, 29, 40
front-fist-closed
bounds: 786, 65, 38, 41
front-fist-open
bounds: 710, 51, 43, 44
rotate: 90
front-foot
bounds: 210, 6, 63, 35
front-shin
bounds: 665, 128, 41, 92
rotate: 90
front-thigh
bounds: 2, 2, 23, 56
rotate: 90
front-upper-arm
bounds: 250, 205, 23, 49
goggles
bounds: 665, 171, 131, 83
gun
bounds: 798, 152, 105, 102
head
bounds: 2, 27, 136, 149
hoverboard-board
bounds: 2, 178, 246, 76
hoverboard-thruster
bounds: 722, 96, 30, 32
rotate: 90
hoverglow-small
bounds: 275, 81, 137, 38
mouth-grind
bounds: 614, 97, 47, 30
mouth-oooo
bounds: 612, 65, 47, 30
mouth-smile
bounds: 661, 64, 47, 30
muzzle-glow
bounds: 382, 54, 25, 25
muzzle-ring
bounds: 275, 54, 25, 105
rotate: 90
muzzle01
bounds: 911, 95, 67, 40
rotate: 90
muzzle02
bounds: 792, 108, 68, 42
muzzle03
bounds: 956, 171, 83, 53
rotate: 90
muzzle04
bounds: 275, 7, 75, 45
muzzle05
bounds: 140, 3, 68, 38
neck
bounds: 250, 182, 18, 21
portal-bg
bounds: 140, 43, 133, 133
portal-flare1
bounds: 554, 65, 56, 30
portal-flare2
bounds: 759, 112, 57, 31
rotate: 90
portal-flare3
bounds: 554, 97, 58, 30
portal-shade
bounds: 275, 121, 133, 133
portal-streaks1
bounds: 410, 126, 126, 128
portal-streaks2
bounds: 538, 129, 125, 125
rear-bracer
bounds: 857, 67, 28, 36
rear-foot
bounds: 663, 96, 57, 30
rear-shin
bounds: 414, 86, 38, 89
rotate: 90
rear-thigh
bounds: 756, 63, 28, 47
rear-upper-arm
bounds: 60, 5, 20, 44
rotate: 90
torso
bounds: 905, 164, 49, 90
Binary file added spine-haxe/example/assets/spineboy-pma.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions spine-haxe/example/assets/spineboy-run.atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
spineboy-run.png
size: 1177, 685
filter: Linear, Linear
pma: true
spineboy-pro-run
index: 8
bounds: 2, 369, 302, 314
offsets: 12, 22, 316, 341
spineboy-pro-run
index: 9
bounds: 2, 42, 278, 325
offsets: 30, 2, 316, 341
spineboy-pro-run
index: 0
bounds: 306, 438, 245, 320
offsets: 67, 1, 316, 341
rotate: 90
spineboy-pro-run
index: 3
bounds: 628, 441, 242, 319
offsets: 12, 21, 316, 341
rotate: 90
spineboy-pro-run
index: 2
bounds: 949, 377, 226, 306
offsets: 34, 26, 316, 341
spineboy-pro-run
index: 4
bounds: 306, 196, 240, 319
offsets: 3, 5, 316, 341
rotate: 90
spineboy-pro-run
index: 1
bounds: 282, 2, 192, 318
offsets: 69, 3, 316, 341
rotate: 90
spineboy-pro-run
index: 5
bounds: 628, 198, 241, 316
offsets: 8, 3, 316, 341
rotate: 90
spineboy-pro-run
index: 7
bounds: 946, 79, 226, 296
offsets: 37, 33, 316, 341
spineboy-pro-run
index: 6
bounds: 627, 2, 194, 315
offsets: 68, 5, 316, 341
rotate: 90
Binary file added spine-haxe/example/assets/spineboy-run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spine-haxe/example/assets/square.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spine-haxe/example/assets/square2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
183 changes: 183 additions & 0 deletions spine-haxe/example/src/FlixelState.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package;

import spine.Skeleton;
import flixel.group.FlxSpriteGroup;
import flixel.group.FlxGroup;
import flixel.FlxSprite;
import flixel.graphics.FlxGraphic;
import flixel.FlxStrip;
import spine.animation.AnimationStateData;
import openfl.Assets;
import spine.atlas.TextureAtlas;
import spine.SkeletonData;
import spine.flixel.SkeletonSprite;
import spine.flixel.FlixelTextureLoader;
import flixel.FlxG;
import flixel.FlxState;
import flixel.text.FlxText;
import openfl.Vector;

class FlixelState extends FlxState
{
var spineSprite:SkeletonSprite;
var sprite:FlxSprite;
var sprite2:FlxSprite;
var myText:FlxText;
var group:FlxSpriteGroup;
var justSetWalking = false;

var jumping = false;

var scale = 4;
var speed:Float;
override public function create():Void
{
FlxG.cameras.bgColor = 0xffa1b2b0;

// setting speed of spineboy (450 is the speed to not let him slide)
speed = 450 / scale;

// creating a group
group = new FlxSpriteGroup();
group.setPosition(50, 50);
add(group);

// creating the sprite to check overlapping
sprite = new FlxSprite();
sprite.loadGraphic(FlxGraphic.fromRectangle(150, 100, 0xff8d008d));
group.add(sprite);

// creating the text to display overlapping state
myText = new FlxText(0, 25, 150, "", 16);
myText.alignment = CENTER;
group.add(myText);

// creating a sprite for the floor
var floor = new FlxSprite();
floor.loadGraphic(FlxGraphic.fromRectangle(FlxG.width, FlxG.height - 100, 0xff822f02));
floor.y = FlxG.height - 100;
add(floor);

// loading spineboy
var atlas = new TextureAtlas(Assets.getText("assets/spineboy.atlas"), new FlixelTextureLoader("assets/spineboy.atlas"));
var skeletondata = SkeletonData.from(Assets.getText("assets/spineboy-pro.json"), atlas, 1/scale);
var animationStateData = new AnimationStateData(skeletondata);
spineSprite = new SkeletonSprite(skeletondata, animationStateData);

// positioning spineboy
spineSprite.setPosition(.5 * FlxG.width, .5 * FlxG.height);

// setting mix times
animationStateData.defaultMix = 0.5;
animationStateData.setMixByName("idle", "walk", 0.1);
animationStateData.setMixByName("walk", "idle", 0.1);
animationStateData.setMixByName("idle", "idle-turn", 0.05);
animationStateData.setMixByName("idle-turn", "idle", 0.05);
animationStateData.setMixByName("idle-turn", "walk", 0.3);
animationStateData.setMixByName("idle", "jump", 0);
animationStateData.setMixByName("jump", "idle", 0.05);
animationStateData.setMixByName("jump", "walk", 0.05);
animationStateData.setMixByName("walk", "jump", 0.05);

// setting idle animation
spineSprite.state.setAnimationByName(0, "idle", true);

// setting y offset function to move object body while jumping
var hip = spineSprite.skeleton.findBone("hip");
var initialY = 0.;
var initialOffsetY = 0.;
spineSprite.state.onStart.add(entry -> {
if (entry.animation.name == "jump") {
initialY = spineSprite.y;
initialOffsetY = spineSprite.offsetY;
}
});
spineSprite.state.onComplete.add(entry -> {
if (entry.animation.name == "jump") {
jumping = false;
spineSprite.y = initialY;
spineSprite.offsetY = initialOffsetY;
}
});
var diff = .0;
spineSprite.afterUpdateWorldTransforms = spineSprite -> {
if (jumping) {
diff -= hip.y;
spineSprite.offsetY -= diff;
spineSprite.y += diff;
}
diff = hip.y;
}

// adding spineboy to the stage
add(spineSprite);

// debug ui
// FlxG.debugger.visible = true;
// FlxG.debugger.drawDebug = true;
// FlxG.log.redirectTraces = true;

// FlxG.debugger.track(spineSprite);
// FlxG.watch.add(spineSprite, "width");
// FlxG.watch.add(spineSprite, "offsetY");
// FlxG.watch.add(spineSprite, "y");
// FlxG.watch.add(this, "jumping");
super.create();
}

var justSetIdle = true;
override public function update(elapsed:Float):Void
{
if (FlxG.overlap(spineSprite, group)) {
myText.text = "Overlapping";
} else {
myText.text = "Non overlapping";
}

if (!jumping && FlxG.keys.anyJustPressed([SPACE])) {
spineSprite.state.setAnimationByName(0, "jump", false);
jumping = true;
justSetIdle = false;
justSetWalking = false;
}

if (FlxG.keys.anyJustPressed([J])) {
spineSprite.antialiasing = !spineSprite.antialiasing;
}

if (FlxG.keys.anyPressed([RIGHT, LEFT])) {
justSetIdle = false;
var flipped = false;
var deltaX;
if (FlxG.keys.anyPressed([RIGHT])) {
if (spineSprite.flipX == true) flipped = true;
spineSprite.flipX = false;
}
if (FlxG.keys.anyPressed([LEFT])) {
if (spineSprite.flipX == false) flipped = true;
spineSprite.flipX = true;
}

deltaX = (spineSprite.flipX == false ? 1 : -1) * speed * elapsed;
spineSprite.x += deltaX;

if (!jumping && !justSetWalking) {
justSetWalking = true;
if (flipped) {
spineSprite.state.setAnimationByName(0, "idle-turn", false);
spineSprite.state.addAnimationByName(0, "walk", true, 0);
} else {
spineSprite.state.setAnimationByName(0, "walk", true);
}
}

} else if (!jumping && !justSetIdle) {
justSetWalking = false;
justSetIdle = true;
spineSprite.state.setAnimationByName(0, "idle", true);
}


super.update(elapsed);
}
}
Loading

0 comments on commit 388d56e

Please sign in to comment.