osb/source/game/StarItemDrop.hpp

130 lines
3.8 KiB
C++
Raw Normal View History

#pragma once
2023-06-20 04:33:09 +00:00
#include "StarNetElementSystem.hpp"
#include "StarMovementController.hpp"
#include "StarItemDescriptor.hpp"
#include "StarGameTimers.hpp"
#include "StarEntity.hpp"
2023-08-18 03:06:07 +00:00
#include "StarDrawable.hpp"
2023-06-20 04:33:09 +00:00
namespace Star {
STAR_CLASS(Item);
STAR_CLASS(ItemDrop);
class ItemDrop : public virtual Entity {
public:
// Creates a drop at the given position and adds a hard-coded amount of
// randomness to the drop position / velocity.
static ItemDropPtr createRandomizedDrop(ItemPtr const& item, Vec2F const& position, bool eternal = false);
static ItemDropPtr createRandomizedDrop(ItemDescriptor const& itemDescriptor, Vec2F const& position, bool eternal = false);
// Create a drop and throw in the given direction with a hard-coded initial
// throw velocity (unrelated to magnitude of direction, direction is
// normalized first). Initially intangible for 1 second.
static ItemDropPtr throwDrop(ItemPtr const& item, Vec2F const& position, Vec2F const& velocity, Vec2F const& direction, bool eternal = false);
static ItemDropPtr throwDrop(ItemDescriptor const& itemDescriptor, Vec2F const& position, Vec2F const& velocity, Vec2F const& direction, bool eternal = false);
2023-06-20 04:33:09 +00:00
ItemDrop(ItemPtr item);
ItemDrop(Json const& diskStore);
ItemDrop(ByteArray netStore);
Json diskStore() const;
ByteArray netStore() const;
EntityType entityType() const override;
void init(World* world, EntityId entityId, EntityMode mode) override;
void uninit() override;
String description() const override;
pair<ByteArray, uint64_t> writeNetState(uint64_t fromVersion = 0) override;
void readNetState(ByteArray data, float interpolationTime = 0.0f) override;
void enableInterpolation(float extrapolationHint = 0.0f) override;
void disableInterpolation() override;
Vec2F position() const override;
RectF metaBoundBox() const override;
bool ephemeral() const override;
RectF collisionArea() const override;
void update(float dt, uint64_t currentStep) override;
2023-06-20 04:33:09 +00:00
bool shouldDestroy() const override;
virtual void render(RenderCallback* renderCallback) override;
2023-08-20 01:56:37 +00:00
virtual void renderLightSources(RenderCallback* renderCallback) override;
2023-06-20 04:33:09 +00:00
// The item that this drop contains
ItemPtr item() const;
void setEternal(bool eternal);
// If intangibleTime is set, will be intangible and unable to be picked up
// until that amount of time has passed.
void setIntangibleTime(float intangibleTime);
// Mark this drop as taken by the given entity. The drop will animate
// towards them for a while and then disappear.
ItemPtr takeBy(EntityId entityId, float timeOffset = 0.0f);
2023-06-20 04:33:09 +00:00
// Mark this drop as taken, but do not animate it towards a player simply
// disappear next step.
ItemPtr take();
// Item is not taken and is not intangible
bool canTake() const;
void setPosition(Vec2F const& position);
Vec2F velocity() const;
void setVelocity(Vec2F const& position);
private:
enum class Mode { Intangible, Available, Taken, Dead };
static EnumMap<Mode> const ModeNames;
ItemDrop();
// Set the movement controller's collision poly to match the
// item drop drawables
void updateCollisionPoly();
2023-08-18 06:45:59 +00:00
void updateTaken(bool master);
2023-06-20 04:33:09 +00:00
Json m_config;
ItemPtr m_item;
RectF m_boundBox;
2023-08-18 03:06:07 +00:00
float m_afterTakenLife;
float m_overheadTime;
float m_pickupDistance;
float m_velocity;
float m_velocityApproach;
float m_overheadApproach;
Vec2F m_overheadOffset;
float m_combineChance;
float m_combineRadius;
double m_ageItemsEvery;
2023-06-20 04:33:09 +00:00
NetElementTopGroup m_netGroup;
NetElementEnum<Mode> m_mode;
NetElementIntegral<EntityId> m_owningEntity;
NetElementData<ItemDescriptor> m_itemDescriptor;
MovementController m_movementController;
// Only updated on master
bool m_eternal;
EpochTimer m_dropAge;
GameTimer m_intangibleTimer;
EpochTimer m_ageItemsTimer;
2023-08-18 03:06:07 +00:00
bool m_overForeground;
2023-08-18 03:06:07 +00:00
Maybe<List<Drawable>> m_drawables;
2023-06-20 04:33:09 +00:00
};
}