Skip to content

Commit

Permalink
Refactor DataReceiver to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
dradcl committed Sep 27, 2023
1 parent c17d89f commit 384163e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 30 deletions.
21 changes: 21 additions & 0 deletions source/DataCollection/DataReceiver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <vector>

namespace DataCollection {

template <typename T>
class DataReceiver {

public:
DataReceiver() = default;
virtual ~DataReceiver() = default;

virtual void DebugPrint() = 0;
inline void store_data(T obj) { storage.push_back(obj); }

protected:
std::vector<T> storage;

};
}
21 changes: 0 additions & 21 deletions source/DataCollection/DateReceiver.hpp

This file was deleted.

17 changes: 17 additions & 0 deletions source/DataCollection/GridPositionReceiver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <iostream>

#include "DataReceiver.hpp"
#include "../core/GridPosition.hpp"

namespace DataCollection {

class GridPositionReceiver : public DataReceiver<cse491::GridPosition> {
public:
void DebugPrint() override {
auto pos = storage.back();
std::cout << "Adding: (" << pos.GetX() << ", " << pos.GetY() << ")" << std::endl;
}
};
}
17 changes: 8 additions & 9 deletions source/core/WorldBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "Data.hpp"
#include "Entity.hpp"
#include "WorldGrid.hpp"
#include "../DataCollection/DateReceiver.hpp"
#include "../DataCollection/GridPositionReceiver.hpp"

namespace cse491 {

Expand All @@ -29,6 +29,8 @@ namespace cse491 {
item_set_t item_set; ///< Vector of pointers to non-agent entities
agent_set_t agent_set; ///< Vector of pointers to agent entities

std::shared_ptr<DataCollection::GridPositionReceiver> grid_receiver;

bool run_over = false; ///< Should the run end?

/// Helper function that is run whenever a new agent is created.
Expand Down Expand Up @@ -102,6 +104,9 @@ namespace cse491 {
return *agent_set.back();
}

void SetGridReceiver(DataCollection::GridPositionReceiver r) {
grid_receiver = std::make_shared<DataCollection::GridPositionReceiver>(r);
}

// -- Action Management --

Expand All @@ -124,14 +129,8 @@ namespace cse491 {
}

void CollectData() {
// calling the collectdata class + the function and store the data (agent_ptr) into it

for (auto & agent_ptr : agent_set) {
//store data
GroupTwo::DataReceiver receiver;
receiver.store_agent(agent_ptr->GetPosition());
}

auto & agent = agent_set.at(2);
grid_receiver->store_data(agent->GetPosition());
}

/// @brief UpdateWorld() is run after every agent has a turn.
Expand Down
3 changes: 3 additions & 0 deletions source/simple_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
#include "Agents/PacingAgent.hpp"
#include "Interfaces/TrashInterface.hpp"
#include "Worlds/MazeWorld.hpp"
#include "DataCollection/GridPositionReceiver.hpp"

int main()
{
cse491::MazeWorld world;
DataCollection::GridPositionReceiver receiver;
world.AddAgent<cse491::PacingAgent>("Pacer 1").SetPosition(3,1);
world.AddAgent<cse491::PacingAgent>("Pacer 2").SetPosition(6,1);
world.AddAgent<cse491::TrashInterface>("Interface").SetProperty("char", '@');
world.SetGridReceiver(receiver);

world.Run();

Expand Down

0 comments on commit 384163e

Please sign in to comment.