-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal.cpp
More file actions
73 lines (67 loc) · 1.43 KB
/
Animal.cpp
File metadata and controls
73 lines (67 loc) · 1.43 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
#pragma once
#include "Animal.h"
#include"World.h"
#include"config.h"
#include<iostream>
using namespace std;
Animal::Animal(int power, int activity, World *world, int x, int y)
{
this->power = power;
this->activity = activity;
this->world = world;
this->x = x;
this->y = y;
}
void Animal::action(int move_dx, int move_dy)
{
if (move_dx == 0 && move_dy == 0)
{
randMove(&move_dx, &move_dy, 1);
}
if (world->checkPlace(x + move_dx, y + move_dy) != '!')
{
if (world->checkPlace(x + move_dx, y + move_dy) == ' ')
{
world->moveOrganism(this, x + move_dx, y + move_dy);
world->addComment(string(1, image), "move");
}
else
{
world->collision(this, x + move_dx, y + move_dy);
}
}
}
void Animal::collision(Organism *attacker)
{
if (attacker->getImage() == image)
{
//copulate
int new_x = x;
int new_y = y;
if (world->findFreeSpace(&new_x, &new_y, FIND_RANGE))
{
world->addOrganism(image, new_x, new_y);
world->addComment(string(1, image), "multiplied");
}
else
{
world->addComment(string(1, image), "don't find space for multiplied");
}
}
else
{
//attack
if (attacker->getPower() < power)
{
world->addComment(string(1, image), "ate", string(1,attacker->getImage()));
world->delOrganism(attacker);
}
else
{
int a = x, b = y;
world->moveOrganism(attacker, a, b);
world->addComment(string(1, attacker->getImage()), "ate", string(1, image));
world->delOrganism(this);
}
}
}