123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627 |
- #include "Entity.h"
- #include <Globals.h>
- #include <math.h>
- #include "Game.h"
- #include "Globals.h"
- Entity::Entity(const EntityType* zType,
- Framework::Model3DData* model,
- Framework::Model3DTextur* texture,
- int id,
- Framework::Vec3<float> position,
- float maxMovementSpeed,
- float gravityMultiplier,
- float jumpSpeed,
- float size)
- : FactoryCraftModel(),
- id(id),
- zType(zType),
- playerControlled(0),
- maxMovementSpeed(maxMovementSpeed),
- lastFlags(0),
- timeSinceSync(0),
- gravityMultiplier(gravityMultiplier),
- jumpSpeed(jumpSpeed),
- speed(0, 0, 0)
- {
- pos = position;
- setModelDaten(model);
- setModelTextur(texture);
- lastDirection = World::INSTANCE->zKamera()->getDirection();
- currentFrame.duration = 0;
- rend = 1;
- lastFlags = 0;
- setSize(size);
- }
- Entity::~Entity() {}
- void Entity::api(char* message)
- {
- switch (message[0])
- {
- case 0:
- { // add movement frame
- MovementFrame frame;
- frame.direction.x = *(float*)(message += 1);
- frame.direction.y = *(float*)(message += 4);
- frame.direction.z = *(float*)(message += 4);
- frame.targetPosition.x = *(float*)(message += 4);
- frame.targetPosition.y = *(float*)(message += 4);
- frame.targetPosition.z = *(float*)(message += 4);
- frame.movementFlags = *(int*)(message += 4);
- frame.duration = *(double*)(message += 4);
- Vec2<float> norm = {0, -1};
- this->setDrehungZ((frame.direction.x < 0 ? -1 : 1)
- * norm.angle(Vec2<float>(
- frame.direction.x, frame.direction.y)));
- if (!playerControlled)
- {
- cs.lock();
- movements.add(frame);
- cs.unlock();
- }
- break;
- }
- case 1:
- { // position correction
- if (playerControlled)
- {
- timeSinceSync = 0;
- pos.x = *(float*)(message += 1);
- pos.y = *(float*)(message += 4);
- pos.z = *(float*)(message += 4);
- lastDirection = World::INSTANCE->zKamera()->getDirection();
- lastFlags = 0;
- }
- break;
- }
- }
- }
- bool Entity::tick(double time)
- {
- if (!World::INSTANCE || !World::INSTANCE->zKamera()) return 0;
- if (playerControlled && GetForegroundWindow() == window->getFensterHandle())
- {
- bool chunkLoaded = 1;
- if (!World::INSTANCE->zChunk(
- World::INSTANCE->getChunkCenter((int)pos.x, (int)pos.y)))
- {
- chunkLoaded = 0;
- }
- Vec3<float> direction = World::INSTANCE->zKamera()->getDirection();
- Vec3<float> lastPos = pos;
- int flags = 0;
- if ((lastFlags | MOVEMENT_FLAG_JUMP) != lastFlags
- && (speed.z == 0.f || (lastFlags | MOVEMENT_FLAG_FLY) == lastFlags))
- { // not jumping and not falling
- speed = {0, 0, speed.z};
- if (GetKeyState('w') & 0x8000 || GetKeyState('W') & 0x8000)
- {
- flags |= MOVEMENT_FLAG_FORWARD;
- speed += {direction.x, direction.y, 0};
- }
- if (GetKeyState('a') & 0x8000 || GetKeyState('A') & 0x8000)
- {
- flags |= MOVEMENT_FLAG_LEFT;
- Vec2<float> norm = {direction.x, direction.y};
- norm.CCW90().normalize();
- speed += {norm.x, norm.y, 0};
- }
- if (GetKeyState('s') & 0x8000 || GetKeyState('S') & 0x8000)
- {
- flags |= MOVEMENT_FLAG_BACKWARD;
- speed += {-direction.x, -direction.y, 0};
- }
- if (GetKeyState('d') & 0x8000 || GetKeyState('D') & 0x8000)
- {
- flags |= MOVEMENT_FLAG_RIGHT;
- Vec2<float> norm = {direction.x, direction.y};
- norm.CW90().normalize();
- speed += {norm.x, norm.y, 0};
- }
- }
- else
- {
- speed = {speed.x, speed.y, speed.z};
- }
- if ((lastFlags | MOVEMENT_FLAG_FLY) == lastFlags)
- { // fly mode
- flags |= MOVEMENT_FLAG_FLY;
- if (GetKeyState(T_Shift) & 0x8000)
- {
- if (getTastenStand(T_Strg))
- { // end fly
- flags &= ~MOVEMENT_FLAG_FLY;
- speed.z = 0;
- }
- else
- {
- flags |= MOVEMENT_FLAG_DOWN;
- speed.z = -maxMovementSpeed;
- }
- }
- else if (GetKeyState(T_Space) & 0x8000)
- {
- flags |= MOVEMENT_FLAG_UP;
- speed.z = maxMovementSpeed;
- }
- else
- {
- speed.z = 0;
- }
- }
- else
- { // walk mode
- if (GetKeyState(T_Space) & 0x8000)
- {
- if (getTastenStand(T_Strg))
- { // begin fly
- flags |= MOVEMENT_FLAG_FLY;
- speed.z = 0;
- }
- else if ((flags | MOVEMENT_FLAG_JUMP) != lastFlags
- && speed.z == 0.f)
- { // begin jump
- flags |= MOVEMENT_FLAG_JUMP;
- speed.z = jumpSpeed;
- }
- }
- if ((flags | MOVEMENT_FLAG_JUMP) == lastFlags
- && (flags | MOVEMENT_FLAG_FLY) != flags)
- {
- flags |= MOVEMENT_FLAG_JUMP; // keep jumping
- }
- }
- Vec2<float> norm = {speed.x, speed.y};
- if (norm.getLengthSq() != 0)
- {
- norm.normalize();
- speed.x = norm.x * maxMovementSpeed;
- speed.y = norm.y * maxMovementSpeed;
- }
- if ((flags | MOVEMENT_FLAG_FLY) != flags)
- {
- if (chunkLoaded)
- {
- speed.z -= World::INSTANCE->zDimension()->getGravity()
- * gravityMultiplier * (float)time;
- }
- }
- // collision checking
- Vec3<float> minP = model->getMinPos();
- Vec3<float> maxP = model->getMaxPos();
- Vec3<float> worldBoundingBox[8];
- worldBoundingBox[0] = applyWorldTransformation(minP);
- worldBoundingBox[1]
- = applyWorldTransformation({minP.x, minP.y, maxP.z});
- worldBoundingBox[2]
- = applyWorldTransformation({minP.x, maxP.y, minP.z});
- worldBoundingBox[3]
- = applyWorldTransformation({maxP.x, minP.y, minP.z});
- worldBoundingBox[4]
- = applyWorldTransformation({maxP.x, minP.y, maxP.z});
- worldBoundingBox[5]
- = applyWorldTransformation({maxP.x, maxP.y, minP.z});
- worldBoundingBox[6]
- = applyWorldTransformation({minP.x, maxP.y, maxP.z});
- worldBoundingBox[7] = applyWorldTransformation(maxP);
- Vec3<float> worldBoundingBoxFloor[8];
- for (int i = 0; i < 8; i++)
- {
- worldBoundingBoxFloor[i] = Vec3<float>(floor(worldBoundingBox[i].x),
- floor(worldBoundingBox[i].y),
- floor(worldBoundingBox[i].z));
- }
- Vec3<float> frameSpeed = speed * (float)time;
- bool hasCollided = 0;
- for (int m = 0; m < 20; m++)
- {
- float tf = 1.f;
- int collType = 0;
- int updateType = 0;
- int updateI = 0;
- if (frameSpeed.x > 0)
- {
- for (int i = 0; i < 8; i++)
- {
- if (abs(frameSpeed.x) >= abs(worldBoundingBoxFloor[i].x
- + 1.f - worldBoundingBox[i].x))
- {
- float xt = (worldBoundingBoxFloor[i].x + 1.f
- - worldBoundingBox[i].x)
- / frameSpeed.x;
- Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * xt;
- if (tmp.y >= worldBoundingBoxFloor[i].y
- && tmp.y < worldBoundingBoxFloor[i].y + 1.f
- && tmp.z >= worldBoundingBoxFloor[i].z
- && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
- {
- Block* b = World::INSTANCE->zBlockAt(
- Vec3<int>{(int)worldBoundingBoxFloor[i].x + 1,
- (int)worldBoundingBoxFloor[i].y,
- (int)worldBoundingBoxFloor[i].z});
- if (b) // TODO: ignore passable blocks
- {
- if (xt < tf)
- {
- tf = xt;
- collType = 1;
- updateType = 0;
- }
- hasCollided = 1;
- }
- else
- {
- if (xt < tf)
- {
- tf = xt;
- collType = 0;
- updateType = 1;
- updateI = i;
- }
- }
- }
- }
- }
- }
- if (frameSpeed.x < 0)
- {
- for (int i = 0; i < 8; i++)
- {
- if (abs(frameSpeed.x) >= abs(
- worldBoundingBoxFloor[i].x - worldBoundingBox[i].x))
- {
- float xt = (worldBoundingBoxFloor[i].x
- - worldBoundingBox[i].x)
- / frameSpeed.x;
- Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * xt;
- if (tmp.y >= worldBoundingBoxFloor[i].y
- && tmp.y < worldBoundingBoxFloor[i].y + 1.f
- && tmp.z >= worldBoundingBoxFloor[i].z
- && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
- {
- Block* b = World::INSTANCE->zBlockAt(
- Vec3<int>{(int)worldBoundingBoxFloor[i].x - 1,
- (int)worldBoundingBoxFloor[i].y,
- (int)worldBoundingBoxFloor[i].z});
- if (b) // TODO: ignore passable blocks
- {
- if (xt < tf)
- {
- tf = xt;
- collType = 1;
- updateType = 0;
- }
- hasCollided = 1;
- }
- else
- {
- if (xt < tf)
- {
- tf = xt;
- collType = 0;
- updateType = 1;
- updateI = i;
- }
- }
- }
- }
- }
- }
- if (frameSpeed.y > 0)
- {
- for (int i = 0; i < 8; i++)
- {
- if (abs(frameSpeed.y) >= abs(worldBoundingBoxFloor[i].y
- + 1.f - worldBoundingBox[i].y))
- {
- float yt = (worldBoundingBoxFloor[i].y + 1.f
- - worldBoundingBox[i].y)
- / frameSpeed.y;
- Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * yt;
- if (tmp.x >= worldBoundingBoxFloor[i].x
- && tmp.x < worldBoundingBoxFloor[i].x + 1.f
- && tmp.z >= worldBoundingBoxFloor[i].z
- && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
- {
- Block* b = World::INSTANCE->zBlockAt(
- Vec3<int>{(int)worldBoundingBoxFloor[i].x,
- (int)worldBoundingBoxFloor[i].y + 1,
- (int)worldBoundingBoxFloor[i].z});
- if (b) // TODO: ignore passable blocks
- {
- if (yt < tf)
- {
- tf = yt;
- collType = 2;
- updateType = 0;
- }
- hasCollided = 1;
- }
- else
- {
- if (yt < tf)
- {
- tf = yt;
- collType = 0;
- updateType = 2;
- updateI = i;
- }
- }
- }
- }
- }
- }
- if (frameSpeed.y < 0)
- {
- for (int i = 0; i < 8; i++)
- {
- if (abs(frameSpeed.y) >= abs(
- worldBoundingBoxFloor[i].y - worldBoundingBox[i].y))
- {
- float yt = (worldBoundingBoxFloor[i].y
- - worldBoundingBox[i].y)
- / frameSpeed.y;
- Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * yt;
- if (tmp.x >= worldBoundingBoxFloor[i].x
- && tmp.x < worldBoundingBoxFloor[i].x + 1.f
- && tmp.z >= worldBoundingBoxFloor[i].z
- && tmp.z < worldBoundingBoxFloor[i].z + 1.f)
- {
- Block* b = World::INSTANCE->zBlockAt(
- Vec3<int>{(int)worldBoundingBoxFloor[i].x,
- (int)worldBoundingBoxFloor[i].y - 1,
- (int)worldBoundingBoxFloor[i].z});
- if (b) // TODO: ignore passable blocks
- {
- if (yt < tf)
- {
- tf = yt;
- collType = 2;
- updateType = 0;
- }
- hasCollided = 1;
- }
- else
- {
- if (yt < tf)
- {
- tf = yt;
- collType = 0;
- updateType = 2;
- updateI = i;
- }
- }
- }
- }
- }
- }
- if (frameSpeed.z > 0)
- {
- for (int i = 0; i < 8; i++)
- {
- if (abs(frameSpeed.z) >= abs(worldBoundingBoxFloor[i].z
- + 1.f - worldBoundingBox[i].z))
- {
- float zt = (worldBoundingBoxFloor[i].z + 1.f
- - worldBoundingBox[i].z)
- / frameSpeed.z;
- Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * zt;
- if (zt <= 1.f && tmp.x >= worldBoundingBoxFloor[i].x
- && tmp.x < worldBoundingBoxFloor[i].x + 1.f
- && tmp.y >= worldBoundingBoxFloor[i].y
- && tmp.y < worldBoundingBoxFloor[i].y + 1.f)
- {
- Block* b = World::INSTANCE->zBlockAt(
- Vec3<int>{(int)worldBoundingBoxFloor[i].x,
- (int)worldBoundingBoxFloor[i].y,
- (int)worldBoundingBoxFloor[i].z + 1});
- if (b) // TODO: ignore passable blocks
- {
- if (zt < tf)
- {
- tf = zt;
- collType = 3;
- }
- hasCollided = 1;
- }
- else
- {
- if (zt < tf)
- {
- tf = zt;
- collType = 0;
- updateType = 3;
- updateI = i;
- }
- }
- }
- }
- }
- }
- if (frameSpeed.z < 0)
- {
- for (int i = 0; i < 8; i++)
- {
- if (abs(frameSpeed.z) >= abs(
- worldBoundingBoxFloor[i].z - worldBoundingBox[i].z))
- {
- float zt = (worldBoundingBoxFloor[i].z
- - worldBoundingBox[i].z)
- / frameSpeed.z;
- Vec3<float> tmp = worldBoundingBox[i] + frameSpeed * zt;
- if (tmp.x >= worldBoundingBoxFloor[i].x
- && tmp.x < worldBoundingBoxFloor[i].x + 1.f
- && tmp.y >= worldBoundingBoxFloor[i].y
- && tmp.y < worldBoundingBoxFloor[i].y + 1)
- {
- Block* b = World::INSTANCE->zBlockAt(
- Vec3<int>{(int)worldBoundingBoxFloor[i].x,
- (int)worldBoundingBoxFloor[i].y,
- (int)worldBoundingBoxFloor[i].z - 1});
- if (b) // TODO: ignore passable blocks
- {
- if (zt < tf)
- {
- tf = zt;
- collType = 3;
- updateType = 0;
- }
- hasCollided = 1;
- }
- else
- {
- if (zt < tf)
- {
- tf = zt;
- collType = 0;
- updateType = 3;
- updateI = i;
- }
- }
- }
- }
- }
- }
- if (collType == 1)
- frameSpeed.x = tf > 0.1f ? frameSpeed.x * (tf - 0.1f) : 0.f;
- if (collType == 2)
- frameSpeed.y = tf > 0.1f ? frameSpeed.y * (tf - 0.1f) : 0.f;
- if (collType == 3)
- {
- frameSpeed.z = tf > 0.1f ? frameSpeed.z * (tf - 0.1f) : 0.f;
- if (speed.z < 0)
- {
- flags &= ~MOVEMENT_FLAG_JUMP;
- if (-speed.z > 2)
- {
- char message[5];
- message[0] = 10;
- *(float*)(message + 1) = -speed.z;
- World::INSTANCE->zClient()->sendPlayerAction(
- message, 5);
- }
- }
- speed.z = 0;
- }
- if (updateType == 1)
- {
- if ((int)worldBoundingBoxFloor[updateI].x
- <= (int)floor(worldBoundingBox[updateI].x + frameSpeed.x))
- worldBoundingBoxFloor[updateI].x++;
- if ((int)worldBoundingBoxFloor[updateI].x
- > (int)floor(worldBoundingBox[updateI].x + frameSpeed.x))
- worldBoundingBoxFloor[updateI].x--;
- }
- if (updateType == 2)
- {
- if ((int)worldBoundingBoxFloor[updateI].y
- <= (int)floor(worldBoundingBox[updateI].y + frameSpeed.y))
- worldBoundingBoxFloor[updateI].y++;
- if ((int)worldBoundingBoxFloor[updateI].y
- > (int)floor(worldBoundingBox[updateI].y + frameSpeed.y))
- worldBoundingBoxFloor[updateI].y--;
- }
- if (updateType == 3)
- {
- if ((int)worldBoundingBoxFloor[updateI].z
- <= (int)floor(worldBoundingBox[updateI].z + frameSpeed.z))
- worldBoundingBoxFloor[updateI].z++;
- if ((int)worldBoundingBoxFloor[updateI].z
- > (int)floor(worldBoundingBox[updateI].z + frameSpeed.z))
- worldBoundingBoxFloor[updateI].z--;
- }
- if (updateType || collType) continue;
- break;
- }
- pos += frameSpeed;
- World::INSTANCE->zKamera()->setPosition(
- pos + Vec3<float>(0.f, 0.f, 1.5f));
- Model3D* target = World::INSTANCE->getCurrentTarget();
- Block* b = target ? dynamic_cast<Block*>(target) : 0;
- ((Game*)(Menu*)menuRegister->get("game"))
- ->updatePosition(
- pos, b != 0, b ? b->getLocation() : Vec3<int>(0, 0, 0));
- if (target) target->release();
- if (flags != lastFlags || direction != lastDirection
- || timeSinceSync >= 1 || hasCollided)
- {
- if (timeSinceSync > 0)
- {
- MovementFrame frame;
- frame.direction = lastDirection;
- frame.targetPosition = lastPos;
- frame.movementFlags = lastFlags;
- frame.duration = timeSinceSync;
- World::INSTANCE->zClient()->sendPlayerMovement(frame);
- }
- lastFlags = flags;
- lastDirection = direction;
- timeSinceSync = 0;
- }
- timeSinceSync += time;
- rend = 1;
- }
- else
- {
- double totalTime = time;
- while (totalTime > 0)
- {
- if (currentFrame.duration <= 0)
- {
- if (movements.getEintragAnzahl() > 0)
- {
- currentFrame = movements.get(0);
- cs.lock();
- movements.remove(0);
- cs.unlock();
- }
- else
- {
- break;
- }
- }
- double t = min(currentFrame.duration, totalTime);
- pos += (currentFrame.targetPosition - pos)
- * (float)(t / currentFrame.duration);
- currentFrame.duration -= t;
- totalTime -= t;
- if (currentFrame.duration <= 0)
- {
- pos = currentFrame.targetPosition;
- }
- rend = 1;
- }
- }
- return Model3D::tick(time);
- }
- int Entity::getId() const
- {
- return id;
- }
- const EntityType* Entity::zEntityType() const
- {
- return zType;
- }
- void Entity::lock()
- {
- cs.lock();
- }
- void Entity::unlock()
- {
- cs.unlock();
- }
- void Entity::setPlayerControlled()
- {
- playerControlled = 1;
- World::INSTANCE->zKamera()->setPosition(pos + Vec3<float>(0.f, 0.f, 1.5f));
- }
|