#include "caporale.h" #include "assets.h" #include "game.h" #include #include void initCaporale(Entity * entity, Game * game) { entity->model = &game->assets.models[CAPORATE_ASSET]; entity->collisionModel = entityCreateCollisionModel(*entity->model); entity->transformedCollisionModel = entityCreateCollisionModel(*entity->model); setEntityRadius(entity); // Allocate data. entity->data = KF_MALLOC(sizeof(Caporale)); if (entity->data == NULL) { ALLOCATION_ERROR; return; } Caporale * data = (Caporale*)entity->data; data->flyToPlayer = (EntityFlyToPointInfo){ .controller.bangbang.speed = 80, .controller.bangbang.stopAt = 0.0, .controlType = ENTITY_FLY_TO_POINT_BANG_BANG, .rotationSpeed = 0.5, .applyRotation = true }; data->timeSinceLastShot = 0.0; } void closeCaporale(Entity * entity) { if (entity->data != NULL) KF_FREE(entity->data); entityFreeCollisionModel(entity->collisionModel); entityFreeCollisionModel(entity->transformedCollisionModel); } void updateGunsCaporale(Game * game, Entity * entity) { double t = GetTime(); Caporale * data = (Caporale*)entity->data; Entity * player = getEntityFromWorld(game->world, 0); // Cool down. if (t - data->timeSinceLastShot < CAPORALE_COOLDOWN) return; data->timeSinceLastShot = t; // Use random number to decide if hit. SetRandomSeed(time(NULL)); if (GetRandomValue(1, CAPORALE_CHANGE_OF_HIT) != 1) return; // If random thingy shoot bullet that can't miss. // Get direction. Vector3 direction = Vector3Subtract(player->position, entity->position); direction = Vector3Normalize(direction); float damage = CAPORALE_BULLET_DAMAGE; if (entity->health <= CAPORALE_LOW_HEALTH_THRESHOLD) damage = CAPORALE_LOW_HEALTH_BULLET_DAMAGE; // Create bullet and shoot. Bullet bullet = createBulletFromDirection(*entity, direction, damage); shootBulletAtEntity(player, bullet); } void updateCaporale(Game * game, Entity * entity) { entityUpdateLastValues(entity); Caporale * data = (Caporale*)entity->data; Entity * player = getEntityFromWorld(game->world, 0); entityFlyToPoint(entity, player->position, &data->flyToPlayer); updateGunsCaporale(game, entity); entityCheckTransformedCollisionModel(entity); } void drawCaporale(Game * game, Entity * entity) { entityDraw(entity); }