void Application::touch(float x, float y) { b2AABB aabb; aabb.lowerBound.x = toWorld(x - 1); aabb.lowerBound.y = toWorld(y - 1); aabb.upperBound.x = toWorld(x + 1); aabb.upperBound.y = toWorld(y + 1); m_world.QueryAABB(this, aabb); }
Application::Application() : m_world(b2Vec2(0, -2)) , m_step(0) , m_appleInterval(100) , m_score(0) , m_lives(10) , m_grass(m_world) , m_leftBound(m_world) , m_rightBound(m_world) { m_world.SetContactListener(this); m_leftBound.setPosition(0, toWorld(FRUITCLICK_HEIGHT / 2)); m_rightBound.setPosition(toWorld(FRUITCLICK_WIDTH), toWorld(FRUITCLICK_HEIGHT / 2)); m_grass.setPosition(toWorld(FRUITCLICK_WIDTH / 2), 0); }
void Application::spawnApple() { if (m_step % m_appleInterval == 0) { float x = (rand() % (FRUITCLICK_WIDTH - FRUITCLICK_APPLE_RADIUS*2)) + FRUITCLICK_APPLE_RADIUS; float y = FRUITCLICK_HEIGHT + 10; float vx = (rand() % 9) - 4; float vy = -3; Apple* apple = new Apple(m_world); apple->setPosition(toWorld(x), toWorld(y)); apple->setVelocity(vx, vy); apple->setAngularVelocity((rand() % 32) - 16); m_apples.insert(apple); if (m_appleInterval > 25) m_appleInterval -= 2; m_step = 0; } }
// Wraps objects that go outside the screen's bounds void wrapPosition(object *o) { // Convert object position to screen space vector toCheck = fromWorld(&(o->pos)); // If a laser, deactivate the object if (o->type == LAZOR && (toCheck.x > 1.0 || toCheck.x < -1.0 || toCheck.y > 1.0 || toCheck.y < -1.0)) { o->state &= !LAZER_ACTIVE; return; } // Adjust position if (toCheck.x > 1.0) toCheck.x = -1.0; else if (toCheck.x < -1.0) toCheck.x = 1.0; if (toCheck.y > 1.0) toCheck.y = -1.0; else if (toCheck.y < -1.0) toCheck.y = 1.0; // Convert position back to world space o->pos = toWorld(&toCheck); }