/
D_Attachments.java
111 lines (93 loc) · 3.3 KB
/
D_Attachments.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.esotericsoftware.spine.workshop;
import com.esotericsoftware.spine.Animation;
import com.esotericsoftware.spine.Attachment;
import com.esotericsoftware.spine.Bone;
import com.esotericsoftware.spine.Skeleton;
import com.esotericsoftware.spine.SkeletonData;
import com.esotericsoftware.spine.SkeletonJson;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
public class D_Attachments extends ApplicationAdapter {
SpriteBatch batch;
ShapeRenderer renderer;
TextureAtlas atlas;
Skeleton skeleton;
Animation animation;
float time;
Bone root;
public void create () {
batch = new SpriteBatch();
renderer = new ShapeRenderer();
atlas = new TextureAtlas(Gdx.files.internal("goblins/goblins.atlas"));
SkeletonJson json = new SkeletonJson(atlas);
SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("goblins/goblins-skeleton.json"));
animation = json.readAnimation(Gdx.files.internal("goblins/goblins-walk.json"), skeletonData);
skeleton = new Skeleton(skeletonData);
root = skeleton.getRootBone();
root.setX(220);
root.setY(20);
skeleton.setSkin("goblin");
skeleton.setSlotsToBindPose(); // Without this, the attachments from the skin won't be shown! See setSkin javadocs.
skeleton.updateWorldTransform();
Gdx.input.setInputProcessor(new InputAdapter() {
public boolean keyDown (int keycode) {
switch (keycode) {
case Keys.NUM_1:
skeleton.setAttachment("left hand item", "dagger");
break;
case Keys.NUM_2:
skeleton.setAttachment("left hand item", "spear");
break;
case Keys.NUM_3:
Attachment attachment = skeleton.getAttachment("right hand item", "dagger");
skeleton.findSlot("right hand item").setAttachment(attachment);
break;
case Keys.NUM_4:
skeleton.setAttachment("left hand item", null);
skeleton.findSlot("right hand item").setAttachment(null);
break;
case Keys.NUM_5:
skeleton.setSkin("goblingirl");
// setSlotsToBindPose isn't needed here, see setSkin javadocs.
break;
case Keys.NUM_6:
skeleton.setSkin("goblin");
// setSlotsToBindPose isn't needed here, see setSkin javadocs.
break;
}
return true;
}
});
}
public void render () {
time += Gdx.graphics.getDeltaTime();
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
animation.apply(skeleton, time, true);
skeleton.updateWorldTransform();
skeleton.draw(batch);
batch.end();
}
public void resize (int width, int height) {
batch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
renderer.setProjectionMatrix(batch.getProjectionMatrix());
}
public void dispose () {
atlas.dispose();
}
public static void main (String[] args) throws Exception {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "Attachments - Spine";
config.width = 640;
config.height = 480;
new LwjglApplication(new D_Attachments(), config);
}
}