#include "entity.h" #include "entities/antifaShip.h" // This fucker is used for creating entities. const EntityTypeInfo entityTypeInfo[ENTITY_TYPE_COUNT] = { (EntityTypeInfo){initAntifaShip, closeAntifaShip, updateAntifaShip, drawAntifaShip} }; Entity createEntity(EntityType type) { EntityTypeInfo info = entityTypeInfo[type]; // Set defaults. Entity entity = (Entity){ .type = type, .position = Vector3Zero(), .angularVelocity = 0, .rotationAxis = Vector3Zero(), .velocity = Vector3Zero(), .rotation = QuaternionIdentity(), .updateCb = info.updateCb, .drawCb = info.drawCb, .data = NULL }; // Init. info.initCb(&entity); return entity; } void closeEntity(Entity * entity) { entityTypeInfo[entity->type].closeCb(entity); } // Basic wireframe drawing. void entityDraw(Entity * entity) { entity->model.transform = QuaternionToMatrix(entity->rotation); DrawModelWires( entity->model, entity->position, 1, GREEN ); } void entityUpdatePosition(Entity * entity) { float t = GetFrameTime(); Vector3 velocity = (Vector3){ entity->velocity.x * t, entity->velocity.y * t, entity->velocity.z * t }; entity->position = Vector3Add(entity->position, velocity); } void entityUpdateRotation(Entity * entity) { float t = GetFrameTime(); Quaternion angularRotation = QuaternionFromAxisAngle( entity->rotationAxis, entity->angularVelocity * t ); entity->rotation = QuaternionMultiply(entity->rotation, angularRotation); } void entityJoystickControl(Entity * entity, Vector3 stick, float speed) { // Set angular velocity. Vector3 angularVelocity = Vector3Scale(stick, PI); entity->angularVelocity = Vector3Length(angularVelocity); entity->rotationAxis = stick; entityUpdateRotation(entity); // Set position. Matrix m = QuaternionToMatrix(QuaternionInvert(entity->rotation)); entity->velocity = (Vector3){ m.m2 * speed, m.m6 * speed, m.m10 * speed, }; entityUpdatePosition(entity); }