From d05b2c28528166eaea8b1be6d47a0d31795ac5f2 Mon Sep 17 00:00:00 2001 From: Blxssy Date: Fri, 10 May 2024 22:43:46 +0300 Subject: [PATCH] belt --- include/belt.h | 26 +++++++++++++++++++++++ include/gameManager.h | 9 +++++++- include/hero.h | 4 ++++ src/belt.cpp | 29 ++++++++++++++++++++++++++ src/cell.cpp | 4 +++- src/gameManager.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++ src/hero.cpp | 19 ++++++++++++++++- src/inventory.cpp | 41 ++++++++++++++++++++++++++++++------ src/main.cpp | 5 +++-- 9 files changed, 174 insertions(+), 11 deletions(-) create mode 100644 include/belt.h create mode 100644 src/belt.cpp diff --git a/include/belt.h b/include/belt.h new file mode 100644 index 0000000..455b656 --- /dev/null +++ b/include/belt.h @@ -0,0 +1,26 @@ +#pragma once + +#ifndef BELT_H +#define BELT_H + +#include + +#include "object.h" +#include "cell.h" + + +class Belt : public Object { +private: + char direction; + +public: + Belt(float x, float y, int h, int w, char ic, Color col, char dir) + : Object(x, y, h, w, ic, col), direction(dir) {} + + char GetDirection(); + + void Action(Cell** cells); +}; + +//34 +#endif \ No newline at end of file diff --git a/include/gameManager.h b/include/gameManager.h index b7a0fd7..e086c1c 100644 --- a/include/gameManager.h +++ b/include/gameManager.h @@ -3,16 +3,19 @@ #ifndef GAMEMANAGER_H #define GAMEMANAGER_H +#include "belt.h" #include "map.h" #include "hero.h" - #include "../raylib/src/raylib.h" +#include + class GameManager{ private: Hero* hero; Map* map; Camera2D* cam; + std::vector belts; public: GameManager(Map* m, Hero* h, Camera2D* c); @@ -21,6 +24,10 @@ class GameManager{ void Update(); void Show(); + + void BeltsAction(); + + void PrintMap(); }; #endif \ No newline at end of file diff --git a/include/hero.h b/include/hero.h index ff1bc1d..06a9e79 100644 --- a/include/hero.h +++ b/include/hero.h @@ -8,6 +8,7 @@ #include "cell.h" #include "ironItem.h" #include "copperItem.h" +#include "belt.h" #include "../raylib/src/raylib.h" @@ -29,6 +30,9 @@ class Hero : public Object{ void Drop(Cell** cells); + void PlaceItems(Cell** cells, std::vector& belts); + + void PickItem(); private: Inventory* inventory; diff --git a/src/belt.cpp b/src/belt.cpp new file mode 100644 index 0000000..63960d5 --- /dev/null +++ b/src/belt.cpp @@ -0,0 +1,29 @@ +#include "../include/belt.h" + +char Belt::GetDirection() { + return direction; +} + +void Belt::Action(Cell** cells) { + int j = std::floor(position.x/ 40); + int i = std::floor(position.y / 40); + Object* obj = cells[i][j].getObject(); + + if(obj->getIcon() == 'b' || obj->getIcon() == '#'){ + return; + } + if(direction == 'u'){ + cells[i - 1][j].setObject(obj); + obj->object.y = (i - 1) * 40 + 10; + cells[i][j].removeObject(); + } else if(direction == 'd'){ + cells[i +1][j].setObject(obj); + cells[i][j].removeObject(); + } else if(direction == 'l'){ + cells[i][j - 1].setObject(obj); + cells[i][j].removeObject(); + } else if(direction == 'r'){ + cells[i][j + 1].setObject(obj); + cells[i][j].removeObject(); + } +} diff --git a/src/cell.cpp b/src/cell.cpp index dbccdf7..5e46e63 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -17,8 +17,10 @@ void Cell::setObject(Object* object) { } void Cell::removeObject(){ + if(objects.empty()){ + return; + } objects.pop_back(); - } Object* Cell::getObjectUnderHero(){ diff --git a/src/gameManager.cpp b/src/gameManager.cpp index eddd159..75bc4b0 100644 --- a/src/gameManager.cpp +++ b/src/gameManager.cpp @@ -2,14 +2,19 @@ #include #include +using namespace std; + GameManager::GameManager(Map* m, Hero* h, Camera2D* c){ map = m; hero = h; cam = c; + // belts = std::vector(); } void GameManager::Update() { hero->Update(); + BeltsAction(); + hero->PlaceItems(map->getCells(), belts); if(IsKeyPressed(KEY_E)){ hero->Dig(map->getCells()); @@ -29,6 +34,8 @@ void GameManager::Update() { cam->target.y = heroPosition.y; hero->getInventory()->showItems(cam->target); + + } void GameManager::detectCollision(){ @@ -51,6 +58,15 @@ void GameManager::Show(){ int x = hero->getPosition().x + 600; int y = hero->getPosition().y - 320; + // int x = GetScreenWidth(); + // int y = GetScreenWidth(); + + // map->getCells()[1080 / 40][1920 / 40].getObject()->setColor(RED); + + + // double x = hero->getPosition().x + GetScreenWidth() / 4; + // double y = hero->getPosition().y + GetScreenHeight()/ 4; + for (int i = 0; i < objects.size(); i++) { std::stringstream ss; @@ -62,3 +78,35 @@ void GameManager::Show(){ } } +void GameManager::BeltsAction() { + if (belts.empty()) + { + return; + } + + for (auto &belt : belts) + { + belt->Action(map->getCells()); + } + +} + +void GameManager::PrintMap() { + int j = std::floor(hero->getPosition().x / 40); + int i = std::floor(hero->getPosition().y / 40); + + for (int a = -10; a < 10; a++) + { + for (int b = -10; b < 10; b++) + { + if(i + a < 0 || j + a < 0) { + return; + } + auto objects = map->getCells()[i + a][j + b].getObjects(); + for (auto &obj : objects) + { + obj->draw(); + } + } + } +} diff --git a/src/hero.cpp b/src/hero.cpp index 98ebb18..8454fed 100644 --- a/src/hero.cpp +++ b/src/hero.cpp @@ -71,7 +71,7 @@ void Hero::Drop(Cell** cells){ return; } - auto newItem = new Object(this->position.x, this->position.y, 20, 20, activeItem->getIcon(), activeItem->getColor()); + auto newItem = new Object(j * 40 + 10, i * 40 + 10, 20, 20, activeItem->getIcon(), activeItem->getColor()); // activeItem->object.x = this->position.x; // activeItem->object.y = this->position.y; @@ -81,4 +81,21 @@ void Hero::Drop(Cell** cells){ cells[i][j].setObject(newItem); inventory->decreaseItemCount(); +} + +void Hero::PlaceItems(Cell** cells, std::vector& belts) { + int j = std::floor(this->position.x / 40); + int i = std::floor(this->position.y / 40) - 1; + if(IsKeyPressed(KEY_C)) { + auto belt = new Belt(j * 40 + 5, i * 40 + 5, 30, 30, 'b', BLUE, 'u'); + cells[i][j].setObject(belt); + belts.push_back(belt); + } +} + +void Hero::PickItem(){ + int j = std::floor(this->position.x / 40); + int i = std::floor(this->position.y / 40); + + } \ No newline at end of file diff --git a/src/inventory.cpp b/src/inventory.cpp index 2071026..c43cb8a 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -17,14 +17,43 @@ void Inventory::showItems(Vector2 target) { for (int i = 0; i < items.size(); i++) { std::stringstream text; - if (i == activeItem){ - text << items[i]->getIcon() << ':' << items[i]->GetCount() << ' '; - DrawText(text.str().c_str(), x, y, 28, WHITE); + + if(items[i]->getIcon() == 'i'){ + if(i == activeItem){ + DrawRectangle(x, y, 24, 24, WHITE); + text << ':' << items[i]->GetCount() << ' '; + DrawText(text.str().c_str(), x + 28, y, 28, WHITE); + } else { + DrawRectangle(x, y, 24, 24, GRAY); + text << ':' << items[i]->GetCount() << ' '; + DrawText(text.str().c_str(), x + 28, y, 28, GRAY); + } + } else if(items[i]->getIcon() == 'c'){ + if(i == activeItem){ + DrawRectangle(x, y, 24, 24, WHITE); + text << ':' << items[i]->GetCount() << ' '; + DrawText(text.str().c_str(), x + 28, y, 28, WHITE); + } else { + DrawRectangle(x, y, 24, 24, ORANGE); + text << ':' << items[i]->GetCount() << ' '; + DrawText(text.str().c_str(), x + 28, y, 28, ORANGE); + } } else { - text << items[i]->getIcon() << ':' << items[i]->GetCount() << ' '; - DrawText(text.str().c_str(), x, y, 28, items[i]->getColor()); + if(i == activeItem){ + text << items[i]->getIcon() << ':' << items[i]->GetCount() << ' '; + DrawText(text.str().c_str(), x, y, 28, WHITE); + } else { + text << items[i]->getIcon() << ':' << items[i]->GetCount() << ' '; + DrawText(text.str().c_str(), x, y, 28, BLACK); + } } - x += 45; + // if (i == activeItem){ + // + // } else { + // text << items[i]->getIcon() << ':' << items[i]->GetCount() << ' '; + // DrawText(text.str().c_str(), x, y, 28, items[i]->getColor()); + // } + x += 55; } } diff --git a/src/main.cpp b/src/main.cpp index 2091e59..7eab244 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,16 +4,16 @@ #include "../include/ground.h" #include "../include/cell.h" #include "../include/map.h" +#include "../include/belt.h" #include "../include/gameManager.h" #include "../include/item.h" #include "../include/inventory.h" - #include int main(){ InitWindow(1920, 1080, "Factorio"); - HideCursor(); + // HideCursor(); SetTargetFPS(144); Hero hero = Hero(1920 / 2.0f, 1080 / 2.0f, 15, 15, '@', BLUE); @@ -44,6 +44,7 @@ int main(){ map.draw(); + gm.PrintMap(); gm.detectCollision(); gm.Show(); gm.Update();