| skipped 41 lines |
42 | 42 | | } |
43 | 43 | | |
44 | 44 | | void DroneBatteryDecorator::charge(double dt) { |
45 | | - | if (currentCharge < maxCharge) { |
46 | | - | timeElapsed += dt; |
47 | | - | if (timeElapsed >= movingDecreaseTime) { |
48 | | - | currentCharge += 2; |
49 | | - | timeElapsed -= movingDecreaseTime; |
| 45 | + | if (malfunctionedStationTime > 0) { |
| 46 | + | // Recharge station is malfunctioned, so do not charge |
| 47 | + | // until the station is no longer malfunctioned. |
| 48 | + | malfunctionedStationTime -= dt; |
| 49 | + | if (malfunctionedStationTime <= 0) { |
| 50 | + | std::string message = "Station charging " + |
| 51 | + | getName() + " has been fixed."; |
| 52 | + | sub->notifyObservers(message); |
| 53 | + | malfunctionedStationTime = 0; |
| 54 | + | } |
| 55 | + | } |
50 | 56 | | |
51 | | - | if (currentCharge >= maxCharge) { |
52 | | - | // Drone has reached max charge |
53 | | - | currentCharge = maxCharge; |
| 57 | + | if (currentCharge >= maxCharge) { |
| 58 | + | // Drone can not charge to be higher than max charge |
| 59 | + | return; |
| 60 | + | } |
| 61 | + | |
| 62 | + | timeElapsed += dt; |
| 63 | + | if (timeElapsed >= movingDecreaseTime) { |
| 64 | + | currentCharge += 2; |
| 65 | + | timeElapsed -= movingDecreaseTime; |
54 | 66 | | |
55 | | - | std::string message = getName() + " is now fully charged"; |
56 | | - | sub->notifyObservers(message); |
57 | | - | } |
| 67 | + | if (currentCharge >= maxCharge) { |
| 68 | + | // Drone has reached max charge |
| 69 | + | currentCharge = maxCharge; |
| 70 | + | |
| 71 | + | std::string message = getName() + " is now fully charged"; |
| 72 | + | sub->notifyObservers(message); |
58 | 73 | | } |
59 | 74 | | } |
60 | 75 | | } |
| skipped 135 lines |
196 | 211 | | |
197 | 212 | | if (isAtRechargeStation()) { |
198 | 213 | | // Drone has reached the recharge station |
199 | | - | delete toRechargeStation; |
200 | | - | toRechargeStation = nullptr; |
201 | | - | std::string message = getName() + " arrived at recharge station"; |
202 | | - | sub->notifyObservers(message); |
| 214 | + | arriveAtRechargeStation(); |
203 | 215 | | } |
204 | 216 | | } else if (droneReady) { |
205 | 217 | | sub->update(dt); |
| skipped 86 lines |
292 | 304 | | getModel()->functionalDrones.end()); |
293 | 305 | | } |
294 | 306 | | |
| 307 | + | void DroneBatteryDecorator::arriveAtRechargeStation() { |
| 308 | + | delete toRechargeStation; |
| 309 | + | toRechargeStation = nullptr; |
| 310 | + | std::string message = getName() + " arrived at recharge station"; |
| 311 | + | sub->notifyObservers(message); |
| 312 | + | |
| 313 | + | // Check to see if the recharge has malfunctioned (10% chance |
| 314 | + | // to malfunction.) If the station has malfunctioned, wait 10 |
| 315 | + | // seconds for it to fix |
| 316 | + | |
| 317 | + | int randomNumber = std::rand(); |
| 318 | + | if (randomNumber % 10 == 0) { |
| 319 | + | // Drone has malfunctioned |
| 320 | + | malfunctionedStationTime = 10; |
| 321 | + | |
| 322 | + | std::string firstHalfMessage = "Station has malfunctioned "; |
| 323 | + | std::string secondHalfMessage = "attempting to recharge" + |
| 324 | + | getName() + |
| 325 | + | ". Please wait 10 seconds for it to be fixed."; |
| 326 | + | |
| 327 | + | std::string message = firstHalfMessage + secondHalfMessage; |
| 328 | + | sub->notifyObservers(message); |
| 329 | + | } |
| 330 | + | |
| 331 | + | |
| 332 | + | } |