1 package cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor;
2
3 import java.util.HashMap;
4
5 import cz.cuni.amis.pogamut.base.communication.exception.CommunicationException;
6 import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
7 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
8 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ItemCategory;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 @AgentScoped
29 public class ItemTranslator {
30
31 private HashMap<ItemType, ItemDescriptor> descriptors = new HashMap<ItemType, ItemDescriptor>();
32
33 private HashMap<ItemType, GeneralDescriptor> userDescriptors = new HashMap<ItemType, GeneralDescriptor>();
34
35 public ItemTranslator() {
36 }
37
38 public ItemType[] getItemTypes() {
39 return descriptors.values().toArray(new ItemType[0]);
40 }
41
42 public ItemDescriptor getDescriptor(ItemTyped msg) {
43 return getDescriptor(msg.getType());
44 }
45
46
47
48
49
50
51
52
53
54 public ItemDescriptor getDescriptor(ItemType type) {
55
56 if (userDescriptors.containsKey(type))
57 return userDescriptors.get(type);
58 return descriptors.get(type);
59 }
60
61
62
63
64
65
66
67
68 public ItemDescriptor getDefaultDescriptor(ItemType type) {
69 return descriptors.get(type);
70 }
71
72
73
74
75
76
77 public void addCustomUserDescriptor(GeneralDescriptor userDescriptor) {
78 userDescriptors.put(userDescriptor.getPickupType(), userDescriptor);
79 }
80
81
82
83
84
85
86 public ItemDescriptor createDescriptor(ItemCategory message) {
87 ItemDescriptor result = null;
88 switch (message.getType().getCategory()) {
89 case AMMO:
90 descriptors.put(message.getType(), result = AmmoDescriptorFactory.getInstance().getNewDescriptor(message));
91 break;
92 case ARMOR:
93 descriptors.put(message.getType(), result = ArmorDescriptorFactory.getInstance().getNewDescriptor(message));
94 break;
95 case OTHER:
96 descriptors.put(message.getType(), result = OtherDescriptorFactory.getInstance().getNewDescriptor(message));
97 break;
98 case HEALTH:
99 descriptors.put(message.getType(), result = HealthDescriptorFactory.getInstance().getNewDescriptor(message));
100 break;
101 case SHIELD:
102 descriptors.put(message.getType(), result = ShieldDescriptorFactory.getInstance().getNewDescriptor(message));
103 break;
104 case ADRENALINE:
105 descriptors.put(message.getType(), result = AdrenalineDescriptorFactory.getInstance().getNewDescriptor(message));
106 break;
107 case WEAPON:
108 descriptors.put(message.getType(), result = WeaponDescriptorFactory.getInstance().getNewDescriptor(message));
109 break;
110 default:
111 throw new CommunicationException("should not reach here - new ItemType.Category has been added and not handled inside the ItemTranslator, item type = " + message.getType(), this);
112 }
113 return result;
114 }
115 }