#include #include "Entity.h" #include "Globals.h" #include "Game.h" Entity::Entity(const EntityType* zType, Framework::Model3DData* model, Framework::Model3DTextur* texture, int id, Framework::Vec3 position, float maxMovementSpeed) : Model3D(), id(id), zType(zType), playerControlled(0), maxMovementSpeed(maxMovementSpeed), lastFlags(0), timeSinceSync(0), speed(0, 0, 0) { pos = position; setModelDaten(model); setModelTextur(texture); lastDirection = currentGame->zKamera()->getDirection(); currentFrame.duration = 0; rend = 1; } Entity::~Entity() {} void Entity::api(char* message) { switch (message[0]) { case 0: { // add movement frame if (!playerControlled) { 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); 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 = currentGame->zKamera()->getDirection(); lastFlags = 0; } break; } } } bool Entity::tick(double time) { if (playerControlled && GetForegroundWindow() == window->getFensterHandle()) { Vec3 direction = currentGame->zKamera()->getDirection(); Vec3 lastPos = pos; int flags = 0; speed = { 0, 0, speed.z }; if (GetKeyState('w') & 0x8000 || GetKeyState('W') & 0x8000) { flags |= 1; speed += {direction.x, direction.y, 0}; } if (GetKeyState('a') & 0x8000 || GetKeyState('A') & 0x8000) { flags |= 2; Vec2 norm = { direction.x, direction.y }; norm.CCW90().normalize(); speed += {norm.x, norm.y, 0}; } if (GetKeyState('s') & 0x8000 || GetKeyState('S') & 0x8000) { flags |= 4; speed += {-direction.x, -direction.y, 0}; } if (GetKeyState('d') & 0x8000 || GetKeyState('D') & 0x8000) { flags |= 8; Vec2 norm = { direction.x, direction.y }; norm.CW90().normalize(); speed += {norm.x, norm.y, 0}; } if (GetKeyState(T_Shift) & 0x8000) { flags |= 16; speed.z = -maxMovementSpeed; } else if (GetKeyState(T_Space) & 0x8000) { flags |= 32; speed.z = maxMovementSpeed; } else { speed.z = 0.f; } Vec2 norm = { speed.x, speed.y }; if (norm.getLengthSq() != 0) { norm.normalize(); speed.x = norm.x * maxMovementSpeed; speed.y = norm.y * maxMovementSpeed; } // TODO: collision check pos += speed * (float)time; currentGame->zKamera()->setPosition(pos + Vec3(0.f, 0.f, 1.5f)); ((Game*)(Menu*)menuRegister->get("game"))->updatePosition(pos, 0, { 0, 0, 0 }); if (flags != lastFlags || direction != lastDirection || timeSinceSync >= 1) { if (timeSinceSync > 0) { MovementFrame frame; frame.direction = lastDirection; frame.targetPosition = lastPos; frame.movementFlags = lastFlags; frame.duration = timeSinceSync; network->zFactoryClient()->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(totalTime, currentFrame.duration); speed = { 0, 0, speed.z }; if ((currentFrame.movementFlags | 1) == currentFrame.movementFlags) { speed += {currentFrame.direction.x, currentFrame.direction.y, 0}; } if ((currentFrame.movementFlags | 2) == currentFrame.movementFlags) { Vec2 norm = { currentFrame.direction.x, currentFrame.direction.y }; norm.CCW90().normalize(); speed += {norm.x, norm.y, 0}; } if ((currentFrame.movementFlags | 4) == currentFrame.movementFlags) { speed += {-currentFrame.direction.x, -currentFrame.direction.y, 0}; } if ((currentFrame.movementFlags | 8) == currentFrame.movementFlags) { Vec2 norm = { currentFrame.direction.x, currentFrame.direction.y }; norm.CW90().normalize(); speed += {norm.x, norm.y, 0}; } if ((currentFrame.movementFlags | 16) == currentFrame.movementFlags) { speed.z = -maxMovementSpeed; } else if ((currentFrame.movementFlags | 32) == currentFrame.movementFlags) { speed.z = maxMovementSpeed; } else { speed.z = 0.f; } Vec2 norm = { speed.x, speed.y }; if (norm.getLengthSq() != 0) { norm.normalize(); speed.x = norm.x * maxMovementSpeed; speed.y = norm.y * maxMovementSpeed; } // TODO: collision check pos += speed * (float)t; 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; currentGame->zKamera()->setPosition(pos + Vec3(0.f, 0.f, 1.5f)); }