#include "Entity.h" #include #include #include "Game.h" #include "Globals.h" Entity::Entity(const EntityType* zType, Framework::Model3DData* model, Framework::Model3DTextur* texture, int id, Framework::Vec3 position, float size) : FactoryCraftModel(), id(id), zType(zType), playerControlled(0) { pos = position; setModelDaten(model); setModelTextur(texture); currentFrame.duration = 0; rend = 1; setSize(size); } Entity::~Entity() {} void Entity::api(char* message) { switch (message[0]) { case 0: { // add movement frame MovementFrame frame; frame.position.x = *(float*)(message += 1); frame.position.y = *(float*)(message += 4); frame.position.z = *(float*)(message += 4); frame.rotation = *(float*)(message += 4); frame.duration = *(float*)(message += 4); cs.lock(); frames.add(frame); if (frames.getEintragAnzahl() > 10) { frames.remove(0); } cs.unlock(); break; } } } bool Entity::tick(double time) { double totalTime = time; cs.lock(); while (totalTime > 0) { if (currentFrame.duration <= 0) { if (frames.getEintragAnzahl() > 0) { currentFrame = frames.get(0); frames.remove(0); } else { break; } } float t = min(currentFrame.duration, (float)totalTime); pos += (currentFrame.position - pos) * (t / currentFrame.duration); if (getZDrehung() - currentFrame.rotation > (float)PI) { setDrehungZ(getZDrehung() - 2.f * (float)PI); } else if (currentFrame.rotation - getZDrehung() > (float)PI) { setDrehungZ(getZDrehung() + 2.f * (float)PI); } setDrehungZ(getZDrehung() + (currentFrame.rotation - getZDrehung()) * (t / currentFrame.duration)); currentFrame.duration -= t; totalTime -= t; if (currentFrame.duration <= 0) { pos = currentFrame.position; setDrehungZ(currentFrame.rotation); } rend = 1; } cs.unlock(); if (playerControlled) { World::INSTANCE->zKamera()->setPosition( pos + Vec3(0.f, 0.f, 1.5f)); Model3D* target = World::INSTANCE->getCurrentTarget(); Block* b = target ? dynamic_cast(target) : 0; ((Game*)(Menu*)menuRegister->get("game")) ->updatePosition( pos, b != 0, b ? b->getLocation() : Vec3(0, 0, 0)); if (target) target->release(); } 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(0.f, 0.f, 1.5f)); }