summaryrefslogtreecommitdiff
path: root/src/list.cpp
blob: cd69f8f198e6693671b60bb6549b9f5ecaec5b2b (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "list.hpp"

List::List(Window m) //Constructor
{
  int i;

  for(i=0;i<10;i++)
  {
    Creature X(m,"img/Cbasic.png");
    C.push_back(X);
  }

  //Creates 5 resources, inserts them into vector R; inserts locations of resources into vector L
  for(i=0;i<5;i++)
  {
    Resource Y(m,"img/Rbasic.png");
    R.push_back(Y);
    L.push_back(Y.getLocation());
  }

  main = m;
}

void List::Place()
{
  int i;

  //if any locations are creatures, erases them from vector L
  for(i = 0;i < L.size(); i++)
    if(L[i].type==1)
      L.erase(L.begin()+i);

  //places each creature on window, inserts their locations into vector L
  for(i = 0; i < C.size(); i++)
  {
    C[i].Place();
    L.push_back(C[i].getLocation());
  }

  //places all resources
  for(i = 0; i < R.size(); i++)
  {
    R[i].Place();
  }
}

void List::Behavior()
{
  int i, j, k, l;
  std::vector<Location> Z;

  for(i = 0; i < C.size(); i++)
  {
    int o = C[i].Behavior();

    if(o==1)
    {
      //If next to creature
    }

    if(o==2)
    {
      //If next to resource
      Location tmp = C[i].getLocation();
      for(k=0;k<R.size();k++)
      {
        if(Distance(tmp,R[k].getLocation())<2)
        {
          R[k].eat();
          if(R[k].getAmount()<=0)
          {
            R.erase(R.begin()+k);
            for(l=0;l<L.size();l++)
            {
              if(L[l].x==R[k].getLocation().x&&L[l].y==R[k].getLocation().y) // NEED TO OPERATOR OVERLOAD FOR THIS
              {
                L.erase(L.begin()+l);
                std::cout << "removing";
              }
            }
          }
        }
      }
    }

    //if the distance between the creature and L[j] is less than 200, insert L[j] into vector Z.
    for(j = 0; j < L.size(); j++)
      if(200>(Distance(C[i].getLocation(),L[j])))
        Z.push_back(L[j]);

    C[i].giveKnown(Z); //sets creature's target location?
    Z.clear(); //clear vector Z for next creature

    // This kills the creature
    if(C[i].getHealth()<=0)
    {
      Location z = C[i].getLocation();
      Resource r = Resource(main,"img/Cdead.png",z);
      R.push_back(r);
      C.erase(C.begin()+i);
    }
  }
}

double List::Distance(Location A, Location B)
{
  //computes distance between two points
  return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2));
}