Fleshed out inherited functions
TODO: * RegionPagerBase::Update() is a stub * MapFileFormat * MapGenerator * Can replace the calls to std::find_if() with a utility function
This commit is contained in:
@@ -38,25 +38,25 @@ RegionPagerBase::~RegionPagerBase() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int RegionPagerBase::SetTile(int x, int y, int z, int v) {
|
int RegionPagerBase::SetTile(int x, int y, int z, int v) {
|
||||||
Region* ptr = GetRegion(snapToBase(regionWidth, x), snapToBase(regionHeight, y));
|
Region* ptr = GetRegion(x, y);
|
||||||
return ptr->SetTile(x - ptr->GetX(), y = ptr->GetY(), z, v);
|
return ptr->SetTile(x - ptr->GetX(), y = ptr->GetY(), z, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
int RegionPagerBase::GetTile(int x, int y, int z) {
|
int RegionPagerBase::GetTile(int x, int y, int z) {
|
||||||
Region* ptr = GetRegion(snapToBase(regionWidth, x), snapToBase(regionHeight, y));
|
Region* ptr = GetRegion(x, y);
|
||||||
return ptr->GetTile(x - ptr->GetX(), y = ptr->GetY(), z);
|
return ptr->GetTile(x - ptr->GetX(), y = ptr->GetY(), z);
|
||||||
}
|
}
|
||||||
|
|
||||||
Region* RegionPagerBase::GetRegion(int x, int y) {
|
Region* RegionPagerBase::GetRegion(int x, int y) {
|
||||||
//TODO: clean this up
|
//snap the coords
|
||||||
|
x = snapToBase(regionWidth, x);
|
||||||
|
y = snapToBase(regionHeight, y);
|
||||||
|
|
||||||
Region* ptr = nullptr;
|
Region* ptr = nullptr;
|
||||||
|
|
||||||
//find the loaded region
|
//find the loaded region
|
||||||
auto iter = std::find_if(regionList.begin(), regionList.end(), [x,y](Region& it) {
|
auto iter = std::find_if(regionList.begin(), regionList.end(), [x,y](Region& it) {
|
||||||
if (it.GetX() == x && it.GetY() == y)
|
return it.GetX() == x && it.GetY() == y;
|
||||||
return true;
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
if (iter != regionList.end()) { //ugly hack
|
if (iter != regionList.end()) { //ugly hack
|
||||||
ptr = &(*iter);
|
ptr = &(*iter);
|
||||||
|
|||||||
+59
-10
@@ -23,7 +23,9 @@
|
|||||||
#define REGIONPAGER_HPP_
|
#define REGIONPAGER_HPP_
|
||||||
|
|
||||||
#include "region.hpp"
|
#include "region.hpp"
|
||||||
|
#include "utility.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
class RegionPagerBase {
|
class RegionPagerBase {
|
||||||
@@ -59,26 +61,73 @@ protected:
|
|||||||
template<typename MapGenerator, typename MapFileFormat>
|
template<typename MapGenerator, typename MapFileFormat>
|
||||||
class RegionPager : public RegionPagerBase {
|
class RegionPager : public RegionPagerBase {
|
||||||
public:
|
public:
|
||||||
|
RegionPager() = delete;
|
||||||
|
RegionPager(int w, int h, int d):
|
||||||
|
RegionPagerBase(w, h, d)
|
||||||
|
{
|
||||||
|
//EMPTY
|
||||||
|
}
|
||||||
|
~RegionPager() = default;
|
||||||
|
|
||||||
Region* LoadRegion(int x, int y) {
|
Region* LoadRegion(int x, int y) {
|
||||||
//TODO
|
//snap the coords
|
||||||
//if there's no region to load, return null
|
x = snapToBase(regionWidth, x);
|
||||||
|
y = snapToBase(regionHeight, y);
|
||||||
|
|
||||||
|
//load the region if possible
|
||||||
|
Region* region = nullptr;
|
||||||
|
format.Load(®ion, x, y);
|
||||||
|
if (region) {
|
||||||
|
regionList.push_back(std::move(*region));
|
||||||
|
return ®ionList.back();
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Region* SaveRegion(int x, int y) {
|
Region* SaveRegion(int x, int y) {
|
||||||
//TODO
|
//snap the coords
|
||||||
//save the region using the functor
|
x = snapToBase(regionWidth, x);
|
||||||
|
y = snapToBase(regionHeight, y);
|
||||||
|
|
||||||
|
//find the specified region
|
||||||
|
auto iter = std::find_if(regionList.begin(), regionList.end(), [x, y](Region& it){
|
||||||
|
return it.GetX() == x && it.GetY() == y;
|
||||||
|
});
|
||||||
|
|
||||||
|
//save the region if it's loaded
|
||||||
|
if (iter != regionList.end()) {
|
||||||
|
format.Save(&(*iter, x, y));
|
||||||
|
return &(*iter);
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Region* CreateRegion(int x, int y) {
|
Region* CreateRegion(int x, int y) {
|
||||||
//TODO
|
//snap the coords
|
||||||
//create the region
|
x = snapToBase(regionWidth, x);
|
||||||
//DON'T call this on a non-zero region,
|
y = snapToBase(regionHeight, y);
|
||||||
//or if there's a region saved to the disk
|
|
||||||
|
//create and push the object
|
||||||
|
Region* region = nullptr;
|
||||||
|
generator.Create(®ion);
|
||||||
|
regionList.push_back(std::move(*region));
|
||||||
|
return regionList.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnloadRegion(int x, int y) {
|
void UnloadRegion(int x, int y) {
|
||||||
//TODO
|
//snap the coords
|
||||||
//free the region, possibly saving it
|
x = snapToBase(regionWidth, x);
|
||||||
|
y = snapToBase(regionHeight, y);
|
||||||
|
|
||||||
|
//find the specified region
|
||||||
|
auto iter = std::find_if(regionList.begin(), regionList.end(), [x, y](Region& it){
|
||||||
|
return it.GetX() == x && it.GetY() == y;
|
||||||
|
});
|
||||||
|
|
||||||
|
//pass it to the generator for unloading
|
||||||
|
if (iter != regionList.end()) {
|
||||||
|
generator.Unload(&(*iter));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//accessors
|
//accessors
|
||||||
|
|||||||
Reference in New Issue
Block a user