Inheritance
Mar 22, 2016 at 6:05pm UTC
I am making an evolution simulator. In it Crawler obj. can create new Crawlers(reproduce). The problem is that I don't know how to pass the new born s to a vector of all the crawlers. The way I was doing it before it would change the previous children of a crawler when it reproduced
Crawler.cpp (updating part)
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
void Crawler::live(WorldTile* worldTile, std::vector<Crawler*>& crawlers) {
m_currentTile = worldTile;
calculateOutputs(crawlers);
updateEnergy(m_currentTile->Temperatur, m_currentTile->lux);
if (_energy <= 0 || m_layers.size() < 1) {
m_isAlive = false ;
std::cout << _name << " - dead\n" ;
}
//drift(worldTile->current);
_inputs[0] = _energy;
return ;
}
void Crawler::calculateOutputs(std::vector<Crawler*>& crawlers){
does some calculating and calls doOutputs.
(function was too long )
}
void Crawler::doOutputs(int chromosomeNum, std::vector<Crawler*>& crawlers) {
const glm::vec2 velocity(0.02f);
for (int i = 0; i < 8; i++) {
if (chromosomeNum == 0 && i == 0) {
m_pos.y -= velocity.y * _outputs[chromosomeNum][i];
_energy -= _outputs[chromosomeNum][i];
}
if (chromosomeNum == 0 && i == 1) {
m_pos.x += velocity.x * _outputs[chromosomeNum][i];
_energy -= _outputs[chromosomeNum][i];
}
if (chromosomeNum == 2 && i == 0 && _outputs[chromosomeNum][0] == 1 &&_energy >= _outputs[chromosomeNum][1] && _outputs[chromosomeNum][1] > 0) {
Reproduce(chromosomeNum, glm::vec2(m_layers[_outputs[chromosomeNum][5]].first + _outputs[chromosomeNum][2] + LAYER_SIZE / 2.0f + m_pos.x, m_layers[_outputs[chromosomeNum][5]].second + _outputs[chromosomeNum][3] + LAYER_SIZE / 2.0f + m_pos.y), crawlers);
}
}
}
void Crawler::Reproduce(int chromosomeNum, glm::vec2 pos, std::vector<Crawler*>& crawlers) {
_energy -= _outputs[chromosomeNum][1];
DNA dna = MutateDNA();
_num++;
std::string n = _name + "_" + std::to_string(_num);
m_b = new Crawler (n, dna);
m_b->init(pos);
FileManager::writeDNAToCrawler(*m_b, dna);
std::cout << "Baby - " << m_b->getName() << "\n" ;
m_b->getEnergy() = _outputs[chromosomeNum][1];
crawlers.push_back(m_b);
}
Crawler.h
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 110 111 112 113 114 115 116 117 118 119 120 121 122
#pragma once
#include <PopSickles/SpriteBatch.h>
#include "DNA.h"
#include "MolekuleList.h"
#include "WorldTile.h"
class Crawler
{
public :
Crawler(std::string name);
Crawler(std::string name, DNA& dna);
~Crawler();
void init(glm::vec2 pos);
void draw(PopSickles::SpriteBatch* spriteBatch);
void live(WorldTile* worldTile, std::vector<Crawler*>& crawlers);
void setDNA(DNA& dna);
void setDecodedDNA(DecodedDNA decodedDNA);
std::string& getName() {
return _name;
}
DNA& getDNA() {
return _dna;
}
DecodedDNA& getDecodedDNA() {
return m_decodedDNA;
}
float getMutationRate() {
return m_mutationRate;
}
float & getEnergy() {
return _energy;
}
void setEnergy(float energy) {
_energy = energy;
}
glm::vec2& getPos() {
return m_pos;
}
Crawler* getBaby() {
return m_b;
}
std::vector<std::pair<int , int >> getLayers() {
return m_layers;
}
bool & isAlive() {
return m_isAlive;
}
float getSize() {
return 2;
}
glm::vec2& getVel(){
return m_vel;
}
void setVel(glm::vec2 vel) {
m_vel = vel;
}
private :
void updateEnergy(float Temperatur, float lux);
DNA MutateDNA();
void updateInputs(int chromosomeNum);
void calculateOutputs(std::vector<Crawler*>& crawlers);
void doOutputs(int chromosomeNum, std::vector<Crawler*>& crawlers);
void Reproduce(int chromosomeNum, glm::vec2 pos, std::vector<Crawler*>& crawlers);
void drift(glm::vec2 current);
float m_mutationRate = 0;
DNA _dna;
DecodedDNA m_decodedDNA;
int _num;
float _inputs [8];
float _outputs [5][8];
std::string _name;
//current usable energy of crawler
float _energy = 0;
//current Temperatur and light energy absorbed by crawler
float _TLEnergy;
//Temperatur and light energy absorbed by crawler last frame
float _formerTLEnergy;
std::vector<std::pair<int , int >> m_layers;
glm::vec2 m_pos = glm::vec2(0.0f);
glm::vec2 m_vel = glm::vec2(0.0f);
PopSickles::ColorRGBA8 m_color = COLORRGBA8_WHITE;
Crawler* m_b = nullptr ;
bool m_isAlive = true ;
WorldTile* m_currentTile = nullptr ;
int m_cellVectorIndex = -1;
};
World.cpp (updating function)
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
void World::update() {
int c = 0;
int upd = UPDATE_BATCH;
if (upd > (signed ) m_crawlers.size()) {
upd = m_crawlers.size();
}
for (int i = 0; i < upd; i++) {
if (m_crawlers[c]->isAlive()) {
WorldTile tmp = getWorldTileOn(m_crawlers[c]->getPos());
m_crawlers[c]->live(&tmp, m_crawlers);
}
if (c == m_crawlers.size() - 1) {
c = -1;
}
c++;
}
for (unsigned int i = 0; i < m_worldTiles.size(); i++) {
m_worldTiles[i].update();
}
}
Last edited on Mar 26, 2016 at 9:37pm UTC
Mar 22, 2016 at 6:06pm UTC
World.h
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
#pragma once
#include <PopSickles\SpriteBatch.h>
#include "WorldTile.h"
#include <memory>
#include "Crawler.h"
class World
{
public :
World();
~World();
void init(float width, float height, int xTiles, int yTiles);
void draw(PopSickles::SpriteBatch* spriteBatch);
void update();
WorldTile getWorldTileOn(glm::vec2 pos);
std::vector<WorldTile> getWorldTiles() {
return m_worldTiles;
}
private :
float m_width, m_height;
float m_timer = 0;
std::vector<Crawler*> m_crawlers;
std::vector<WorldTile> m_worldTiles;
int m_screenWidth = 1024;
int m_screenHeight = 786;
Crawler m_crawler;
};
Topic archived. No new replies allowed.