forked from suzieh/EvoSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplant.cpp
More file actions
82 lines (71 loc) · 1.76 KB
/
Copy pathplant.cpp
File metadata and controls
82 lines (71 loc) · 1.76 KB
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
#ifdef MACOSX
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif
#include <time.h>
#include <stdlib.h>
#include "plant.h"
using namespace std;
Plant::Plant(int x, int y)
{
width = 20; height = 20;
pos_x = x; pos_y = y;
life = 2000;
mateReduc = 0;
}
float Plant::getPosX()
{
return pos_x;
}
float Plant::getPosY()
{
return pos_y;
}
int Plant::getLife()
{
return life;
}
void Plant::draw()
{
glColor3f(0., 1., 0.); // green square
glBegin(GL_POLYGON);
glVertex2f(pos_x, pos_y);
glVertex2f(pos_x, pos_y + height);
glVertex2f(pos_x + width, pos_y + height);
glVertex2f(pos_x + width, pos_y);
glEnd();
glColor3f(1., 1., 1.); // white outline
glBegin(GL_LINE_STRIP);
glVertex2f(pos_x, pos_y);
glVertex2f(pos_x, pos_y + height);
glVertex2f(pos_x, pos_y + height);
glVertex2f(pos_x + width, pos_y + height);
glVertex2f(pos_x + width, pos_y + height);
glVertex2f(pos_x + width, pos_y);
glVertex2f(pos_x + width, pos_y);
glVertex2f(pos_x, pos_y);
glEnd();
}
void Plant::update(vector<unique_ptr<Plant>> & plant_list)
{
//spawn plant
float range = rand()%120+30;
if(rand()%4000+1 > 3985 + mateReduc + (plant_list.size() * .1))
{
plant_list.push_back(move(make_unique<Plant>(pos_x + ((rand()%2+1 > 1)?range:-range),
pos_y + ((rand()%2+1 > 1)?range:-range))));
mateReduc++;
}
//live
life--;
for(unsigned int other = 0; other < plant_list.size(); other++)
{
float buffer = 80.0;
// check plant's life
if(plant_list[other]->getLife() <= 0 || plant_list[other]->getPosX() < buffer || plant_list[other]->getPosX() > Screen_Width - buffer || plant_list[other]->getPosY() < buffer || plant_list[other]->getPosY() > Screen_Height - buffer)
{
plant_list.erase(plant_list.begin() + other);
}
}
}