Creatures are replaced with barriers

This commit is contained in:
2016-04-14 04:45:30 +10:00
parent 5d217d7cf9
commit 1267b30806
4 changed files with 84 additions and 55 deletions
+44 -4
View File
@@ -32,12 +32,52 @@ BarrierManager::~BarrierManager() {
} }
//arg: a list of barriers to be updated in the clients //arg: a list of barriers to be updated in the clients
void BarrierManager::Update(std::list<std::pair<const int, BarrierData*>>* barrierList) { void BarrierManager::Update(
int ret; std::list<std::tuple<const int, BarrierData*, int>>* barrierList,
std::list<std::tuple<const int, CreatureData*, int>>* creatureList,
std::list<CharacterData*>* characterList
)
{
//for each given creature, if a collision was detected, make a new barrier
for (auto& it : *creatureList) {
if (std::get<2>(it) & 2) {
Create(-1); //instance from creature index?
}
}
//TODO: merge barriers
//TODO: absorb creatures into existing barriers
//update the barriers
//TODO: how to delete the barriers?
int ret; //0 = no action, ret&1 = update clients, ret&2 = collision detected
for (auto& it : elementMap) { for (auto& it : elementMap) {
ret = it.second.Update(lua); //normal update
ret = it.second.Update(lua) ? 1 : 0;
//check for collision with a character
BoundingBox barrierBox = it.second.GetRealBounds();
for (auto& it : *characterList) {
if (barrierBox.CheckOverlap(it->GetRealBounds())) {
//this will need updating
ret |= 2;
}
//TODO: absorb characters
}
if (ret) { if (ret) {
barrierList->push_back(std::pair<const int, BarrierData*>(it.first, &it.second)); //push to the return list
barrierList->push_back(std::make_tuple(it.first, &it.second, ret));
}
}
}
void BarrierManager::Cleanup(std::list<std::tuple<const int, BarrierData*, int>>* barrierList) {
//unload the given barrier objects
for (auto& it : *barrierList) {
if (std::get<2>(it) & 2) {
Unload(std::get<0>(it));
} }
} }
} }
+9 -1
View File
@@ -22,6 +22,8 @@
#pragma once #pragma once
#include "barrier_data.hpp" #include "barrier_data.hpp"
#include "character_data.hpp"
#include "creature_data.hpp"
#include "lua.hpp" #include "lua.hpp"
#include "sqlite3.h" #include "sqlite3.h"
@@ -29,6 +31,7 @@
#include <algorithm> #include <algorithm>
#include <list> #include <list>
#include <map> #include <map>
#include <tuple>
class BarrierManager { class BarrierManager {
public: public:
@@ -36,7 +39,12 @@ public:
~BarrierManager(); ~BarrierManager();
//common public methods //common public methods
void Update(std::list<std::pair<const int, BarrierData*>>* barrierList); void Update(
std::list<std::tuple<const int, BarrierData*, int>>* barrierList,
std::list<std::tuple<const int, CreatureData*, int>>* creatureList,
std::list<CharacterData*>* characterList
);
void Cleanup(std::list<std::tuple<const int, BarrierData*, int>>* barrierList);
int Create(int instanceIndex); int Create(int instanceIndex);
void Unload(int uid); void Unload(int uid);
+3 -3
View File
@@ -38,7 +38,7 @@ void CreatureManager::Update(
) )
{ {
//for each creature //for each creature
int ret; //0 = no action, ret&1 = update clients, ret&2 = unload during cleanup step int ret; //0 = no action, ret&1 = update clients, ret&2 = collision detected
for (auto& it : elementMap) { for (auto& it : elementMap) {
//normal update //normal update
ret = it.second.Update(lua) ? 1 : 0; ret = it.second.Update(lua) ? 1 : 0;
@@ -48,7 +48,7 @@ void CreatureManager::Update(
for (auto& it : *characterList) { for (auto& it : *characterList) {
if (creatureBox.CheckOverlap(it->GetRealBounds())) { if (creatureBox.CheckOverlap(it->GetRealBounds())) {
//this will need updating //this will need updating
ret += 2; ret |= 2;
break; break;
} }
} }
@@ -64,7 +64,7 @@ void CreatureManager::Cleanup(std::list<std::tuple<const int, CreatureData*, int
//unload the given creature objects //unload the given creature objects
for (auto& it : *creatureList) { for (auto& it : *creatureList) {
if (std::get<2>(it) & 2) { if (std::get<2>(it) & 2) {
// Unload(std::get<0>(it)); Unload(std::get<0>(it));
} }
} }
} }
+28 -47
View File
@@ -30,6 +30,7 @@
#include <iostream> #include <iostream>
#include <stack> #include <stack>
#include <stdexcept> #include <stdexcept>
#include <tuple>
//TODO: (9) character collisions should be preformed client-side //TODO: (9) character collisions should be preformed client-side
void RoomData::RunFrame() { void RoomData::RunFrame() {
@@ -48,15 +49,38 @@ void RoomData::RunFrame() {
} }
//lists of non-character entities that need updating client-side //lists of non-character entities that need updating client-side
std::list<std::pair<const int, CreatureData*>> creatureList; //types are index, ptr, action (0 = update, 1 = unload)
std::list<std::pair<const int, BarrierData*>> barrierList; std::list<std::tuple<const int, CreatureData*, int>> creatureList;
std::list<std::tuple<const int, BarrierData*, int>> barrierList;
//update the entities in the room //update the entities in the room
for (auto& it : characterList) { for (auto& it : characterList) {
it->Update(); it->Update();
} }
creatureMgr.Update(&creatureList); creatureMgr.Update(&creatureList, &characterList);
barrierMgr.Update(&barrierList); barrierMgr.Update(&barrierList, &creatureList, &characterList);
//send the creature updates
for (auto& it : creatureList) {
CreaturePacket packet;
copyCreatureToPacket(&packet, std::get<1>(it), std::get<0>(it));
packet.type = std::get<2>(it) != 0 ? SerialPacketType::CREATURE_UPDATE : SerialPacketType::CREATURE_UNLOAD;
packet.roomIndex = roomIndex;
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, std::get<1>(it)->GetOrigin(), INFLUENCE_RADIUS);
}
//send the barrier updates
for (auto& it : barrierList) {
BarrierPacket packet;
copyBarrierToPacket(&packet, std::get<1>(it), std::get<0>(it));
packet.type = std::get<2>(it) != 0 ? SerialPacketType::BARRIER_UPDATE : SerialPacketType::BARRIER_UNLOAD;
packet.roomIndex = roomIndex;
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, std::get<1>(it)->GetOrigin(), INFLUENCE_RADIUS);
}
//cleanup the lists
creatureMgr.Cleanup(&creatureList);
barrierMgr.Cleanup(&barrierList);
//build a list of entities for use with the triggers //build a list of entities for use with the triggers
std::stack<Entity*> entityStack; std::stack<Entity*> entityStack;
@@ -66,49 +90,6 @@ void RoomData::RunFrame() {
//Compare the triggers to the entities, using their real hitboxes //Compare the triggers to the entities, using their real hitboxes
triggerMgr.Compare(entityStack); triggerMgr.Compare(entityStack);
//Creature/character collisions, O(m*n)
for (auto characterIt : characterList) {
BoundingBox characterBox = characterIt->GetBounds() + characterIt->GetOrigin();
for (auto creatureIt : *creatureMgr.GetContainer()) {
BoundingBox creatureBox = creatureIt.second.GetBounds() + creatureIt.second.GetOrigin();
if (characterBox.CheckOverlap(creatureBox)) {
int barrierIndex = barrierMgr.Create(-1);
BarrierData* barrierData = barrierMgr.Find(barrierIndex);
barrierData->SetRoomIndex(roomIndex);
barrierData->SetOrigin({
(CREATURE_BOUNDS_WIDTH - BARRIER_BOUNDS_WIDTH) / 2 + creatureBox.x,
(CREATURE_BOUNDS_HEIGHT - BARRIER_BOUNDS_HEIGHT) / 2 + creatureBox.y,
});
BarrierPacket barrierPacket;
barrierPacket.type = SerialPacketType::BARRIER_CREATE;
copyBarrierToPacket(&barrierPacket, barrierData, barrierIndex);
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&barrierPacket), roomIndex, characterIt->GetOrigin(), INFLUENCE_RADIUS);
}
}
}
//send the creature updates
for (auto& it : creatureList) {
CreaturePacket packet;
copyCreatureToPacket(&packet, it.second, it.first);
packet.type = SerialPacketType::CREATURE_UPDATE;
packet.roomIndex = roomIndex;
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, it.second->GetOrigin(), INFLUENCE_RADIUS);
}
//send the barrier updates
for (auto& it : barrierList) {
BarrierPacket packet;
copyBarrierToPacket(&packet, it.second, it.first);
packet.type = SerialPacketType::BARRIER_UPDATE;
packet.roomIndex = roomIndex;
pumpPacketProximity(reinterpret_cast<SerialPacket*>(&packet), roomIndex, it.second->GetOrigin(), INFLUENCE_RADIUS);
}
} }
std::string RoomData::SetName(std::string s) { std::string RoomData::SetName(std::string s) {