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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
package ch.asynk.rustanddust.engine.gfx.animations;
import java.util.Random;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import ch.asynk.rustanddust.engine.gfx.Drawable;
import ch.asynk.rustanddust.engine.gfx.Animation;
public class DiceAnimation implements Animation, Drawable
{
private static final float DURATION = 0.7f;
private static final float DURATION_SCATTERING = 0.5f;
private static Random random = new Random();
private static Sprites dice;
private static Sound sound;
private static double sndId;
private static float volume;
private static int[][] rolls = new int[][]{
{ 25, 40, 55, 70, 85, 100, 115, 99, 83, 67, 51, 36, 37, 52, 67, 66, 65, 64 },
{ 58, 74, 59, 60, 45, 62, 78, 94, 109, 108, 123, 106, 89, 71, 70, 69, 68 },
{ 106, 121, 120, 103, 86, 70, 54, 37, 20, 19, 18, 34, 50, 51, 52, 69, 86, 103, 119, 128 },
{ 95, 79, 93, 92, 91, 90, 104, 103, 102, 85, 84, 67, 66, 65, 49, 32, 16, 0 },
{ 22, 39, 56, 73, 90, 107, 124, 128, 113, 98, 83, 68, 53, 38, 23, 0, 25, 42, 59, 76 },
{ 79, 78, 61, 76, 91, 106, 121, 120, 119, 102, 101, 84, 68, 52, 37, 38, 39, 40, 41, 58, 75, 74, 73, 72 },
};
private float x;
private float y;
private int frame;
private int[] roll;
private float elapsed;
private float duration;
private int dimension;
// public boolean stop;
public DiceAnimation(int dimension)
{
this.dimension = dimension;
}
public static void init(Texture texture, int cols, int rows, Sound s)
{
dice = new Sprites(texture, cols, rows);
sound = s;
sndId = -1;
}
public static void initSound(float v)
{
sndId = -1;
volume = v;
}
public static void free()
{
sound.dispose();
dice.dispose();
}
public void translate(float dx, float dy)
{
x += dx;
y += dy;
}
public float getX()
{
return x;
}
public float getY()
{
return y;
}
public int getWidth()
{
return dimension;
}
public int getHeight()
{
return dimension;
}
public void setPosition(float x, float y)
{
this.x = x;
this.y = y;
}
public void set(int result)
{
this.frame = 0;
this.elapsed = 0f;
this.roll = rolls[result - 1];
this.duration = DURATION + (DURATION_SCATTERING * random.nextFloat());
// this.stop = false;
}
public boolean isDone()
{
return (elapsed >= duration);
}
@Override
public void dispose()
{
}
@Override
public boolean animate(float delta)
{
// if (stop)
// return true;
elapsed += delta;
if (elapsed < duration) {
int idx = (int) (roll.length * elapsed / duration);
if (idx >= roll.length)
idx = (roll.length -1);
frame = roll[idx];
}
if (sndId == -1)
sndId = sound.play(volume);
return false;
}
@Override
public void draw(Batch batch)
{
batch.draw(dice.frames[frame], x, y, dimension, dimension);
}
@Override
public void drawDebug(ShapeRenderer debugShapes)
{
debugShapes.rect(x, y, dice.frames[frame].getRegionWidth(), dice.frames[frame].getRegionHeight());
}
}
|