170 lines
6.4 KiB
C++
170 lines
6.4 KiB
C++
#include "pch.h"
|
|
#include "WorldLevel.h"
|
|
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <ostream>
|
|
|
|
#include "colors.h"
|
|
#include "utils.h"
|
|
|
|
|
|
WorldLevel::WorldLevel(Camera* camera) : Level(camera), m_mousePos{ 0, 0 }, m_player(Player{ Point2f{ 0, 100 } }) {
|
|
// The grid is 34 x 50 big, the top center is 0,0 in world coords
|
|
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
|
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
|
int actualX = x - WORLD_WIDTH / 2;
|
|
m_worldTiles[x][y] = new WorldTile{ Point2f{ float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT}, GroundTileTypes::Dirt};
|
|
}
|
|
}
|
|
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{20, 40}, 40, 50, Point2f{0,0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{510.0f, 210.0f}, 30.0f, 120.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{450.0f, 150.0f}, 60.0f, 60.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{450.0f, 450.0f}, 225.0f, 60.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{510.0f, 150.0f}, 60.0f, 60.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{570.0f, 150.0f}, 60.0f, 60.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{330.0f, 150.0f}, 60.0f, 60.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{150.0f, 390.0f}, 60.0f, 60.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{150.0f, 450.0f}, 60.0f, 60.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{150.0f, 510.0f}, 60.0f, 60.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{450.0f, 300.0f}, 30.0f, 3.0f, Point2f{0, 0}});
|
|
m_Rects.push_back(utils::MovingRectf{Point2f{600.0f, 300.0f}, 60.0f, 180.0f, Point2f{0, 0}});
|
|
|
|
|
|
|
|
// std::string dirtPath = + "tiles/dirt/dirt" + std::to_string(utils::randRange(1, 5)) + ".png";
|
|
// m_pTextTexture = new Texture(dirtPath);
|
|
}
|
|
WorldLevel::~WorldLevel() {
|
|
//delete m_pTextTexture;
|
|
|
|
}
|
|
void WorldLevel::Update(float elapsedSec) {
|
|
int mouseX, mouseY;
|
|
SDL_GetMouseState(&mouseX, &mouseY);
|
|
m_mousePos = Point2f{ float(mouseX), float(mouseY) };
|
|
m_mousePos = m_pCamera->TransformMouse(m_mousePos);
|
|
//m_player.Update(elapsedSec, *this);
|
|
|
|
|
|
|
|
|
|
Point2f RayPoint = Point2f{m_pCamera->Viewport.width, m_pCamera->Viewport.height};
|
|
Point2f RayDir = Point2f{m_mousePos.x - RayPoint.x, m_mousePos.y - RayPoint.y};
|
|
|
|
//wasd movement
|
|
if(utils::isKeyDown(SDL_SCANCODE_W)) {
|
|
m_Rects[0].velocity.y += 10;
|
|
}
|
|
if(utils::isKeyDown(SDL_SCANCODE_S)) {
|
|
m_Rects[0].velocity.y += -10;
|
|
}
|
|
if(utils::isKeyDown(SDL_SCANCODE_A)) {
|
|
m_Rects[0].velocity.x += -10;
|
|
}
|
|
if(utils::isKeyDown(SDL_SCANCODE_D)) {
|
|
m_Rects[0].velocity.x += 10;
|
|
}
|
|
|
|
Point2f intersectionPoint, normal;
|
|
float contactTime;
|
|
|
|
std::vector<std::pair<int, float>> contactTimes{};
|
|
|
|
for (size_t i { 1 }; i < m_Rects.size(); ++i) {
|
|
Rectf target = Rectf{m_Rects[i].bottomLeft.x, m_Rects[i].bottomLeft.y, m_Rects[i].width, m_Rects[i].height};
|
|
if(utils::DynamicRectVsRect(m_Rects[0], target, intersectionPoint, normal, contactTime, elapsedSec)) {
|
|
// m_Rects[0].velocity.x += normal.x * (1 - contactTime) * -utils::DotProduct( m_Rects[0].velocity, normal);
|
|
// m_Rects[0].velocity.y += normal.y * (1 - contactTime) * -utils::DotProduct( m_Rects[0].velocity, normal);
|
|
//
|
|
// //m_Rects[0].velocity = Point2f{0, 0};
|
|
|
|
contactTimes.push_back({i, contactTime});
|
|
}
|
|
}
|
|
|
|
std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b) {
|
|
return a.second < b.second;
|
|
});
|
|
|
|
for (std::pair<int, float> contact : contactTimes) {
|
|
Rectf target = Rectf{m_Rects[contact.first].bottomLeft.x, m_Rects[contact.first].bottomLeft.y, m_Rects[contact.first].width, m_Rects[contact.first].height};
|
|
if(utils::DynamicRectVsRect(m_Rects[0], target, intersectionPoint, normal, contactTime, elapsedSec)) {
|
|
m_Rects[0].velocity.x += normal.x * (1 - contactTime) * -utils::DotProduct( m_Rects[0].velocity, normal);
|
|
m_Rects[0].velocity.y += normal.y * (1 - contactTime) * -utils::DotProduct( m_Rects[0].velocity, normal);
|
|
}
|
|
}
|
|
|
|
m_Rects[0].bottomLeft.x = m_Rects[0].bottomLeft.x + m_Rects[0].velocity.x * elapsedSec;
|
|
m_Rects[0].bottomLeft.y = m_Rects[0].bottomLeft.y + m_Rects[0].velocity.y * elapsedSec;
|
|
|
|
}
|
|
void WorldLevel::Draw() const {
|
|
m_pCamera->BeginRendering();
|
|
|
|
for (utils::MovingRectf rect : m_Rects) {
|
|
utils::DrawRect(rect.bottomLeft, rect.width, rect.height);
|
|
}
|
|
|
|
Rectf expanded = Rectf{ m_Rects[1].bottomLeft.x - m_Rects[0].width / 2, m_Rects[1].bottomLeft.y - m_Rects[0].height / 2, m_Rects[1].width + m_Rects[0].width, m_Rects[1].height + m_Rects[0].height };
|
|
utils::SetColor(Colors::RED);
|
|
utils::DrawRect(expanded);
|
|
|
|
|
|
Point2f RayPoint = Point2f{m_pCamera->Viewport.width, m_pCamera->Viewport.height};
|
|
Point2f RayDir = Point2f{m_mousePos.x - RayPoint.x, m_mousePos.y - RayPoint.y};
|
|
|
|
utils::SetColor(Colors::WHITE);
|
|
utils::DrawLine(RayPoint, m_mousePos);
|
|
|
|
utils::FillEllipse(m_mousePos, 20, 20);
|
|
|
|
Point2f intersectionPoint, normal;
|
|
float contactTime;
|
|
Rectf testRect = Rectf{m_Rects[1].bottomLeft.x, m_Rects[1].bottomLeft.y, m_Rects[1].width, m_Rects[1].height};
|
|
if(utils::RayVsRect(RayPoint, RayDir, testRect, intersectionPoint, normal, contactTime) && contactTime > 0.0f && contactTime < 1.0f) {
|
|
utils::SetColor(Colors::GREEN);
|
|
utils::FillEllipse(intersectionPoint, 5, 5);
|
|
utils::DrawLine(intersectionPoint, Point2f{intersectionPoint.x + normal.x * 50, intersectionPoint.y + normal.y * 50});
|
|
}
|
|
|
|
|
|
// utils::SetColor(Colors::YELLOW);
|
|
// utils::FillEllipse(m_mousePos,2,2);
|
|
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
|
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
|
m_worldTiles[x][y]->Draw();
|
|
}
|
|
}
|
|
|
|
// utils::SetColor(Colors::WHITE);
|
|
// for (int x { -100 }; x < 100; ++x) {
|
|
// for (int y { -100 }; y < 100; ++y) {
|
|
// utils::DrawLine(x * 50, -5000, x * 50, 50000);
|
|
// utils::DrawLine(-5000, y * 50, 50000, y * 50);
|
|
// }
|
|
// }
|
|
utils::SetColor(Colors::MAGENTA);
|
|
utils::FillEllipse(0, 0, 5, 5);
|
|
|
|
//m_player.Draw();
|
|
m_pCamera->EndRendering();
|
|
|
|
//utils::SetColor(Colors::WHITE);
|
|
//m_pTextTexture->Draw(Point2f{ 0, 0 });
|
|
|
|
}
|
|
void WorldLevel::MouseMove(const Point2f& mousePos) {
|
|
m_mousePos = mousePos;
|
|
}
|
|
WorldTile* WorldLevel::GetTileAt(const Point2f& pos) const {
|
|
return nullptr;
|
|
}
|
|
void WorldLevel::SetTileAt(const Point2f& pos, WorldTile* tile) {
|
|
}
|
|
std::array<std::array<WorldTile*, WorldLevel::WORLD_WIDTH>, WorldLevel::WORLD_HEIGHT> WorldLevel::GetAllTiles() const {
|
|
return m_worldTiles;
|
|
}
|
|
|