| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #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 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<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();
- }
- 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));
- }
|