summaryrefslogtreecommitdiff
path: root/cop.hpp
blob: f6f821dd0e93d4c9c349352b54ee4b3b46050601 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
39
40
41
42
43
44
45
46
47
48
#include <typeinfo>
#include "robber.h"

// Programmer: Brett Weiland
// Date: 4/30/23
// File: cop.hpp
// Assignment: SP-B-finalProject
// Purpose: all the functions specific to how cops interact and move.

template <class T> char cop<T>::getIcon() const { return 'p'; }
template <class T> unsigned int cop<T>::get_drawPriority() const { return 1; }
template <class T> unsigned int cop<T>::get_robbersCaught() const { return robbers_caught; }
template <class T> void cop<T>::robbersReleased() { robbers_caught = 0; }

template <class T> void cop<T>::move() {
  this->location.stepDirection(this->parentCity->randomDirection(this->location));
  for(gameObject *peer = this->parentCity->findObjsByLocation(this->location); 
      peer != NULL; peer = peer->get_top()) {

    const std::type_info &peer_type = typeid(*peer);

    if(peer_type == typeid(robber<T>)) { 
      robber<T> &rob = dynamic_cast<robber<T>&>(*peer);
      if(rob.is_active()) arrest(rob);
    }

    if(peer_type == typeid(T)) {
      this->inventory[this->inventory_qty] = dynamic_cast<T*>(peer);
      this->inventory[this->inventory_qty++]->pickup();
    }
  }
}

template <class T> void cop<T>::arrest(robber<T> &offender) { 
  T **crinimal_stash = offender.getArrested();
  size_t loot_i;
  for(loot_i = 0; crinimal_stash[loot_i] != NULL; loot_i++) { //assumes robbers 
    this->inventory[this->inventory_qty + loot_i] = crinimal_stash[loot_i];
    crinimal_stash[loot_i] = NULL;
  }
  this->inventory_qty += loot_i;
  robbers_caught++;
}

template <class T> void cop<T>::takeBribe(T *bribe) {
  this->inventory[this->inventory_qty++] = bribe;
  this->parentCity->freeAllRobbers();
}