1 package cz.cuni.amis.pogamut.ut2004.communication.translator.itemdescriptor;
2
3 import java.util.HashMap;
4
5 import com.google.inject.Inject;
6
7 import cz.cuni.amis.pogamut.base.communication.exception.CommunicationException;
8 import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
9 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemType;
10 import cz.cuni.amis.pogamut.ut2004.communication.messages.ItemTypeTranslator;
11 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ItemCategory;
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 @AgentScoped
39 public class ItemTranslator {
40
41 private HashMap<ItemType, ItemDescriptor> descriptors = new HashMap<ItemType, ItemDescriptor>();
42
43 private HashMap<ItemType, GeneralDescriptor> userDescriptors = new HashMap<ItemType, GeneralDescriptor>();
44
45
46
47 private final IDescriptorFactory<WeaponDescriptor> weaponDescriptorFactory;
48 private final IDescriptorFactory<AdrenalineDescriptor> adrenalineDescriptorFactory = new AdrenalineDescriptorFactory();
49 private final IDescriptorFactory<ShieldDescriptor> shieldDescriptorFactory = new ShieldDescriptorFactory();
50 private final IDescriptorFactory<HealthDescriptor> healthDescriptorFactory = new HealthDescriptorFactory();
51 private final IDescriptorFactory<OtherDescriptor> otherDescriptorFactory = new OtherDescriptorFactory();
52 private final IDescriptorFactory<ArmorDescriptor> armorDescriptorFactory = new ArmorDescriptorFactory();
53 private final IDescriptorFactory<AmmoDescriptor> ammoDescriptorFactory = new AmmoDescriptorFactory();
54
55 private final ItemTypeTranslator itemTypeTranslator;
56
57 @Inject
58 public ItemTranslator(ItemTypeTranslator translator) {
59 this.itemTypeTranslator = translator;
60 this.weaponDescriptorFactory = new WeaponDescriptorFactory(
61 itemTypeTranslator);
62 }
63
64 public ItemType[] getItemTypes() {
65 return descriptors.values().toArray(new ItemType[0]);
66 }
67
68 public ItemDescriptor getDescriptor(ItemTyped msg) {
69 return getDescriptor(msg.getType());
70 }
71
72
73
74
75
76
77
78
79
80 public ItemDescriptor getDescriptor(ItemType type) {
81
82 if (userDescriptors.containsKey(type))
83 return userDescriptors.get(type);
84 return descriptors.get(type);
85 }
86
87
88
89
90
91
92
93 public ItemDescriptor getDefaultDescriptor(ItemType type) {
94 return descriptors.get(type);
95 }
96
97
98
99
100
101
102 public void addCustomUserDescriptor(GeneralDescriptor userDescriptor) {
103 userDescriptors.put(userDescriptor.getPickupType(), userDescriptor);
104 }
105
106
107
108
109
110
111 public ItemDescriptor createDescriptor(ItemCategory message) {
112 ItemDescriptor result = null;
113 switch (message.getType().getCategory()) {
114 case AMMO:
115 descriptors.put(message.getType(),
116 result = ammoDescriptorFactory.getNewDescriptor(message));
117 break;
118 case ARMOR:
119 descriptors.put(message.getType(),
120 result = armorDescriptorFactory.getNewDescriptor(message));
121 break;
122 case OTHER:
123 descriptors.put(message.getType(),
124 result = otherDescriptorFactory.getNewDescriptor(message));
125 break;
126 case HEALTH:
127 descriptors.put(message.getType(),
128 result = healthDescriptorFactory.getNewDescriptor(message));
129 break;
130 case SHIELD:
131 descriptors.put(message.getType(),
132 result = shieldDescriptorFactory.getNewDescriptor(message));
133 break;
134 case ADRENALINE:
135 descriptors.put(
136 message.getType(),
137 result = adrenalineDescriptorFactory
138 .getNewDescriptor(message));
139 break;
140 case WEAPON:
141 descriptors.put(message.getType(),
142 result = weaponDescriptorFactory.getNewDescriptor(message));
143 break;
144 default:
145 throw new CommunicationException(
146 "should not reach here - new ItemType.Category has been added and not handled inside the ItemTranslator, item type = "
147 + message.getType(), this);
148 }
149 return result;
150 }
151 }