View Javadoc

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   * Module wrapping animation playing.
17   * @author knight
18   */
19  public class Animations extends SensomotoricModule<UT2004Bot> {
20  
21      /** Object providing animation character mapping */
22      AnimationCharacterMapping animCharMapping;
23      /** Current intialized character type */
24      CharacterType currentCharacterType = null;
25      /** Point to self object - provides us with info about current played animation */
26      Self self;
27  
28      /**
29       * Returns current animation of input player.
30       * @param player
31       * @return
32       */
33      public AnimType getPlayerAnimation(Player player) {
34          return animCharMapping.getAnimTypeFromString(player.getAnim());
35      }
36      
37      /**
38       * Returns true if our current character can play input animation.
39       * @param anim
40       * @return 
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       * Returns true if input character can play input animation.
51       * @param anim
52       * @return 
53       */    
54      public boolean hasAnim(AnimType anim, CharacterType charType) {
55          return animCharMapping.hasAnim(anim, charType);
56      }
57      
58      /**
59       * Returns current bot anim thas is currently being played.
60       * @return
61       */
62      public AnimType getCurrentPlayedAnimation() {
63          if (self == null) return AnimType.NONE;        
64          return animCharMapping.getAnimTypeFromString(self.getAnim());         
65      }
66      
67      /**
68       * Returns all available animations for a character.
69       * @param charType
70       * @return
71       */
72      public HashSet<AnimType> getAvailableAnimations(CharacterType charType) {
73          return animCharMapping.getAvailableAnimations(charType);
74      }
75  
76      /**
77       * Returns all available animations for our character.
78       * @param charType
79       * @return
80       */
81      public HashSet<AnimType> getAvailableAnimations() {
82          if (currentCharacterType != null)
83              return animCharMapping.getAvailableAnimations(currentCharacterType);
84  
85          return new HashSet(); //empty hash set if our character not set.
86      }
87      /**
88       * Plays input animation on character. Animation will be looped when bLoop set to true.
89       * 
90       * Note that movement animations may override animation played this way!
91       * 
92       * @param animation
93       * @param bLoop
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      * Plays input animation on character only if the character IS NOT playing this animation
105      * allready. Animation will be looped when bLoop set to true.
106      * 
107      * Note that movement animations may override animation played this way!
108      * 
109      * @param animation
110      * @param bLoop
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      * Plays input animation on character once! Note that movement animations may override
124      * animation played this way!
125      * @param animation
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      * Stops animation and replaces it with "standard" one.
137      * NOTE: Not yet implemeted.
138      */
139     public void stopAnimation() {
140     	// TODO
141     }
142 
143     /**
144      * Returns current character type. Null if not recognized or not set!!
145      * @return
146      */
147     public CharacterType getCurrentCharacter() {
148         return currentCharacterType;
149     }
150 
151     /**
152      * Listener for Initialize command - we determine here the type of our character
153      * for playing animation purposes.
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             //TODO - unhook the listener - not needed anymore - only one init per agent. ?            
165         }
166     };
167 
168     /**
169      * Listener for Self object - stores Self object into our link.
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         //init mapping
185         animCharMapping = new AnimationCharacterMapping();
186         //init listeners
187         act.addCommandListener(Initialize.class, initCommandListener);
188         worldView.addObjectListener(Self.class, storeSelfListener);
189     }
190 
191     public Animations(UT2004Bot agent) {
192         super(agent);
193         //init mapping
194         animCharMapping = new AnimationCharacterMapping();
195         //init listeners
196         act.addCommandListener(Initialize.class, initCommandListener);
197         worldView.addObjectListener(Self.class, storeSelfListener);
198     }
199 
200 }