1 package cz.cuni.amis.pogamut.emohawk.agent.module.sensomotoric;
2
3 import cz.cuni.amis.pogamut.base.agent.module.SensomotoricModule;
4 import cz.cuni.amis.pogamut.base.communication.command.ICommandListener;
5 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
6 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectListener;
7 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
8 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Initialize;
9 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.PlayAnimation;
10 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
11 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
12 import java.util.HashSet;
13 import java.util.logging.Logger;
14
15
16
17
18
19 public class Animations extends SensomotoricModule<UT2004Bot> {
20
21
22 AnimationCharacterMapping animCharMapping;
23
24 CharacterType currentCharacterType = null;
25
26 Self self;
27
28
29
30
31
32
33 public AnimType getPlayerAnimation(Player player) {
34 return animCharMapping.getAnimTypeFromString(player.getAnim());
35 }
36
37
38
39
40
41
42 public boolean hasAnim(AnimType anim) {
43 if (currentCharacterType != null)
44 return animCharMapping.hasAnim(anim, currentCharacterType);
45 else
46 return false;
47 }
48
49
50
51
52
53
54 public boolean hasAnim(AnimType anim, CharacterType charType) {
55 return animCharMapping.hasAnim(anim, charType);
56 }
57
58
59
60
61
62 public AnimType getCurrentPlayedAnimation() {
63 if (self == null) return AnimType.NONE;
64 return animCharMapping.getAnimTypeFromString(self.getAnim());
65 }
66
67
68
69
70
71
72 public HashSet<AnimType> getAvailableAnimations(CharacterType charType) {
73 return animCharMapping.getAvailableAnimations(charType);
74 }
75
76
77
78
79
80
81 public HashSet<AnimType> getAvailableAnimations() {
82 if (currentCharacterType != null)
83 return animCharMapping.getAvailableAnimations(currentCharacterType);
84
85 return new HashSet();
86 }
87
88
89
90
91
92
93
94
95 public void playAnimation(AnimType animation, boolean bLoop) {
96 if (!animCharMapping.hasAnim(animation, currentCharacterType)) {
97 String outString = "Character " + currentCharacterType + " does not own " + animation.getUnrealID() + " animation.";
98 this.getLog().warning(outString);
99 }
100 act.act(new PlayAnimation(animation.getUnrealID(), bLoop));
101 }
102
103
104
105
106
107
108
109
110
111
112 public void playAnimationSafe(AnimType animation, boolean bLoop) {
113 if (!animCharMapping.hasAnim(animation, currentCharacterType)) {
114 String outString = "Character " + currentCharacterType + " does not own " + animation.getUnrealID() + " animation.";
115 this.getLog().warning(outString);
116 }
117 AnimType current = getCurrentPlayedAnimation();
118 if (current != animation)
119 act.act(new PlayAnimation(animation.getUnrealID(), bLoop));
120 }
121
122
123
124
125
126
127 public void playAnimation(AnimType animation) {
128 if (!animCharMapping.hasAnim(animation, currentCharacterType)) {
129 String outString = "Character " + currentCharacterType + " does not own " + animation.getUnrealID() + " animation.";
130 this.getLog().warning(outString);
131 }
132 act.act(new PlayAnimation(animation.getUnrealID(), Boolean.FALSE));
133 }
134
135
136
137
138
139 public void stopAnimation() {
140
141 }
142
143
144
145
146
147 public CharacterType getCurrentCharacter() {
148 return currentCharacterType;
149 }
150
151
152
153
154
155 ICommandListener<Initialize> initCommandListener = new ICommandListener<Initialize>() {
156 @Override
157 public void notify(Initialize event) {
158 if (event.getClassName() != null && !event.getClassName().isEmpty())
159 for (CharacterType charType : CharacterType.values()) {
160 if (charType.getUE2Class().contains(event.getClassName()) || charType.getUDKClass().contains(event.getClassName())) {
161 currentCharacterType = charType;
162 }
163 }
164
165 }
166 };
167
168
169
170
171 IWorldObjectListener<Self> storeSelfListener = new IWorldObjectListener<Self>() {
172 @Override
173 public void notify(IWorldObjectEvent<Self> event) {
174 if (self == null) {
175 self = event.getObject();
176 worldView.removeObjectListener(Self.class, this);
177 storeSelfListener = null;
178 }
179 }
180 };
181
182 public Animations(UT2004Bot agent, Logger log) {
183 super(agent, log);
184
185 animCharMapping = new AnimationCharacterMapping();
186
187 act.addCommandListener(Initialize.class, initCommandListener);
188 worldView.addObjectListener(Self.class, storeSelfListener);
189 }
190
191 public Animations(UT2004Bot agent) {
192 super(agent);
193
194 animCharMapping = new AnimationCharacterMapping();
195
196 act.addCommandListener(Initialize.class, initCommandListener);
197 worldView.addObjectListener(Self.class, storeSelfListener);
198 }
199
200 }