RingRacers/src/mobj_list.hpp
Ashnal df4e99b050 WIP Race Checkpoints
MobjList count

WIP: Checkpoints grant lap bonus

help?

can't allocate vector

 fixed tagged line iteration and collision detection

Multiplayer animations and map retart fixes

Clear between maps
2024-08-13 20:20:21 -04:00

93 lines
1.7 KiB
C++

// DR. ROBOTNIK'S RING RACERS
//-----------------------------------------------------------------------------
// Copyright (C) 2024 by James Robert Roman
// Copyright (C) 2024 by Kart Krew
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
#ifndef mobj_list_hpp
#define mobj_list_hpp
#include <type_traits>
#include "cxxutil.hpp"
#include "mobj.hpp"
#include "mobj_list_view.hpp"
namespace srb2
{
// Requires:
// void T::next(T*)
// T* T::next() const
template <typename T, mobj_t*& Head>
struct MobjList
{
static_assert(std::is_convertible_v<T, mobj_t>);
MobjList() {}
T* front() const { return static_cast<T*>(Head); }
bool empty() const { return !front(); }
void push_front(T* ptr)
{
ptr->next(front());
front(ptr);
count_++;
}
void erase(T* node)
{
if (front() == node)
{
front(node->next());
node->next(nullptr);
count_--;
return;
}
auto view = this->view();
auto end = view.end();
auto it = view.begin();
SRB2_ASSERT(it != end);
for (;;)
{
T* prev = *it;
it++;
if (it == end)
{
break;
}
if (*it == node)
{
prev->next(node->next());
node->next(nullptr);
count_--;
break;
}
}
}
auto begin() const { return view().begin(); }
auto end() const { return view().end(); }
auto count() { return count_; }
private:
void front(T* ptr) { Mobj::ManagedPtr {Head} = ptr; }
auto view() const { return MobjListView(front(), [](T* node) { return node->next(); }); }
UINT32 count_ = 0;
};
}; // namespace srb2
#endif/*mobj_list_hpp*/