Skip to content
Snippets Groups Projects

update from master

Closed BERTHOUMIEU Aymeric requested to merge master into Aymeric
37 files
+ 2295
60
Compare changes
  • Side-by-side
  • Inline

Files

+ 83
30
#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.;
const double Animal::MAX_SPEED = 10.;
@@ -27,6 +30,12 @@ Animal::Animal() {
orientation = static_cast<double>( rand() )/RAND_MAX*2.*M_PI;
speed = static_cast<double>( rand() )/RAND_MAX*MAX_SPEED;
// isMultiple = 1;
// //behaviour = new KamikazeBehaviour();
// behaviour = FearfulBehaviour::getBehaviourInstance();
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. );
@@ -46,14 +55,21 @@ 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 = 1;
// //behaviour = new KamikazeBehaviour();
// behaviour = FearfulBehaviour::getBehaviourInstance();
color = new T[ 3 ];
memcpy( color, a.color, 3*sizeof(T) );}
Animal::~Animal( void ){
if (color != NULL){
delete[] color;
}
//delete behaviour;
cout << "dest Pet" << endl;
}
@@ -104,37 +120,16 @@ void Animal::initCoords( int xLim, int yLim ){
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);
}
}
@@ -174,7 +169,7 @@ int Animal::getLife() const {
}
int Animal::getIdentity() const {
return identity;
return this->identity;
}
double Animal::getProbabilityOfFatalCollision() const {
@@ -185,6 +180,64 @@ std::tuple<int, int> Animal::getCoordinates(){
return std::make_tuple(this->x,this->y);
}
std::tuple<double, double> Animal::getCumul(){
return std::make_tuple(this->cumulX,this->cumulY);
}
std::tuple<double, double> Animal::getOrientationSpeed(){
return std::make_tuple(this->orientation,this->speed);
}
void Animal::setCoordinates(int new_x,int new_y){
this->x = new_x;
this->y = new_y;
}
void Animal::setCumul(double new_cumul_x,double new_cumul_y){
this->cumulX = new_cumul_x;
this->cumulY = new_cumul_y;
}
void Animal::setOrientationSpeed(double new_orientation,double new_speed){
this->orientation = new_orientation;
this->speed = new_speed;
}
void Animal::changeBehaviour(){
double proba_to_change = ((double) rand() / (RAND_MAX));
if (isMultiple && proba_to_change >= 0.9) {
cout << "Reach Here ? Change Behaviour" << endl;
//delete behaviour;
int which_behaviour;
which_behaviour = rand() % 3 + 1;
if ( which_behaviour == 1 ){
//behaviour = new GregariousBehaviour();
behaviour = GregariousBehaviour::getBehaviourInstance();
}
if ( which_behaviour == 2 ){
//behaviour = new FearfulBehaviour();
behaviour = FearfulBehaviour::getBehaviourInstance();
}
if ( which_behaviour == 3 ){
//behaviour = new KamikazeBehaviour();
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