aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/olacraft/modelAnimator/BoneSet.java
blob: 0e3ce2cd5d70cb273557907f9be9e2e680e2107f (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package ru.olamedia.olacraft.modelAnimator;

import java.util.Iterator;
import java.util.Random;

public class BoneSet implements ISkeletonNode, Iterable<Bone> {

	private class BonesIterator implements Iterator<Bone> {
		private BoneSet set;
		private Iterator<Bone> childIterator = null;
		private int current = 0;

		public BonesIterator(BoneSet boneSet) {
			set = boneSet;
		}

		@Override
		public boolean hasNext() {
			if (null == childIterator) {
				return current < set.getChildrenCount();
			} else {
				return childIterator.hasNext() || current < set.getChildrenCount();
			}
		}

		private boolean nextIsChild;

		@Override
		public Bone next() {
			if (null == childIterator) {
				ISkeletonNode node = set.getChild(current);
				if (node instanceof Bone) {
					Bone b = (Bone) node;
					current++;
					return b;
				} else if (node instanceof BoneSet) {
					childIterator = ((BoneSet) node).iterator();
					current++;
				}
			}
			if (null != childIterator) {
				nextIsChild = childIterator.hasNext();
				if (nextIsChild) {
					Bone b = childIterator.next();
					return b;
				}
				if (!nextIsChild) {
					childIterator = null;
				}
			}
			if (current < set.getChildrenCount()) {
				return next();
			}
			return null;
		}

		@Override
		public void remove() {

		}

	}

	public BoneSet(int bonesCount) {
		nodes = new ISkeletonNode[bonesCount];
		for (int i = 0; i < bonesCount; i++) {
			// bones[i] = new Bone();
		}
	}

	private ISkeletonNode[] nodes;
	private String name;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int getChildrenCount() {
		return nodes.length;
	}

	public ISkeletonNode getChild(int i) {
		return nodes[i];
	}

	@Override
	public void setSpeed(float speed) {
		for (ISkeletonNode b : nodes) {
			b.setSpeed(speed);
		}
	}

	@Override
	public void copyOrientation(ISkeletonNode bs) {
		for (int i = 0; i < nodes.length; i++) {
			nodes[i].copyOrientation(bs.getChild(i));
		}
	}

	@Override
	public void setDelta(ISkeletonNode first, ISkeletonNode second, float delta) {
		for (int i = 0; i < nodes.length; i++) {
			nodes[i].setDelta(first.getChild(i), second.getChild(i), delta);
		}
	}

	@Override
	public void reset() {
		for (int i = 0; i < nodes.length; i++) {
			nodes[i].reset();
		}
	}

	@Override
	public void randomize(Random rand, Random prev, float delta) {
		for (int i = 0; i < nodes.length; i++) {
			nodes[i].randomize(rand, prev, delta);
		}
	}

	@Override
	public void setChild(int i, ISkeletonNode node) {
		nodes[i] = node;
	}

	@Override
	public Iterator<Bone> iterator() {
		return new BonesIterator(this);
	}

	public int getBoneCount() {
		int n = 0;
		Iterator<Bone> it = iterator();
		while (it.hasNext()) {
			it.next();
			n++;
		}
		return n;
	}

	public Bone getBone(int i) {
		int j = 0;
		Iterator<Bone> it = iterator();
		Bone b = null;
		while (it.hasNext() && j <= i) {
			b = it.next();
			j++;
		}
		return b;
	}
}