Skip to content
Snippets Groups Projects

Master

49 files
+ 2717
112
Compare changes
  • Side-by-side
  • Inline

Files

+ 133
36
#include "Animal.h"
#include "Environment.h"
#include "GregariousBehaviour.h"
#include "FearfulBehaviour.h"
#include "KamikazeBehaviour.h"
#include <cstdlib>
#include <cmath>
#include <stdlib.h>
const double Animal::AFF_SIZE = 8.;
@@ -17,7 +21,6 @@ Animal::Animal() {
identity = ++next;
cout << "const Animal (" << identity << ") par défaut" << endl;
x = y = 0;
cumulX = cumulY = 0.;
@@ -28,12 +31,10 @@ Animal::Animal() {
speed = static_cast<double>( rand() )/RAND_MAX*MAX_SPEED;
color = new T[ 3 ];
color[ 0 ] = static_cast<int>( static_cast<double>( rand() )/RAND_MAX*230. );
color[ 1 ] = static_cast<int>( static_cast<double>( rand() )/RAND_MAX*230. );
color[ 2 ] = static_cast<int>( static_cast<double>( rand() )/RAND_MAX*230. );}
behaviour = FearfulBehaviour::getBehaviourInstance();
}
Animal::Animal( const Animal & a ){
Animal::Animal( const Animal& a ){
identity = ++next;
cout << "const Animal (" << identity << ") par copie" << endl;
@@ -46,17 +47,23 @@ Animal::Animal( const Animal & a ){
cumulX = cumulY = 0.;
orientation = a.orientation;
speed = a.speed;
color = new T[ 3 ];
memcpy( color, a.color, 3*sizeof(T) );}
isMultiple = a.isMultiple;
behaviour = a.behaviour;
color = new T[ 3 ];
memcpy( color, a.color, 3*sizeof(T) );}
Animal::~Animal( void ){
Animal::~Animal(){
if (color != NULL){
delete[] color;
}
cout << "dest Pet" << endl;
}
Animal& Animal::operator=(Animal&& p) noexcept
{
// Guard self assignment
@@ -99,45 +106,25 @@ Animal& Animal::operator=(const Animal& p) noexcept
return *this;
}
void Animal::initCoords( int xLim, int yLim ){
x = rand() % xLim;
y = rand() % yLim;}
void Animal::move( int xLim, int yLim ){
double nx, ny;
double dx = cos( orientation )*speed;
double dy = -sin( orientation )*speed;
int cx, cy;
cx = static_cast<int>( cumulX ); cumulX -= cx;
cy = static_cast<int>( cumulY ); cumulY -= cy;
nx = x + dx + cx;
ny = y + dy + cy;
if ( (nx < 0) || (nx > xLim - 1) ) {
orientation = M_PI - orientation;
cumulX = 0.;}
else {
x = static_cast<int>( nx );
cumulX += nx - x;}
if ( (ny < 0) || (ny > yLim - 1) ) {
orientation = -orientation;
cumulY = 0.;}
else {
y = static_cast<int>( ny );
cumulY += ny - y;}}
void Animal::move( int xLim, int yLim, Environment& myEnvironment){
behaviour->move(xLim,yLim,this, myEnvironment);}
void Animal::action( Environment & myEnvironment ){
this->decrement();
if (life > 0) {
move( myEnvironment.getWidth(), myEnvironment.getHeight() );
changeBehaviour();
move( myEnvironment.getWidth(), myEnvironment.getHeight(), myEnvironment);
}
}
void Animal::draw( UImg & support ){
double xt = x + cos( orientation )*AFF_SIZE/2.1;
double yt = y - sin( orientation )*AFF_SIZE/2.1;
@@ -156,11 +143,13 @@ bool Animal::isDetecting( const Animal & a ) const{
dist = std::sqrt( (x-a.x)*(x-a.x) + (y-a.y)*(y-a.y) );
return ( dist <= LIMIT_VIEW );}
void Animal::decrement() {
// decrement the life of the animal
--life;
}
void Animal::onCollision(){
double proba = ((double) rand() / (RAND_MAX));
if (proba < this->getProbabilityOfFatalCollision()) {
@@ -169,22 +158,130 @@ void Animal::onCollision(){
}
}
int Animal::getLife() const {
return life;
}
int Animal::getIdentity() const {
return identity;
return this->identity;
}
double Animal::getProbabilityOfFatalCollision() const {
return probabilityOfFatalCollision;
}
std::tuple<int, int> Animal::getCoordinates(){
return std::make_tuple(this->x,this->y);
}
void Animal::setCoordinates(int new_x,int new_y){
this->x = new_x;
this->y = new_y;
}
std::tuple<double, double> Animal::getCumul(){
return std::make_tuple(this->cumulX,this->cumulY);
}
void Animal::setCumul(double new_cumul_x,double new_cumul_y){
this->cumulX = new_cumul_x;
this->cumulY = new_cumul_y;
}
std::tuple<double, double> Animal::getOrientationSpeed(){
return std::make_tuple(this->orientation,this->speed);
}
void Animal::setOrientationSpeed(double new_orientation,double new_speed){
this->orientation = new_orientation;
this->speed = new_speed;
}
BehaviourStrategy* choose_behaviour() {
BehaviourStrategy* behaviour;
int which_behaviour;
which_behaviour = rand() % 3 + 1;
if ( which_behaviour == 1 ){
behaviour = GregariousBehaviour::getBehaviourInstance();
}
if ( which_behaviour == 2 ){
behaviour = FearfulBehaviour::getBehaviourInstance();
}
if ( which_behaviour == 3 ){
behaviour = KamikazeBehaviour::getBehaviourInstance();
}
return behaviour;
}
void Animal::setBehaviourAsMultiple(){
T c[3] = {0, 0, 0};
this->setColor(c);
this->isMultiple = true;
this->behaviour = choose_behaviour();
}
// Method to randomly change the behaviour of an animal with multiple behaviour
void Animal::changeBehaviour(){
double proba_to_change = ((double) rand() / (RAND_MAX));
if (isMultiple && proba_to_change >= 0.9) {
this->behaviour = choose_behaviour();
}
}
void Animal::setColor(const T c[3]) {
this->color[0] = c[0];
this->color[1] = c[1];
this->color[2] = c[2];
}
void Animal::setBehaviour(string behaviourName) {
if (behaviourName == GregariousBehaviour::getBehaviourInstance()->getBehaviourName()) {
this->setColor(GregariousBehaviour::getColor());
this->behaviour = GregariousBehaviour::getBehaviourInstance();
}
else {
if (behaviourName == FearfulBehaviour::getBehaviourInstance()->getBehaviourName()) {
this->setColor(FearfulBehaviour::getColor());
this->behaviour = FearfulBehaviour::getBehaviourInstance();
}
else {
if (behaviourName == KamikazeBehaviour::getBehaviourInstance()->getBehaviourName()) {
this->setColor(KamikazeBehaviour::getColor());
this->behaviour = KamikazeBehaviour::getBehaviourInstance();
}
}
}
}
double Animal::getMaxSpeed(){
return MAX_SPEED;
}
bool Animal::getIsMultiple(){
return isMultiple;
}
std::string Animal::getBehaviourName(){
return behaviour->getBehaviourName();
}
// ############################## for tests ########################################
void Animal::setLife(int i){
life = i;
Loading