Pick-up item animation
unknown
c_cpp
20 days ago
2.3 kB
4
Indexable
Never
const float player_pickup_radius = 50.0; typedef struct Entity { bool is_valid; EntityArchetype arch; Vector2 pos; Vector2 velocity; // NEW bool render_sprite; enum SpriteID sprite_id; int health; bool destroyable_world_item; bool is_item; bool is_being_picked_up; // NEW } Entity; void update_entity_position(Entity* entity, Entity* player, float delta_time) { // Multiplier for acceleration float attraction_force_multiplier = 5.0f; // Sharp turn functionality (minimize orbit of item) const float sharp_turn_pick_up_radius = 20.0f; const float sharp_turn_factor_min = 1.0f; const float sharp_turn_factor_max = 100.0f; // Item pick-up radius const float item_pickup_radius = 8.0f; // Number of items to add const int number_of_items = 1; if (entity->is_being_picked_up) { Vector2 direction = v2_normalize(v2_sub(player->pos, entity->pos)); float distance = v2_dist(entity->pos, player->pos); float attraction_force = distance * attraction_force_multiplier; float sharp_turn_factor = sharp_turn_factor_min; if (distance <= sharp_turn_pick_up_radius) { sharp_turn_factor = sharp_turn_factor_max; } Vector2 new_velocity = v2_mulf(direction, attraction_force * sharp_turn_factor * delta_time); entity->velocity = v2_add(entity->velocity, new_velocity); entity->pos = v2_add(entity->pos, v2_mulf(entity->velocity, delta_time)); // Check if the entity is close enough to be considered picked up if (distance <= item_pickup_radius) { world->inventory_items[entity->arch].amount += number_of_items; entity_destroy(entity); } } } // !!!!!This is inside the game-loop!!!!! // :update entities { for (int i = 0; i < MAX_ENTITY_COUNT; i++) { Entity* entity = &world->entities[i]; if (entity->is_valid) { // Pick up item if (entity->is_item) { // Pick up item if (entity->is_item && !entity->is_being_picked_up) { if (fabsf(v2_dist(entity->pos, player_en->pos)) < player_pickup_radius) { entity->is_being_picked_up = true; } } update_entity_position(entity, player_en, delta_t); } } } }
Leave a Comment