bool Unit::Move(float dt) { bool ret = true; C_Vec2<float> vel = currentVelocity * dt; //Continuous evaluation if (App->entityManager->continuous) { //TODO 3: Split the velocity in parts and check for the target //------------------------------------------------------- //Splitting the velocity into smaller pieces to check if the unit reaches the target float module = vel.GetModule(); int steps = (int)floor(vel.GetModule() / targetRadius); C_Vec2<float> velStep = (vel.GetNormal() * targetRadius); C_Vec2<float> rest = vel - velStep * (float)steps; for (int i = 0; i < steps && ret; i++) { position.x += velStep.x; position.y += velStep.y; if (isTargetReached()) ret = false; } if (ret) { position.x += rest.x; position.y += rest.y; if (isTargetReached()) ret = false; } //------------------------------------------------------- } //Normal movement else { //TODO 1: Move the unit with the velocity obtained previously //------------------------------------------------------ position.x += vel.x; position.y += vel.y; //----------------------------------------------------- if (isTargetReached()) ret = false; } currentVelocity.position = desiredVelocity.position = position; UpdateCollider(); return ret; }
bool Unit::isTargetReached() { //TODO 2: Check if we have reached the target //------------------------------------------------------ C_Vec2<float> vec; vec.x = target.x - position.x; vec.y = target.y - position.y; float distance = vec.GetModule(); if (distance < targetRadius) { position.x = (float)target.x; position.y = (float)target.y; currentVelocity.position = desiredVelocity.position = position; return true; } //----------------------------------------------------- return false; }
float Bot::DistanceBetweenUnits(Unit* unit1, Unit* unit2) { C_Vec2<float> distance = { unit1->GetPosition().x - unit2->GetPosition().x, unit1->GetPosition().y - unit2->GetPosition().y }; return distance.GetModule(); }
// Useful Distance functions bool Bot::EnemyOnRange(Unit* unit1, Unit* unit2, float range) { C_Vec2<float> distance = { unit1->GetPosition().x - unit2->GetPosition().x, unit1->GetPosition().y - unit2->GetPosition().y }; return (distance.GetModule() < range + unit1->colRadius + unit2->colRadius); }