Untitled

mail@pastecode.io avatar
unknown
c_cpp
17 days ago
1.4 kB
2
Indexable
Never
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);
        } 
	}
}
Leave a Comment