123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #include "NetworkResponse.h"
- #include "Chunk.h"
- #include "Entity.h"
- #include "Game.h"
- NetworkResponse::NetworkResponse()
- {
- adress = 0;
- adressLength = 0;
- broadcast = 0;
- message = 0;
- msgDelete = 0;
- msgLength = 0;
- useBackground = 0;
- }
- NetworkResponse::~NetworkResponse()
- {
- if (msgDelete)
- delete[] message;
- delete[] adress;
- }
- void NetworkResponse::adressChunck(Chunk* zChunk)
- {
- adressLength = 14;
- adress = new char[adressLength];
- adress[0] = 1; // dimension response
- *(int*)(adress + 1) = zChunk->getDimensionId();
- adress[5] = 1; // chunck
- Framework::Punkt center = zChunk->getCenter();
- *(int*)(adress + 6) = center.x;
- *(int*)(adress + 10) = center.y;
- minPosition = zChunk->getMin();
- maxPosition = zChunk->getMax();
- }
- void NetworkResponse::adressEntity(Entity* zEntity)
- {
- adressLength = 10;
- adress = new char[adressLength];
- adress[0] = 1; // dimension response
- *(int*)(adress + 1) = zEntity->getCurrentDimensionId();
- adress[5] = 2; // entity
- *(int*)(adress + 6) = zEntity->getId();
- minPosition = zEntity->getPosition();
- maxPosition = zEntity->getPosition();
- }
- void NetworkResponse::adressBlock(Block* zBlock)
- {
- adressLength = 18;
- adress = new char[adressLength];
- adress[0] = 1; // dimension response
- *(int*)(adress + 1) = zBlock->getDimensionId();
- adress[5] = 3; // block
- Framework::Vec3<int> pos = zBlock->getPos();
- *(int*)(adress + 6) = pos.x;
- *(int*)(adress + 10) = pos.y;
- *(int*)(adress + 14) = pos.z;
- minPosition = pos;
- maxPosition = pos;
- }
- void NetworkResponse::openDialog(Framework::Text dialogName)
- {
- adressLength = (char)(4 + dialogName.getLength());
- adress = new char[adressLength];
- adress[0] = 2; // gui message
- adress[1] = 0; // open dialog
- *(short*)(adress + 2) = (short)dialogName.getLength(); // block
- memcpy(adress + 4, dialogName.getText(), dialogName.getLength());
- minPosition.x = std::numeric_limits<float>::min();
- minPosition.y = std::numeric_limits<float>::min();
- minPosition.z = std::numeric_limits<float>::min();
- maxPosition.x = std::numeric_limits<float>::max();
- maxPosition.y = std::numeric_limits<float>::max();
- maxPosition.z = std::numeric_limits<float>::max();
- }
- void NetworkResponse::adressGui(Framework::Text elementId)
- {
- adressLength = (char)(4 + elementId.getLength());
- adress = new char[adressLength];
- adress[0] = 2; // gui message
- adress[1] = 1; // element message
- *(short*)(adress + 2) = (short)elementId.getLength(); // block
- memcpy(adress + 4, elementId.getText(), elementId.getLength());
- minPosition.x = std::numeric_limits<float>::min();
- minPosition.y = std::numeric_limits<float>::min();
- minPosition.z = std::numeric_limits<float>::min();
- maxPosition.x = std::numeric_limits<float>::max();
- maxPosition.y = std::numeric_limits<float>::max();
- maxPosition.z = std::numeric_limits<float>::max();
- }
- void NetworkResponse::setMessage(char* msg, int length, bool deleteMsg)
- {
- message = msg;
- msgLength = length;
- msgDelete = deleteMsg;
- }
- void NetworkResponse::setUseBackground()
- {
- useBackground = 1;
- }
- void NetworkResponse::sendToAll()
- {
- broadcast = true;
- }
- bool NetworkResponse::isAreaAffected(Framework::Vec3<float> min, Framework::Vec3<float> max) const
- {
- return minPosition.x <= max.x && maxPosition.x >= min.x &&
- minPosition.y <= max.y && maxPosition.y >= min.y &&
- minPosition.z <= max.z && maxPosition.z >= min.z;
- }
- void NetworkResponse::writeTo(Framework::StreamWriter* zWriter) const
- {
- int total = msgLength + adressLength;
- if (total)
- {
- zWriter->schreibe((char*)&total, 4);
- zWriter->schreibe(adress, adressLength);
- zWriter->schreibe(message, msgLength);
- }
- }
- bool NetworkResponse::isBroadcast() const
- {
- return broadcast;
- }
- bool NetworkResponse::isEmpty() const
- {
- return msgLength + adressLength <= 0;
- }
- bool NetworkResponse::isUseBackground() const
- {
- return useBackground;
- }
|