/
Kotopeska
/
RiseUpGame
Обзор
Документация
Войти
/
Kotopeska
/
RiseUpGame
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
model/Obstacle.cpp
117 строк
3 KB
Kotopeska
init
18 май 2025, 19:03
18 май 2025, 19:03
668dfef
Код
Авторство
О чём код?
#include "Obstacle.h" #include <cmath> namespace Model { Obstacle::Obstacle(float x, float y, float width, float height, ObstacleType type) : GameObject(x, y, width, height, "Obstacle"), m_type(type), m_color(ObstacleColor::RED), m_velocityX(0.0f), m_velocityY(0.0f), m_rotationAngle(0.0f), m_rotationSpeed(0.0f), m_isColliding(false) { } Obstacle::~Obstacle() { } void Obstacle::update() { setX(getX() + m_velocityX); setY(getY() + m_velocityY); m_rotationAngle += m_rotationSpeed; if (m_rotationAngle >= 360.0f) { m_rotationAngle -= 360.0f; } if (m_isColliding) { m_velocityX *= 0.98f; m_velocityY *= 0.98f; if (std::abs(m_velocityX) < 0.1f && std::abs(m_velocityY) < 0.1f) { m_isColliding = false; } } } void Obstacle::reset() { m_velocityX = 0.0f; m_velocityY = 0.0f; m_rotationAngle = 0.0f; m_rotationSpeed = 0.0f; m_isColliding = false; } bool Obstacle::isOffScreen(int screenWidth, int screenHeight) const { const float buffer = 100.0f; return (getX() + getWidth() < -buffer || getX() > screenWidth + buffer || getY() + getHeight() < -buffer || getY() > screenHeight + buffer); } void Obstacle::applyForce(float forceX, float forceY) { m_velocityX += forceX; m_velocityY += forceY; m_isColliding = true; const float maxSpeed = 15.0f; float speed = std::sqrt(m_velocityX * m_velocityX + m_velocityY * m_velocityY); if (speed > maxSpeed) { float ratio = maxSpeed / speed; m_velocityX *= ratio; m_velocityY *= ratio; } } Obstacle Obstacle::createRandom(int screenWidth, int screenHeight, std::mt19937 &rng) { std::uniform_int_distribution<int> typeDist(0, 2); std::uniform_real_distribution<float> sizeDist(30.0f, 60.0f); std::uniform_real_distribution<float> speedDist(0.3f, 1.0f); std::uniform_real_distribution<float> rotationDist(0.1f, 0.5f); std::uniform_int_distribution<int> colorDist(0, 6); ObstacleType type = static_cast<ObstacleType>(typeDist(rng)); float size = sizeDist(rng); float width = size; float height = size; if (type == ObstacleType::RECTANGLE) { width = sizeDist(rng); height = sizeDist(rng); } float x = std::uniform_real_distribution<float>(0.0f, screenWidth - width)(rng); float y = -height - std::uniform_real_distribution<float>(0.0f, 100.0f)(rng); Obstacle obstacle(x, y, width, height, type); obstacle.setColor(static_cast<ObstacleColor>(colorDist(rng))); float speed = speedDist(rng); obstacle.setVelocityX(std::uniform_real_distribution<float>(-0.5f, 0.5f)(rng)); obstacle.setVelocityY(speed + 0.5f); float rotationDirection = (std::uniform_real_distribution<float>(0.0f, 1.0f)(rng) > 0.5f ? 1.0f : -1.0f); obstacle.m_rotationSpeed = rotationDist(rng) * rotationDirection; return obstacle; } } // namespace Model