12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- #include "ItemEntity.h"
- #include "Game.h"
- ItemEntity::ItemEntity(Framework::Vec3<float> location, int dimensionId, int entityId)
- : Entity(ItemEntityType::INSTANCE, location, dimensionId, entityId)
- {
- slot = new ItemSlot("Inventory", __INT32_MAX__, 0, 0, 0, ANY_DIRECTION, 0);
- addSlot(slot);
- faceOffset = { 0.f, 0.f, 0.f };
- maxHP = 10;
- currentHP = 10;
- stamina = 10;
- maxStamina = 10;
- hunger = 10;
- maxHunger = 10;
- thirst = 10;
- maxThirst = 10;
- targetDistanceLimit = 4;
- }
- void ItemEntity::tick(const Dimension* zDimension)
- {
- if (slot->zStack() == 0 && !removed)
- throw "Illegal State exception";
- // add speed to next entity with free inventory
- Entity* zOther = Game::INSTANCE->zNearestEntity(currentDimensionId, location, [this](Entity* zOther)
- {
- return zOther != this && zOther->numberOfAddableItems(slot->zStack(), NO_DIRECTION);
- });
- bool found = 1;
- if (zOther)
- {
- float d = location.abstand(zOther->getPosition());
- if (d < 0.5f)
- {
- // add items of this entity to the other entity
- zOther->interactWith(this, NO_DIRECTION).pullItems(slot->getNumberOfItems(), 0);
- if (slot->getNumberOfItems() == 0)
- onDeath();
- }
- else if (d < 3.f)
- {
- // accelerate towards of the other entity
- speed += (zOther->getPosition() - location).normalize() * (20 / (d + 0.5f)) / 30.f;
- }
- else
- found = 0;
- }
- else
- found = 0;
- if (!found)
- {
- speed -= speed / 30.f;
- if (speed.getLength() < 0.2f)
- speed = { 0.f, 0.f, 0.f };
- }
- Entity::tick(zDimension);
- }
- void ItemEntity::onFall(float collisionSpeed)
- {
- if (collisionSpeed >= 50.f)
- this->currentHP = 0;
- }
- bool ItemEntity::hasDefaultModel() const
- {
- return 0;
- }
- ModelInfo ItemEntity::getSpecialModel() const
- {
- const ItemType* zItemType = 0;
- if (!slot->isEmpty())
- zItemType = slot->zStack()->zItem()->zItemType();
- return !zItemType ? ModelInfo("", "", 0) : zItemType->getModel();
- }
- ItemEntityType::ItemEntityType()
- : EntityType(ID, ModelInfo("", "", 0))
- {}
- Entity* ItemEntityType::createEntity(Framework::Vec3<float> position, int dimensionId, int entityId) const
- {
- return new ItemEntity(position, dimensionId, entityId);
- }
|